This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Schema/Result/Leaderboard.pm

252 lines
5.3 KiB
Perl
Raw Normal View History

2017-05-23 22:06:07 +00:00
package Pear::LocalLoop::Schema::Result::Leaderboard;
use strict;
use warnings;
use base 'DBIx::Class::Core';
use DateTime;
__PACKAGE__->table("leaderboards");
__PACKAGE__->add_columns(
"id" => {
data_type => "integer",
is_auto_increment => 1,
is_nullable => 0,
},
"name" => {
data_type => "varchar",
size => 255,
is_nullable => 0,
},
"type" => {
data_type => "varchar",
size => 255,
is_nullable => 0,
},
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->add_unique_constraint(["type"]);
__PACKAGE__->has_many(
"sets",
"Pear::LocalLoop::Schema::Result::LeaderboardSet",
{ "foreign.leaderboard_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
sub create_new {
my $self = shift;
my $start = shift;
my $type = $self->type;
if ( $type eq 'daily_total' ) {
return $self->_create_total_set( $start, $start->clone->add( days => 1 ) );
} elsif ( $type eq 'weekly_total' ) {
return $self->_create_total_set( $start, $start->clone->add( days => 7 ) );
} elsif ( $type eq 'monthly_total' ) {
return $self->_create_total_set( $start, $start->clone->add( months => 1 ) );
} elsif ( $type eq 'all_time_total' ) {
return $self->_create_total_all_time( $start );
2017-05-23 22:06:07 +00:00
} elsif ( $type eq 'daily_count' ) {
return $self->_create_count_set( $start, $start->clone->add( days => 1 ) );
} elsif ( $type eq 'weekly_count' ) {
return $self->_create_count_set( $start, $start->clone->add( days => 7 ) );
} elsif ( $type eq 'monthly_count' ) {
return $self->_create_count_set( $start, $start->clone->add( months => 1 ) );
} elsif ( $type eq 'all_time_count' ) {
return $self->_create_count_all_time( $start );
2017-05-23 22:06:07 +00:00
}
warn "Unrecognised type";
return $self;
}
sub _get_customer_rs {
my $self = shift;
return $self->result_source->schema->resultset('Entity')->search({
type => 'customer',
});
}
sub _set_position_and_trend {
my ( $self, @leaderboard ) = @_;
2017-05-23 22:06:07 +00:00
# Sort numerically descending
@leaderboard = sort { $b->{value} <=> $a->{value} } @leaderboard;
2017-05-23 22:06:07 +00:00
my $position = 0;
2017-05-23 22:06:07 +00:00
my $previous_board = $self->get_latest;
if ( defined $previous_board ) {
$previous_board = $previous_board->values;
}
2017-05-23 22:06:07 +00:00
for my $lb_val ( @leaderboard ) {
$position++;
$lb_val->{position} = $position;
2017-05-23 22:06:07 +00:00
my $previous_value;
if ( defined $previous_board ) {
$previous_value = $previous_board->find({ entity_id => $lb_val->{entity_id} });
}
my $trend;
if ( ! defined $previous_value ) {
$trend = 0;
} elsif ( $previous_value->position > $position ) {
$trend = -1;
} elsif ( $previous_value->position < $position ) {
$trend = 1;
} else {
$trend = 0;
}
$lb_val->{trend} = $trend;
}
return @leaderboard;
}
sub _create_total_set {
my ( $self, $start, $end ) = @_;
my $user_rs = $self->_get_customer_rs;
my @leaderboard;
while ( my $user_result = $user_rs->next ) {
my $transaction_rs = $user_result->purchases->search_between( $start, $end );
my $transaction_sum = $transaction_rs->get_column('value')->sum;
2017-05-23 22:06:07 +00:00
push @leaderboard, {
entity_id => $user_result->id,
2017-05-23 22:06:07 +00:00
value => $transaction_sum || 0,
};
}
@leaderboard = $self->_set_position_and_trend(@leaderboard);
2017-05-23 22:06:07 +00:00
$self->create_related(
'sets',
{
date => $start,
values => \@leaderboard,
},
);
return $self;
}
sub _create_count_set {
my ( $self, $start, $end ) = @_;
my $user_rs = $self->_get_customer_rs;
2017-05-23 22:06:07 +00:00
my @leaderboard;
while ( my $user_result = $user_rs->next ) {
my $transaction_rs = $user_result->purchases->search_between( $start, $end );
2017-05-23 22:06:07 +00:00
my $transaction_count = $transaction_rs->count;
push @leaderboard, {
entity_id => $user_result->id,
2017-05-23 22:06:07 +00:00
value => $transaction_count || 0,
};
}
@leaderboard = $self->_set_position_and_trend(@leaderboard);
2017-05-23 22:06:07 +00:00
$self->create_related(
'sets',
{
date => $start,
values => \@leaderboard,
},
);
return $self;
}
sub _create_total_all_time {
my ( $self, $end ) = @_;
2017-05-23 22:06:07 +00:00
my $user_rs = $self->_get_customer_rs;
2017-05-23 22:06:07 +00:00
my @leaderboard;
while ( my $user_result = $user_rs->next ) {
my $transaction_rs = $user_result->purchases->search_before( $end );
2017-05-23 22:06:07 +00:00
my $transaction_sum = $transaction_rs->get_column('value')->sum;
push @leaderboard, {
entity_id => $user_result->id,
2017-05-23 22:06:07 +00:00
value => $transaction_sum || 0,
};
}
@leaderboard = $self->_set_position_and_trend(@leaderboard);
2017-05-23 22:06:07 +00:00
$self->create_related(
'sets',
{
date => $end,
values => \@leaderboard,
},
);
return $self;
}
sub _create_count_all_time {
my ( $self, $end ) = @_;
2017-05-23 22:06:07 +00:00
my $user_rs = $self->_get_customer_rs;
2017-05-23 22:06:07 +00:00
my @leaderboard;
while ( my $user_result = $user_rs->next ) {
my $transaction_rs = $user_result->purchases->search_before( $end );
2017-05-23 22:06:07 +00:00
my $transaction_count = $transaction_rs->count;
push @leaderboard, {
entity_id => $user_result->id,
2017-05-23 22:06:07 +00:00
value => $transaction_count || 0,
};
}
@leaderboard = $self->_set_position_and_trend(@leaderboard);
2017-05-23 22:06:07 +00:00
$self->create_related(
'sets',
{
date => $end,
values => \@leaderboard,
},
);
return $self;
}
sub get_latest {
my $self = shift;
my $latest = $self->search_related('sets', {}, {
order_by => { -desc => 'date' },
})->first;
return $latest;
2017-05-23 22:06:07 +00:00
}
1;