Added new stats and fixed test

This commit is contained in:
Finn 2017-12-14 20:30:44 +00:00
parent 12bd6b3ec9
commit cb39b419d6
5 changed files with 97 additions and 116 deletions

View file

@ -17,44 +17,42 @@ sub post_index {
my $user = $c->stash->{api_user}->entity;
my $today_rs = $user->purchases->today_rs;
my $today_sum = $today_rs->get_column('value')->sum || 0;
my $today_count = $today_rs->count;
my $duration = DateTime::Duration->new( weeks => 7 );
my $end = DateTime->today;
my $start = $end->clone->subtract_duration( $duration );
my $week_rs = $user->purchases->week_rs;
my $week_sum = $week_rs->get_column('value')->sum || 0;
my $week_count = $week_rs->count;
my $data = { purchases => [] };
my $month_rs = $user->purchases->month_rs;
my $month_sum = $month_rs->get_column('value')->sum || 0;
my $month_count = $month_rs->count;
my $dtf = $c->schema->storage->datetime_parser;
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
my $transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search(
{
purchase_time => {
-between => [
$dtf->format_datetime($start),
$dtf->format_datetime($end),
],
},
},
{
columns => [
{
quantised => 'quantised_weeks',
count => \"COUNT(*)",
}
],
group_by => 'quantised_weeks',
order_by => { '-asc' => 'quantised_weeks' },
}
);
my $user_rs = $user->purchases;
my $user_sum = $user_rs->get_column('value')->sum || 0;
my $user_count = $user_rs->count;
my $global_rs = $c->schema->resultset('Transaction');
my $global_sum = $global_rs->get_column('value')->sum || 0;
my $global_count = $global_rs->count;
my $leaderboard_rs = $c->schema->resultset('Leaderboard');
my $monthly_board = $leaderboard_rs->get_latest( 'monthly_total' );
my $monthly_values = $monthly_board->values;
my $current_user_position = $monthly_values ? $monthly_values->find({ entity_id => $user->id }) : undef;
for ( $transaction_rs->all ) {
push @{ $data->{ purchases } }, ($_->get_column('count') || 0);
}
return $c->render( json => {
success => Mojo::JSON->true,
today_sum => $today_sum / 100000,
today_count => $today_count,
week_sum => $week_sum / 100000,
week_count => $week_count,
month_sum => $month_sum / 100000,
month_count => $month_count,
user_sum => $user_sum / 100000,
user_count => $user_count,
global_sum => $global_sum / 100000,
global_count => $global_count,
user_position => defined $current_user_position ? $current_user_position->position : 0,
data => $data,
});
}

View file

@ -0,0 +1,28 @@
package Pear::LocalLoop::Controller::Api::V1::Customer::Pies;
use Mojo::Base 'Mojolicious::Controller';
sub index {
my $c = shift;
my $entity = $c->stash->{api_user}->entity;
my $data = { data => [] };
my $data = {
'Local shop local purchaser' => 20,
'Local shop non-local purchaser' => 20,
'Non-local shop local purchaser' => 20,
'Non-local shop non-local purchaser' => 20,
};
#TODO insert code fetching numbers here
return $c->render(
json => {
success => Mojo::JSON->true,
pie => $data,
}
);
}
1;

View file

@ -14,7 +14,8 @@ SELECT "value",
"distance",
"purchase_time",
DATE_TRUNC('hour', "purchase_time") AS "quantised_hours",
DATE_TRUNC('day', "purchase_time") AS "quantised_days"
DATE_TRUNC('day', "purchase_time") AS "quantised_days",
DATE_TRUNC('week', "purchase_time") AS "quantised_weeks"
FROM "transactions"
/);

View file

@ -14,7 +14,8 @@ SELECT "value",
"distance",
"purchase_time",
DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"purchase_time")) AS "quantised_hours",
DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time")) AS "quantised_days"
DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time")) AS "quantised_days",
DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time", 'weekday 1')) AS "quantised_weeks"
FROM "transactions"
/);

View file

