Add leaderboard stats endpoint and trends

This commit is contained in:
Tom Bloor 2017-06-06 22:30:02 +01:00
parent daeeac811c
commit 9cd9f8079e
6 changed files with 557 additions and 0 deletions

View file

@ -148,6 +148,7 @@ sub startup {
$api->post('/fetchuser')->to('api-api#post_fetchuser');
$api->post('/user-history')->to('api-user#post_user_history');
$api->post('/stats')->to('api-stats#post_index');
$api->post('/stats/leaderboard')->to('api-stats#post_leaderboards');
my $api_admin = $api->under('/')->to('api-admin#auth');

View file

@ -1,6 +1,17 @@
package Pear::LocalLoop::Controller::Api::Stats;
use Mojo::Base 'Mojolicious::Controller';
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 },
},
};
};
sub post_index {
my $c = shift;
@ -41,4 +52,49 @@ sub post_index {
});
}
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(
{},
{
order_by => { -desc => 'me.value' },
columns => [
qw/
me.value
me.trend
me.user_id
/,
{ display_name => 'customer.display_name' },
],
join => { user => 'customer' },
},
);
$today_values->result_class( 'DBIx::Class::ResultClass::HashRefInflator' );
my @leaderboard_array = $today_values->all;
my $current_user_index = first { $leaderboard_array[$_]->{user_id} == $c->stash->{api_user}->id } 0..$#leaderboard_array;
# Dont leak user id's
map { delete $_->{user_id} } @leaderboard_array;
return $c->render( json => {
success => Mojo::JSON->true,
leaderboard => [ @leaderboard_array ],
user_position => $current_user_index,
});
}
1;

View file

