Advertisement
Guest User

Untitled

a guest
Sep 27th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.34 KB | None | 0 0
  1. <%class>
  2. has 'dstart' => (isa => 'Str', required => 0);
  3. has 'istart' => (isa => 'Str', required => 0);
  4. has 'iend' => (isa => 'Str', required => 0);
  5. has 'mfstart' => (isa => 'Str', required => 0);
  6. has 'wfstart' => (isa => 'Str', required => 0);
  7. has 'afstart' => (isa => 'Str', required => 0);
  8. has 'dfstart' => (isa => 'Str', required => 0);
  9. has  'display_csv' => (isa => 'Str', required => 0);
  10. </%class>
  11.  
  12. <%init>
  13. use Data::Dumper;
  14. use Time::Local;
  15. use POSIX qw/strftime/;
  16. use Text::CSV_XS;
  17.  
  18. my ($start, $end, $item, $s, $r, @table, $t2);
  19. my $count = 1;
  20. my $j = 1;
  21. my $ms = $.istart;
  22. my $ds = $.dstart;
  23.  
  24. my $currentyear = (localtime)[5] + 1900;
  25. my $lenght = $currentyear - 2004;  #2004 date 1st created item
  26. $lenght++;  #10 years
  27.  
  28. #print STDERR Dumper ("lenght : ".$lenght);
  29. #print STDERR Dumper ("-----------------------------------------------------------------");
  30. #print STDERR Dumper ($.dstart);
  31. #print STDERR Dumper ($.istart);
  32. #print STDERR Dumper ($.mend);
  33.  
  34. if ($.dstart){  #day
  35.     $count = 1;
  36. }
  37.  
  38. if ($.istart){  #intervale date
  39.     $count = 1;
  40. }
  41.  
  42. if ($.dfstart){ #daily
  43.     $count = 400 * $lenght;# last 4 years in weeks
  44. }
  45.  
  46. if ($.wfstart){ #weekly
  47.     $count = 52 * $lenght;# last 10 years in weeks
  48. }
  49.  
  50. if ($.mfstart){ #monthly
  51.     $count = 12 * $lenght;# last 10 years in weeks
  52. }
  53.  
  54. if ($.afstart){ #annually
  55.     #$count = 52; #53 * 7 * 4 ; # last 4 years in  days
  56. }
  57.  
  58.  
  59. while( $count >= 1) {  # week breakdown for a year
  60. print STDERR Dumper ($count);
  61.  
  62. if ($.dstart || $.iend || $.dfstart || $.wfstart || $.mfstart || $.mfstart)
  63. {
  64.   if ($j<=1){
  65. print STDERR Dumper ("j = " .$j);
  66.     if ($.dstart) {
  67.         $s = $.dstart;
  68.         $start = $.dstart;
  69.         $ds = $.dstart;
  70.     }
  71.     elsif ($.iend) {
  72.         $s = $.iend;
  73.         $start = $.istart;
  74.         $ds= $.istart;
  75. #       print STDERR Dumper ("mend");
  76.     }
  77.     elsif ($.dfstart) {
  78.         $s = $.dfstart;
  79.         $start = $.dfstart;
  80.         $ds = $.dfstart;
  81.     }
  82.     elsif ($.wfstart) {
  83.         $s = $.wfstart;
  84.         $start = $.wfstart;
  85.         $ds = $.wfstart;
  86.     }
  87.     elsif ($.mfstart) {
  88.         $s = $.mfstart;
  89.         $start = $.mfstart;
  90.         $ds = $.mfstart;
  91.     }
  92.     elsif ($.afstart) {
  93.         $s = $.afstart;
  94.         $start = $.afstart;
  95.         $ds = $.afstart;
  96.     }
  97.   }
  98.     else {
  99.         $s= $r;
  100.         $start = $r;
  101.         print STDERR Dumper ("in the loop" .$count.", s : ".$end);
  102.     }
  103.    
  104. #   print STDERR Dumper ("**************************************************************************");
  105.  
  106.  
  107. my @tval2 = ( $s =~ m|(\d{4})-(\d{2})-(\d{2})| );   #2012-07-29
  108. my $tstr2 = $tval2[1]."/".$tval2[2]."/".$tval2[0];      #29/02/2012
  109. print STDERR Dumper ("str2 : ".$tstr2);
  110.  
  111.  
  112. my @tval3 = ( $tstr2 =~ m|(\d{2})/(\d{2})/(\d{4})| );  
  113.  
  114. my $time2 = timelocal( 0, 0, 0, $tval3[1], $tval3[0] -1, $tval3[2] );
  115. print STDERR Dumper ("time2 : ".$time2);
  116.  
  117. if ($.dstart){  #day
  118.     $t2 = strftime( "%m/%d/%Y", localtime( $time2 + 24 * 3600));    #24*7 = 168
  119. }
  120.  
  121. if ($.istart){  #intervale
  122.     $t2 = strftime( "%m/%d/%Y", localtime( $time2 + 24 * 3600));   
  123. }
  124.  
  125. if ($.dfstart){ #daily
  126.     $t2 = strftime( "%m/%d/%Y", localtime( $time2 + 24 * 3600));   
  127. }
  128.  
  129. if ($.wfstart){ #weekly
  130.     $t2 = strftime( "%m/%d/%Y", localtime( $time2 + 168 * 3600));   #24*7 = 168
  131. }
  132.  
  133. if ($.mfstart){ #monthly
  134.     print STDERR Dumper ("t2 : ".$t2);
  135.     $t2 = strftime( "%m/%d/%Y", localtime( $time2 + 756 * 3600));   #168 * 4 = 672
  136.    
  137. }
  138.  
  139.  
  140. #print STDERR Dumper ( $s);
  141. print STDERR Dumper ($t2);
  142.  
  143. my @t3 = ( $t2 =~ m|(\d{2})/(\d{2})/(\d{4})| );
  144.  
  145. $end = $t3[2]."-".$t3[0]."-".$t3[1];
  146. print STDERR Dumper (" start: ".$start.", end : ".$end);
  147.  
  148. }
  149. elsif ($.istart and $.iend)
  150. {
  151.     $start = $.istart;
  152.     #$end = $.iend; #2012-07-29
  153. }
  154.  
  155.     $item = $db->do(q{
  156.                 select ?::varchar as date_end, ?::varchar as date_start, it.display_name, count(it.id) as num_item
  157. from items i
  158. left join item_item_types iit on (iit.item_id = i.id)
  159. left join item_types it on (it.id = iit.item_type_id)
  160. left join item_sites ist on (ist.item_id = i.id)
  161.             where i.live and i.published_at between ? and ?
  162.             group by it.display_name order by it.display_name, num_item desc
  163.     }, $end, $start, $start, $end);
  164.  
  165. $table[$count]= $item;
  166.  
  167. print STDERR Dumper ("end: ".$end);
  168.  
  169.     $j++;
  170.     $count--;
  171.     $r = $end;
  172.  }
  173.  
  174.  
  175. if ($.display_csv){
  176.  
  177.     $m->clear_buffer;
  178.     my @rows;
  179.     my $row;
  180.     my $csv = Text::CSV_XS->new({
  181.      'quote_char'  => '"',
  182.      'escape_char' => '"',
  183.      'sep_char'    => ',',
  184.      'binary'      => 1
  185.     });
  186.  
  187.     $csv->types([Text::CSV_XS::PV(),Text::CSV_XS::IV()]);
  188.     # print array of resultrow
  189. for my $i (@table) {
  190.  
  191.     while ($i && $i->next) { # print each resultrow
  192.  
  193.         @rows=($i->{'date_start'},$i->{'date_end'},$i->{'display_name'},$i->{'num_item'}); 
  194.         $csv->combine(@rows);
  195.         $m->print($csv->string."\n");
  196.     }
  197. }
  198. print STDERR Dumper ($ds);
  199.     $m->res->content_type('text/csv');
  200.     $m->res->header( 'Content-disposition' =>'attachment; filename=stat_'.$ds.'_'.$end.'.csv' );
  201.     $m->abort;
  202. }
  203.  
  204.  
  205. </%init>
  206.  
  207.  
  208. <div class="left">
  209.  
  210. <& /stats/index.mc &>
  211.  
  212. <div>
  213. % if ($.dstart) {
  214.         <h4>Between : <% $.dstart %> and  <% $.dstart %></h4>
  215. %   }
  216. %   else {
  217.         <h4>Between : <% $.istart %> and  <% $.iend %></h4>
  218. %   }
  219.  
  220.    
  221. </div>
  222.  
  223. <div class="clear"><br></div>
  224.  
  225. </div>
  226.  
  227.  
  228. <table>
  229. <thead>
  230.  
  231. <tr>
  232.     <th>Item Type Name</th>
  233.     <th>Quantity  </th>
  234. </tr>
  235. </thead>
  236. % while ($item && $item->next) {
  237. <tbody>
  238. % if ($item->{'num_item'} > 0)
  239. % {
  240.         <td><% $item->{'display_name'} %></td>
  241.         <td><% $item->{'num_item'} %> </td>
  242. % }
  243. </tr>
  244. % }
  245. </tbody>
  246.  
  247. </table>
  248. <div class="clear"><br></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement