Initial pass of Transaction reports

This commit is contained in:
Tom Bloor 2017-09-29 14:45:44 +01:00
parent 2486c701a5
commit cf2c7dcc4b
6 changed files with 214 additions and 1 deletions

View file

@ -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');

View file

@ -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;
}

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -0,0 +1,116 @@
% layout 'admin';
% title 'Tokens';
% content_for javascript => begin
<script src="//www.gstatic.com/charts/loader.js"></script>
<script>
var raw_data = <%== $transaction_rs %>;
var mapped_data = $.map(raw_data, function( val, i ) {
return [[
new Date(val.quantised_hours),
val.count,
val.average_value / 100000,
val.sum_value / 100000,
val.average_distance / 1000,
val.sum_distance / 1000,
]];
});
var data;
google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(loadData);
function loadData() {
data = new google.visualization.DataTable();
data.addColumn('datetime', 'Hours');
data.addColumn('number', 'Count');
data.addColumn('number', 'Average Value');
data.addColumn('number', 'Sum Value');
data.addColumn('number', 'Average Distance');
data.addColumn('number', 'Sum Distance');
data.addRows(mapped_data);
drawCountChart();
drawDistanceChart();
drawValueChart();
}
function drawCountChart() {
var options = {
title: 'Transaction Count',
height: 500,
series: {
0: {targetAxisIndex: 0},
},
vAxes: {
0: { title: 'Count' },
},
explorer: { axis: 'horizontal' }
};
var chart_data = new google.visualization.DataView(data);
chart_data.setColumns([0, 1]);
var chart = new google.visualization.LineChart(document.getElementById('count_chart_div'));
chart.draw(chart_data, options);
}
function drawDistanceChart() {
var options = {
title: 'Transaction Distance',
height: 500,
series: {
0: {targetAxisIndex: 0},
1: {targetAxisIndex: 0},
},
vAxes: {
0: { title: 'Distance (km)' },
},
explorer: { axis: 'horizontal' }
};
var chart_data = new google.visualization.DataView(data);
chart_data.setColumns([0, 4, 5]);
var chart = new google.visualization.LineChart(document.getElementById('distance_chart_div'));
chart.draw(chart_data, options);
}
function drawValueChart() {
var options = {
title: 'Transaction Value',
height: 500,
series: {
0: {targetAxisIndex: 0},
1: {targetAxisIndex: 0},
},
vAxes: {
0: { title: 'Value (GBP)' },
},
explorer: { axis: 'horizontal' }
};
var chart_data = new google.visualization.DataView(data);
chart_data.setColumns([0, 2, 3]);
var chart = new google.visualization.LineChart(document.getElementById('value_chart_div'));
chart.draw(chart_data, options);
}
</script>
% end
% if ( my $error = flash 'error' ) {
<div class="alert alert-danger" role="alert">
<strong>Error!</strong> <%= $error %>
</div>
% } elsif ( my $success = flash 'success' ) {
<div class="alert alert-success" role="alert">
<strong>Success!</strong> <%= $success %>
</div>
% }
<div id="count_chart_div"></div>
<div id="distance_chart_div"></div>
<div id="value_chart_div"></div>
<pre>
%= $transaction_rs;
</pre>