From f1a571b2740d726779c9a5f2adefa15a4f2c57c0 Mon Sep 17 00:00:00 2001 From: Michael Pucyk Date: Fri, 23 Aug 2013 15:18:42 -0400 Subject: [PATCH] Add count option - Count the number of times a key/value pair appears in the data section of each log entry. --- bin/log-defer-viz | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/bin/log-defer-viz b/bin/log-defer-viz index f7d4e0e..66475dc 100755 --- a/bin/log-defer-viz +++ b/bin/log-defer-viz @@ -22,7 +22,7 @@ my $colour_spec = { my @opt_spec = ( ## INPUT FORMAT - + 'input-format=s', ## DATE @@ -55,6 +55,7 @@ my @opt_spec = ( 'data!', 'data-format=s', 'data-only', + 'count=s@', ## MISC @@ -114,6 +115,7 @@ if ($opt->{'data-only'}) { $opt->{data} = 1; } +my $count = {} if exists $opt->{count}; my $columns = $opt->{'timer-columns'}; @@ -157,6 +159,10 @@ while (my $file = shift) { close($fh); } +if (scalar @{$opt->{count}}) { + print " Count:\n"; + print output_data($count); +} sub handle_entry { @@ -227,7 +233,7 @@ sub handle_entry { print " |_" . format_time_offset($entry->{end}) . " [END]\n\n"; } - + if ($opt->{timers}) { print Log::Defer::Viz::render_timers(width => $columns-10, timers => $entry->{timers}) if $entry->{timers}; } @@ -240,6 +246,15 @@ sub handle_entry { print " ** This log has associated data. See it with --data\n" unless $opt->{quiet} || (defined $opt->{data} && !$opt->{data}); } + + foreach my $key (@{ $opt->{count} }) { + if (!exists $count->{$key}) { + $count->{$key} = {}; + } + if (exists $entry->{data}->{$key}) { + $count->{$key}->{$entry->{data}->{$key}} += 1; + } + } } print "\n"; @@ -389,6 +404,13 @@ Data is extra embedded information in the log file. The available outputs are C< $ log-defer-viz --data-format=dumper ## Data::Dumper $ log-defer-viz --data-only ## only show data +=head1 COUNT + +The count parameter provides a method to count the number of times a specific key/value appears in the data section of the log file. + + $ log-defer-viz --data --count ip_address ## Display how many log lines for each ip address + $ log-defer-viz --data --count ip_address --count user_id ## Display how many log lines for each ip address and each user_id + =head1 MISC $ log-defer-viz --help ## the text you're reading now