Support for data munging in User Reports

This commit is contained in:
Oliver Gorwits
2014-03-02 23:29:09 +00:00
parent 3ff9f3c587
commit 32771b5ada
7 changed files with 53 additions and 19 deletions

View File

@@ -243,8 +243,14 @@ code or HTML templates. For example:
ORDER BY name
Each key of the C<reports> configuration is an alias for the report, and
becomes part of the web path. Within the tree you must provide each of the
keys shown:
becomes part of the web path.
You can munge the data retrieved from the database by placing a Perl script
with the same name as the C<reports> key into the C<site_plugins> directory of
Netdisco's home area. The script can access C<$config> for its configuration
and C<@data> for the retrieved data. It should return a list of munged data.
Within the tree you can provide each of the keys below:
=head4 C<label>
@@ -258,8 +264,9 @@ heading.
=head4 C<query>
SQL which returns the data. Make sure that the columns are named the same as
the keys of the C<columns> configuration. Note the way the SQL is specified in
the example above, using the pipe symbol and then indenting the query text.
the keys of the C<columns> or C<query_columns> configuration. Note the way the
SQL is specified in the example above, using the pipe symbol and then
indenting the query text.
=head4 C<category> (optional)
@@ -267,6 +274,13 @@ Section of the Reports menu where this report will appear. See
L<WritingPlugins|App::Netdisco::Manual::WritingPlugins> for the full list.
If not supplied, reports appear in a I<My Reports> category.
=head4 C<query_columns> (optional)
If supplying code to munge the data, the columns returned from your database
C<query> may not be the same as those in the web report. Set this to a list of
the columns in C<query>. The C<columns> setting will then be used for the web
report.
=head3 C<jobqueue_refresh>
Value: Integer Number. Default: 5.

View File

@@ -6,9 +6,10 @@ use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
use Path::Class 'file';
use Safe;
# in case user did not set
config->{reports} ||= {};
use vars qw/$config @data/;
foreach my $r (keys %{setting('reports')}) {
my $report = setting('reports')->{$r};
@@ -23,28 +24,44 @@ foreach my $r (keys %{setting('reports')}) {
get "/ajax/content/report/$r" => require_login sub {
my $rs = schema('netdisco')->resultset('Virtual::GenericReport')->result_source;
# this should be done by creating a new Virtual Result class on the fly
# (package...) and then calling DBIC register_class on it.
# TODO: this should be done by creating a new Virtual Result class on
# the fly (package...) and then calling DBIC register_class on it.
$rs->view_definition($report->{query});
$rs->remove_columns($rs->columns);
$rs->add_columns(map {keys %{$_}} @{$report->{columns}});
$rs->add_columns( exists $report->{query_columns}
? @{ $report->{query_columns} }
: (map {keys %{$_}} @{$report->{columns}})
);
my $set = schema('netdisco')->resultset('Virtual::GenericReport');
return unless $set->count;
my $set = schema('netdisco')->resultset('Virtual::GenericReport')
->search(undef, {result_class => 'DBIx::Class::ResultClass::HashRefInflator'});
@data = $set->all;
# Data Munging support...
my $compartment = Safe->new;
$config = setting('reports')->{$r};
$compartment->share(qw/$config @data/);
$compartment->permit_only(qw/:default sort/);
my $munger = file(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'site_plugins', $r)->stringify;
my @results = ((-f $munger) ? $compartment->rdo( $munger ) : @data);
return if $@ or (0 == scalar @results);
if (request->is_ajax) {
template 'ajax/report/generic_report.tt',
{ results => $set,
{ results => \@results,
headings => [map {values %{$_}} @{$report->{columns}}],
columns => [$rs->columns] },
columns => [map {keys %{$_}} @{$report->{columns}}] },
{ layout => undef };
}
else {
header( 'Content-Type' => 'text/comma-separated-values' );
template 'ajax/report/generic_report_csv.tt',
{ results => $set,
{ results => \@results,
headings => [map {values %{$_}} @{$report->{columns}}],
columns => [$rs->columns] },
columns => [map {keys %{$_}} @{$report->{columns}}] },
{ layout => undef };
}
};