From 17ba1e75fd645bb8235104ee5f7000aaed7b49dc Mon Sep 17 00:00:00 2001 From: Finn Date: Tue, 19 Dec 2017 18:03:15 +0000 Subject: [PATCH] frontpage stats amended for sectors and weeks --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 104 ++++++++++++--------- t/api/stats.t | 8 +- 2 files changed, 65 insertions(+), 47 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index ccbb435..8545406 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -1,7 +1,7 @@ package Pear::LocalLoop::Controller::Api::Stats; use Mojo::Base 'Mojolicious::Controller'; -use List::Util qw/ first /; +use List::Util qw/ max sum /; has error_messages => sub { return { @@ -67,9 +67,6 @@ sub post_customer { my $end = DateTime->today; my $start = $end->clone->subtract_duration( $duration ); - my $weeks = { purchases => [] }; - my $sectors = { sectors => [], purchases => [] }; - my $dtf = $c->schema->storage->datetime_parser; my $driver = $c->schema->storage->dbh->{Driver}->{Name}; my $week_transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search( @@ -94,9 +91,22 @@ sub post_customer { } ); - for ( $week_transaction_rs->all ) { - push @{ $weeks->{ purchases } }, ($_->get_column('count') || 0); - } + my @all_weeks = $week_transaction_rs->all; + 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({}, { @@ -189,52 +199,58 @@ sub post_leaderboards_paged { my $page = 1; 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 ) { - my $user_position = $today_board->values->find({ entity_id => $c->stash->{api_user}->entity->id }); - $page = int(defined $user_position ? $user_position->{position} : 0 / 10) + 1; - } else { - $page = $validation->param('page'); - } + if ( !defined $validation->param('page') || $validation->param('page') < 1 ) { + my $user_position = $today_board->values->find({ entity_id => $c->stash->{api_user}->entity->id }); + $page = int(defined $user_position ? $user_position->{position} : 0 / 10) + 1; + } else { + $page = $validation->param('page'); + } - my $today_values = $today_board->values->search( - {}, - { - page => $page, - rows => 10, - order_by => { -asc => 'me.position' }, - columns => [ - qw/ - me.value - me.trend - me.position - /, - { display_name => 'customer.display_name' }, - ], - join => { entity => 'customer' }, - }, - ); - $today_values->result_class( 'DBIx::Class::ResultClass::HashRefInflator' ); - - my @leaderboard_array = $today_values->all; - - if ( $validation->param('type') =~ /total$/ ) { - @leaderboard_array = (map { + my $today_values = $today_board->values->search( + {}, { - %$_, - value => $_->{value} / 100000, - } - } @leaderboard_array); + page => $page, + rows => 10, + order_by => { -asc => 'me.position' }, + columns => [ + qw/ + me.value + me.trend + me.position + /, + { display_name => 'customer.display_name' }, + ], + join => { entity => 'customer' }, + }, + ); + $today_values->result_class( 'DBIx::Class::ResultClass::HashRefInflator' ); + + @leaderboard_array = $today_values->all; + + $values_count = $today_values->pager->total_entries; + + if ( $validation->param('type') =~ /total$/ ) { + @leaderboard_array = (map { + { + %$_, + value => $_->{value} / 100000, + } + } @leaderboard_array); + } + + $current_user_position = $today_values->find({ entity_id => $c->stash->{api_user}->entity->id }); } - - my $current_user_position = $today_values->find({ entity_id => $c->stash->{api_user}->entity->id }); - return $c->render( json => { success => Mojo::JSON->true, leaderboard => [ @leaderboard_array ], user_position => defined $current_user_position ? $current_user_position->{position} : 0, page => $page, - count => $today_values->pager->total_entries, + count => $values_count, }); } diff --git a/t/api/stats.t b/t/api/stats.t index 4fe849b..3df45c8 100644 --- a/t/api/stats.t +++ b/t/api/stats.t @@ -44,14 +44,16 @@ my $session_key = $framework->login({ password => 'abc123', }); -#TODO be able to define start and end below in request - $t->post_ok('/api/stats/customer' => json => { session_key => $session_key, }) ->status_is(200)->or($framework->dump_error) ->json_is('/weeks', { - purchases => [ 2, 21, 20, 21, 19, 22, 13 ], + first => 2, + second => 21, + max => 22, + sum => 118, + count => 7, }) ->json_is('/sectors', { sectors => ['A'],