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 2bbc1d508e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 47 deletions

View file

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

View file

@ -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'],