frontpage stats amended for sectors and weeks

This commit is contained in:
Finn 2017-12-19 18:03:15 +00:00
parent 86184cfbdb
commit 17ba1e75fd
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,52 +199,58 @@ 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 });
$page = int(defined $user_position ? $user_position->{position} : 0 / 10) + 1; $page = int(defined $user_position ? $user_position->{position} : 0 / 10) + 1;
} else { } else {
$page = $validation->param('page'); $page = $validation->param('page');
} }
my $today_values = $today_board->values->search( 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 {
{ {
%$_, page => $page,
value => $_->{value} / 100000, rows => 10,
} order_by => { -asc => 'me.position' },
} @leaderboard_array); 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 => { 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'],