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

281 lines
6.1 KiB
Perl
Raw Permalink 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(
2021-03-20 12:09:50 +00:00
"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,
},
2017-05-23 22:06:07 +00:00
);
__PACKAGE__->set_primary_key("id");
2021-03-20 12:09:50 +00:00
__PACKAGE__->add_unique_constraint( ["type"] );
2017-05-23 22:06:07 +00:00
__PACKAGE__->has_many(
2021-03-20 12:09:50 +00:00
"sets",
"Pear::LocalLoop::Schema::Result::LeaderboardSet",
{ "foreign.leaderboard_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
2017-05-23 22:06:07 +00:00
);
sub create_new {
2021-03-20 12:09:50 +00:00
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);
}
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);
}
warn "Unrecognised type";
return $self;
2017-05-23 22:06:07 +00:00
}
sub _get_customer_rs {
2021-03-20 12:09:50 +00:00
my $self = shift;
return $self->result_source->schema->resultset('Entity')->search(
{
type => 'customer',
}
);
}
sub _set_position_and_trend {
2021-03-20 12:09:50 +00:00
my ( $self, @leaderboard ) = @_;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
# Sort numerically descending
@leaderboard = sort { $b->{value} <=> $a->{value} } @leaderboard;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
my $position = 0;
2021-03-20 12:09:50 +00:00
my $previous_board = $self->get_latest;
if ( defined $previous_board ) {
2021-03-20 12:09:50 +00:00
$previous_board = $previous_board->values;
}
2021-03-20 12:09:50 +00:00
for my $lb_val (@leaderboard) {
$position++;
$lb_val->{position} = $position;
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;
}
2021-03-20 12:09:50 +00:00
return @leaderboard;
}
sub _create_total_set {
2021-03-20 12:09:50 +00:00
my ( $self, $start, $end ) = @_;
2021-03-20 12:09:50 +00:00
my $user_rs = $self->_get_customer_rs;
2021-03-20 12:09:50 +00:00
my @leaderboard;
2021-03-20 12:09:50 +00:00
while ( my $user_result = $user_rs->next ) {
my $transaction_rs =
$user_result->purchases->search_between( $start, $end );
2021-03-20 12:09:50 +00:00
my $transaction_sum = $transaction_rs->get_column('value')->sum;
2021-03-20 12:09:50 +00:00
push @leaderboard,
{
entity_id => $user_result->id,
value => $transaction_sum || 0,
};
}
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
@leaderboard = $self->_set_position_and_trend(@leaderboard);
2021-03-20 12:09:50 +00:00
$self->create_related(
'sets',
{
date => $start,
values => \@leaderboard,
},
);
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
return $self;
2017-05-23 22:06:07 +00:00
}
sub _create_count_set {
2021-03-20 12:09:50 +00:00
my ( $self, $start, $end ) = @_;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
my $user_rs = $self->_get_customer_rs;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
my @leaderboard;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
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
2021-03-20 12:09:50 +00:00
my $transaction_count = $transaction_rs->count;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
push @leaderboard,
{
entity_id => $user_result->id,
value => $transaction_count || 0,
};
}
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
@leaderboard = $self->_set_position_and_trend(@leaderboard);
2021-03-20 12:09:50 +00:00
$self->create_related(
'sets',
{
date => $start,
values => \@leaderboard,
},
);
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
return $self;
2017-05-23 22:06:07 +00:00
}
sub _create_total_all_time {
2021-03-20 12:09:50 +00:00
my ( $self, $end ) = @_;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
my $user_rs = $self->_get_customer_rs;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
my @leaderboard;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
while ( my $user_result = $user_rs->next ) {
my $transaction_rs = $user_result->purchases->search_before($end);
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
my $transaction_sum = $transaction_rs->get_column('value')->sum;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
push @leaderboard,
{
entity_id => $user_result->id,
value => $transaction_sum || 0,
};
}
2021-03-20 12:09:50 +00:00
@leaderboard = $self->_set_position_and_trend(@leaderboard);
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
$self->create_related(
'sets',
{
date => $end,
values => \@leaderboard,
},
);
return $self;
2017-05-23 22:06:07 +00:00
}
sub _create_count_all_time {
2021-03-20 12:09:50 +00:00
my ( $self, $end ) = @_;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
my $user_rs = $self->_get_customer_rs;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
my @leaderboard;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
while ( my $user_result = $user_rs->next ) {
my $transaction_rs = $user_result->purchases->search_before($end);
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
my $transaction_count = $transaction_rs->count;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
push @leaderboard,
{
entity_id => $user_result->id,
value => $transaction_count || 0,
};
}
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
@leaderboard = $self->_set_position_and_trend(@leaderboard);
2021-03-20 12:09:50 +00:00
$self->create_related(
'sets',
{
date => $end,
values => \@leaderboard,
},
);
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
return $self;
2017-05-23 22:06:07 +00:00
}
sub get_latest {
2021-03-20 12:09:50 +00:00
my $self = shift;
2017-05-23 22:06:07 +00:00
2021-03-20 12:09:50 +00:00
my $latest = $self->search_related(
'sets',
{},
{
order_by => { -desc => 'date' },
}
)->first;
2021-03-20 12:09:50 +00:00
return $latest;
2017-05-23 22:06:07 +00:00
}
1;