Skip to content

Commit

Permalink
Allow --count to accept expressions, just like --grep and --map
Browse files Browse the repository at this point in the history
- One of the reasons this is useful is you can fetch arbitratily nested items from the data section

- Thanks to Avianna for the suggestion
  • Loading branch information
hoytech committed Oct 9, 2014
1 parent a192c67 commit 1d65d4b
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions bin/log-defer-viz
Expand Up @@ -125,20 +125,15 @@ if ($opt->{'data-only'}) {
}


my $count = {};

foreach my $key (@{ $opt->{count} }) {
$count->{$key} = {};
}


my $columns = $opt->{'timer-columns'};

my ($term_cols, $term_rows) = Term::Size::chars(*STDOUT{IO});
$columns = $term_cols if $term_cols;


## The _ sub is a shortcut for use in --grep and --map expressions, ie --grep "_->{data}"

## The _ sub is a shortcut for use in --grep, --map, and --count expressions, ie --grep "_->{data}"
sub _ () { $_ }

if ($opt->{grep}) {
Expand All @@ -152,6 +147,24 @@ if ($opt->{map}) {
}


my $count = {};
my $count_subs = {};

foreach my $key (@{ $opt->{count} }) {
if ($key =~ /^\w+$/) {
## The original format for count arguments were keys to the data hash
$count_subs->{$key} = sub { $_[0]->{data}->{$key} };
} else {
## But the more general way is with an accessor
$count_subs->{$key} = eval('sub { local $_ = $_[0]; ' . $key . '}');
die "Error compiling --count expression '$key' ($@)" if $@;
}

$count->{$key} = {};
}



unshift(@ARGV, '-') unless @ARGV;
if (@ARGV > 1 && $opt->{follow}) {
die "You've specified --follow but have also provided multiple files, which doesn't make sense. Did you mean to do this?";
Expand Down Expand Up @@ -266,8 +279,10 @@ sub handle_entry {

if (@{ $opt->{count} }) {
foreach my $key (@{ $opt->{count} }) {
if (exists $entry->{data}->{$key}) {
$count->{$key}->{$entry->{data}->{$key}} += 1;
my $val = $count_subs->{$key}->($entry);

if (defined $val) {
$count->{$key}->{$val} += 1;
}
}

Expand Down

0 comments on commit 1d65d4b

Please sign in to comment.