Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/bin/migrations/2.4.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS `statistics` (
`date` date NOT NULL,
`kind` varchar(32) NOT NULL DEFAULT '0.00',
`field` varchar(32) NOT NULL DEFAULT '0',
`count` int(11) NOT NULL DEFAULT '0',
`sum` decimal(12,2) NOT NULL DEFAULT '0.00',
`min` decimal(12,2) NOT NULL DEFAULT '0.00',
`max` decimal(12,2) NOT NULL DEFAULT '0.00',
`avg` decimal(12,2) NOT NULL DEFAULT '0.00',
UNIQUE KEY (`date`, `kind`, `field`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
36 changes: 36 additions & 0 deletions app/lib/Core/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ our @EXPORT = qw(
cache
get_smart_args
first_item
stats
);

use vars qw($AUTOLOAD);
Expand Down Expand Up @@ -614,4 +615,39 @@ sub cfg {
return wantarray ? %{ $data } : $data;
}

sub stats_fields {
my $self = shift;

my $structure = $self->structure;
my @fields;

for my $field (keys %$structure) {
push @fields, $field if $structure->{$field}->{user_for_stats};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Проверяй наличие поля: stats_mode

}

return @fields;
}

sub stats {
my ($self, $action, $args) = @_;
Copy link
Owner

@danuk danuk Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для режима diff используй разницу значений:

my $method = "get_$field";
$value = $self->$method + 0 - $args->{$field}


my @fields = $self->stats_fields;
return unless @fields;

for my $field (@fields) {

next unless exists $args->{$field};

my $value = $self->structure->{$field}->{count_for_stats}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Проверяем, что stats_mode eq 'inc'

? 1
: $args->{$field};

$self->srv('statistics')->add(
$self->kind,
$field,
$value
);
}
}

1;
1 change: 1 addition & 0 deletions app/lib/Core/Bonus.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sub structure {
type => 'number',
required => 1,
title => 'кол-во бонусов',
user_for_stats => 1,
},
comment => {
type => 'json',
Expand Down
1 change: 1 addition & 0 deletions app/lib/Core/Pay.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ sub structure {
type => 'number',
required => 1,
title => 'сумма платежа',
user_for_stats => 1,
},
date => {
type => 'now',
Expand Down
2 changes: 2 additions & 0 deletions app/lib/Core/Sql/Data.pm
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ sub add {
my $self = shift;
my %args = ( @_ );

$self->stats('add', \%args);

$args{table}||= $self->table;
my $table = delete $args{table};

Expand Down
84 changes: 84 additions & 0 deletions app/lib/Core/Statistics.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package Core::Statistics;

use v5.14;
use utf8;
use parent 'Core::Base';
use Core::Base;
use Core::Utils qw(
now
);
use Core::System::ServiceManager qw( logger );

sub table { return 'statistics' };

sub structure {
return {
date => {
type => 'date',
key => 1,
title => 'дата',
},
kind => {
type => 'text',
title => 'контроллер',
},
field => {
type => 'text',
title => 'тип',
},
count => {
type => 'number',
title => 'кол-во',
},
sum => {
type => 'number',
title => 'сумма',
},
min => {
type => 'number',
title => 'минимальная сумма',
},
max => {
type => 'number',
title => 'максимальная сумма',
},
avg => {
type => 'number',
title => 'средняя сумма',
},
};
}

sub add {
my ($self, $kind, $field, $value) = @_;

return unless defined $value;

my $date = now('date');

$kind = lc $kind;

$self->do(q{
INSERT INTO statistics
(`date`,`kind`,`field`,`count`,`sum`,`min`,`max`,`avg`)
VALUES (?, ?, ?, 1, ?, ?, ?, ?)

ON DUPLICATE KEY UPDATE
count = count + 1,
sum = sum + VALUES(sum),
min = LEAST(min, VALUES(min)),
max = GREATEST(max, VALUES(max)),
avg = (sum + VALUES(sum)) / (count + 1)
},
$date,
$kind,
$field,
$value + 0,
$value + 0,
$value + 0,
$value + 0
);

}

1;
2 changes: 2 additions & 0 deletions app/lib/Core/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ sub structure {
type => 'text',
required => 1,
title => 'логин',
user_for_stats => 1,
count_for_stats => 1,
},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Причем тут user? Может use?
И не нужно двух переменных, сделай проще:

stats_mode => 'inc' # inc или diff

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inc - режим счетчика
diff - режим, когда вычисляем разницу между текущим значением и новым
asis - режим, когда используем новое значение (из args)

password => {
type => 'text',
Expand Down