summaryrefslogtreecommitdiff
path: root/httemplate/search/sqlradius_usage.html
blob: b810a8bf641532e3872a6407d7f15e1639798fb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
% if ( @include_agents ) {
%   # jumbo report
<& /elements/header.html, $title &>
%   foreach my $agent ( @include_agents ) {
% $cgi->param('agentnum', $agent->agentnum); #for download links
<DIV WIDTH="100%" STYLE="page-break-after: always">
<FONT SIZE=6><% $agent->agent %></FONT><BR><BR>
  <& sqlradius_usage.html, 
      exports           => \@exports,
      agentnum          => $agent->agentnum,
      nohtmlheader      => 1,
      download_label    => 'Download this section',
      &>
</DIV>
<BR><BR>
%  }
<& /elements/footer.html &>
% } else {
<& elements/search.html,
  'title'       => $title,
  'name'        => $combine_svcs ? 'packages' : 'services',
  'query'       => $sql_query,
  'count_query' => $count_query,
  'header'      => [ #FS::UI::Web::cust_header(),
                     '#',
                     'Customer',
                     'Package',
                     'User',
                     'Upload (GB)',
                     'Download (GB)',
                     'Total (GB)',
                   ],
  'footer'      => \@footer,
  'fields'      => [ #\&FS::UI::Web::cust_fields,
                     'display_custnum',
                     'name',
                     'pkg',
                     @pkg_usage, # username, upload, download, total
                   ],
  'order_by_sql' => $order_by_sql,
  'links'       => [ #( map { $_ ne 'Cust. Status' ? $link_cust : '' }
                     #  FS::UI::Web::cust_header() ),
                     $link_cust,
                     $link_cust,
                     $link_pkg,
                     $link_svc,
                     '',
                     '',
                   ],
  'align'       => #FS::UI::Web::cust_aligns() .
                   'rlcrrr',
  'nohtmlheader'    => ($opt{'nohtmlheader'} || 0),
  'download_label'  => $opt{'download_label'},
&>
% }
<%init>

my %opt = @_;

my $curuser = $FS::CurrentUser::CurrentUser;
die "access denied" unless $curuser->access_right('List packages');

my $title = 'Data Usage Report - '; 
my $agentnum = '';
my @include_agents = ();

my $combine_svcs = $cgi->param('combine_svcs') ? 1 : 0;

if ( $opt{'agentnum'} =~ /^(\d+)$/ ) {
  $agentnum = $opt{'agentnum'};
} else {

  my @agentnums = grep /^(\d+)$/, $cgi->param('agentnum');

  if ( ! @agentnums ) {
    @include_agents = qsearch({ 'table'     => 'agent',
                                'hashref'   => { 'disabled'=>'' },
                                'extra_sql' => ' AND '. $curuser->agentnums_sql,
                             });
  } elsif ( scalar(@agentnums) == 1 ) {
    $agentnum = $agentnums[0];
  } else {
    @include_agents = qsearch({ 'table'     => 'agent',
                                'hashref'   => { 'disabled' => '', },
                                'extra_sql' => 'AND agentnum IN ('.
                                                 join(',',@agentnums). ') '.
                                               ' AND '. $curuser->agentnums_sql,
                             });
  }

}

if ( $agentnum ) {
  my $agent = FS::agent->by_key($agentnum);
  $title = $agent->agent." $title";
}

# usage query params
my( $beginning, $ending ) = FS::UI::Web::parse_beginning_ending($cgi);

if ( $beginning ) {
  $title .= time2str('%h %o %Y ', $beginning);
}
$title .= 'through ';
if ( $ending == 4294967295 ) {
  $title .= 'now';
} else {
  $title .= time2str('%h %o %Y', $ending);
}

# can also show a specific customer / service. the main query will handle
# agent restrictions, but we need a list of the services to ask the export
# for usage data.
my $cust_main;
if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
  $cust_main = qsearchs( {
    'table'     => 'cust_main',
    'hashref'   => { 'custnum' => $1 },
    'extra_sql' => ' AND '. $curuser->agentnums_sql,
  });
  die "Customer not found!" unless $cust_main;
  # then only report on this agent
  $agentnum = $cust_main->agentnum;
  @include_agents = ();
  # and announce that we're doing it
  $title .= ' - ' . $cust_main->name_short;
}

my @exports;
if ( $opt{exports} ) {
  @exports = @{ $opt{exports} };
} elsif ( $cgi->param('exportnum') ) {
  foreach my $exportnum ($cgi->param('exportnum')) {
    $exportnum =~ /^(\d+)$/
      or die "illegal export: '".$cgi->param('exportnum')."'";
    my $export = FS::part_export->by_key($1)
      or die "exportnum $1 not found";
    $export->exporttype =~ /sqlradius/
      or die "exportnum ".$export->exportnum." is type ".$export->exporttype.
             ", not sqlradius";
    push @exports, $export;
  }
  die "exportnum required" unless @exports;
} else {
  # do something sensible if no exports were selected
  @exports = FS::part_export::sqlradius->all_sqlradius;
}

