Merge pull request #89 from Pear-Trading/finn/loopdashboard

frontpage stats amended for sectors and weeks
This commit is contained in:
Finn 2017-12-20 13:25:22 +00:00 committed by GitHub
commit cd9420da42
2 changed files with 65 additions and 47 deletions

View file

@ -1,7 +1,7 @@
package Pear::LocalLoop::Controller::Api::Stats; package Pear::LocalLoop::Controller::Api::Stats;
use Mojo::Base 'Mojolicious::Controller'; use Mojo::Base 'Mojolicious::Controller';
use List::Util qw/ first /; use List::Util qw/ max sum /;
has error_messages => sub { has error_messages => sub {
return { return {
@ -67,9 +67,6 @@ sub post_customer {
my $end = DateTime->today; my $end = DateTime->today;
my $start = $end->clone->subtract_duration( $duration ); my $start = $end->clone->subtract_duration( $duration );
my $weeks = { purchases => [] };
my $sectors = { sectors => [], purchases => [] };
my $dtf = $c->schema->storage->datetime_parser; my $dtf = $c->schema->storage->datetime_parser;
my $driver = $c->schema->storage->dbh->{Driver}->{Name}; my $driver = $c->schema->storage->dbh->{Driver}->{Name};
my $week_transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search( my $week_transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search(
@ -94,9 +91,22 @@ sub post_customer {
} }
); );
for ( $week_transaction_rs->all ) { my @all_weeks = $week_transaction_rs->all;
push @{ $weeks->{ purchases } }, ($_->get_column('count') || 0); my $first = $all_weeks[0]->get_column('count') || 0;
} my $second = $all_weeks[1]->get_column('count') || 0;
my $max = max( map { $_->get_column('count') } @all_weeks );
my $sum = sum( map { $_->get_column('count') } @all_weeks );
my $count = $week_transaction_rs->count;
my $weeks = {
first => $first,
second => $second,
max => $max,
sum => $sum,
count => $count,
};
my $sectors = { sectors => [], purchases => [] };
my $sector_purchase_rs = $entity->purchases->search({}, my $sector_purchase_rs = $entity->purchases->search({},
{ {
@ -189,6 +199,10 @@ sub post_leaderboards_paged {
my $page = 1; my $page = 1;
my $today_board = $leaderboard_rs->get_latest( $validation->param('type') ); my $today_board = $leaderboard_rs->get_latest( $validation->param('type') );
my @leaderboard_array;
my $current_user_position;
my $values_count = 0;
if ( defined $today_board ) {
if ( !defined $validation->param('page') || $validation->param('page') < 1 ) { if ( !defined $validation->param('page') || $validation->param('page') < 1 ) {
my $user_position = $today_board->values->find({ entity_id => $c->stash->{api_user}->entity->id }); my $user_position = $today_board->values->find({ entity_id => $c->stash->{api_user}->entity->id });
@ -216,7 +230,9 @@ sub post_leaderboards_paged {
); );
$today_values->result_class( 'DBIx::Class::ResultClass::HashRefInflator' ); $today_values->result_class( 'DBIx::Class::ResultClass::HashRefInflator' );
my @leaderboard_array = $today_values->all; @leaderboard_array = $today_values->all;
$values_count = $today_values->pager->total_entries;
if ( $validation->param('type') =~ /total$/ ) { if ( $validation->param('type') =~ /total$/ ) {
@leaderboard_array = (map { @leaderboard_array = (map {
@ -227,14 +243,14 @@ sub post_leaderboards_paged {
} @leaderboard_array); } @leaderboard_array);
} }
my $current_user_position = $today_values->find({ entity_id => $c->stash->{api_user}->entity->id }); $current_user_position = $today_values->find({ entity_id => $c->stash->{api_user}->entity->id });
}
return $c->render( json => { return $c->render( json => {
success => Mojo::JSON->true, success => Mojo::JSON->true,
leaderboard => [ @leaderboard_array ], leaderboard => [ @leaderboard_array ],
user_position => defined $current_user_position ? $current_user_position->{position} : 0, user_position => defined $current_user_position ? $current_user_position->{position} : 0,
page => $page, page => $page,
count => $today_values->pager->total_entries, count => $values_count,
}); });
} }

View file

@ -44,14 +44,16 @@ my $session_key = $framework->login({
password => 'abc123', password => 'abc123',
}); });
#TODO be able to define start and end below in request
$t->post_ok('/api/stats/customer' => json => { $t->post_ok('/api/stats/customer' => json => {
session_key => $session_key, session_key => $session_key,
}) })
->status_is(200)->or($framework->dump_error) ->status_is(200)->or($framework->dump_error)
->json_is('/weeks', { ->json_is('/weeks', {
purchases => [ 2, 21, 20, 21, 19, 22, 13 ], first => 2,
second => 21,
max => 22,
sum => 118,
count => 7,
}) })
->json_is('/sectors', { ->json_is('/sectors', {
sectors => ['A'], sectors => ['A'],