use IO::All;
use JSON;
use URI::Escape;
+use Data::Dump qw( pp );
has root => (is => 'lazy');
return $struct;
}
+sub merge_pair_structures {
+ my ($self, $cols, @structures) = @_;
+ my ($key_name, $value_name) = @$cols;
+ my %name;
+ $name{ $_ }++
+ for map $_->{$key_name},
+ map { ($_ ? @$_ : ()) } map $_->[1]{data}, @structures;
+ my %value_by_host = (map {
+ my $host = $_->[0];
+ my $data = $_->[1]{data};
+ ($host, +{
+ map { ($_->{$key_name}, $_->{$value_name}) } @$data,
+ });
+ } @structures);
+ my @hosts = map $_->[0], @structures;
+ return [{
+ columns => ['key', @hosts],
+ show_columns => 1,
+ data => [ map {
+ my $key = $_;
+ +{ key => $key, (map {
+ ($_, $value_by_host{$_}{$key});
+ } @hosts)};
+ } sort keys %name ],
+ }];
+}
+
+sub merge_generic_structures {
+ my ($self, $cols, @structures) = @_;
+ my %by_name;
+ my %host_cols;
+ my %complex_cols;
+ my %alias;
+ my $is_explore = grep { $_ eq 'explore' } @$cols;
+ foreach my $thing (@structures) {
+ foreach my $el (@{$thing->[1]{data}}) {
+ my $by = $by_name{$el->{name}} ||= { name => $el->{name} };
+ foreach my $key (keys %$el) {
+ next if $is_explore and $key eq 'name';
+ if (ref($el->{$key}) eq 'HASH') {
+ $complex_cols{$key} = 1;
+ $by->{$key} = {};
+ } else {
+ my $full_key = $key.' ('.$thing->[0].')';
+ $alias{$full_key} = $key;
+ $host_cols{$full_key} = 1;
+ $by->{$full_key} = $el->{$key};
+ }
+ }
+ }
+ }
+ return [{
+ columns => [
+ $is_explore ? ('name') : (),
+ sort(keys %host_cols),
+ sort(keys %complex_cols),
+ ],
+ show_columns => 1,
+ aliases => \%alias,
+ data => [
+ map $by_name{$_}, sort keys %by_name
+ ],
+ }];
+}
+
+sub merge_unrelated_structures {
+ my ($self, $cols, @structures) = @_;
+ my $done = [{
+ columns => ['host', sort @$cols],
+ show_columns => 1,
+ data => [ map {
+ my ($host, $data) = @$_;
+ (map +{ host => $host, %$_ }, @{ $data->{data} || [] });
+ } @structures ],
+ }];
+ return $done;
+}
+
+sub _pred_columns_kv { @_ == 3 and $_[1] eq 'key' and $_[2] eq 'value' }
+
sub structure {
my ($self, @parts) = @_;
if ($parts[0] =~ /\+/) {
($parts[0], my @extra) = split /\+/, $parts[0];
my $struct = $self->mangle_structure($self->descend($self->root, @parts));
my $first = shift @parts;
- my @rest = map [ $_, $self->mangle_structure(
- $self->descend($self->root, $_, @parts)
- )->[0] ], @extra;
- unless ($struct->[0]{show_columns}) {
- my @cols = @{ $struct->[0]{columns} };
- if (@cols == 2) {
- my ($key_name, $value_name) = @cols;
- my %name;
- $name{ $_ }++
- for map $_->{$key_name},
- map @$_, $struct->[0]{data}, map $_->[1]{data}, @rest;
- my %value_by_host = (map {
- my $host = $_->[0];
- my $data = $_->[1]{data};
- ($host, +{
- map { ($_->{$key_name}, $_->{$value_name}) } @$data,
- });
- } [$first, $struct->[0]], @rest);
- my @hosts = ($first, @extra);
- return [{
- columns => ['key', @hosts],
- show_columns => 1,
- data => [ map {
- my $key = $_;
- +{ key => $key, (map {
- ($_, $value_by_host{$_}{$key});
- } @hosts)};
- } sort keys %name ],
- }];
- }
+ my @rest = map {
+ my $name = $_;
+ my $data = $self->mangle_structure(
+ $self->descend($self->root, $name, @parts),
+ );
+ [$name, $data ? $data->[0] : {}];
+ } @extra;
+ my %col;
+ $col{$_}++
+ for map { (@{$_->{columns}||[]}) }
+ $struct->[0], map $_->[1], @rest;
+ my @cols = sort keys %col;
+ my $show_cols;
+ $show_cols++
+ for grep { $_->{show_columns} }
+ $struct->[0], map $_->[1], @rest;
+ my @structures = ([$first, $struct->[0]], @rest);
+ if ($self->_pred_columns_kv(@cols)) {
+ return $self->merge_pair_structures(
+ [@cols],
+ @structures,
+ );
}
- my %by_name;
- my %host_cols;
- my %complex_cols;
- foreach my $thing ([ $first, $struct->[0] ], @rest) {
- foreach my $el (@{$thing->[1]{data}}) {
- my $by = $by_name{$el->{name}} ||= { name => $el->{name} };
- foreach my $key (keys %$el) {
- next if $key eq 'name';
- if (ref($el->{$key}) eq 'HASH') {
- $complex_cols{$key} = 1;
- $by->{$key} = {};
- } else {
- my $full_key = $key.' ('.$thing->[0].')';
- $host_cols{$full_key} = 1;
- $by->{$full_key} = $el->{$key};
- }
- }
- }
+ elsif (not $col{name}) {
+ return $self->merge_unrelated_structures([@cols], @structures);
+ }
+ return $self->merge_generic_structures([@cols], @structures);
+ }
+ return $self->mangle_structure($self->descend($self->root, @parts));
+}
+
+sub mangle_hash_structure {
+ my ($self, $data) = @_;
+ if (keys %$data > 1
+ and values %$data == grep ref($_) eq 'HASH', values %$data) {
+ my %tmp;
+ $tmp{join '|', keys %$_} = 1 for values %$data;
+ if (keys %tmp == 1) {
+ $data->{$_}->{name} ||= $_ for keys %$data;
+ my @cols = grep $_ ne 'name', sort keys %{(values %$data)[0]};
+ unshift @cols, 'name';
+ return [{
+ columns => \@cols,
+ show_columns => 1,
+ data => [ @{$data}{sort keys %$data} ],
+ }]
}
+ }
+ return [{
+ columns => [ 'key', 'value' ],
+ data => [ map +{ key => $_, value => $data->{$_} }, sort keys %$data ],
+ }];
+}
+
+sub mangle_array_structure {
+ my ($self, $data) = @_;
+ if (not grep { not ref($_) eq 'HASH' } @$data) {
+ my %key;
+ $key{$_} = 1
+ for map { keys %$_ } @$data;
return [{
- columns => [ 'name', sort(keys %host_cols), sort(keys %complex_cols) ],
+ columns => [sort keys %key],
show_columns => 1,
- data => [
- map $by_name{$_}, sort keys %by_name
- ],
+ data => $data,
}];
}
- return $self->mangle_structure($self->descend($self->root, @parts));
+}
+
+sub mangle_directory {
+ my ($self, $data) = @_;
+ return [{
+ columns => [ 'name', 'explore' ],
+ data => [
+ map +{ name => $_, explore => $self->link_to($_) }, keys %$data,
+ ]
+ }];
}
sub mangle_structure {
my ($self, $data) = @_;
return unless $data;
if (ref($data) eq 'HASH') {
- if (keys %$data > 1
- and values %$data == grep ref($_) eq 'HASH', values %$data) {
- my %tmp;
- $tmp{join '|', keys %$_} = 1 for values %$data;
- if (keys %tmp == 1) {
- $data->{$_}->{name} ||= $_ for keys %$data;
- my @cols = grep $_ ne 'name', sort keys %{(values %$data)[0]};
- unshift @cols, 'name';
- return [{
- columns => \@cols,
- show_columns => 1,
- data => [ @{$data}{sort keys %$data} ],
- }]
- }
- }
- return [{
- columns => [ 'key', 'value' ],
- data => [ map +{ key => $_, value => $data->{$_} }, sort keys %$data ],
- }];
+ return $self->mangle_hash_structure($data);
} elsif (ref($data) eq 'ARRAY') {
- if (not grep { not ref($_) eq 'HASH' } @$data) {
- my %key;
- $key{$_} = 1
- for map { keys %$_ } @$data;
- return [{
- columns => [sort keys %key],
- show_columns => 1,
- data => $data,
- }];
- }
+ return $self->mangle_array_structure($data);
} elsif (blessed($data) and $data->isa('IO::All::Dir')) {
- return [{
- columns => [ 'name', 'explore' ],
- data => [
- map +{ name => $_, explore => $self->link_to($_) }, keys %$data,
- ]
- }];
+ return $self->mangle_directory($data);
} else {
die "Confused by $data";
}
(map { my $el = $_;
' ', ($el->{key} eq '__error__') ? <tr class="error"> : <tr>,
(map {
- <td>, $self->render_el($el, $_, $el->{$_}), </td>
+ <td>, $self->render_el(
+ $el,
+ $_,
+ $el->{$_},
+ $data->{aliases}{$_},
+ ), </td>
} @{$data->{columns}}),
</tr>, "\n"
} @rows),
}
sub render_el {
- my ($self, $whole, $key, $part) = @_;
+ my ($self, $whole, $key, $part, $alias) = @_;
+ my $link_key = defined($alias) ? $alias : $key;
if (ref($part) eq 'ARRAY') {
if (grep { ref($_) eq 'HASH' } @$part) {
if ($whole->{key}) {
return $self->link_to($whole->{key})
} elsif ($whole->{name}) {
- return $self->link_to($whole->{name}, $key);
+ return $self->link_to($whole->{name}, $link_key);
}
}
return join ', ', @$part
if ($whole->{key}) {
return $self->link_to($whole->{key})
} elsif ($whole->{name}) {
- return $self->link_to($whole->{name}, $key);
+ return $self->link_to($whole->{name}, $link_key);
}
$part = '(complex)';
}