-
Notifications
You must be signed in to change notification settings - Fork 45
bk: Подготовка к сбору статистики #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: stats
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) = @_; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Для режима |
||
|
|
||
| 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} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| 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; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,8 @@ sub structure { | |
| type => 'text', | ||
| required => 1, | ||
| title => 'логин', | ||
| user_for_stats => 1, | ||
| count_for_stats => 1, | ||
| }, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Причем тут user? Может use? stats_mode => 'inc' # inc или diff
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inc - режим счетчика |
||
| password => { | ||
| type => 'text', | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Проверяй наличие поля: stats_mode