- my $now = time;
-
- $start = $opt{start} if $opt{start};
- $end = $opt{end} if $opt{end};
-
- return 'Invalid date range' if ($start < 0 || $start >= $end
- || $end <= $start || $end < 0 || $end > $now || $start > $now
- || $end-$start > 86400*366 );
-
- 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';
- }
-
-
- # assume data in DB is correct,
- # assume always _IN and _OUT pair, assume intvl = 300
-
- my @times;
- my @in;
- my @out;
- foreach my $rec ( @records ) {
- push @times, $rec->_date
- unless grep { $_ eq $rec->_date } @times;
- push @in, $rec->value if $rec->serviceid =~ /_IN$/;
- push @out, $rec->value if $rec->serviceid =~ /_OUT$/;
- }
-
- my $timediff = $times[-1] - $times[0]; # they're sorted ascending
-
- my $y_min = 999999999999; # ~1Tbps
- my $y_max = 0;
- my $in_sum = 0;
- my $out_sum = 0;
- my $in_min = 999999999999;
- my $in_max = 0;
- my $out_min = 999999999999;
- my $out_max = 0;
- foreach my $in ( @in ) {
- $y_max = $in if $in > $y_max;
- $y_min = $in if $in < $y_min;
- $in_sum += $in;
- $in_max = $in if $in > $in_max;
- $in_min = $in if $in < $in_min;
- }
- foreach my $out ( @out ) {
- $y_max = $out if $out > $y_max;
- $y_min = $out if $out < $y_min;
- $out_sum += $out;
- $out_max = $out if $out > $out_max;
- $out_min = $out if $out < $out_min;
- }
- my $bwdiff = $y_max - $y_min;
- $in_min = $self->_format_bandwidth($in_min);
- $out_min = $self->_format_bandwidth($out_min);
- $in_max = $self->_format_bandwidth($in_max);
- $out_max = $self->_format_bandwidth($out_max);
- my $in_curr = $self->_format_bandwidth($in[-1]);
- my $out_curr = $self->_format_bandwidth($out[-1]);
- my $numsamples = scalar(@records)/2;
- my $in_avg = $self->_format_bandwidth($in_sum/$numsamples);
- my $out_avg = $self->_format_bandwidth($out_sum/$numsamples);
+ my $now = time;
+
+ $start = $opt{start} if $opt{start};
+ $end = $opt{end} if $opt{end};
+
+ return 'Invalid date range' if ($start < 0 || $start >= $end
+ || $end <= $start || $end < 0 || $end > $now || $start > $now
+ || $end-$start > 86400*366 );
+
+ local($FS::Record::nowarn_classload) = 1;
+
+ 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';
+ }
+
+ #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
+
+ my @times;
+ my @in;
+ my @out;
+ foreach my $rec ( @records ) {
+ push @times, $rec->_date
+ unless grep { $_ eq $rec->_date } @times;
+ push @in, $rec->value if $rec->serviceid =~ /_IN$/;
+ push @out, $rec->value if $rec->serviceid =~ /_OUT$/;
+ }
+
+ my $timediff = $times[-1] - $times[0]; # they're sorted ascending
+
+ my $y_min = 999999999999; # ~1Tbps
+ my $y_max = 0;
+ my $in_sum = 0;
+ my $out_sum = 0;
+ my $in_min = 999999999999;
+ my $in_max = 0;
+ my $out_min = 999999999999;
+ my $out_max = 0;
+ foreach my $in ( @in ) {
+ $y_max = $in if $in > $y_max;
+ $y_min = $in if $in < $y_min;
+ $in_sum += $in;
+ $in_max = $in if $in > $in_max;
+ $in_min = $in if $in < $in_min;
+ }
+ foreach my $out ( @out ) {
+ $y_max = $out if $out > $y_max;
+ $y_min = $out if $out < $y_min;
+ $out_sum += $out;
+ $out_max = $out if $out > $out_max;
+ $out_min = $out if $out < $out_min;
+ }
+ my $bwdiff = $y_max - $y_min;
+ $in_min = $self->_format_bandwidth($in_min);
+ $out_min = $self->_format_bandwidth($out_min);
+ $in_max = $self->_format_bandwidth($in_max);
+ $out_max = $self->_format_bandwidth($out_max);
+ my $in_curr = $self->_format_bandwidth($in[-1]);
+ my $out_curr = $self->_format_bandwidth($out[-1]);
+ my $numsamples = scalar(@records)/2;
+ my $in_avg = $self->_format_bandwidth($in_sum/$numsamples);
+ my $out_avg = $self->_format_bandwidth($out_sum/$numsamples);