working API with arrays

This commit is contained in:
Finn 2018-01-22 16:26:45 +00:00
parent 6e970de92c
commit 6cd7df1259
2 changed files with 29 additions and 23 deletions

View file

@ -8,11 +8,9 @@ sub post_category_list {
my $entity = $c->stash->{api_user}->entity; my $entity = $c->stash->{api_user}->entity;
my $duration = DateTime::Duration->new( days => 30 ); my $duration = DateTime::Duration->new( days => 28 );
my $start = DateTime->today; my $end = DateTime->today;
my $end = $start->clone->subtract_duration( $duration ); my $start = $end->clone->subtract_duration( $duration );
my $data = { days => [], category => [], value => [] };
my $dtf = $c->schema->storage->datetime_parser; my $dtf = $c->schema->storage->datetime_parser;
my $driver = $c->schema->storage->dbh->{Driver}->{Name}; my $driver = $c->schema->storage->dbh->{Driver}->{Name};
@ -29,26 +27,34 @@ sub post_category_list {
{ {
columns => [ columns => [
{ {
quantised => 'quantised_days', quantised => 'quantised_weeks',
count => \"COUNT(*)", value => 'value',
category_id => 'category_id',
} }
], ],
group_by => 'quantised_days', order_by => { '-desc' => 'quantised_weeks' },
order_by => { '-asc' => 'quantised_days' },
} }
); );
my $data = {};
for ( $month_transaction_rs->all ) { for ( $month_transaction_rs->all ) {
my $quantised = $c->db_datetime_parser->parse_datetime($_->get_column('quantised')); my $quantised = $c->db_datetime_parser->parse_datetime($_->get_column('quantised'));
push @{ $data->{ days } }, ($c->format_iso_datetime( $quantised ) || 0); my $days = $c->format_iso_date( $quantised ) || 0;
push @{ $data->{ category } }, ($_->get_column('category_id') || 0); my $category = $_->get_column('category_id') || 0;
push @{ $data->{ value } }, ($_->get_column('value') || 0) / 100000; my $value = ($_->get_column('value') || 0) / 100000;
$data->{$days} = [] unless exists $data->{$days};
push @{ $data->{$days} }, {
days => $days,
value => $value,
category => $category,
};
} }
return $c->render( return $c->render(
json => { json => {
success => Mojo::JSON->true, success => Mojo::JSON->true,
graph => $data, data => $data,
} }
); );
} }

View file

@ -10,17 +10,17 @@ __PACKAGE__->table('view_quantised_transactions');
__PACKAGE__->result_source_instance->is_virtual(1); __PACKAGE__->result_source_instance->is_virtual(1);
__PACKAGE__->result_source_instance->view_definition( qq/ __PACKAGE__->result_source_instance->view_definition( qq/
SELECT "transactions.value", SELECT "transactions"."value",
"transactions.distance", "transactions"."distance",
"transactions.purchase_time", "transactions"."purchase_time",
"transactions.buyer_id", "transactions"."buyer_id",
"transactions.seller_id", "transactions"."seller_id",
"transaction_category.category_id", "transaction_category"."category_id",
DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"transactions.purchase_time")) AS "quantised_hours", DATETIME(STRFTIME('%Y-%m-%d %H:00:00',"transactions"."purchase_time")) AS "quantised_hours",
DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions.purchase_time")) AS "quantised_days", DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions"."purchase_time")) AS "quantised_days",
DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions.purchase_time", 'weekday 1')) AS "quantised_weeks" DATETIME(STRFTIME('%Y-%m-%d 00:00:00',"transactions"."purchase_time", 'weekday 1')) AS "quantised_weeks"
FROM "transactions" FROM "transactions"
LEFT JOIN "transaction_category" ON "transactions.id" = "transaction_category.transaction_id" LEFT JOIN "transaction_category" ON "transactions"."id" = "transaction_category"."transaction_id"
/); /);
1; 1;