2017-05-16 20:30:38 +00:00
|
|
|
package Pear::LocalLoop::Controller::Api::Stats;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
2017-06-06 21:30:02 +00:00
|
|
|
use List::Util qw/ first /;
|
|
|
|
|
|
|
|
has error_messages => sub {
|
|
|
|
return {
|
|
|
|
type => {
|
|
|
|
required => { message => 'Type of Leaderboard Required', status => 400 },
|
|
|
|
in_resultset => { message => 'Unrecognised Leaderboard Type', status => 400 },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-05-16 21:45:49 +00:00
|
|
|
sub post_index {
|
2017-05-16 20:30:38 +00:00
|
|
|
my $c = shift;
|
|
|
|
|
2017-12-15 15:30:47 +00:00
|
|
|
my $entity = $c->stash->{api_user}->entity;
|
2017-05-16 20:30:38 +00:00
|
|
|
|
2017-12-14 20:30:44 +00:00
|
|
|
my $duration = DateTime::Duration->new( weeks => 7 );
|
|
|
|
my $end = DateTime->today;
|
|
|
|
my $start = $end->clone->subtract_duration( $duration );
|
2017-05-16 20:30:38 +00:00
|
|
|
|
2017-12-15 14:59:38 +00:00
|
|
|
my $weeks = { purchases => [] };
|
|
|
|
my $sectors = { sectors => [], purchases => [] };
|
2017-05-16 21:45:49 +00:00
|
|
|
|
2017-12-14 20:30:44 +00:00
|
|
|
my $dtf = $c->schema->storage->datetime_parser;
|
|
|
|
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
2017-12-15 15:30:47 +00:00
|
|
|
my $week_transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search(
|
2017-12-14 20:30:44 +00:00
|
|
|
{
|
|
|
|
purchase_time => {
|
|
|
|
-between => [
|
|
|
|
$dtf->format_datetime($start),
|
|
|
|
$dtf->format_datetime($end),
|
|
|
|
],
|
|
|
|
},
|
2017-12-15 15:30:47 +00:00
|
|
|
buyer_id => $entity->id,
|
2017-12-14 20:30:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
columns => [
|
|
|
|
{
|
|
|
|
quantised => 'quantised_weeks',
|
|
|
|
count => \"COUNT(*)",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
group_by => 'quantised_weeks',
|
|
|
|
order_by => { '-asc' => 'quantised_weeks' },
|
|
|
|
}
|
|
|
|
);
|
2017-05-16 21:45:49 +00:00
|
|
|
|
2017-12-15 15:30:47 +00:00
|
|
|
for ( $week_transaction_rs->all ) {
|
2017-12-15 14:59:38 +00:00
|
|
|
push @{ $weeks->{ purchases } }, ($_->get_column('count') || 0);
|
2017-12-14 20:30:44 +00:00
|
|
|
}
|
2017-07-19 16:29:00 +00:00
|
|
|
|
2017-12-15 15:30:47 +00:00
|
|
|
my $sector_purchase_rs = $entity->purchases->search({},
|
|
|
|
{
|
|
|
|
join => { 'seller' => 'organisation' },
|
|
|
|
columns => {
|
|
|
|
sector => "organisation.sector",
|
|
|
|
count => \"COUNT(*)",
|
|
|
|
},
|
|
|
|
group_by => "organisation.sector",
|
|
|
|
order_by => { '-desc' => "COUNT(*)" },
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
for ( $sector_purchase_rs->all ) {
|
|
|
|
push @{ $sectors->{ sectors } }, $_->get_column('sector');
|
|
|
|
push @{ $sectors->{ purchases } }, ($_->get_column('count') || 0);
|
|
|
|
}
|
|
|
|
|
2017-05-16 20:30:38 +00:00
|
|
|
return $c->render( json => {
|
|
|
|
success => Mojo::JSON->true,
|
2017-12-15 14:59:38 +00:00
|
|
|
weeks => $weeks,
|
|
|
|
sectors => $sectors,
|
2017-05-16 20:30:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-06 21:30:02 +00:00
|
|
|
sub post_leaderboards {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $validation = $c->validation;
|
|
|
|
$validation->input( $c->stash->{api_json} );
|
|
|
|
|
|
|
|
my $leaderboard_rs = $c->schema->resultset('Leaderboard');
|
|
|
|
|
|
|
|
$validation->required('type')->in_resultset( 'type', $leaderboard_rs );
|
|
|
|
|
|
|
|
return $c->api_validation_error if $validation->has_error;
|
|
|
|
|
|
|
|
my $today_board = $leaderboard_rs->get_latest( $validation->param('type') );
|
|
|
|
|
|
|
|
my $today_values = $today_board->values->search(
|
|
|
|
{},
|
|
|
|
{
|
2017-06-13 22:15:10 +00:00
|
|
|
order_by => { -asc => 'me.position' },
|
2017-06-06 21:30:02 +00:00
|
|
|
columns => [
|
|
|
|
qw/
|
|
|
|
me.value
|
|
|
|
me.trend
|
2017-06-13 22:15:10 +00:00
|
|
|
me.position
|
2017-06-06 21:30:02 +00:00
|
|
|
/,
|
|
|
|
{ display_name => 'customer.display_name' },
|
|
|
|
],
|
2017-08-31 17:31:42 +00:00
|
|
|
join => { entity => 'customer' },
|
2017-06-06 21:30:02 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
$today_values->result_class( 'DBIx::Class::ResultClass::HashRefInflator' );
|
|
|
|
|
|
|
|
my @leaderboard_array = $today_values->all;
|
|
|
|
|
2017-09-13 15:07:23 +00:00
|
|
|
if ( $validation->param('type') =~ /total$/ ) {
|
2017-09-15 11:54:36 +00:00
|
|
|
@leaderboard_array = (map {
|
|
|
|
{
|
|
|
|
%$_,
|
|
|
|
value => $_->{value} / 100000,
|
|
|
|
}
|
|
|
|
} @leaderboard_array);
|
2017-09-13 15:07:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 17:31:42 +00:00
|
|
|
my $current_user_position = $today_values->find({ entity_id => $c->stash->{api_user}->entity->id });
|
2017-06-06 21:30:02 +00:00
|
|
|
|
|
|
|
return $c->render( json => {
|
|
|
|
success => Mojo::JSON->true,
|
|
|
|
leaderboard => [ @leaderboard_array ],
|
2017-06-13 22:15:10 +00:00
|
|
|
user_position => defined $current_user_position ? $current_user_position->{position} : 0,
|
2017-06-06 21:30:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-10 16:45:58 +00:00
|
|
|
sub post_leaderboards_paged {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $validation = $c->validation;
|
|
|
|
$validation->input( $c->stash->{api_json} );
|
|
|
|
|
|
|
|
my $leaderboard_rs = $c->schema->resultset('Leaderboard');
|
|
|
|
|
|
|
|
$validation->required('type')->in_resultset( 'type', $leaderboard_rs );
|
|
|
|
$validation->optional('page')->number;
|
|
|
|
|
|
|
|
return $c->api_validation_error if $validation->has_error;
|
|
|
|
|
|
|
|
my $page = 1;
|
|
|
|
|
|
|
|
my $today_board = $leaderboard_rs->get_latest( $validation->param('type') );
|
|
|
|
|
|
|
|
if ( !defined $validation->param('page') || $validation->param('page') < 1 ) {
|
|
|
|
my $user_position = $today_board->values->find({ entity_id => $c->stash->{api_user}->entity->id });
|
2017-11-10 18:39:00 +00:00
|
|
|
$page = int(defined $user_position ? $user_position->{position} : 0 / 10) + 1;
|
|
|
|
} else {
|
|
|
|
$page = $validation->param('page');
|
2017-11-10 16:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $today_values = $today_board->values->search(
|
|
|
|
{},
|
|
|
|
{
|
2017-11-10 18:39:00 +00:00
|
|
|
page => $page,
|
2017-11-10 16:45:58 +00:00
|
|
|
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 {
|
|
|
|
{
|
|
|
|
%$_,
|
|
|
|
value => $_->{value} / 100000,
|
|
|
|
}
|
|
|
|
} @leaderboard_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2017-11-10 18:39:00 +00:00
|
|
|
page => $page,
|
|
|
|
count => $today_values->pager->total_entries,
|
2017-11-10 16:45:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-16 20:30:38 +00:00
|
|
|
1;
|