Add more stats to default stat set
This commit is contained in:
parent
a680d7b9df
commit
9586d1a637
4 changed files with 95 additions and 13 deletions
|
@ -147,7 +147,7 @@ sub startup {
|
||||||
$api->post('/edit')->to('api-api#post_edit');
|
$api->post('/edit')->to('api-api#post_edit');
|
||||||
$api->post('/fetchuser')->to('api-api#post_fetchuser');
|
$api->post('/fetchuser')->to('api-api#post_fetchuser');
|
||||||
$api->post('/user-history')->to('api-user#post_user_history');
|
$api->post('/user-history')->to('api-user#post_user_history');
|
||||||
$api->post('/stats')->to('api-stats#post_today');
|
$api->post('/stats')->to('api-stats#post_index');
|
||||||
|
|
||||||
my $api_admin = $api->under('/')->to('api-admin#auth');
|
my $api_admin = $api->under('/')->to('api-admin#auth');
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package Pear::LocalLoop::Controller::Api::Stats;
|
package Pear::LocalLoop::Controller::Api::Stats;
|
||||||
use Mojo::Base 'Mojolicious::Controller';
|
use Mojo::Base 'Mojolicious::Controller';
|
||||||
|
|
||||||
sub post_today {
|
sub post_index {
|
||||||
my $c = shift;
|
my $c = shift;
|
||||||
|
|
||||||
my $user = $c->stash->{api_user};
|
my $user = $c->stash->{api_user};
|
||||||
|
@ -10,12 +10,35 @@ sub post_today {
|
||||||
my $today_sum = $today_rs->get_column('value')->sum;
|
my $today_sum = $today_rs->get_column('value')->sum;
|
||||||
my $today_count = $today_rs->count;
|
my $today_count = $today_rs->count;
|
||||||
|
|
||||||
|
my $week_rs = $user->transactions->week_rs;
|
||||||
|
my $week_sum = $week_rs->get_column('value')->sum;
|
||||||
|
my $week_count = $week_rs->count;
|
||||||
|
|
||||||
|
my $month_rs = $user->transactions->month_rs;
|
||||||
|
my $month_sum = $month_rs->get_column('value')->sum;
|
||||||
|
my $month_count = $month_rs->count;
|
||||||
|
|
||||||
|
my $user_rs = $user->transactions;
|
||||||
|
my $user_sum = $user_rs->get_column('value')->sum;
|
||||||
|
my $user_count = $user_rs->count;
|
||||||
|
|
||||||
|
my $global_rs = $c->schema->resultset('Transaction');
|
||||||
|
my $global_sum = $global_rs->get_column('value')->sum;
|
||||||
|
my $global_count = $global_rs->count;
|
||||||
|
|
||||||
return $c->render( json => {
|
return $c->render( json => {
|
||||||
success => Mojo::JSON->true,
|
success => Mojo::JSON->true,
|
||||||
today_sum => $today_sum,
|
today_sum => $today_sum,
|
||||||
today_count => $today_count,
|
today_count => $today_count,
|
||||||
|
week_sum => $week_sum,
|
||||||
|
week_count => $week_count,
|
||||||
|
month_sum => $month_sum,
|
||||||
|
month_count => $month_count,
|
||||||
|
user_sum => $user_sum,
|
||||||
|
user_count => $user_count,
|
||||||
|
global_sum => $global_sum,
|
||||||
|
global_count => $global_count,
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
|
@ -7,23 +7,39 @@ use base 'DBIx::Class::ResultSet';
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
|
||||||
sub today_rs {
|
sub search_between {
|
||||||
my ( $self ) = @_;
|
my ( $self, $from, $to ) = @_;
|
||||||
|
|
||||||
my $dtf = $self->result_source->schema->storage->datetime_parser;
|
my $dtf = $self->result_source->schema->storage->datetime_parser;
|
||||||
return $self->search({
|
return $self->search({
|
||||||
submitted_at => {
|
submitted_at => {
|
||||||
-between => [
|
-between => [
|
||||||
$dtf->format_datetime(DateTime->today()),
|
$dtf->format_datetime($from),
|
||||||
$dtf->format_datetime(DateTime->today()->add( days => 1 )),
|
$dtf->format_datetime($to),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
sub today_for_user {
|
sub today_rs {
|
||||||
my ( $self, $user ) = @_;
|
my ( $self ) = @_;
|
||||||
return $self->search({ buyer_id => $user->id })->today_rs;
|
|
||||||
|
my $today = DateTime->today();
|
||||||
|
return $self->search_between( $today, $today->clone->add( days => 1 ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
sub week_rs {
|
||||||
|
my ( $self ) = @_;
|
||||||
|
|
||||||
|
my $today = DateTime->today();
|
||||||
|
return $self->search_between( $today->clone->subtract( days => 7 ), $today );
|
||||||
|
}
|
||||||
|
|
||||||
|
sub month_rs {
|
||||||
|
my ( $self ) = @_;
|
||||||
|
|
||||||
|
my $today = DateTime->today();
|
||||||
|
return $self->search_between( $today->clone->subtract( days => 30 ), $today );
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
|
@ -8,6 +8,7 @@ use DateTime;
|
||||||
my $framework = Test::Pear::LocalLoop->new;
|
my $framework = Test::Pear::LocalLoop->new;
|
||||||
my $t = $framework->framework;
|
my $t = $framework->framework;
|
||||||
my $schema = $t->app->schema;
|
my $schema = $t->app->schema;
|
||||||
|
my $dtf = $schema->storage->datetime_parser;
|
||||||
|
|
||||||
my $user = {
|
my $user = {
|
||||||
token => 'a',
|
token => 'a',
|
||||||
|
@ -46,7 +47,42 @@ for ( 1 .. 10 ) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
my $dtf = $schema->storage->datetime_parser;
|
for ( 11 .. 20 ) {
|
||||||
|
$user_result->create_related( 'transactions', {
|
||||||
|
seller_id => $org_result->id,
|
||||||
|
value => $_,
|
||||||
|
proof_image => 'a',
|
||||||
|
submitted_at => $dtf->format_datetime(DateTime->today()->subtract( days => 5 )),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( 21 .. 30 ) {
|
||||||
|
$user_result->create_related( 'transactions', {
|
||||||
|
seller_id => $org_result->id,
|
||||||
|
value => $_,
|
||||||
|
proof_image => 'a',
|
||||||
|
submitted_at => $dtf->format_datetime(DateTime->today()->subtract( days => 25 )),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( 31 .. 40 ) {
|
||||||
|
$user_result->create_related( 'transactions', {
|
||||||
|
seller_id => $org_result->id,
|
||||||
|
value => $_,
|
||||||
|
proof_image => 'a',
|
||||||
|
submitted_at => $dtf->format_datetime(DateTime->today()->subtract( days => 50 )),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( 41 .. 50 ) {
|
||||||
|
$org_result->user->create_related( 'transactions', {
|
||||||
|
seller_id => $org_result->id,
|
||||||
|
value => $_,
|
||||||
|
proof_image => 'a',
|
||||||
|
submitted_at => $dtf->format_datetime(DateTime->today()->subtract( days => 50 )),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
is $user_result->transactions->search({
|
is $user_result->transactions->search({
|
||||||
submitted_at => {
|
submitted_at => {
|
||||||
-between => [
|
-between => [
|
||||||
|
@ -56,7 +92,6 @@ is $user_result->transactions->search({
|
||||||
},
|
},
|
||||||
})->get_column('value')->sum, 55, 'Got correct sum';
|
})->get_column('value')->sum, 55, 'Got correct sum';
|
||||||
is $user_result->transactions->today_rs->get_column('value')->sum, 55, 'Got correct sum through rs';
|
is $user_result->transactions->today_rs->get_column('value')->sum, 55, 'Got correct sum through rs';
|
||||||
is $schema->resultset('Transaction')->today_for_user($user_result)->get_column('value')->sum, 55, 'Got correct sum through rs';
|
|
||||||
|
|
||||||
my $session_key = $framework->login({
|
my $session_key = $framework->login({
|
||||||
email => $user->{email},
|
email => $user->{email},
|
||||||
|
@ -67,6 +102,14 @@ $t->post_ok('/api/stats' => json => { session_key => $session_key } )
|
||||||
->status_is(200)
|
->status_is(200)
|
||||||
->json_is('/success', Mojo::JSON->true)
|
->json_is('/success', Mojo::JSON->true)
|
||||||
->json_is('/today_sum', 55)
|
->json_is('/today_sum', 55)
|
||||||
->json_is('/today_count', 10);
|
->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;
|
done_testing;
|
||||||
|
|
Reference in a new issue