@ -79,14 +79,39 @@ sub _create_total_set {
my @leaderboard;
my $previous_board = $self->get_latest;
if ( defined $previous_board ) {
$previous_board = $previous_board->values;
}
while ( my $user_result = $user_rs->next ) {
my $transaction_rs = $user_result->transactions->search_between( $start, $end );
my $transaction_sum = $transaction_rs->get_column('value')->sum;
my $previous_value;
if ( defined $previous_board ) {
$previous_value = $previous_board->find({ user_id => $user_result->id });
}
my $trend;
if ( ! defined $previous_value ) {
$trend = 0;
} elsif ( $previous_value->value > $transaction_sum ) {
$trend = -1;
} elsif ( $previous_value->value < $transaction_sum ) {
$trend = 1;
} else {
$trend = 0;
}
push @leaderboard, {
user_id => $user_result->id,
value => $transaction_sum || 0,
trend => $trend,
};
}
@ -108,14 +133,39 @@ sub _create_count_set {
my @leaderboard;
my $previous_board = $self->get_latest;
if ( defined $previous_board ) {
$previous_board = $previous_board->values;
}
while ( my $user_result = $user_rs->next ) {
my $transaction_rs = $user_result->transactions->search_between( $start, $end );
my $transaction_count = $transaction_rs->count;
my $previous_value;
if ( defined $previous_board ) {
$previous_value = $previous_board->find({ user_id => $user_result->id });
}
my $trend;
if ( ! defined $previous_value ) {
$trend = 0;
} elsif ( $previous_value->value > $transaction_count ) {
$trend = -1;
} elsif ( $previous_value->value < $transaction_count ) {
$trend = 1;
} else {
$trend = 0;
}
push @leaderboard, {
user_id => $user_result->id,
value => $transaction_count || 0,
trend => $trend,
};
}
@ -137,11 +187,35 @@ sub _create_total_all_time {
my @leaderboard;
my $previous_board = $self->get_latest;
if ( defined $previous_board ) {
$previous_board = $previous_board->values;
}
while ( my $user_result = $user_rs->next ) {
my $transaction_rs = $user_result->transactions;
my $transaction_sum = $transaction_rs->get_column('value')->sum;
my $previous_value;
if ( defined $previous_board ) {
$previous_value = $previous_board->find({ user_id => $user_result->id });
}
my $trend;
if ( ! defined $previous_value ) {
$trend = 0;
} elsif ( $previous_value->value > $transaction_sum ) {
$trend = -1;
} elsif ( $previous_value->value < $transaction_sum ) {
$trend = 1;
} else {
$trend = 0;
}
push @leaderboard, {
user_id => $user_result->id,
value => $transaction_sum || 0,
@ -166,14 +240,39 @@ sub _create_count_all_time {
my @leaderboard;
my $previous_board = $self->get_latest;
if ( defined $previous_board ) {
$previous_board = $previous_board->values;
}
while ( my $user_result = $user_rs->next ) {
my $transaction_rs = $user_result->transactions;
my $transaction_count = $transaction_rs->count;
my $previous_value;
if ( defined $previous_board ) {
$previous_value = $previous_board->find({ user_id => $user_result->id });
}
my $trend;
if ( ! defined $previous_value ) {
$trend = 0;
} elsif ( $previous_value->value > $transaction_count ) {
$trend = -1;
} elsif ( $previous_value->value < $transaction_count ) {
$trend = 1;
} else {
$trend = 0;
}
push @leaderboard, {
user_id => $user_result->id,
value => $transaction_count || 0,
trend => $trend,
};
}

View file

@ -28,6 +28,10 @@ __PACKAGE__->add_columns(
size => [ 16, 2 ],
is_nullable => 0,
},
"trend" => {
data_type => "integer",
default_value => 0,
},
);
__PACKAGE__->set_primary_key("id");

198
t/api/stats_leaderboards.t Normal file
View file

@ -0,0 +1,198 @@
use Mojo::Base -strict;
use Test::More;
use Mojo::JSON;
use Test::Pear::LocalLoop;
use DateTime;
my $framework = Test::Pear::LocalLoop->new;
my $t = $framework->framework;
my $schema = $t->app->schema;
my $dtf = $schema->storage->datetime_parser;
my $user1 = {
token => 'a',
full_name => 'Test User1',
display_name => 'Test User1',
email => 'test1@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
};
my $user2 = {
token => 'b',
full_name => 'Test User2',
display_name => 'Test User2',
email => 'test2@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
};
my $user3 = {
token => 'c',
full_name => 'Test User3',
display_name => 'Test User3',
email => 'test3@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
};
my $user4 = {
token => 'd',
full_name => 'Test User4',
display_name => 'Test User4',
email => 'test4@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
};
my $org = {
token => 'e',
email => 'test5@example.com',
name => 'Test Org',
street_name => 'Test Street',
town => 'Lancaster',
postcode => 'LA1 1AA',
password => 'abc123',
};
$schema->resultset('AccountToken')->create({ name => $_->{token} })
for ( $user1, $user2, $user3, $user4, $org );
$framework->register_customer($_)
for ( $user1, $user2, $user3, $user4 );
$framework->register_organisation($org);
my $org_result = $schema->resultset('Organisation')->find({ name => $org->{name} });
my $tweak = 0;
my $now = DateTime->today();
{
my $user_result = $schema->resultset('User')->find({ email => $user1->{email} });
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 1,
proof_image => 'a',
});
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 9,
proof_image => 'a',
submitted_at => $dtf->format_datetime($now->clone->subtract( days => 1 )),
});
}
{
my $user_result = $schema->resultset('User')->find({ email => $user2->{email} });
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 3,
proof_image => 'a',
});
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 1,
proof_image => 'a',
submitted_at => $dtf->format_datetime($now->clone->subtract( days => 1 )),
});
}
{
my $user_result = $schema->resultset('User')->find({ email => $user3->{email} });
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 5,
proof_image => 'a',
});
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 5,
proof_image => 'a',
submitted_at => $dtf->format_datetime($now->clone->subtract( days => 1 )),
});
}
{
my $user_result = $schema->resultset('User')->find({ email => $user4->{email} });
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 9,
proof_image => 'a',
});
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 3,
proof_image => 'a',
submitted_at => $dtf->format_datetime($now->clone->subtract( days => 1 )),
});
}
my $session_key = $framework->login({
email => $user1->{email},
password => $user1->{password},
});
sub test_leaderboard {
my ( $title, $name, $date, $expected, $user_place ) = @_;
subtest $title => sub {
my $leaderboard_rs = $schema->resultset('Leaderboard');
$leaderboard_rs->create_new( $name, $date );
$t->post_ok('/api/stats/leaderboard' => json => { session_key => $session_key, type => $name } )
->status_is(200)
->or($framework->dump_error)
->json_is('/success', Mojo::JSON->true)
->or($framework->dump_error)
->json_is('/leaderboard', $expected)
->or($framework->dump_error)
->json_is('/user_position', $user_place)
->or($framework->dump_error);
};
}
$schema->resultset('Leaderboard')->create_new( 'daily_total', $now->clone->subtract( days => 1 ) );
test_leaderboard(
'Daily Total',
'daily_total',
$now,
[
{ display_name => 'Test User4', value => 9, trend => 1 },
{ display_name => 'Test User3', value => 5, trend => 0 },
{ display_name => 'Test User2', value => 3, trend => 1 },
{ display_name => 'Test User1', value => 1, trend => -1},
],
3
);
test_leaderboard(
'Daily Count',
'daily_count',
$now,
[
{ display_name => 'Test User1', value => 1, trend => 0 },
{ display_name => 'Test User2', value => 1, trend => 0 },
{ display_name => 'Test User3', value => 1, trend => 0 },
{ display_name => 'Test User4', value => 1, trend => 0 },
],
0
);
done_testing;