@ -14,98 +14,51 @@ $framework->install_fixtures('users');
my $t = $framework->framework;
my $schema = $t->app->schema;
my $dtf = $schema->storage->datetime_parser;
my $org_result = $schema->resultset('Organisation')->find({ name => 'Test Org' })->entity;
my $user_result = $schema->resultset('User')->find({ email => 'test1@example.com' })->entity;
my $start = DateTime->today->subtract( hours => 12 );
# create 40 days worth of data
for my $count ( 0 .. 40 ) {
my $trans_day = $start->clone->subtract( days => $count );
create_random_transaction( 'test1@example.com', $trans_day );
if ( $count % 2 ) {
create_random_transaction( 'test1@example.com', $trans_day );
}
if ( $count % 3 ) {
create_random_transaction( 'test1@example.com', $trans_day );
}
if ( $count % 4 ) {
create_random_transaction( 'test1@example.com', $trans_day );
}
}
my $session_key = $framework->login({
email => 'test1@example.com',
password => 'abc123',
});
$t->app->schema->resultset('Leaderboard')->create_new( 'monthly_total', DateTime->now->truncate(to => 'month' )->subtract( months => 1) );
$t->post_ok('/api/stats' => json => { session_key => $session_key } )
$t->post_ok('/api/stats' => json => {
session_key => $session_key,
})
->status_is(200)->or($framework->dump_error)
->json_is('/success', Mojo::JSON->true)
->json_is('/today_sum', 0)
->json_is('/today_count', 0)
->json_is('/week_sum', 0)
->json_is('/week_count', 0)
->json_is('/month_sum', 0)
->json_is('/month_count', 0)
->json_is('/user_sum', 0)
->json_is('/user_count', 0)
->json_is('/global_sum', 0)
->json_is('/global_count', 0);
->json_is('/data', {
purchases => [ 12, 19, 21, 20, 21, 20, 5 ],
});
for ( 1 .. 10 ) {
$user_result->create_related( 'purchases', {
seller_id => $org_result->id,
value => $_ * 100000,
sub create_random_transaction {
my $buyer = shift;
my $time = shift;
my $buyer_result = $schema->resultset('User')->find({ email => $buyer })->entity;
my $seller_result = $schema->resultset('Organisation')->find({ name => 'Test Org' })->entity;
$schema->resultset('Transaction')->create({
buyer => $buyer_result,
seller => $seller_result,
value => 10 * 100000,
proof_image => 'a',
purchase_time => $time,
});
}
for ( 11 .. 20 ) {
$user_result->create_related( 'purchases', {
seller_id => $org_result->id,
value => $_ * 100000,
proof_image => 'a',
purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 5 )),
});
}
for ( 21 .. 30 ) {
$user_result->create_related( 'purchases', {
seller_id => $org_result->id,
value => $_ * 100000,
proof_image => 'a',
purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 25 )),
});
}
for ( 31 .. 40 ) {
$user_result->create_related( 'purchases', {
seller_id => $org_result->id,
value => $_ * 100000,
proof_image => 'a',
purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 50 )),
});
}
for ( 41 .. 50 ) {
$org_result->create_related( 'purchases', {
seller_id => $org_result->id,
value => $_ * 100000,
proof_image => 'a',
purchase_time => $dtf->format_datetime(DateTime->today()->subtract( days => 50 )),
});
}
is $user_result->purchases->search({
purchase_time => {
-between => [
$dtf->format_datetime(DateTime->today()),
$dtf->format_datetime(DateTime->today()->add( days => 1 )),
],
},
})->get_column('value')->sum, 5500000, 'Got correct sum';
is $user_result->purchases->today_rs->get_column('value')->sum, 5500000, 'Got correct sum through rs';
$t->post_ok('/api/stats' => json => { session_key => $session_key } )
->status_is(200)
->json_is('/success', Mojo::JSON->true)
->json_is('/today_sum', 55)
->json_is('/today_count', 10)
->json_is('/week_sum', 155)
->json_is('/week_count', 10)
->json_is('/month_sum', 410)
->json_is('/month_count', 20)
->json_is('/user_sum', 820)
->json_is('/user_count', 40)
->json_is('/global_sum', 1275)
->json_is('/global_count', 50);
done_testing;