diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index a2f3ce1..b9eed08 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -193,6 +193,8 @@ sub startup { $admin_routes->get('/transactions/:id/image')->to('admin-transactions#image'); $admin_routes->post('/transactions/:id/delete')->to('admin-transactions#delete'); + $admin_routes->get('/reports/transactions')->to('admin-reports#transaction_data'); + # my $user_routes = $r->under('/')->to('root#under'); # $user_routes->get('/home')->to('root#home'); diff --git a/lib/Pear/LocalLoop/Controller/Admin.pm b/lib/Pear/LocalLoop/Controller/Admin.pm index 8030d59..7a9cf8d 100644 --- a/lib/Pear/LocalLoop/Controller/Admin.pm +++ b/lib/Pear/LocalLoop/Controller/Admin.pm @@ -7,7 +7,7 @@ sub under { if ( $c->is_user_authenticated ) { return 1 if $c->current_user->is_admin; } - $c->redirect_to('/'); + $c->redirect_to('/admin'); return 0; } diff --git a/lib/Pear/LocalLoop/Controller/Admin/Reports.pm b/lib/Pear/LocalLoop/Controller/Admin/Reports.pm new file mode 100644 index 0000000..658d4e5 --- /dev/null +++ b/lib/Pear/LocalLoop/Controller/Admin/Reports.pm @@ -0,0 +1,62 @@ +package Pear::LocalLoop::Controller::Admin::Reports; +use Mojo::Base 'Mojolicious::Controller'; + +use Mojo::JSON qw/ encode_json /; + +sub transaction_data { + my $c = shift; + + my $driver = $c->schema->storage->dbh->{Driver}->{Name}; + my $transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search( + {}, + { + columns => [ + 'quantised_hours', + { + count => \"COUNT(*)", + sum_distance => $c->pg_or_sqlite( + '', + 'SUM("me"."distance")', + ), + average_distance => $c->pg_or_sqlite( + '', + 'AVG("me"."distance")', + ), + sum_value => $c->pg_or_sqlite( + '', + 'SUM("me"."value")', + ), + average_value => $c->pg_or_sqlite( + '', + 'AVG("me"."value")', + ), + } + ], + group_by => 'quantised_hours', + order_by => { '-asc' => 'quantised_hours' }, + } + ); + + $transaction_rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); + + $c->stash( + transaction_rs => encode_json( [$transaction_rs->all] ), + ); +} + +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; diff --git a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm new file mode 100644 index 0000000..adfcd7a --- /dev/null +++ b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm @@ -0,0 +1,12 @@ +package Pear::LocalLoop::Schema::Result::ViewQuantisedTransactionPg; + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; + +__PACKAGE__->table_class('DBIx::Class::ResultSource::View'); +__PACKAGE__->table('view_quantised_transactions'); +__PACKAGE__->result_source_instance->is_virtual(1); + +1; diff --git a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionSQLite.pm b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionSQLite.pm new file mode 100644 index 0000000..2ce3aac --- /dev/null +++ b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionSQLite.pm @@ -0,0 +1,21 @@ +package Pear::LocalLoop::Schema::Result::ViewQuantisedTransactionSQLite; + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; + +__PACKAGE__->table_class('DBIx::Class::ResultSource::View'); +__PACKAGE__->table('view_quantised_transactions'); +__PACKAGE__->result_source_instance->is_virtual(1); + +__PACKAGE__->result_source_instance->view_definition( qq/ +SELECT "value", + "distance", + "purchase_time", + DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"purchase_time")) AS "quantised_hours", + DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time")) AS "quantised_days" + FROM "transactions" +/); + +1; diff --git a/templates/admin/reports/transaction_data.html.ep b/templates/admin/reports/transaction_data.html.ep new file mode 100644 index 0000000..a3457b9 --- /dev/null +++ b/templates/admin/reports/transaction_data.html.ep @@ -0,0 +1,116 @@ +% layout 'admin'; +% title 'Tokens'; +% content_for javascript => begin + + +% end +% if ( my $error = flash 'error' ) { + +% } elsif ( my $success = flash 'success' ) { + +% } +
+
+
+
+%= $transaction_rs;
+