View file

@ -0,0 +1,199 @@
use Mojo::Base -strict;
use Test::More;
use Mojo::JSON;
use Test::Pear::LocalLoop;
use DateTime;
my $framework = Test::Pear::LocalLoop->new;
my $t = $framework->framework;
my $schema = $t->app->schema;
my $dtf = $schema->storage->datetime_parser;
my $user1 = {
token => 'a',
full_name => 'Test User1',
display_name => 'Test User1',
email => 'test1@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
};
my $user2 = {
token => 'b',
full_name => 'Test User2',
display_name => 'Test User2',
email => 'test2@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
};
my $user3 = {
token => 'c',
full_name => 'Test User3',
display_name => 'Test User3',
email => 'test3@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
};
my $user4 = {
token => 'd',
full_name => 'Test User4',
display_name => 'Test User4',
email => 'test4@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
};
my $org = {
token => 'e',
email => 'test5@example.com',
name => 'Test Org',
street_name => 'Test Street',
town => 'Lancaster',
postcode => 'LA1 1AA',
password => 'abc123',
};
$schema->resultset('AccountToken')->create({ name => $_->{token} })
for ( $user1, $user2, $user3, $user4, $org );
$framework->register_customer($_)
for ( $user1, $user2, $user3, $user4 );
$framework->register_organisation($org);
my $org_result = $schema->resultset('Organisation')->find({ name => $org->{name} });
my $tweak = 0;
my $now = DateTime->today();
{
my $user_result = $schema->resultset('User')->find({ email => $user1->{email} });
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 1,
proof_image => 'a',
});
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 9,
proof_image => 'a',
submitted_at => $dtf->format_datetime($now->clone->subtract( days => 1 )),
});
}
{
my $user_result = $schema->resultset('User')->find({ email => $user2->{email} });
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 3,
proof_image => 'a',
});
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 1,
proof_image => 'a',
submitted_at => $dtf->format_datetime($now->clone->subtract( days => 1 )),
});
}
{
my $user_result = $schema->resultset('User')->find({ email => $user3->{email} });
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 5,
proof_image => 'a',
});
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 5,
proof_image => 'a',
submitted_at => $dtf->format_datetime($now->clone->subtract( days => 1 )),
});
}
{
my $user_result = $schema->resultset('User')->find({ email => $user4->{email} });
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 9,
proof_image => 'a',
});
$user_result->create_related( 'transactions', {
seller_id => $org_result->id,
value => 3,
proof_image => 'a',
submitted_at => $dtf->format_datetime($now->clone->subtract( days => 1 )),
});
}
my $session_key = $framework->login({
email => $user1->{email},
password => $user1->{password},
});
sub test_leaderboard {
my ( $title, $name, $date, $expected ) = @_;
subtest $title => sub {
my $leaderboard_rs = $schema->resultset('Leaderboard');
my $today_board = $leaderboard_rs->find({ type => $name })->create_new($date)->get_latest;
is $today_board->values->count, 4, 'correct value count';
my $today_values = $today_board->values->search(
{},
{
order_by => { -desc => 'value' },
columns => [ qw/ user_id value trend / ],
},
);
$today_values->result_class( 'DBIx::Class::ResultClass::HashRefInflator' );
is_deeply [ $today_values->all ], $expected, 'array as expected';
};
}
$schema->resultset('Leaderboard')->find({ type => 'daily_total' })->create_new( $now->clone->subtract( days => 1 ) );
test_leaderboard(
'Daily Total',
'daily_total',
$now,
[
{ user_id => 4, value => 9, trend => 1 },
{ user_id => 3, value => 5, trend => 0 },
{ user_id => 2, value => 3, trend => 1 },
{ user_id => 1, value => 1, trend => -1},
]
);
test_leaderboard(
'Daily Count',
'daily_count',
$now,
[
{ user_id => 1, value => 1, trend => 0 },
{ user_id => 2, value => 1, trend => 0 },
{ user_id => 3, value => 1, trend => 0 },
{ user_id => 4, value => 1, trend => 0 },
]
);
done_testing;