custom graph duration and dateRange added

(untested)
This commit is contained in:
Felix 2019-08-14 14:00:34 +01:00
parent 461dd88691
commit 4a1fe64711
2 changed files with 57 additions and 8 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View file

@ -20,9 +20,20 @@ sub index {
avg_spend_last_week
total_last_month
avg_spend_last_month
total_duration
total_range
avg_spend_duration
avg_spend_range
/ );
return $c->api_validation_error if $validation->has_error;
if ($validation->param('graph') == 'total_range' || $validation->param('graph') == 'avg_spend_range') {
$validation->required('start_date', 'end_date');
}
if ($validation->param('graph') == 'total_duration' || $validation->param('graph') == 'avg_spend_duration') {
$validation->required('duration');
}
my $graph_sub = "graph_" . $validation->param('graph');
@ -44,15 +55,26 @@ sub index {
sub graph_total_last_week { return shift->_purchases_total_duration( 7 ) }
sub graph_total_last_month { return shift->_purchases_total_duration( 30 ) }
sub graph_total_duration {
my ( $c, $duration ) = @_;
return shift->__purchases_total_duration( $duration );
}
sub graph_total_range {
my ( $c, $start_date, $end_date ) = @_;
return shift->__purchases_total_duration( $duration, $start_date, $end_date );
}
sub _purchases_total_duration {
my ( $c, $day_duration ) = @_;
my ( $c, $day_duration, $start_date, $end_date ) = @_;
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 );
# if $start_date and $end_date are not present it will use $duration
my ( $start, $end ) = $c->_get_start_end_duration( $duration, $start_date, $end_date );
$data->{bounds} = {
min => $c->format_iso_datetime( $start ),
@ -81,15 +103,26 @@ sub _purchases_total_duration {
sub graph_avg_spend_last_week { return shift->_purchases_avg_spend_duration( 7 ) }
sub graph_avg_spend_last_month { return shift->_purchases_avg_spend_duration( 30 ) }
sub graph_avg_spend_duration {
my ( $c, $duration ) = @_;
return shift->_purchases_avg_spend_duration( $duration );
}
sub graph_avg_spend_range {
my ( $c, $start_date, $end_date ) = @_;
return shift->_purchases_avg_spend_duration( $duration, $start_date, $end_date );
}
sub _purchases_avg_spend_duration {
my ( $c, $day_duration ) = @_;
my ( $c, $day_duration, $start_date, $end_date ) = @_;
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 );
# if $start_date and $end_date are not present it will use $duration
my ( $start, $end ) = $c->_get_start_end_duration( $duration, $start_date, $end_date );
$data->{bounds} = {
min => $c->format_iso_datetime( $start ),
@ -143,9 +176,25 @@ sub _purchases_avg_spend_duration {
}
sub _get_start_end_duration {
my ( $c, $duration ) = @_;
my $end = DateTime->today;
my $start = $end->clone->subtract_duration( $duration );
my ( $c, $duration, $start_date, $end_date ) = @_;
my $start;
my $end;
if ($end_date && $start_date) {
$start = DateTime->new(
year => substr $start_date, 0, 4,
month => substr $start_date, 4, 2,
days => substr $start_date, 7, 2,
);
$end = DateTime->new(
year => substr $end_date, 0, 4,
month => substr $end_date, 4, 2,
days => substr $end_date, 7, 2,
);
} else {
$end = $end->clone->subtract_duration( $duration );
}
return ( $start, $end );
}