From 9f719712492c3d7cdd1eb39ca0224b6cd5a014d8 Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Wed, 21 Mar 2018 17:24:13 +0000 Subject: [PATCH 01/15] Set secrets with decent default for production --- lib/Pear/LocalLoop.pm | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index c996567..db7bba8 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -34,6 +34,13 @@ sub startup { }); my $config = $self->config; + if ( defined $config->{secret} ) { + $self->secrets([ $config->{secret} ]); + } elsif ( $self->mode eq 'production' ) { + # Just incase we end up in production and it hasnt been set! + $self->secrets([ Data::UUID->new->create() ]); + } + push @{ $self->commands->namespaces }, __PACKAGE__ . '::Command'; $self->plugin('Pear::LocalLoop::Plugin::BootstrapPagination', { bootstrap4 => 1 } ); @@ -247,9 +254,9 @@ sub startup { # $portal_api->post('/search')->to('api-upload#post_search'); $self->hook( before_dispatch => sub { - my $self = shift; + my $c = shift; - $self->res->headers->header('Access-Control-Allow-Origin' => '*') if $self->app->mode eq 'development'; + $c->res->headers->header('Access-Control-Allow-Origin' => '*') if $c->app->mode eq 'development'; }); $self->helper( copy_transactions_and_delete => sub { From b937badc6eb08a7cf9ef65d66ff5b4b6608dd317 Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Wed, 21 Mar 2018 17:24:41 +0000 Subject: [PATCH 02/15] Stop error on large csv exceeding size of cookies --- lib/Pear/LocalLoop/Controller/Admin/Import.pm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Pear/LocalLoop/Controller/Admin/Import.pm b/lib/Pear/LocalLoop/Controller/Admin/Import.pm index 0416012..630bf84 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Import.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Import.pm @@ -144,7 +144,8 @@ sub _csv_flash_error { $c->flash( error => $error, - csv_data => $c->param('csv'), + # If csv info is huge, this fails epically + #csv_data => $c->param('csv'), date_format => $c->param('date_format'), ); } From 9b761ee34345832d9c928b247942ea0ebb06b93b Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Wed, 21 Mar 2018 17:24:55 +0000 Subject: [PATCH 03/15] Allow for parsing currency without a currency sign in front --- lib/Pear/LocalLoop/Plugin/Currency.pm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/Pear/LocalLoop/Plugin/Currency.pm b/lib/Pear/LocalLoop/Plugin/Currency.pm index f256f80..fa5333f 100644 --- a/lib/Pear/LocalLoop/Plugin/Currency.pm +++ b/lib/Pear/LocalLoop/Plugin/Currency.pm @@ -9,6 +9,8 @@ sub register { my $value; if ( $currency_string =~ /^£([\d.]+)/ ) { $value = $1 * 1; + } elsif ( $currency_string =~ /^([\d.]+)/ ) { + $value = $1 * 1; } return $value; }); From 6df772da98b16b4c93cc8b48573a112f873fe133 Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Wed, 21 Mar 2018 17:49:00 +0000 Subject: [PATCH 04/15] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb8bb99..a033783 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ # Next Release +* **Admin Fix** Parse currency without a currency symbol on import +* **Admin Fix** Fix large CSV issue on import +* Use custom secrets for encryption + # v0.10.6 * Fixed organisation submission From 81e171c413334bf4870d2ebe5d8aead53f13e3a8 Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 21 Mar 2018 17:58:50 +0000 Subject: [PATCH 05/15] fixed display error --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index cd348c3..711de88 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -92,7 +92,7 @@ sub post_customer { ); my @all_weeks = $week_transaction_rs->all; - my $first = $all_weeks[0]->get_column('count') || 0; + my $first = defined $all_weeks[0] ? $all_weeks[0]->get_column('count') || 0 : 0; my $second = defined $all_weeks[1] ? $all_weeks[1]->get_column('count') || 0 : 0; my $max = max( map { $_->get_column('count') } @all_weeks ); my $sum = sum( map { $_->get_column('count') } @all_weeks ); From 711daf859452dab0f367462deaf4ba2d5f20cad4 Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Mon, 26 Mar 2018 14:26:02 +0100 Subject: [PATCH 06/15] Added new Daily Cron script --- script/cron_daily | 10 ++++++++++ script/recalc_leaderboards | 5 ----- 2 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 script/cron_daily delete mode 100755 script/recalc_leaderboards diff --git a/script/cron_daily b/script/cron_daily new file mode 100644 index 0000000..1f53201 --- /dev/null +++ b/script/cron_daily @@ -0,0 +1,10 @@ +#! /bin/bash + +# Scripts to run daily. +# This will be run sometime between 2 & 3AM every morning. +# If order matters, make sure they are in the right place. + +eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib) + +MOJO_MODE=production ./script/pear-local_loop recur_transactions --force +MOJO_MODE=production ./script/pear-local_loop recalc_leaderboards diff --git a/script/recalc_leaderboards b/script/recalc_leaderboards deleted file mode 100755 index 73ce46c..0000000 --- a/script/recalc_leaderboards +++ /dev/null @@ -1,5 +0,0 @@ -#! /bin/bash - -eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib) - -MOJO_MODE=production ./script/pear-local_loop recalc_leaderboards From 483ea471772f99cbf0595f889829f9283d405255 Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Mon, 26 Mar 2018 14:27:04 +0100 Subject: [PATCH 07/15] Updated Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a033783..b673ce8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ # Next Release +* Added `cron_daily` script for holding all daily cronjobs * **Admin Fix** Parse currency without a currency symbol on import * **Admin Fix** Fix large CSV issue on import * Use custom secrets for encryption From 3a738a10004a5af692d09af4bec9f7b84202aef0 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 26 Mar 2018 14:42:18 +0100 Subject: [PATCH 08/15] Amended category listing --- .../LocalLoop/Controller/Api/Categories.pm | 21 +++++++++--------- lib/Pear/LocalLoop/Controller/Api/Upload.pm | 15 +++---------- .../LocalLoop/Schema/ResultSet/Category.pm | 22 +++++++++++++++++++ 3 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 lib/Pear/LocalLoop/Schema/ResultSet/Category.pm diff --git a/lib/Pear/LocalLoop/Controller/Api/Categories.pm b/lib/Pear/LocalLoop/Controller/Api/Categories.pm index ed5b0c1..0ffeae7 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Categories.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Categories.pm @@ -33,30 +33,31 @@ sub post_category_list { essential => 'essential', }, ], - group_by => [ qw/ category_id quantised_weeks essential value / ], - order_by => { '-desc' => 'value' }, + group_by => [ qw/ category_id quantised_weeks essential / ], } ); my $data = { categories => {}, essentials => {} }; + my $category_list = $c->schema->resultset('Category')->as_hash; + for my $cat_trans ( $month_transaction_category_rs->all ) { my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised')); my $days = $c->format_iso_date( $quantised ) || 0; - my $category = $cat_trans->get_column('category_id') || undef; + my $category = $cat_trans->get_column('category_id') || 0; my $value = ($cat_trans->get_column('value') || 0) / 100000; - $data->{categories}->{$days} = [] unless exists $data->{categories}->{$days}; - push @{ $data->{categories}->{$days} }, { - days => $days, - value => $value, - category => $category, - }; + $data->{categories}->{$days}->{$category_list->{$category}} += $value; next unless $cat_trans->get_column('essential'); $data->{essentials}->{$days}->{value} += $value; } for my $day ( keys %{ $data->{categories} } ) { - $data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @{ $data->{categories}->{$day} } ]; + my @days = ( map{ { + days => $day, + value => $data->{categories}->{$day}->{$_}, + category => $_, + } } keys %{ $data->{categories}->{$day} } ); + $data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @days ]; } return $c->render( diff --git a/lib/Pear/LocalLoop/Controller/Api/Upload.pm b/lib/Pear/LocalLoop/Controller/Api/Upload.pm index 31fa234..c638645 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Upload.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Upload.pm @@ -243,21 +243,12 @@ sub post_category { my $c = shift; my $self = $c; - my $category_rs = $c->schema->resultset('Category'); - - # for ( $category_rs->all ) { - # push @{ $categories->{ ids } }, $_->get_column('id'); - # push @{ $categories->{ names } }, $_->get_column('name'); - # } - my %category_list = ( - map { - $_->id => $_->name, - } $category_rs->all - ); + my $category_list = $c->schema->resultset('Category')->as_hash; + delete $category_list->{0}; return $self->render( json => { success => Mojo::JSON->true, - categories => \%category_list, + categories => $category_list, }); } diff --git a/lib/Pear/LocalLoop/Schema/ResultSet/Category.pm b/lib/Pear/LocalLoop/Schema/ResultSet/Category.pm new file mode 100644 index 0000000..59f9b98 --- /dev/null +++ b/lib/Pear/LocalLoop/Schema/ResultSet/Category.pm @@ -0,0 +1,22 @@ +package Pear::LocalLoop::Schema::ResultSet::Category; + +use strict; +use warnings; + +use base 'DBIx::Class::ResultSet'; + +sub as_hash { + my ( $self ) = @_; + + my %category_list = ( + ( + map { + $_->id => $_->name, + } $self->all + ), + 0 => 'Uncategorised', + ); + return \%category_list; +} + +1; From 5f324dd00a69c39d9bbe3c6fbb87e2067906b80f Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 26 Mar 2018 14:44:03 +0100 Subject: [PATCH 09/15] changing pie format (broken) --- .../Controller/Api/V1/Customer/Pies.pm | 58 ++++++++++++++++++- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm index 06c57b1..ebaf866 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm @@ -6,6 +6,8 @@ sub index { my $entity = $c->stash->{api_user}->entity; + my $data = { local_all => {}, categories => {}, essentials => {} }; + my $purchase_rs = $entity->purchases; my $local_org_local_purchase = $purchase_rs->search({ "me.distance" => { '<', 20000 }, @@ -43,17 +45,67 @@ sub index { } ); - my $data = { + $data->{local_all} = { 'Local shop local purchaser' => $local_org_local_purchase->count, 'Local shop non-local purchaser' => $local_org_non_local_purchase->count, 'Non-local shop local purchaser' => $non_local_org_local_purchase->count, 'Non-local shop non-local purchaser' => $non_local_org_non_local_purchase->count, }; - + + my $duration = DateTime::Duration->new( days => 28 ); + my $end = DateTime->today; + my $start = $end->clone->subtract_duration( $duration ); + + my $dtf = $c->schema->storage->datetime_parser; + my $driver = $c->schema->storage->dbh->{Driver}->{Name}; + my $month_transaction_category_rs = $c->schema->resultset('ViewQuantisedTransactionCategory' . $driver)->search( + { + purchase_time => { + -between => [ + $dtf->format_datetime($start), + $dtf->format_datetime($end), + ], + }, + buyer_id => $entity->id, + }, + { + columns => [ + { + quantised => 'quantised_weeks', + value => { sum => 'value' }, + category_id => 'category_id', + essential => 'essential', + }, + ], + group_by => [ qw/ category_id quantised_weeks essential / ], + } + ); + + my $category_list = $c->schema->resultset('Category')->as_hash; + + for my $cat_trans ( $month_transaction_category_rs->all ) { + my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised')); + my $days = $c->format_iso_date( $quantised ) || 0; + my $category = $cat_trans->get_column('category_id') || 0; + my $value = ($cat_trans->get_column('value') || 0) / 100000; + $data->{categories}->{$days}->{$category_list->{$category}} += $value; + next unless $cat_trans->get_column('essential'); + $data->{essentials}->{$days}->{value} += $value; + } + + for my $day ( keys %{ $data->{categories} } ) { + my @days = ( map{ { + days => $day, + value => $data->{categories}->{$day}->{$_}, + category => $_, + } } keys %{ $data->{categories}->{$day} } ); + $data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @days ]; + } + return $c->render( json => { success => Mojo::JSON->true, - pie => $data, + data => $data, } ); From 9ea05090300c2fa1a062871860c14dc4ebc83179 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 26 Mar 2018 15:13:35 +0100 Subject: [PATCH 10/15] fixed category list API --- t/api/categories.t | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/t/api/categories.t b/t/api/categories.t index 10d11ab..93c1f3d 100644 --- a/t/api/categories.t +++ b/t/api/categories.t @@ -24,7 +24,7 @@ $schema->resultset('Category')->create({ name => 'test', }); -set_absolute_time('2017-01-02T00:00:00Z'); +set_absolute_time('2018-01-08T00:00:00Z'); my $start = DateTime->today->subtract( hours => 12 ); @@ -55,38 +55,38 @@ $t->post_ok('/api/stats/category' => json => { ->status_is(200)->or($framework->dump_error) ->json_is('/data', { categories => { - "2016-12-05" => [{ - days => "2016-12-05", + "2017-12-11" => [{ + days => "2017-12-11", value => 210, - category => 1, + category => 'test', }], - "2016-12-12" => [{ - days => "2016-12-12", + "2017-12-18" => [{ + days => "2017-12-18", value => 200, - category => 1, + category => 'test', }], - "2016-12-19" => [{ - days => "2016-12-19", + "2017-12-25" => [{ + days => "2017-12-25", value => 210, - category => 1, + category => 'test', }], - "2016-12-26" => [{ - days => "2016-12-26", + "2018-01-01" => [{ + days => "2018-01-01", value => 190, - category => 1, + category => 'test', }], }, essentials => { - "2016-12-05" => { + "2017-12-11" => { value => 210, }, - "2016-12-12" => { + "2017-12-18" => { value => 200, }, - "2016-12-19" => { + "2017-12-25" => { value => 210, }, - "2016-12-26" => { + "2018-01-01" => { value => 190, }, } From e7dee614339c838670ebd7d16071e119e93c097e Mon Sep 17 00:00:00 2001 From: Finn Date: Thu, 5 Apr 2018 17:19:53 +0100 Subject: [PATCH 11/15] changed pie data structure --- .../Controller/Api/V1/Customer/Pies.pm | 56 +------------------ 1 file changed, 2 insertions(+), 54 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm index ebaf866..9f9ef0c 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm @@ -6,8 +6,6 @@ sub index { my $entity = $c->stash->{api_user}->entity; - my $data = { local_all => {}, categories => {}, essentials => {} }; - my $purchase_rs = $entity->purchases; my $local_org_local_purchase = $purchase_rs->search({ "me.distance" => { '<', 20000 }, @@ -45,67 +43,17 @@ sub index { } ); - $data->{local_all} = { + my $data = { 'Local shop local purchaser' => $local_org_local_purchase->count, 'Local shop non-local purchaser' => $local_org_non_local_purchase->count, 'Non-local shop local purchaser' => $non_local_org_local_purchase->count, 'Non-local shop non-local purchaser' => $non_local_org_non_local_purchase->count, }; - my $duration = DateTime::Duration->new( days => 28 ); - my $end = DateTime->today; - my $start = $end->clone->subtract_duration( $duration ); - - my $dtf = $c->schema->storage->datetime_parser; - my $driver = $c->schema->storage->dbh->{Driver}->{Name}; - my $month_transaction_category_rs = $c->schema->resultset('ViewQuantisedTransactionCategory' . $driver)->search( - { - purchase_time => { - -between => [ - $dtf->format_datetime($start), - $dtf->format_datetime($end), - ], - }, - buyer_id => $entity->id, - }, - { - columns => [ - { - quantised => 'quantised_weeks', - value => { sum => 'value' }, - category_id => 'category_id', - essential => 'essential', - }, - ], - group_by => [ qw/ category_id quantised_weeks essential / ], - } - ); - - my $category_list = $c->schema->resultset('Category')->as_hash; - - for my $cat_trans ( $month_transaction_category_rs->all ) { - my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised')); - my $days = $c->format_iso_date( $quantised ) || 0; - my $category = $cat_trans->get_column('category_id') || 0; - my $value = ($cat_trans->get_column('value') || 0) / 100000; - $data->{categories}->{$days}->{$category_list->{$category}} += $value; - next unless $cat_trans->get_column('essential'); - $data->{essentials}->{$days}->{value} += $value; - } - - for my $day ( keys %{ $data->{categories} } ) { - my @days = ( map{ { - days => $day, - value => $data->{categories}->{$day}->{$_}, - category => $_, - } } keys %{ $data->{categories}->{$day} } ); - $data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @days ]; - } - return $c->render( json => { success => Mojo::JSON->true, - data => $data, + pie => $data, } ); From 45f0edef47db416e2751312e480fdab18b9719bb Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 9 Apr 2018 19:21:14 +0100 Subject: [PATCH 12/15] essential data added for bar chart --- .../Controller/Api/V1/Customer/Pies.pm | 66 ++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm index 9f9ef0c..8a1210f 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm @@ -6,7 +6,19 @@ sub index { my $entity = $c->stash->{api_user}->entity; + my $data = { local_all => {}, categories => {}, essentials => {} }; + my $purchase_rs = $entity->purchases; + + my $purchase_no_essential_rs = $purchase_rs->search({ + "me.essential" => 1, + }); + + $data->{essentials} = { + purchase_no_total => $purchase_rs->count, + purchase_no_essential_total => $purchase_no_essential_rs->count, + }; + my $local_org_local_purchase = $purchase_rs->search({ "me.distance" => { '<', 20000 }, 'organisation.is_local' => 1, @@ -43,17 +55,67 @@ sub index { } ); - my $data = { + $data->{local_all} = { 'Local shop local purchaser' => $local_org_local_purchase->count, 'Local shop non-local purchaser' => $local_org_non_local_purchase->count, 'Non-local shop local purchaser' => $non_local_org_local_purchase->count, 'Non-local shop non-local purchaser' => $non_local_org_non_local_purchase->count, }; + my $duration = DateTime::Duration->new( days => 28 ); + my $end = DateTime->today; + my $start = $end->clone->subtract_duration( $duration ); + + my $dtf = $c->schema->storage->datetime_parser; + my $driver = $c->schema->storage->dbh->{Driver}->{Name}; + my $month_transaction_category_rs = $c->schema->resultset('ViewQuantisedTransactionCategory' . $driver)->search( + { + purchase_time => { + -between => [ + $dtf->format_datetime($start), + $dtf->format_datetime($end), + ], + }, + buyer_id => $entity->id, + }, + { + columns => [ + { + quantised => 'quantised_weeks', + value => { sum => 'value' }, + category_id => 'category_id', + essential => 'essential', + }, + ], + group_by => [ qw/ category_id quantised_weeks essential / ], + } + ); + + my $category_list = $c->schema->resultset('Category')->as_hash; + + for my $cat_trans ( $month_transaction_category_rs->all ) { + my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised')); + my $days = $c->format_iso_date( $quantised ) || 0; + my $category = $cat_trans->get_column('category_id') || 0; + my $value = ($cat_trans->get_column('value') || 0) / 100000; + $data->{categories}->{$days}->{$category_list->{$category}} += $value; + next unless $cat_trans->get_column('essential'); + $data->{essentials}->{$days}->{value} += $value; + } + + for my $day ( keys %{ $data->{categories} } ) { + my @days = ( map{ { + days => $day, + value => $data->{categories}->{$day}->{$_}, + category => $_, + } } keys %{ $data->{categories}->{$day} } ); + $data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @days ]; + } + return $c->render( json => { success => Mojo::JSON->true, - pie => $data, + data => $data, } ); From 662db25c3c559477138074c8f0d79249ed7dfd4c Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 11 Apr 2018 19:10:19 +0100 Subject: [PATCH 13/15] added month listing --- lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm index 8a1210f..2ae26cb 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm @@ -6,7 +6,7 @@ sub index { my $entity = $c->stash->{api_user}->entity; - my $data = { local_all => {}, categories => {}, essentials => {} }; + my $data = { local_all => {}, cat_total => {}, categories => {}, essentials => {} }; my $purchase_rs = $entity->purchases; @@ -98,6 +98,7 @@ sub index { my $days = $c->format_iso_date( $quantised ) || 0; my $category = $cat_trans->get_column('category_id') || 0; my $value = ($cat_trans->get_column('value') || 0) / 100000; + $data->{cat_total}->{$category_list->{$category}} += $value; $data->{categories}->{$days}->{$category_list->{$category}} += $value; next unless $cat_trans->get_column('essential'); $data->{essentials}->{$days}->{value} += $value; From 2d92ac2f031cd7ac10d5b847f8e0e064a7ba3182 Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 13 Apr 2018 18:08:10 +0100 Subject: [PATCH 14/15] changed stat viewing and amended tests --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 67 ++++++++++++++++++- .../Controller/Api/V1/Customer/Pies.pm | 66 +----------------- t/api/stats.t | 43 ++++++++++-- t/api/v1/customer/pies.t | 2 +- 4 files changed, 105 insertions(+), 73 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index 711de88..3a7579d 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -63,9 +63,9 @@ sub post_customer { my $entity = $c->stash->{api_user}->entity; - my $duration = DateTime::Duration->new( weeks => 7 ); + my $duration_weeks = DateTime::Duration->new( weeks => 7 ); my $end = DateTime->today; - my $start = $end->clone->subtract_duration( $duration ); + my $start_weeks = $end->clone->subtract_duration( $duration_weeks ); my $dtf = $c->schema->storage->datetime_parser; my $driver = $c->schema->storage->dbh->{Driver}->{Name}; @@ -73,7 +73,7 @@ sub post_customer { { purchase_time => { -between => [ - $dtf->format_datetime($start), + $dtf->format_datetime($start_weeks), $dtf->format_datetime($end), ], }, @@ -125,8 +125,69 @@ sub post_customer { push @{ $sectors->{ purchases } }, ($_->get_column('count') || 0); } + my $data = { cat_total => {}, categories => {}, essentials => {} }; + + my $purchase_rs = $entity->purchases; + + my $purchase_no_essential_rs = $purchase_rs->search({ + "me.essential" => 1, + }); + + $data->{essentials} = { + purchase_no_total => $purchase_rs->count, + purchase_no_essential_total => $purchase_no_essential_rs->count, + }; + + my $duration_month = DateTime::Duration->new( days => 28 ); + my $start_month = $end->clone->subtract_duration( $duration_month ); + my $month_transaction_category_rs = $c->schema->resultset('ViewQuantisedTransactionCategory' . $driver)->search( + { + purchase_time => { + -between => [ + $dtf->format_datetime($start_month), + $dtf->format_datetime($end), + ], + }, + buyer_id => $entity->id, + }, + { + columns => [ + { + quantised => 'quantised_weeks', + value => { sum => 'value' }, + category_id => 'category_id', + essential => 'essential', + }, + ], + group_by => [ qw/ category_id quantised_weeks essential / ], + } + ); + + my $category_list = $c->schema->resultset('Category')->as_hash; + + for my $cat_trans ( $month_transaction_category_rs->all ) { + my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised')); + my $days = $c->format_iso_date( $quantised ) || 0; + my $category = $cat_trans->get_column('category_id') || 0; + my $value = ($cat_trans->get_column('value') || 0) / 100000; + $data->{cat_total}->{$category_list->{$category}} += $value; + $data->{categories}->{$days}->{$category_list->{$category}} += $value; + next unless $cat_trans->get_column('essential'); + $data->{essentials}->{$days}->{value} += $value; + } + + for my $day ( keys %{ $data->{categories} } ) { + my @days = ( map{ { + days => $day, + value => $data->{categories}->{$day}->{$_}, + category => $_, + } } keys %{ $data->{categories}->{$day} } ); + $data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @days ]; + } + return $c->render( json => { success => Mojo::JSON->true, + data => $data, weeks => $weeks, sectors => $sectors, }); diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm index 2ae26cb..353ab06 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Pies.pm @@ -6,19 +6,8 @@ sub index { my $entity = $c->stash->{api_user}->entity; - my $data = { local_all => {}, cat_total => {}, categories => {}, essentials => {} }; - my $purchase_rs = $entity->purchases; - my $purchase_no_essential_rs = $purchase_rs->search({ - "me.essential" => 1, - }); - - $data->{essentials} = { - purchase_no_total => $purchase_rs->count, - purchase_no_essential_total => $purchase_no_essential_rs->count, - }; - my $local_org_local_purchase = $purchase_rs->search({ "me.distance" => { '<', 20000 }, 'organisation.is_local' => 1, @@ -55,68 +44,17 @@ sub index { } ); - $data->{local_all} = { + my $local_all = { 'Local shop local purchaser' => $local_org_local_purchase->count, 'Local shop non-local purchaser' => $local_org_non_local_purchase->count, 'Non-local shop local purchaser' => $non_local_org_local_purchase->count, 'Non-local shop non-local purchaser' => $non_local_org_non_local_purchase->count, }; - my $duration = DateTime::Duration->new( days => 28 ); - my $end = DateTime->today; - my $start = $end->clone->subtract_duration( $duration ); - - my $dtf = $c->schema->storage->datetime_parser; - my $driver = $c->schema->storage->dbh->{Driver}->{Name}; - my $month_transaction_category_rs = $c->schema->resultset('ViewQuantisedTransactionCategory' . $driver)->search( - { - purchase_time => { - -between => [ - $dtf->format_datetime($start), - $dtf->format_datetime($end), - ], - }, - buyer_id => $entity->id, - }, - { - columns => [ - { - quantised => 'quantised_weeks', - value => { sum => 'value' }, - category_id => 'category_id', - essential => 'essential', - }, - ], - group_by => [ qw/ category_id quantised_weeks essential / ], - } - ); - - my $category_list = $c->schema->resultset('Category')->as_hash; - - for my $cat_trans ( $month_transaction_category_rs->all ) { - my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised')); - my $days = $c->format_iso_date( $quantised ) || 0; - my $category = $cat_trans->get_column('category_id') || 0; - my $value = ($cat_trans->get_column('value') || 0) / 100000; - $data->{cat_total}->{$category_list->{$category}} += $value; - $data->{categories}->{$days}->{$category_list->{$category}} += $value; - next unless $cat_trans->get_column('essential'); - $data->{essentials}->{$days}->{value} += $value; - } - - for my $day ( keys %{ $data->{categories} } ) { - my @days = ( map{ { - days => $day, - value => $data->{categories}->{$day}->{$_}, - category => $_, - } } keys %{ $data->{categories}->{$day} } ); - $data->{categories}->{$day} = [ sort { $b->{value} <=> $a->{value} } @days ]; - } - return $c->render( json => { success => Mojo::JSON->true, - data => $data, + local_all => $local_all, } ); diff --git a/t/api/stats.t b/t/api/stats.t index 2fa8a9a..c7922af 100644 --- a/t/api/stats.t +++ b/t/api/stats.t @@ -19,7 +19,7 @@ $framework->install_fixtures('users'); my $t = $framework->framework; my $schema = $t->app->schema; -set_absolute_time('2017-01-01T00:00:00Z'); +set_absolute_time('2018-01-08T00:00:00Z'); my $start = DateTime->today->subtract( hours => 12 ); @@ -49,17 +49,50 @@ $t->post_ok('/api/stats/customer' => json => { }) ->status_is(200)->or($framework->dump_error) ->json_is('/weeks', { - first => 20, + first => 17, second => 20, - max => 22, + max => 21, sum => 118, count => 6, }) - ->or($framework->dump_error) ->json_is('/sectors', { sectors => ['A'], purchases => [118], - }); + }) + ->json_is('/data', { + cat_total => { + Uncategorised => 810, + }, + categories => { + "2017-12-11" => [{ + days => "2017-12-11", + value => 210, + category => 'Uncategorised', + }], + "2017-12-18" => [{ + days => "2017-12-18", + value => 200, + category => 'Uncategorised', + }], + "2017-12-25" => [{ + days => "2017-12-25", + value => 210, + category => 'Uncategorised', + }], + "2018-01-01" => [{ + days => "2018-01-01", + value => 190, + category => 'Uncategorised', + }], + }, + cat_total => { + Uncategorised => 810, + }, + essentials => { + purchase_no_essential_total => 0, + purchase_no_total => 118, + }, + })->or($framework->dump_error); sub create_random_transaction { my $buyer = shift; diff --git a/t/api/v1/customer/pies.t b/t/api/v1/customer/pies.t index 53fdd8f..babd443 100644 --- a/t/api/v1/customer/pies.t +++ b/t/api/v1/customer/pies.t @@ -42,7 +42,7 @@ $t->post_ok('/api/v1/customer/pies' => json => { session_key => $session_key, }) ->status_is(200)->or($framework->dump_error) - ->json_is('/pie', { + ->json_is('/local_all', { 'Local shop local purchaser' => 0, 'Local shop non-local purchaser' => 0, 'Non-local shop local purchaser' => 0, From 71526250c9d9d1400b61d4a74bd24030d2b678d8 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 16 Apr 2018 16:25:07 +0100 Subject: [PATCH 15/15] Changelog amended --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b673ce8..a0810f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,16 @@ # Next Release +# v.10.7 + * Added `cron_daily` script for holding all daily cronjobs * **Admin Fix** Parse currency without a currency symbol on import * **Admin Fix** Fix large CSV issue on import * Use custom secrets for encryption +* Made purchase categories easier to pull +* Added dashboard data for getting essential for all purchases along with +weekly and monthly view of category purchases +* Amended tests where relevant # v0.10.6