forked from globau/logbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogbot-nightly
executable file
·232 lines (189 loc) · 6.89 KB
/
logbot-nightly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/perl
# update channel metadata, etc
use local::lib;
use v5.10;
use strict;
use warnings;
use FindBin qw( $RealBin );
use lib "$RealBin/lib";
BEGIN {
$ENV{TZ} = 'UTC';
$ENV{CRON} = !$ENV{DEBUG};
}
use DateTime ();
use File::Path qw( make_path );
use JSON ();
use JSON::XS qw( encode_json );
use LogBot::Config qw( find_config load_all_configs load_config reload_config save_config );
use LogBot::Database qw( dbh replace_sql_placeholders );
use LogBot::Util qw( file_for logbot_init nick_is_bot path_for round spurt );
use LogBot::Web::Util qw( nick_hash );
use Readonly;
Readonly::Scalar my $ARCHIVE_TIME => 60 * 60 * 24 * (365 / 2); # 6 months
my @configs;
if (@ARGV && $ARGV[0] eq '--all') {
shift;
@configs = values %{ load_all_configs() };
} else {
push @configs, load_config(find_config(shift));
}
@configs || die "syntax: syntax: logbot-nightly <config file|--all>\n";
my %configs = map { $_->{name} => $_ } @configs;
foreach my $config_name (sort keys %configs) {
my $config = $configs{$config_name};
logbot_init($config, name => 'logbot-nightly', quiet => 1);
# generate network meta
generate_meta($config_name, file_for($config, 'meta', undef, 'meta'));
generate_hours($config_name, file_for($config, 'meta', undef, 'hours'));
foreach my $channel ('_empty', sort keys %{ $config->{channels} }) {
next if $config->{channels}->{$channel}->{no_logs};
say $config_name, ': ', $channel if $ENV{DEBUG};
# generate data for about #channel
generate_meta($config_name, file_for($config, 'meta', $channel, 'meta'), $channel);
generate_hours($config_name, file_for($config, 'meta', $channel, 'hours'), $channel);
generate_nicks($config_name, file_for($config, 'meta', $channel, 'nicks'), $channel);
}
delete $config->{channels}->{_empty};
# stop logging and leave stale channels
archive_stale_channels($config_name);
}
sub query {
my %params = @_;
my @where =
exists($params{where})
? (ref($params{where}) ? @{ $params{where} } : $params{where})
: ();
my @values;
if ($params{channel}) {
push @where, 'channel = ?';
push @values, $params{channel};
}
my $sql = 'SELECT ' . $params{select} . ' FROM logs';
$sql .= ' WHERE ' . join(' AND ', @where) if @where;
$sql .= ' ORDER BY ' . $params{order} if $params{order};
if ($params{row_count}) {
$sql = 'SELECT COUNT(*) FROM (' . $sql . ') t';
} else {
$sql .= ' LIMIT 1';
}
say ' ', replace_sql_placeholders($params{dbh}, $sql, \@values) if $ENV{DEBUG};
return $params{dbh}->selectrow_array($sql, undef, @values) // undef;
}
sub generate_meta {
my ($config_name, $output_file, $channel) = @_;
my $config = $configs{$config_name};
my $dbh = dbh($config);
my $now = DateTime->now();
my $six_months = $now->clone()->subtract(months => 6);
my $one_month = $now->clone()->subtract(months => 1);
my $json = {
first_time => query(
dbh => $dbh,
channel => $channel,
select => 'time',
order => 'time ASC',
),
active_events => query(
dbh => $dbh,
channel => $channel,
select => 'COUNT(*)',
where => 'time >= ' . $six_months->epoch,
),
active_events_days => $six_months->delta_days($now)->in_units('days'),
active_nicks => query(
dbh => $dbh,
channel => $channel,
row_count => 1,
select => 'DISTINCT nick',
where => 'time >= ' . $six_months->epoch,
),
event_count => query(
dbh => $dbh,
channel => $channel,
select => 'COUNT(*)',
),
};
# because we calculate the event count nightly for the network-wide stats,
# round to nearest 1000 to make it clear it's an approximation
if (!$channel) {
$json->{event_count} = round($json->{event_count} / 1000) * 1000;
}
say ' ', encode_json($json) if $ENV{DEBUG};
spurt($output_file, encode_json($json));
}
sub generate_hours {
my ($config_name, $output_file, $channel) = @_;
my $config = $configs{$config_name};
my $dbh = dbh($config);
my $sql = 'SELECT CAST(time % (24 * 3600) AS INT), COUNT(*) FROM logs';
$sql .= ' WHERE channel = ?' if $channel;
$sql .= ' GROUP BY CAST(time % (24 * 3600) AS INT)';
my @values;
push @values, $channel if $channel;
say ' ', replace_sql_placeholders($dbh, $sql, \@values) if $ENV{DEBUG};
my %values;
foreach my $row (@{ $dbh->selectall_arrayref($sql, undef, @values) }) {
my ($ss, $value) = @{$row};
my $hh = sprintf('%.1f', $ss / (60 * 60));
$hh = '0.0' if $hh == 24;
$values{$hh} += $value;
}
my @data;
for (my $hh = 0; $hh < 24; $hh += 0.1) {
$hh = sprintf('%.1f', $hh);
push @data, [$hh * 1.0, ($values{$hh} || 0) * 1];
}
my $json = {
data => \@data,
lines => {
show => JSON::true,
fill => JSON::true,
},
};
spurt($output_file, encode_json($json));
}
sub generate_nicks {
my ($config_name, $output_file, $channel) = @_;
my $config = $configs{$config_name};
my $dbh = dbh($config);
my $sql = 'SELECT nick, COUNT(*) AS count FROM logs';
$sql .= ' WHERE channel = ?' if $channel;
$sql .= ' GROUP BY nick';
$sql .= ' ORDER BY COUNT(*) DESC, nick ASC';
$sql .= ' LIMIT 20';
my @values;
push @values, $channel if $channel;
say ' ', replace_sql_placeholders($dbh, $sql, \@values) if $ENV{DEBUG};
my $data = $dbh->selectall_arrayref($sql, { Slice => {} }, @values);
my $json = [];
foreach my $event (@{$data}) {
$event->{hash} = nick_hash($event->{nick});
$event->{bot} = nick_is_bot($config, $event->{nick});
push @{$json}, $event;
}
spurt($output_file, encode_json($json));
}
sub archive_stale_channels {
my ($config_name) = @_;
my $config = $configs{$config_name};
my $now = time();
my $reload = 0;
foreach my $channel (sort keys %{ $config->{channels} }) {
next if $config->{channels}->{$channel}->{disabled};
next if $config->{channels}->{$channel}->{archived};
my $dbh = dbh($config);
my $last_time =
$dbh->selectrow_array('SELECT time FROM logs WHERE channel = ? ORDER BY time DESC LIMIT 1', undef,
$channel);
next unless $last_time;
next unless $now - $last_time > $ARCHIVE_TIME;
say $config->{name}, ': archiving ', $channel if $ENV{DEBUG};
$config = reload_config($config);
$config->{channels}->{$channel}->{archived} = 1;
save_config($config);
$configs{$config_name} = $config;
}
if ($reload) {
system("$RealBin/logbot-irc", $config->{_internal}->{file}, '--reload');
}
}