From 314c3e4bd5c5e65cfb07dd853ad2a0a6c2722434 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Mon, 15 Jul 2019 02:41:23 +0100 Subject: [PATCH] More graph niceness --- lib/Pear/LocalLoop.pm | 1 + lib/Pear/LocalLoop/Controller/Api/External.pm | 48 +++++++++++++++++++ .../Result/ViewQuantisedTransactionPg.pm | 14 ++++++ 3 files changed, 63 insertions(+) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index eccb0a3..12a87ed 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -194,6 +194,7 @@ sub startup { $api_v1_org->post('/external/transactions')->to('api-external#post_lcc_transactions'); $api_v1_org->post('/external/suppliers')->to('api-external#post_lcc_suppliers'); $api_v1_org->post('/external/year_spend')->to('api-external#post_year_spend'); + $api_v1_org->post('/external/supplier_count')->to('api-external#post_supplier_count'); $api_v1_org->post('/pies')->to('api-v1-organisation-pies#index'); diff --git a/lib/Pear/LocalLoop/Controller/Api/External.pm b/lib/Pear/LocalLoop/Controller/Api/External.pm index 40a63fe..8183b18 100644 --- a/lib/Pear/LocalLoop/Controller/Api/External.pm +++ b/lib/Pear/LocalLoop/Controller/Api/External.pm @@ -165,4 +165,52 @@ sub post_year_spend { }); } +sub post_supplier_count { + my $c = shift; + + my $user = $c->stash->{api_user}; + + my $last = DateTime->today; + my $first = $last->clone->subtract( years => 1 ); + + my $dtf = $c->schema->storage->datetime_parser; + my $driver = $c->schema->storage->dbh->{Driver}->{Name}; + my $spend_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search( + { + purchase_time => { + -between => [ + $dtf->format_datetime($first), + $dtf->format_datetime($last), + ], + }, + buyer_id => $user->entity->id, + }, + { + columns => [ + { + quantised => 'quantised_days', + count => \"COUNT(*)", + total_spend => { sum => 'value' }, + } + ], + group_by => [ 'quantised_days', 'seller_id' ], + order_by => { '-asc' => 'quantised_days' }, + } + ); + + my @graph_data = ( + map { { + count => $_->get_column('count'), + value => $_->get_column('total_spend'), + date => $_->get_column('quantised'), + seller => $_->seller->name, + } } $spend_rs->all, + ); + + return $c->render( json => { + success => Mojo::JSON->true, + data => \@graph_data, + }); +} + 1; diff --git a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm index a524424..c710908 100644 --- a/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm +++ b/lib/Pear/LocalLoop/Schema/Result/ViewQuantisedTransactionPg.pm @@ -22,4 +22,18 @@ SELECT "value", FROM "transactions" /); +__PACKAGE__->belongs_to( + "buyer", + "Pear::LocalLoop::Schema::Result::Entity", + { id => "buyer_id" }, + { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, +); + +__PACKAGE__->belongs_to( + "seller", + "Pear::LocalLoop::Schema::Result::Entity", + { id => "seller_id" }, + { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, +); + 1;