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;
|
|
|
|
|
2017-09-29 15:04:41 +00:00
|
|
|
my $quantised_column = 'quantised_hours';
|
2017-09-29 15:14:02 +00:00
|
|
|
if ( defined $c->param('scale') && $c->param('scale') eq 'days' ) {
|
2017-09-29 15:04:41 +00:00
|
|
|
$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 => [
|
|
|
|
{
|
2017-09-29 15:04:41 +00:00
|
|
|
quantised => $quantised_column,
|
2017-09-29 13:45:44 +00:00
|
|
|
count => \"COUNT(*)",
|
|
|
|
sum_distance => $c->pg_or_sqlite(
|
2017-10-02 13:08:24 +00:00
|
|
|
'SUM("me"."distance")',
|
2017-09-29 13:45:44 +00:00
|
|
|
'SUM("me"."distance")',
|
|
|
|
),
|
|
|
|
average_distance => $c->pg_or_sqlite(
|
2017-10-02 13:08:24 +00:00
|
|
|
'AVG("me"."distance")',
|
2017-09-29 13:45:44 +00:00
|
|
|
'AVG("me"."distance")',
|
|
|
|
),
|
|
|
|
sum_value => $c->pg_or_sqlite(
|
2017-10-02 13:08:24 +00:00
|
|
|
'SUM("me"."value")',
|
2017-09-29 13:45:44 +00:00
|
|
|
'SUM("me"."value")',
|
|
|
|
),
|
|
|
|
average_value => $c->pg_or_sqlite(
|
2017-10-02 13:08:24 +00:00
|
|
|
'AVG("me"."value")',
|
2017-09-29 13:45:44 +00:00
|
|
|
'AVG("me"."value")',
|
|
|
|
),
|
|
|
|
}
|
|
|
|
],
|
2017-09-29 15:04:41 +00:00
|
|
|
group_by => $quantised_column,
|
|
|
|
order_by => { '-asc' => $quantised_column },
|
2017-09-29 13:45:44 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-10-02 13:08:24 +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
|
|
|
|
2017-10-02 13:08:24 +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;
|