From 2012e672733107af33c7a5923e011fc751cdc7f4 Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 15 Dec 2017 15:30:47 +0000 Subject: [PATCH] sectors code added --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 24 ++++++++++++++++--- .../Controller/Api/V1/Customer/Graphs.pm | 1 + .../Result/ViewQuantisedTransactionPg.pm | 2 ++ .../Result/ViewQuantisedTransactionSQLite.pm | 2 ++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index af36a67..4e9b4ff 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -15,7 +15,7 @@ has error_messages => sub { sub post_index { my $c = shift; - my $user = $c->stash->{api_user}->entity; + my $entity = $c->stash->{api_user}->entity; my $duration = DateTime::Duration->new( weeks => 7 ); my $end = DateTime->today; @@ -26,7 +26,7 @@ sub post_index { my $dtf = $c->schema->storage->datetime_parser; my $driver = $c->schema->storage->dbh->{Driver}->{Name}; - my $transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search( + my $week_transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search( { purchase_time => { -between => [ @@ -34,6 +34,7 @@ sub post_index { $dtf->format_datetime($end), ], }, + buyer_id => $entity->id, }, { columns => [ @@ -47,10 +48,27 @@ sub post_index { } ); - for ( $transaction_rs->all ) { + for ( $week_transaction_rs->all ) { push @{ $weeks->{ purchases } }, ($_->get_column('count') || 0); } + my $sector_purchase_rs = $entity->purchases->search({}, + { + join => { 'seller' => 'organisation' }, + columns => { + sector => "organisation.sector", + count => \"COUNT(*)", + }, + group_by => "organisation.sector", + order_by => { '-desc' => "COUNT(*)" }, + } + ); + + for ( $sector_purchase_rs->all ) { + push @{ $sectors->{ sectors } }, $_->get_column('sector'); + push @{ $sectors->{ purchases } }, ($_->get_column('count') || 0); + } + return $c->render( json => { success => Mojo::JSON->true, weeks => $weeks, diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm index 06f45d0..f48e52b 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm @@ -106,6 +106,7 @@ sub _purchases_avg_spend_duration { $dtf->format_datetime($end), ], }, + buyer_id => $entity->id, }, { columns => [ diff --git a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm index 68513ac..91ceede 100644 --- a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm +++ b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm @@ -13,6 +13,8 @@ __PACKAGE__->result_source_instance->view_definition( qq/ SELECT "value", "distance", "purchase_time", + "buyer_id", + "seller_id", DATE_TRUNC('hour', "purchase_time") AS "quantised_hours", DATE_TRUNC('day', "purchase_time") AS "quantised_days", DATE_TRUNC('week', "purchase_time") AS "quantised_weeks" diff --git a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionSQLite.pm b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionSQLite.pm index 649d6cd..abf95dd 100644 --- a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionSQLite.pm +++ b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionSQLite.pm @@ -13,6 +13,8 @@ __PACKAGE__->result_source_instance->view_definition( qq/ SELECT "value", "distance", "purchase_time", + "buyer_id", + "seller_id", DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"purchase_time")) AS "quantised_hours", DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time")) AS "quantised_days", DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"purchase_time", 'weekday 1')) AS "quantised_weeks"