From ad86463585498c6cfd014be1a0b71586ce52b696 Mon Sep 17 00:00:00 2001 From: Bkeenke Date: Thu, 5 Mar 2026 23:31:10 +0300 Subject: [PATCH] =?UTF-8?q?bk:=20=D0=9F=D0=BE=D0=B4=D0=B3=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=D0=BA=D0=B0=20=D0=BA=20=D1=81=D0=B1=D0=BE=D1=80?= =?UTF-8?q?=D1=83=20=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=81=D1=82=D0=B8=D0=BA?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/bin/migrations/2.4.2.sql | 11 +++++ app/lib/Core/Base.pm | 36 ++++++++++++++++ app/lib/Core/Bonus.pm | 1 + app/lib/Core/Pay.pm | 1 + app/lib/Core/Sql/Data.pm | 2 + app/lib/Core/Statistics.pm | 84 ++++++++++++++++++++++++++++++++++++ app/lib/Core/User.pm | 2 + 7 files changed, 137 insertions(+) create mode 100644 app/bin/migrations/2.4.2.sql create mode 100644 app/lib/Core/Statistics.pm diff --git a/app/bin/migrations/2.4.2.sql b/app/bin/migrations/2.4.2.sql new file mode 100644 index 00000000..f07d1009 --- /dev/null +++ b/app/bin/migrations/2.4.2.sql @@ -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; \ No newline at end of file diff --git a/app/lib/Core/Base.pm b/app/lib/Core/Base.pm index af474cd8..b70fa36b 100644 --- a/app/lib/Core/Base.pm +++ b/app/lib/Core/Base.pm @@ -30,6 +30,7 @@ our @EXPORT = qw( cache get_smart_args first_item + stats ); use vars qw($AUTOLOAD); @@ -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}; + } + + return @fields; +} + +sub stats { + my ($self, $action, $args) = @_; + + 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} + ? 1 + : $args->{$field}; + + $self->srv('statistics')->add( + $self->kind, + $field, + $value + ); + } +} + 1; diff --git a/app/lib/Core/Bonus.pm b/app/lib/Core/Bonus.pm index 663c6f40..add765ae 100644 --- a/app/lib/Core/Bonus.pm +++ b/app/lib/Core/Bonus.pm @@ -27,6 +27,7 @@ sub structure { type => 'number', required => 1, title => 'кол-во бонусов', + user_for_stats => 1, }, comment => { type => 'json', diff --git a/app/lib/Core/Pay.pm b/app/lib/Core/Pay.pm index cf4a75c0..9685dfed 100644 --- a/app/lib/Core/Pay.pm +++ b/app/lib/Core/Pay.pm @@ -33,6 +33,7 @@ sub structure { type => 'number', required => 1, title => 'сумма платежа', + user_for_stats => 1, }, date => { type => 'now', diff --git a/app/lib/Core/Sql/Data.pm b/app/lib/Core/Sql/Data.pm index da90581a..c3e99384 100644 --- a/app/lib/Core/Sql/Data.pm +++ b/app/lib/Core/Sql/Data.pm @@ -568,6 +568,8 @@ sub add { my $self = shift; my %args = ( @_ ); + $self->stats('add', \%args); + $args{table}||= $self->table; my $table = delete $args{table}; diff --git a/app/lib/Core/Statistics.pm b/app/lib/Core/Statistics.pm new file mode 100644 index 00000000..677bee81 --- /dev/null +++ b/app/lib/Core/Statistics.pm @@ -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; \ No newline at end of file diff --git a/app/lib/Core/User.pm b/app/lib/Core/User.pm index 69259835..a2dd512d 100644 --- a/app/lib/Core/User.pm +++ b/app/lib/Core/User.pm @@ -41,6 +41,8 @@ sub structure { type => 'text', required => 1, title => 'логин', + user_for_stats => 1, + count_for_stats => 1, }, password => { type => 'text',