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-08-31 17:04:51 +00:00
|
|
|
my $user = $c->stash->{api_user}->entity;
|
2017-05-16 20:30:38 +00:00
|
|
|
|
2017-08-31 17:04:51 +00:00
|
|
|
my $today_rs = $user->purchases->today_rs;
|
2017-09-13 15:07:23 +00:00
|
|
|
my $today_sum = $today_rs->get_column('value')->sum || 0;
|
2017-05-16 20:30:38 +00:00
|
|
|
my $today_count = $today_rs->count;
|
|
|
|
|
2017-08-31 17:04:51 +00:00
|
|
|
my $week_rs = $user->purchases->week_rs;
|
2017-09-13 15:07:23 +00:00
|
|
|
my $week_sum = $week_rs->get_column('value')->sum || 0;
|
2017-05-16 21:45:49 +00:00
|
|
|
my $week_count = $week_rs->count;
|
|
|
|
|
2017-08-31 17:04:51 +00:00
|
|
|
my $month_rs = $user->purchases->month_rs;
|
2017-09-13 15:07:23 +00:00
|
|
|
my $month_sum = $month_rs->get_column('value')->sum || 0;
|
2017-05-16 21:45:49 +00:00
|
|
|
my $month_count = $month_rs->count;
|
|
|
|
|
2017-08-31 17:04:51 +00:00
|
|
|
my $user_rs = $user->purchases;
|
2017-09-13 15:07:23 +00:00
|
|
|
my $user_sum = $user_rs->get_column('value')->sum || 0;
|
2017-05-16 21:45:49 +00:00
|
|
|
my $user_count = $user_rs->count;
|
|
|
|
|
|
|
|
my $global_rs = $c->schema->resultset('Transaction');
|
2017-09-13 15:07:23 +00:00
|
|
|
my $global_sum = $global_rs->get_column('value')->sum || 0;
|
2017-05-16 21:45:49 +00:00
|
|
|
my $global_count = $global_rs->count;
|
|
|
|
|
2017-07-19 16:29:00 +00:00
|
|
|
my $leaderboard_rs = $c->schema->resultset('Leaderboard');
|
|
|
|
my $monthly_board = $leaderboard_rs->get_latest( 'monthly_total' );
|
2017-07-20 16:41:59 +00:00
|
|
|
my $monthly_values = $monthly_board->values;
|
2017-08-31 17:04:51 +00:00
|
|
|
my $current_user_position = $monthly_values ? $monthly_values->find({ entity_id => $user->id }) : undef;
|
2017-07-19 16:29:00 +00:00
|
|
|
|
2017-05-16 20:30:38 +00:00
|
|
|
return $c->render( json => {
|
|
|
|
success => Mojo::JSON->true,
|
2017-09-13 15:07:23 +00:00
|
|
|
today_sum => $today_sum / 100000,
|
2017-05-16 20:30:38 +00:00
|
|
|
today_count => $today_count,
|
2017-09-13 15:07:23 +00:00
|
|
|
week_sum => $week_sum / 100000,
|
2017-05-16 21:45:49 +00:00
|
|
|
week_count => $week_count,
|
2017-09-13 15:07:23 +00:00
|
|
|
month_sum => $month_sum / 100000,
|
2017-05-16 21:45:49 +00:00
|
|
|
month_count => $month_count,
|
2017-09-13 15:07:23 +00:00
|
|
|
user_sum => $user_sum / 100000,
|
2017-05-16 21:45:49 +00:00
|
|
|
user_count => $user_count,
|
2017-09-13 15:07:23 +00:00
|
|
|
global_sum => $global_sum / 100000,
|
2017-05-16 21:45:49 +00:00
|
|
|
global_count => $global_count,
|
2017-07-20 10:56:24 +00:00
|
|
|
user_position => defined $current_user_position ? $current_user_position->position : 0,
|
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$/ ) {
|
|
|
|
map { $_->{value} / 100000 } @leaderboard_array;
|
|
|
|
}
|
|
|
|
|
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-05-16 20:30:38 +00:00
|
|
|
1;
|