-#warn Dumper(\%usage_by_username);
-my @total_usage = (0, 0, 0, 0); # session time, input, output, input + output
-my @svc_usage = map {
- my $i = $_;
- sub {
- my $username = $export->export_username(shift);
- return '' if !exists($usage_by_username{$username});
- my $value = $usage_by_username{ $username }->[$i];
- $total_usage[$i] += $value;
- # for now, always show in GB, rounded to 3 digits
- bytes_to_gb($value);
+my %usage_param = (
+ stoptime_start => $beginning,
+ stoptime_end => $ending,
+ summarize => 1
+);
+
+my @total_usage = ('', 0, 0, 0); # username, input, output, input + output
+
+# a single sub to collect data for each package, aggregated across both
+# services and exports. when we add per-service breakdown, this should also
+# keep the per-service data, but not needed yet
+my $cust_pkg_stats_sub = sub {
+ my $cust_pkg = shift;
+ if (! $cust_pkg->get('_stats') ) {
+ my ($upbytes, $downbytes, $totalbytes) = (0, 0, 0);
+ my $display_username;
+ foreach my $svcnum ( split(',', $cust_pkg->get('svcnums_concat')) ) {
+ foreach my $export (@exports) {
+ my $svc = FS::cust_svc->by_key($svcnum)->svc_x;
+ my $username = $export->export_username($svc);
+ my $usage = $export->usage_sessions({ %usage_param, 'svc' => $svc });
+ # returns arrayref with one row
+ $upbytes += $usage->[0]->{'acctinputoctets'};
+ $downbytes += $usage->[0]->{'acctoutputoctets'};
+ # in combined services mode with multiple users/MAC addresses per
+ # package, this will just show one of them arbitrarily.
+ $display_username ||= $username;
+ }
+ }
+ $total_usage[1] += $upbytes;
+ $total_usage[2] += $downbytes;
+ $total_usage[3] += $upbytes + $downbytes;
+ $cust_pkg->set('_stats', [ $display_username,
+ $upbytes,
+ $downbytes,
+ $upbytes + $downbytes ]);