changed stat viewing and amended tests
This commit is contained in:
parent
78fdfc1e1d
commit
a4bcd9c6d7
4 changed files with 105 additions and 73 deletions
|
@ -63,9 +63,9 @@ sub post_customer {
|
||||||
|
|
||||||
my $entity = $c->stash->{api_user}->entity;
|
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 $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 $dtf = $c->schema->storage->datetime_parser;
|
||||||
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
|
||||||
|
@ -73,7 +73,7 @@ sub post_customer {
|
||||||
{
|
{
|
||||||
purchase_time => {
|
purchase_time => {
|
||||||
-between => [
|
-between => [
|
||||||
$dtf->format_datetime($start),
|
$dtf->format_datetime($start_weeks),
|
||||||
$dtf->format_datetime($end),
|
$dtf->format_datetime($end),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -125,8 +125,69 @@ sub post_customer {
|
||||||
push @{ $sectors->{ purchases } }, ($_->get_column('count') || 0);
|
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 => {
|
return $c->render( json => {
|
||||||
success => Mojo::JSON->true,
|
success => Mojo::JSON->true,
|
||||||
|
data => $data,
|
||||||
weeks => $weeks,
|
weeks => $weeks,
|
||||||
sectors => $sectors,
|
sectors => $sectors,
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,19 +6,8 @@ sub index {
|
||||||
|
|
||||||
my $entity = $c->stash->{api_user}->entity;
|
my $entity = $c->stash->{api_user}->entity;
|
||||||
|
|
||||||
my $data = { local_all => {}, cat_total => {}, categories => {}, essentials => {} };
|
|
||||||
|
|
||||||
my $purchase_rs = $entity->purchases;
|
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({
|
my $local_org_local_purchase = $purchase_rs->search({
|
||||||
"me.distance" => { '<', 20000 },
|
"me.distance" => { '<', 20000 },
|
||||||
'organisation.is_local' => 1,
|
'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 local purchaser' => $local_org_local_purchase->count,
|
||||||
'Local shop non-local purchaser' => $local_org_non_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 local purchaser' => $non_local_org_local_purchase->count,
|
||||||
'Non-local shop non-local purchaser' => $non_local_org_non_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(
|
return $c->render(
|
||||||
json => {
|
json => {
|
||||||
success => Mojo::JSON->true,
|
success => Mojo::JSON->true,
|
||||||
data => $data,
|
local_all => $local_all,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ $framework->install_fixtures('users');
|
||||||
my $t = $framework->framework;
|
my $t = $framework->framework;
|
||||||
my $schema = $t->app->schema;
|
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 );
|
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)
|
->status_is(200)->or($framework->dump_error)
|
||||||
->json_is('/weeks', {
|
->json_is('/weeks', {
|
||||||
first => 20,
|
first => 17,
|
||||||
second => 20,
|
second => 20,
|
||||||
max => 22,
|
max => 21,
|
||||||
sum => 118,
|
sum => 118,
|
||||||
count => 6,
|
count => 6,
|
||||||
})
|
})
|
||||||
->or($framework->dump_error)
|
|
||||||
->json_is('/sectors', {
|
->json_is('/sectors', {
|
||||||
sectors => ['A'],
|
sectors => ['A'],
|
||||||
purchases => [118],
|
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 {
|
sub create_random_transaction {
|
||||||
my $buyer = shift;
|
my $buyer = shift;
|
||||||
|
|
|
@ -42,7 +42,7 @@ $t->post_ok('/api/v1/customer/pies' => json => {
|
||||||
session_key => $session_key,
|
session_key => $session_key,
|
||||||
})
|
})
|
||||||
->status_is(200)->or($framework->dump_error)
|
->status_is(200)->or($framework->dump_error)
|
||||||
->json_is('/pie', {
|
->json_is('/local_all', {
|
||||||
'Local shop local purchaser' => 0,
|
'Local shop local purchaser' => 0,
|
||||||
'Local shop non-local purchaser' => 0,
|
'Local shop non-local purchaser' => 0,
|
||||||
'Non-local shop local purchaser' => 0,
|
'Non-local shop local purchaser' => 0,
|
||||||
|
|
Reference in a new issue