Merge pull request #45 from Pear-Trading/TBSliver/Org-Graph-Improvements

Added extra graph types
This commit is contained in:
Tom Bloor 2017-09-05 12:19:18 +01:00 committed by GitHub
commit 1693bc7fb6
2 changed files with 112 additions and 20 deletions

View file

@ -18,6 +18,10 @@ sub index {
$validation->required('graph')->in( qw/
customers_last_7_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;
@ -56,24 +60,20 @@ sub graph_customers_last_30_days {
sub _customers_last_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 $end = $start->clone->subtract_duration( $duration );
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
my $dtf = $c->schema->storage->datetime_parser;
while ( $end < $start ) {
my $moving_end = $end->clone->add( days => 1 );
my $transactions = $c->schema->resultset('Transaction')->search({
seller_id => $org->id,
purchase_time => { '-between' => [ $dtf->format_datetime($end), $dtf->format_datetime($moving_end) ] },
})->count;
push @{$data->{day}}, $end->day_name;
push @{$data->{count}}, $transactions;
$end->add( days => 1 );
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->sales
->search_between( $start, $next_end )
->count;
push @{ $data->{ labels } }, $start->day_name;
push @{ $data->{ data } }, $transactions;
$start->add( days => 1 );
}
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 || 0;
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 || 0;
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;

View file

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