- my %usage_param = (
- stoptime_start => $beginning,
- stoptime_end => $ending,
- summarize => 1
- );
- # usage_sessions() returns an arrayref of hashrefs of
- # (username, acctsessiontime, acctinputoctets, acctoutputoctets)
- # (XXX needs to include 'realm' for sqlradius_withdomain)
- my $usage;
- if ( @svc_x ) {
- # then query once per service
- $usage = [];
- foreach my $svc ( @svc_x ) {
- $usage_param{'svc'} = $svc;
- push @$usage, @{ $export->usage_sessions(\%usage_param) };
+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;
+ }