torrus various changes, RT10574
[freeside.git] / FS / FS / svc_port.pm
index 16d5208..afd412e 100644 (file)
@@ -3,10 +3,13 @@ package FS::svc_port;
 use strict;
 use vars qw($conf $system $DEBUG $me );
 use base qw( FS::svc_Common );
-use FS::Record qw( qsearch qsearchs dbh str2time_sql str2time_sql_closing );
+use FS::UID qw( driver_name );
+use FS::Record qw( qsearch qsearchs
+                   str2time_sql str2time_sql_closing concat_sql );
 use FS::cust_svc;
 use GD::Graph;
 use GD::Graph::mixed;
+use GD;
 use Date::Format qw(time2str);
 use Data::Dumper;
 
@@ -255,50 +258,31 @@ sub graph_png {
             || $end <= $start || $end < 0 || $end > $now || $start > $now
             || $end-$start > 86400*366 );
 
-        local($FS::Record::nowarn_classload) = 1;
+        my $_date = concat_sql([ 'srv_date', "' '", 'srv_time' ]);
+        $_date = "CAST( $_date AS TIMESTAMP )" if driver_name =~ /^Pg/i;
+        $_date = str2time_sql. $_date.  str2time_sql_closing;
 
         my $serviceid_sql = "('${serviceid}_IN','${serviceid}_OUT')";
 
-        my @records;
-        my $dbh = dbh;
-        if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
-            @records = qsearch({ 
-                'table' => 'srvexport',
-                'select' => "*, date_part('epoch',to_timestamp(srv_date||' '||srv_time,'YYYY-MM-DD HH:MI:SS')) as _date",
-                'extra_sql' => "where serviceid in $serviceid_sql and 
-                    date_part('epoch',to_timestamp(srv_date||' '||srv_time,'YYYY-MM-DD HH:MI:SS')) >= $start
-                    and date_part('epoch',to_timestamp(srv_date||' '||srv_time,'YYYY-MM-DD HH:MI:SS')) <= $end",
-                'order_by' => "order by date_part('epoch',to_timestamp(srv_date||' '||srv_time,'YYYY-MM-DD HH:MI:SS')) asc",
-             });
-         } elsif ( $dbh->{Driver}->{Name} eq 'mysql' ) {
-                @records = qsearch({ 
-                    'table' => 'srvexport',
-                    'select' => "*, unix_timestamp(srv_date||' '||srv_time) as _date",
-                    'extra_sql' => "where serviceid in $serviceid_sql and 
-                        unix_timestamp(srv_date||' '||srv_time) >= $start
-                        and unix_timestamp(srv_date||' '||srv_time) <= $end",
-                    'order_by' => "order by unix_timestamp(srv_date||' '||srv_time) asc",
-                 });
-        } else {
-              return 'Unsupported DBMS';
+        local($FS::Record::nowarn_classload) = 1;
+        my @records = qsearch({
+          'table'     => 'srvexport',
+          'select'    => "*, $_date as _date",
+          'extra_sql' => "where serviceid in $serviceid_sql
+                            and $_date >= $start
+                            and $_date <= $end",
+          'order_by'  => "order by $_date asc",
+        });
+
+        if ( ! scalar(@records) ) {
+          warn "$me no records returned for $serviceid\n";
+          return ''; #should actually return a blank png (or, even better, the
+                     # error message in the image)
         }
 
-        #my $_date = str2time_sql. "srv_date||' '||srv_time".
-        #            str2time_sql_closing;
-
-        #my @records = qsearch({
-        #  'table'     => 'srvexport',
-        #  'select'    => "*, $_date as _date",
-        #  'extra_sql' => "where serviceid in $serviceid_sql
-        #                    and $_date >= $start
-        #                    and $_date <= $end",
-        #  'order_by'  => "order by $_date asc",
-        #});
-
         warn "$me ". scalar(@records). " records returned for $serviceid\n"
           if $DEBUG;
 
-
         # assume data in DB is correct,
         # assume always _IN and _OUT pair, assume intvl = 300
 
@@ -357,11 +341,11 @@ sub graph_png {
       my @data = ( \@times, \@in, \@out );
 
       # hardcoded size, colour, etc.
-      my $graph = new GD::Graph::mixed(600,400); 
+      my $graph = new GD::Graph::mixed(600,320);  #600,400
       $graph->set(
         types => ['area','lines'],
         dclrs => ['green','blue'],
-        x_label => "(In Out)  Current: $in_curr $out_curr  Average: $in_avg $out_avg  Maximum: $in_max $out_max  Minimum: $in_min $out_min",
+        x_label => '   ',
         x_tick_number => 'auto',
         x_number_format => sub {
             my $value = shift;
@@ -382,6 +366,7 @@ sub graph_png {
         },
         y_label => 'bps',
         legend_placement => 'BR',
+       lg_cols => 1,
         title => $self->serviceid,
       ) or return "can't create graph: ".$graph->error;
       
@@ -389,9 +374,26 @@ sub graph_png {
         or return "can't set text colour: ".$graph->error;
       $graph->set_legend(('In','Out')) 
         or return "can't set legend: ".$graph->error;
+      $graph->set_title_font(['verdana', 'arial', gdGiantFont], 16)
+       or return "can't set title font: ".$graph->error;
+      $graph->set_legend_font(['verdana', 'arial', gdMediumBoldFont], 12)
+       or return "can't set legend font: ".$graph->error;
+      $graph->set_x_axis_font(['verdana', 'arial', gdMediumBoldFont], 12)
+       or return "can't set font: ".$graph->error;
+      $graph->set_y_axis_font(['verdana', 'arial', gdMediumBoldFont], 12)
+       or return "can't set font: ".$graph->error;
+      $graph->set_y_label_font(['verdana', 'arial', gdMediumBoldFont], 12)
+       or return "can't set font: ".$graph->error;
 
       my $gd = $graph->plot(\@data);
       return "graph error: ".$graph->error unless($gd);
+
+      my $black = $gd->colorAllocate(0,0,0);       
+      $gd->string(gdMediumBoldFont,50,285,
+           "Current: $in_curr   Average: $in_avg   Maximum: $in_max   Minimum: $in_min",$black);
+      $gd->string(gdMediumBoldFont,50,305,
+           "Current: $out_curr   Average: $out_avg   Maximum: $out_max   Minimum: $out_min",$black);
+
       return $gd->png;
   }