|
| 1 | +package Scot::App::Report; |
| 2 | + |
| 3 | +use lib '../../../lib'; |
| 4 | +use strict; |
| 5 | +use warnings; |
| 6 | +use v5.18; |
| 7 | + |
| 8 | +use DateTime; |
| 9 | +use Time::Duration; |
| 10 | +use Data::Dumper; |
| 11 | + |
| 12 | +use Moose; |
| 13 | +extends 'Scot::App'; |
| 14 | + |
| 15 | +has months_ago => ( |
| 16 | + is => 'ro', |
| 17 | + isa => 'Int', |
| 18 | + required => 1, |
| 19 | + default => 6, |
| 20 | +); |
| 21 | + |
| 22 | +has nowdt => ( |
| 23 | + is => 'rw', |
| 24 | + isa => 'DateTime', |
| 25 | + lazy => 1, |
| 26 | + required => 1, |
| 27 | + default => sub { DateTime->now; }, |
| 28 | +); |
| 29 | + |
| 30 | +has thendt => ( |
| 31 | + is => 'rw', |
| 32 | + isa => 'DateTime', |
| 33 | + required => 1, |
| 34 | + lazy => 1, |
| 35 | + builder => '_build_thendt', |
| 36 | +); |
| 37 | + |
| 38 | +sub _build_thendt { |
| 39 | + my $self = shift; |
| 40 | + my $nowdt = $self->nowdt; |
| 41 | + my $thendt = $nowdt->clone(); |
| 42 | + $thendt->subtract( months => $self->months_ago ); |
| 43 | + return $thendt; |
| 44 | +} |
| 45 | + |
| 46 | +sub alertgroup_counts { |
| 47 | + my $self = shift; |
| 48 | + my $env = $self->env; |
| 49 | + my $mongo = $env->mongo; |
| 50 | + my $log = $env->log; |
| 51 | + my $nowdt = $self->nowdt; |
| 52 | + my $thendt = $self->thendt; |
| 53 | + |
| 54 | + $log->debug("counting alertgroups"); |
| 55 | + |
| 56 | + my $query = { |
| 57 | + metric => 'alertgroups created', |
| 58 | + epoch => { |
| 59 | + '$gte' => $thendt->epoch, |
| 60 | + '$lte' => $nowdt->epoch, |
| 61 | + }, |
| 62 | + }; |
| 63 | + |
| 64 | + $log->debug("query is ",{filter=>\&Dumper, value=>$query}); |
| 65 | + |
| 66 | + my $cursor = $mongo->collection('Stat')->find($query); |
| 67 | + my %result; |
| 68 | + |
| 69 | + while ( my $stat = $cursor->next ) { |
| 70 | + my $key = $stat->year . '/' . $stat->month; |
| 71 | + $result{$key} += $stat->value; |
| 72 | + } |
| 73 | + return wantarray ? %result : \%result; |
| 74 | +} |
| 75 | + |
| 76 | +sub event_counts { |
| 77 | + my $self = shift; |
| 78 | + my $env = $self->env; |
| 79 | + my $mongo = $env->mongo; |
| 80 | + my $log = $env->log; |
| 81 | + my $nowdt = $self->nowdt; |
| 82 | + my $thendt = $self->thendt; |
| 83 | + |
| 84 | + $log->debug("counting events"); |
| 85 | + |
| 86 | + my $query = { |
| 87 | + metric => 'event created', |
| 88 | + epoch => { |
| 89 | + '$gte' => $thendt->epoch, |
| 90 | + '$lte' => $nowdt->epoch, |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + $log->debug("query is ",{filter=>\&Dumper, value=>$query}); |
| 95 | + |
| 96 | + my $cursor = $mongo->collection('Stat')->find($query); |
| 97 | + my %result; |
| 98 | + |
| 99 | + while ( my $stat = $cursor->next ) { |
| 100 | + my $key = $stat->year . '/' . $stat->month; |
| 101 | + $result{$key} += $stat->value; |
| 102 | + } |
| 103 | + return wantarray ? %result : \%result; |
| 104 | +} |
| 105 | + |
| 106 | +sub incident_counts { |
| 107 | + my $self = shift; |
| 108 | + my $env = $self->env; |
| 109 | + my $mongo = $env->mongo; |
| 110 | + my $log = $env->log; |
| 111 | + my $nowdt = $self->nowdt; |
| 112 | + my $thendt = $self->thendt; |
| 113 | + |
| 114 | + $log->debug("counting incidents"); |
| 115 | + |
| 116 | + my $query = { |
| 117 | + metric => 'incident created', |
| 118 | + epoch => { |
| 119 | + '$gte' => $thendt->epoch, |
| 120 | + '$lte' => $nowdt->epoch, |
| 121 | + }, |
| 122 | + }; |
| 123 | + |
| 124 | + $log->debug("query is ",{filter=>\&Dumper, value=>$query}); |
| 125 | + |
| 126 | + my $cursor = $mongo->collection('Stat')->find($query); |
| 127 | + my %result; |
| 128 | + |
| 129 | + while ( my $stat = $cursor->next ) { |
| 130 | + my $key = $stat->year . '/' . $stat->month; |
| 131 | + $result{$key} += $stat->value; |
| 132 | + } |
| 133 | + return wantarray ? %result : \%result; |
| 134 | +} |
| 135 | + |
| 136 | +sub response_times { |
| 137 | + my $self = shift; |
| 138 | + my $env = $self->env;; |
| 139 | + my $mongo = $env->mongo; |
| 140 | + my $log = $env->log; |
| 141 | + my $nowdt = $self->nowdt; |
| 142 | + my $thendt = $self->thendt; |
| 143 | + |
| 144 | + my $query = { |
| 145 | + metric => qr/Alertgroup Response Times/i, |
| 146 | + epoch => { |
| 147 | + '$gte' => $thendt->epoch, |
| 148 | + '$lte' => $nowdt->epoch, |
| 149 | + }, |
| 150 | + }; |
| 151 | + |
| 152 | + my $cursor = $mongo->collection('Stat')->find($query); |
| 153 | + my %result; |
| 154 | + |
| 155 | + while ( my $stat = $cursor->next ) { |
| 156 | + my $key = $stat->year . '/' . $stat->month; |
| 157 | + my $metric = $stat->metric; |
| 158 | + my @words = split(/ /,$metric); |
| 159 | + my $type = lc($words[0]); |
| 160 | + my $category = lc($words[2]); |
| 161 | + my $value = $stat->value; |
| 162 | + $result{$key}{$category}{$type} += $value; |
| 163 | + } |
| 164 | + |
| 165 | + foreach my $key (keys %result) { |
| 166 | + $log->debug("$key"); |
| 167 | + foreach my $category (keys %{$result{$key}}) { |
| 168 | + $log->debug(" $category"); |
| 169 | + my $seconds = $result{$key}{$category}{sum}; |
| 170 | + my $count = $result{$key}{$category}{count}; |
| 171 | + $log->debug({filter=>\&Dumper, value=> $result{$key}{$category}}); |
| 172 | + if ( !defined $count or $count == 0 ) { |
| 173 | + $log->error("count zero or undef!"); |
| 174 | + if ($seconds == 0) { |
| 175 | + $result{$key}{$category}{avg} = 0; |
| 176 | + } |
| 177 | + else { |
| 178 | + $result{$key}{$category}{avg} = 'na'; |
| 179 | + } |
| 180 | + next; |
| 181 | + } |
| 182 | + |
| 183 | + $result{$key}{$category}{avg} = $seconds / $count; |
| 184 | + $result{$key}{$category}{humanavg} = duration($seconds / $count); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return wantarray ? %result : \%result; |
| 189 | +} |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | +1; |
0 commit comments