Improved Graphs and added sales and purchase data graphs.

Includes an API change for development.
This commit is contained in:
Tom Bloor 2017-09-04 17:29:28 +01:00
parent 537048b506
commit 4d21de7bae
2 changed files with 112 additions and 20 deletions

View file

@ -18,6 +18,10 @@ sub index {
$validation->required('graph')->in( qw/ $validation->required('graph')->in( qw/
customers_last_7_days customers_last_7_days
customers_last_30_days customers_last_30_days
sales_last_7_days
sales_last_30_days
purchases_last_7_days
purchases_last_30_days
/ ); / );
return $c->api_validation_error if $validation->has_error; return $c->api_validation_error if $validation->has_error;
@ -56,24 +60,20 @@ sub graph_customers_last_30_days {
sub _customers_last_duration { sub _customers_last_duration {
my ( $c, $duration ) = @_; my ( $c, $duration ) = @_;
my $org = $c->stash->{api_user}->entity; my $entity = $c->stash->{api_user}->entity;
my $data = { day => [], count => [] }; my $data = { labels => [], data => [] };
my $start = DateTime->today; my ( $start, $end ) = $c->_get_start_end_duration( $duration );
my $end = $start->clone->subtract_duration( $duration );
my $dtf = $c->schema->storage->datetime_parser; while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
while ( $end < $start ) { my $transactions = $entity->sales
my $moving_end = $end->clone->add( days => 1 ); ->search_between( $start, $next_end )
my $transactions = $c->schema->resultset('Transaction')->search({ ->count;
seller_id => $org->id, push @{ $data->{ labels } }, $start->day_name;
purchase_time => { '-between' => [ $dtf->format_datetime($end), $dtf->format_datetime($moving_end) ] }, push @{ $data->{ data } }, $transactions;
})->count; $start->add( days => 1 );
push @{$data->{day}}, $end->day_name;
push @{$data->{count}}, $transactions;
$end->add( days => 1 );
} }
return $c->render( return $c->render(
@ -84,4 +84,75 @@ sub _customers_last_duration {
); );
} }
sub graph_sales_last_7_days { return shift->_sales_last_duration( 7 ) }
sub graph_sales_last_30_days { return shift->_sales_last_duration( 30 ) }
sub _sales_last_duration {
my ( $c, $day_duration ) = @_;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $data = { labels => [], data => [] };
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->sales
->search_between( $start, $next_end )
->get_column('value')
->sum;
push @{ $data->{ labels } }, $start->day_name;
push @{ $data->{ data } }, $transactions;
$start->add( days => 1 );
}
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
}
);
}
sub graph_purchases_last_7_days { return shift->_purchases_last_duration( 7 ) }
sub graph_purchases_last_30_days { return shift->_purchases_last_duration( 30 ) }
sub _purchases_last_duration {
my ( $c, $day_duration ) = @_;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $data = { labels => [], data => [] };
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->purchases
->search_between( $start, $next_end )
->get_column('value')
->sum;
push @{ $data->{ labels } }, $start->day_name;
push @{ $data->{ data } }, $transactions;
$start->add( days => 1 );
}
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
}
);
}
sub _get_start_end_duration {
my ( $c, $duration ) = @_;
my $end = DateTime->today;
my $start = $end->clone->subtract_duration( $duration );
return ( $start, $end );
}
1; 1;

View file

@ -44,8 +44,8 @@ $t->post_ok('/api/v1/organisation/graphs' => json => {
}) })
->status_is(200)->or($framework->dump_error) ->status_is(200)->or($framework->dump_error)
->json_is('/graph', { ->json_is('/graph', {
day => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 6 ) ], labels => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 6 ) ],
count => [ 2, 4, 2, 3, 3, 4, 1 ], data => [ 2, 4, 2, 3, 3, 4, 1 ],
}); });
$t->post_ok('/api/v1/organisation/graphs' => json => { $t->post_ok('/api/v1/organisation/graphs' => json => {
@ -54,8 +54,28 @@ $t->post_ok('/api/v1/organisation/graphs' => json => {
}) })
->status_is(200)->or($framework->dump_error) ->status_is(200)->or($framework->dump_error)
->json_is('/graph', { ->json_is('/graph', {
day => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 29 ) ], labels => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 29 ) ],
count => [ 4, 2, 3, 3, 4, 1, 4, 3, 3, 2, 4, 2, 4, 2, 3, 3, 4, 1, 4, 3, 3, 2, 4, 2, 4, 2, 3, 3, 4, 1 ], data => [ 4, 2, 3, 3, 4, 1, 4, 3, 3, 2, 4, 2, 4, 2, 3, 3, 4, 1, 4, 3, 3, 2, 4, 2, 4, 2, 3, 3, 4, 1 ],
});
$t->post_ok('/api/v1/organisation/graphs' => json => {
session_key => $session_key,
graph => 'sales_last_7_days',
})
->status_is(200)->or($framework->dump_error)
->json_is('/graph', {
labels => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 6 ) ],
data => [ 20, 40, 20, 30, 30, 40, 10 ],
});
$t->post_ok('/api/v1/organisation/graphs' => json => {
session_key => $session_key,
graph => 'sales_last_30_days',
})
->status_is(200)->or($framework->dump_error)
->json_is('/graph', {
labels => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 29 ) ],
data => [ 40, 20, 30, 30, 40, 10, 40, 30, 30, 20, 40, 20, 40, 20, 30, 30, 40, 10, 40, 30, 30, 20, 40, 20, 40, 20, 30, 30, 40, 10 ],
}); });
$framework->logout( $session_key ); $framework->logout( $session_key );
@ -73,6 +93,7 @@ $t->post_ok('/api/v1/organisation/graphs' => json => {
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->json_is('/error', 'user_not_org'); ->json_is('/error', 'user_not_org');
sub create_random_transaction { sub create_random_transaction {
my $buyer = shift; my $buyer = shift;
my $time = shift; my $time = shift;
@ -82,7 +103,7 @@ sub create_random_transaction {
$schema->resultset('Transaction')->create({ $schema->resultset('Transaction')->create({
buyer => $buyer_result, buyer => $buyer_result,
seller => $seller_result, seller => $seller_result,
value => ( int( rand( 10000 ) ) / 100 ), value => 10,
proof_image => 'a', proof_image => 'a',
purchase_time => $time, purchase_time => $time,
}); });