my %usage_param = (
  stoptime_start  => $beginning,
  stoptime_end    => $ending,
  summarize       => 1
);

my @total_usage = ('', 0, 0, 0); # username, input, output, input + output

# remember which exports apply to which services, so we don't inappropriately
# ask the wrong ones for usage stats
my %export_svcparts;
foreach my $export (@exports) {
  my %seen;
  foreach ($export->export_svc) {
    $seen{ $_->svcpart } = 1;
  }
  $export_svcparts{ $export->exportnum } = \%seen;
}

# 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')) ) {
      my $cust_svc = FS::cust_svc->by_key($svcnum);
      my $svc = $cust_svc->svc_x;
      foreach my $export (@exports) {
        if ( $export_svcparts{ $export->exportnum }{ $cust_svc->svcpart } ) {
          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;
        }
        # else this export doesn't apply so skip it
      }
    }
    $total_usage[1] += $upbytes;
    $total_usage[2] += $downbytes;
    $total_usage[3] += $upbytes + $downbytes;
    $cust_pkg->set('_stats', [ $display_username,
                               $upbytes,
                               $downbytes,
                               $upbytes + $downbytes ]);
  }
  return $cust_pkg->get('_stats');
};

my @pkg_usage;
$pkg_usage[0] = sub { # username
 return &{ $cust_pkg_stats_sub }(shift)->[0];
};
foreach my $i (1, 2, 3) { # numeric fields
  $pkg_usage[$i] = sub { # cust_pkg arg
    my $value = &{ $cust_pkg_stats_sub }(shift)->[$i];
    # for now, always show in GB, rounded to 3 digits
    $value ? bytes_to_gb($value) : '';
  };
}

my $link_cust = [ $p.'view/cust_main.cgi?', 'custnum' ];

# cust_pkg search params
my %search_hash = ( 'agentnum' => $agentnum );
if ($cust_main) {
  $search_hash{'custnum'} = $cust_main->custnum;
}

# construct a subquery for services/packages with relevant exports

my $group_by = ' GROUP BY pkgnum';
if ( !$combine_svcs ) {
  $group_by .= ', svcnum';
}

my $exportnums = join(',', map { $_->get('exportnum') } @exports);
my $svcnums_table = 'SELECT pkgnum, ' .  FS::Record::group_concat_sql('DISTINCT svcnum', ',') . ' AS svcnums_concat
FROM cust_svc
  JOIN part_svc USING (svcpart)
  JOIN export_svc USING (svcpart)
WHERE exportnum IN(' . $exportnums . ')' . $group_by;

my $sql_query = FS::cust_pkg->search( \%search_hash );
# also get the svcnum-list column
$sql_query->{'select'}    .= ', svcnums_concat' .
# and a workaround for the implicit DISTINCTing that happens in qsearch
                             ', NULL AS pkgnum, pkgnum AS real_pkgnum';
$sql_query->{'addl_from'} .= " JOIN ($svcnums_table) AS svcnums
USING (pkgnum)";
$sql_query->{'order_by'}  = ' ORDER BY cust_pkg.pkgnum, svcnums_concat'; # for stability

my $count_query = "SELECT COUNT(*) FROM cust_pkg ".
  $sql_query->{addl_from} .
  $sql_query->{extra_sql};

my $num_rows = FS::Record->scalar_sql($count_query);
my $itemname = $combine_svcs ? 'package' : 'service';
my @footer = (
  '',
  emt("[quant,_1,$itemname]", $num_rows), 
  '', #pkg label
  '', #username
  map {
    my $i = $_;
    sub { # defer this until the rows have been processed
      bytes_to_gb($total_usage[$i])
    }
  } (1,2,3)
);

sub bytes_to_gb {
  $_[0] ?  sprintf('%.3f', $_[0] / (1024*1024*1024.0)) : '';
}

my $conf = new FS::Conf;
my $order_by_sql = {
  'name'            => "CASE WHEN cust_main.company IS NOT NULL
                                  AND cust_main.company != ''
                             THEN CONCAT(cust_main.company,' (',cust_main.last,', ',cust_main.first,')')
                             ELSE CONCAT(cust_main.last,', ',cust_main.first)
                        END",
  'display_custnum' => $conf->exists('cust_main-default_agent_custid')
                       ? "CASE WHEN cust_main.agent_custid IS NOT NULL
                                    AND cust_main.agent_custid != ''
                                    AND cust_main.agent_custid ". regexp_sql. " '^[0-9]+\$'
                               THEN CAST(cust_main.agent_custid AS BIGINT)
                               ELSE cust_main.custnum
                          END"
                       : "custnum",
};

my $link_pkg = sub {
  my $self = shift;
  [ "${p}view/cust_pkg.cgi?", 'real_pkgnum' ];
};

my $link_svc = sub {
  my $self = shift;
  if ($self->svcnums_concat =~ /^(\d+)$/) {
    return [ $p.'view/cust_svc.cgi?' . $1 ];
  } else {
    return '';
  }
};
</%init>