fixing graph code and added placeholder pie code

This commit is contained in:
Finn 2017-12-14 17:20:06 +00:00
parent af95b31eb8
commit b2627eea4f
5 changed files with 114 additions and 60 deletions

View File

@ -175,6 +175,7 @@ sub startup {
$api_v1_cust->post('/graphs')->to('api-v1-customer-graphs#index');
$api_v1_cust->post('/snippets')->to('api-v1-customer-snippets#index');
$api_v1_cust->post('/pies')->to('api-v1-customer-pies#index');
my $admin_routes = $r->under('/admin')->to('admin#under');

View File

@ -41,44 +41,6 @@ sub index {
return $c->$graph_sub;
}
# Replace with code for doughnut
=pod
sub graph_customers_range {
my $c = shift;
my $validation = $c->validation;
$validation->input( $c->stash->{api_json} );
$validation->required('start')->is_iso_date;
$validation->required('end')->is_iso_date;
return $c->api_validation_error if $validation->has_error;
my $entity = $c->stash->{api_user}->entity;
my $data = { labels => [], data => [] };
my $start = $c->parse_iso_date( $validation->param('start') );
my $end = $c->parse_iso_date( $validation->param('end') );
while ( $start <= $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->sales
->search_between( $start, $next_end )
->count;
push @{ $data->{ labels } }, $c->format_iso_date( $start );
push @{ $data->{ data } }, $transactions;
$start->add( days => 1 );
}
return $c->render(
json => {
success => Mojo::JSON->true,
graph => $data,
}
);
}
=cut
sub graph_total_last_week { return shift->_purchases_total_duration( 7 ) }
sub graph_total_last_month { return shift->_purchases_total_duration( 30 ) }
@ -92,6 +54,11 @@ sub _purchases_total_duration {
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
$data->{bounds} = {
min => $c->format_iso_datetime( $start ),
max => $c->format_iso_datetime( $end ),
};
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->purchases
@ -124,6 +91,11 @@ sub _purchases_avg_spend_duration {
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
$data->{bounds} = {
min => $c->format_iso_datetime( $start ),
max => $c->format_iso_datetime( $end ),
};
my $dtf = $c->schema->storage->datetime_parser;
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
my $transaction_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search(

View File

@ -78,35 +78,30 @@ sub graph_customers_range {
);
}
sub graph_customers_last_7_days {
my $c = shift;
my $duration = DateTime::Duration->new( days => 7 );
return $c->_customers_last_duration( $duration );
}
sub graph_customers_last_30_days {
my $c = shift;
my $duration = DateTime::Duration->new( days => 30 );
return $c->_customers_last_duration( $duration );
}
sub graph_customers_last_7_days { return shift->_customers_last_duration( 7 ) }
sub graph_customers_last_30_days { return shift->_customers_last_duration( 30 ) }
sub _customers_last_duration {
my ( $c, $duration ) = @_;
my ( $c, $day_duration ) = @_;
my $duration = DateTime::Duration->new( days => $day_duration );
my $entity = $c->stash->{api_user}->entity;
my $data = { labels => [], data => [] };
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
$data->{bounds} = {
min => $c->format_iso_datetime( $start ),
max => $c->format_iso_datetime( $end ),
};
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->sales
->search_between( $start, $next_end )
->count;
push @{ $data->{ labels } }, $start->day_name;
push @{ $data->{ labels } }, $c->format_iso_datetime( $start );
push @{ $data->{ data } }, $transactions;
$start->add( days => 1 );
}
@ -132,13 +127,18 @@ sub _sales_last_duration {
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
$data->{bounds} = (
min => $c->format_iso_datetime( $start ),
max => $c->format_iso_datetime( $end ),
);
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->sales
->search_between( $start, $next_end )
->get_column('value')
->sum || 0 + 0;
push @{ $data->{ labels } }, $start->day_name;
push @{ $data->{ labels } }, $c->format_iso_datetime( $start );
push @{ $data->{ data } }, $transactions / 100000;
$start->add( days => 1 );
}
@ -164,13 +164,18 @@ sub _purchases_last_duration {
my ( $start, $end ) = $c->_get_start_end_duration( $duration );
$data->{bounds} = {
min => $c->format_iso_datetime( $start ),
max => $c->format_iso_datetime( $end ),
};
while ( $start < $end ) {
my $next_end = $start->clone->add( days => 1 );
my $transactions = $entity->purchases
->search_between( $start, $next_end )
->get_column('value')
->sum || 0 + 0;
push @{ $data->{ labels } }, $start->day_name;
push @{ $data->{ labels } }, $c->format_iso_datetime( $start );
push @{ $data->{ data } }, $transactions / 100000;
$start->add( days => 1 );
}

View File

@ -40,16 +40,68 @@ my $session_key = $framework->login({
$t->post_ok('/api/v1/customer/graphs' => json => {
session_key => $session_key,
graph => 'avg_spend_last_week',
graph => 'total_last_week',
})
->status_is(200)->or($framework->dump_error)
->json_is('/graph', {
labels => [ map { $t->app->format_iso_datetime(
$start->clone->subtract( days => $_ )->subtract( hours => 12 )
) } reverse ( 0 .. 6 ) ],
bounds => {
min => $t->app->format_iso_datetime($start->clone->subtract( days => 6 )->subtract( hours => 12 ) ),
max => $t->app->format_iso_datetime($start->clone->add( hours => 12 )),
},
data => [ 20, 40, 20, 30, 30, 40, 10 ],
});
$t->post_ok('/api/v1/customer/graphs' => json => {
session_key => $session_key,
graph => 'avg_spend_last_week',
})
->status_is(200)->or($framework->dump_error)
->json_is('/graph', {
labels => [ map { $t->app->format_iso_datetime(
$start->clone->subtract( days => $_ )->subtract( hours => 12 )
) } reverse ( 0 .. 29 ) ],
bounds => {
min => $t->app->format_iso_datetime($start->clone->subtract( days => 6 )->subtract( hours => 12 ) ),
max => $t->app->format_iso_datetime($start->clone->add( hours => 12 )),
},
data => [ 10, 10, 10, 10, 10, 10, 10 ],
});
$t->post_ok('/api/v1/customer/graphs' => json => {
session_key => $session_key,
graph => 'total_last_month',
})
->status_is(200)->or($framework->dump_error)
->json_is('/graph', {
labels => [ map { $t->app->format_iso_datetime(
$start->clone->subtract( days => $_ )->subtract( hours => 12 )
) } reverse ( 0 .. 29 ) ],
bounds => {
min => $t->app->format_iso_datetime($start->clone->subtract( days => 29 )->subtract( hours => 12 ) ),
max => $t->app->format_iso_datetime($start->clone->add( hours => 12 )),
},
data => [ 40, 20, 30, 30, 40, 10, 40, 30, 30, 20, 40, 20, 40, 20, 30, 30, 40, 10, 40, 30, 30, 20, 40, 20, 40, 20, 30, 30, 40, 10 ],
});
$t->post_ok('/api/v1/customer/graphs' => json => {
session_key => $session_key,
graph => 'avg_spend_last_month',
})
->status_is(200)->or($framework->dump_error)
->json_is('/graph', {
labels => [ map { $t->app->format_iso_datetime(
$start->clone->subtract( days => $_ )->subtract( hours => 12 )
) } reverse ( 0 .. 29 ) ],
bounds => {
min => $t->app->format_iso_datetime($start->clone->subtract( days => 29 )->subtract( hours => 12 ) ),
max => $t->app->format_iso_datetime($start->clone->add( hours => 12 )),
},
data => [ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 ],
});
$framework->logout( $session_key );
$session_key = $framework->login({

View File

@ -44,7 +44,13 @@ $t->post_ok('/api/v1/organisation/graphs' => json => {
})
->status_is(200)->or($framework->dump_error)
->json_is('/graph', {
labels => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 6 ) ],
labels => [ map { $t->app->format_iso_datetime(
$start->clone->subtract( days => $_ )->subtract( hours => 12 )
) } reverse ( 0 .. 6 ) ],
bounds => {
min => $t->app->format_iso_datetime($start->clone->subtract( days => 6 )->subtract( hours => 12 ) ),
max => $t->app->format_iso_datetime($start->clone->add( hours => 12 )),
},
data => [ 2, 4, 2, 3, 3, 4, 1 ],
});
@ -54,7 +60,13 @@ $t->post_ok('/api/v1/organisation/graphs' => json => {
})
->status_is(200)->or($framework->dump_error)
->json_is('/graph', {
labels => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 29 ) ],
labels => [ map { $t->app->format_iso_datetime(
$start->clone->subtract( days => $_ )->subtract( hours => 12 )
) } reverse ( 0 .. 29 ) ],
bounds => {
min => $t->app->format_iso_datetime($start->clone->subtract( days => 6 )->subtract( hours => 12 ) ),
max => $t->app->format_iso_datetime($start->clone->add( hours => 12 )),
},
data => [ 4, 2, 3, 3, 4, 1, 4, 3, 3, 2, 4, 2, 4, 2, 3, 3, 4, 1, 4, 3, 3, 2, 4, 2, 4, 2, 3, 3, 4, 1 ],
});
@ -64,7 +76,13 @@ $t->post_ok('/api/v1/organisation/graphs' => json => {
})
->status_is(200)->or($framework->dump_error)
->json_is('/graph', {
labels => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 6 ) ],
labels => [ map { $t->app->format_iso_datetime(
$start->clone->subtract( days => $_ )->subtract( hours => 12 )
) } reverse ( 0 .. 6 ) ],
bounds => {
min => $t->app->format_iso_datetime($start->clone->subtract( days => 6 )->subtract( hours => 12 ) ),
max => $t->app->format_iso_datetime($start->clone->add( hours => 12 )),
},
data => [ 20, 40, 20, 30, 30, 40, 10 ],
});
@ -74,7 +92,13 @@ $t->post_ok('/api/v1/organisation/graphs' => json => {
})
->status_is(200)->or($framework->dump_error)
->json_is('/graph', {
labels => [ map { $start->clone->subtract( days => $_ )->day_name } reverse ( 0 .. 29 ) ],
labels => [ map { $t->app->format_iso_datetime(
$start->clone->subtract( days => $_ )->subtract( hours => 12 )
) } reverse ( 0 .. 29 ) ],
bounds => {
min => $t->app->format_iso_datetime($start->clone->subtract( days => 6 )->subtract( hours => 12 ) ),
max => $t->app->format_iso_datetime($start->clone->add( hours => 12 )),
},
data => [ 40, 20, 30, 30, 40, 10, 40, 30, 30, 20, 40, 20, 40, 20, 30, 30, 40, 10, 40, 30, 30, 20, 40, 20, 40, 20, 30, 30, 40, 10 ],
});