Option to ignore old CDRs, RT#81480
[freeside.git] / httemplate / browse / nas.cgi
1 %print header('NAS ports');
2 %
3 %my $now = time;
4 %
5 %foreach my $nas ( sort { $a->nasnum <=> $b->nasnum } qsearch( 'nas', {} ) ) {
6 %  print $nas->nasnum. ": ". $nas->nas. " ".
7 %        $nas->nasfqdn. " (". $nas->nasip. ") ".
8 %        "as of ". time2str("%c",$nas->last).
9 %        " (". &pretty_interval($now - $nas->last). " ago)<br>".
10 %        &table(). "<TR><TH>Nas<BR>Port #</TH><TH>Global<BR>Port #</BR></TH>".
11 %        "<TH>IP address</TH><TH>User</TH><TH>Since</TH><TH>Duration</TH><TR>",
12 %  ;
13 %  foreach my $port ( sort {
14 %    $a->nasport <=> $b->nasport || $a->portnum <=> $b->portnum
15 %  } qsearch( 'port', { 'nasnum' => $nas->nasnum } ) ) {
16 %    my $session = $port->session;
17 %    my($user, $since, $pretty_since, $duration);
18 %    if ( ! $session ) {
19 %      $user = "(empty)";
20 %      $since = 0;
21 %      $pretty_since = "(never)";
22 %      $duration = '';
23 %    } elsif ( $session->logout ) {
24 %      $user = "(empty)";
25 %      $since = $session->logout;
26 %    } else {
27 %      my $svc_acct = $session->svc_acct;
28 %      $user = "<A HREF=\"$p/view/svc_acct.cgi?". $svc_acct->svcnum. "\">".
29 %              $svc_acct->username. "</A>";
30 %      $since = $session->login;
31 %    }
32 %    $pretty_since = time2str("%c", $since) if $since;
33 %    $duration = pretty_interval( $now - $since ). " ago"
34 %      unless defined($duration);
35 %    print "<TR><TD>". $port->nasport. "</TD><TD>". $port->portnum. "</TD><TD>".
36 %          $port->ip. "</TD><TD>$user</TD><TD>$pretty_since".
37 %          "</TD><TD>$duration</TD></TR>"
38 %    ;
39 %  }
40 %  print "</TABLE><BR>";
41 %}
42 %
43 %#Time::Duration??
44 %sub pretty_interval {
45 %  my $interval = shift;
46 %  my %howlong = (
47 %    '604800' => 'week',
48 %    '86400'  => 'day',
49 %    '3600'   => 'hour',
50 %    '60'     => 'minute',
51 %    '1'      => 'second',
52 %  );
53 %
54 %  my $pretty = "";
55 %  foreach my $key ( sort { $b <=> $a } keys %howlong ) {
56 %    my $value = int( $interval / $key );
57 %    if ( $value  ) {
58 %      if ( $value == 1 ) {
59 %        $pretty .=
60 %          ( $howlong{$key} eq 'hour' ? 'an ' : 'a ' ). $howlong{$key}. " "
61 %      } else {
62 %        $pretty .= $value. ' '. $howlong{$key}. 's ';
63 %      }
64 %    }
65 %    $interval -= $value * $key;
66 %  }
67 %  $pretty =~ /^\s*(\S.*\S)\s*$/;
68 %  $1;
69 %} 
70 %
71 %#print &table(), <<END;
72 %#<TR>
73 %#  <TH>#</TH>
74 %#  <TH>NAS</
75 %
76
77 <%init>
78
79 #this hasn't been used in ages, and isn't linked from anywhere...
80 die 'NAS browse not currently active';
81
82 </%init>