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

84 lines
2.6 KiB
Perl
Raw 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 {
2021-03-20 12:09:50 +00:00
my $c = shift;
2017-09-29 13:45:44 +00:00
2021-03-20 12:09:50 +00:00
my $quantised_column = 'quantised_hours';
if ( defined $c->param('scale') && $c->param('scale') eq 'days' ) {
$quantised_column = 'quantised_days';
}
2021-03-20 12:09:50 +00:00
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
my $transaction_rs =
$c->schema->resultset( 'ViewQuantisedTransaction' . $driver )->search(
{},
2017-09-29 13:45:44 +00:00
{
2021-03-20 12:09:50 +00:00
columns => [
{
quantised => $quantised_column,
count => \"COUNT(*)",
sum_distance => $c->pg_or_sqlite(
'SUM("me"."distance")', 'SUM("me"."distance")',
),
average_distance => $c->pg_or_sqlite(
'AVG("me"."distance")', 'AVG("me"."distance")',
),
sum_value => $c->pg_or_sqlite(
'SUM("me"."value")', 'SUM("me"."value")',
),
average_value => $c->pg_or_sqlite(
'AVG("me"."value")', 'AVG("me"."value")',
),
}
],
group_by => $quantised_column,
order_by => { '-asc' => $quantised_column },
2017-09-29 13:45:44 +00:00
}
2021-03-20 12:09:50 +00:00
);
2017-09-29 13:45:44 +00:00
2021-03-20 12:09:50 +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
2021-03-20 12:09:50 +00:00
$c->respond_to(
json => { json => { data => $transaction_data } },
html => { transaction_rs => encode_json($transaction_data) },
);
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
2017-09-29 13:45:44 +00:00
}
sub pg_or_sqlite {
2021-03-20 12:09:50 +00:00
my ( $c, $pg_sql, $sqlite_sql ) = @_;
2017-09-29 13:45:44 +00:00
2021-03-20 12:09:50 +00:00
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
2017-09-29 13:45:44 +00:00
2021-03-20 12:09:50 +00:00
if ( $driver eq 'Pg' ) {
return \$pg_sql;
}
elsif ( $driver eq 'SQLite' ) {
return \$sqlite_sql;
}
else {
$c->app->log->warn('Unknown Driver Used');
return;
}
2017-09-29 13:45:44 +00:00
}
1;