This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Controller/Admin/Reports.pm

81 lines
2.5 KiB
Perl
Raw Permalink Normal View History

2017-09-29 13:45:44 +00:00
package Pear::LocalLoop::Controller::Admin::Reports;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::JSON qw/ encode_json /;
sub transaction_data {
my $c = shift;
my $quantised_column = 'quantised_hours';
2017-09-29 15:14:02 +00:00
if ( defined $c->param('scale') && $c->param('scale') eq 'days' ) {
$quantised_column = 'quantised_days';
}
2017-09-29 13:45:44 +00:00
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
my $transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search(
{},
{
columns => [
{
quantised => $quantised_column,
count => \"COUNT(*)",
2017-09-29 13:45:44 +00:00
sum_distance => $c->pg_or_sqlite(
'SUM("me"."distance")',
2017-09-29 13:45:44 +00:00
'SUM("me"."distance")',
),
average_distance => $c->pg_or_sqlite(
'AVG("me"."distance")',
2017-09-29 13:45:44 +00:00
'AVG("me"."distance")',
),
sum_value => $c->pg_or_sqlite(
'SUM("me"."value")',
2017-09-29 13:45:44 +00:00
'SUM("me"."value")',
),
average_value => $c->pg_or_sqlite(
'AVG("me"."value")',
2017-09-29 13:45:44 +00:00
'AVG("me"."value")',
),
}
],
group_by => $quantised_column,
order_by => { '-asc' => $quantised_column },
2017-09-29 13:45:44 +00:00
}
);
my $transaction_data = [
map{
my $quantised = $c->db_datetime_parser->parse_datetime($_->get_column('quantised'));
{
sum_value => ($_->get_column('sum_value') || 0) * 1,
sum_distance => ($_->get_column('sum_distance') || 0) * 1,
average_value => ($_->get_column('average_value') || 0) * 1,
average_distance => ($_->get_column('average_distance') || 0) * 1,
count => $_->get_column('count'),
quantised => $c->format_iso_datetime($quantised),
}
} $transaction_rs->all
];
2017-09-29 13:45:44 +00:00
$c->respond_to(
json => { json => { data => $transaction_data } },
html => { transaction_rs => encode_json( $transaction_data ) },
2017-09-29 13:45:44 +00:00
);
}
sub pg_or_sqlite {
my ( $c, $pg_sql, $sqlite_sql ) = @_;
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
if ( $driver eq 'Pg' ) {
return \$pg_sql;
} elsif ( $driver eq 'SQLite' ) {
return \$sqlite_sql;
} else {
$c->app->log->warn('Unknown Driver Used');
return undef;
}
}
1;