From 4d21de7bae2edc887dcc47a080b377c8d6fb683d Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Mon, 4 Sep 2017 17:29:28 +0100 Subject: [PATCH] Improved Graphs and added sales and purchase data graphs. Includes an API change for development. --- .../Controller/Api/V1/Organisation/Graphs.pm | 101 +++++++++++++++--- t/api/v1/organisation/graphs.t | 31 +++++- 2 files changed, 112 insertions(+), 20 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm index bc78d91..07b952b 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Organisation/Graphs.pm @@ -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; + 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; diff --git a/t/api/v1/organisation/graphs.t b/t/api/v1/organisation/graphs.t index c5ee6f9..45531a6 100644 --- a/t/api/v1/organisation/graphs.t +++ b/t/api/v1/organisation/graphs.t @@ -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, });