-
Notifications
You must be signed in to change notification settings - Fork 6
/
npp_chart.rb
97 lines (77 loc) · 2.41 KB
/
npp_chart.rb
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
$LOAD_PATH << '..'
require 'musikbot'
module NPPChart
# Date of first available data.
INITIAL_DATE = '2017-08-29'.freeze
def self.run
@mb = MusikBot::Session.new(inspect)
[:monthly, :weekly, :daily, :hourly].each do |type|
offset = @mb.config["#{type}_offset".to_sym]
next unless should_refresh_data(type)
start_date = get_start_date(type, offset)
data = []
historical_data(start_date, type).each do |row|
data << {
'date' => format_time(type, row['timestamp']),
'value' => row['count']
}
end
@mb.edit("Wikipedia:New pages patrol/Backlog chart/#{type}",
content: data.to_json,
summary: "Updating #{type} data for NPP backlog size"
)
end
end
def self.should_refresh_data(type)
case type
when :hourly
return true
when :daily
return @mb.now.hour == 0
when :weekly
return @mb.now.wday == 0
when :monthly
return @mb.now.mday == 1
end
end
def self.get_start_date(type, offset, offset_date = @mb.now)
return @mb.parse_date(INITIAL_DATE) if offset == '*'
case type
when :hourly
return offset_date - (offset.to_f / 24)
when :daily
return offset_date - offset.to_i
when :weekly
return offset_date - (offset.to_i * 7)
when :monthly
return offset_date.prev_month(offset.to_i)
end
end
def self.format_time(type, time)
if type == :hourly
return time.to_datetime.new_offset(0).strftime('%Y-%m-%d %H:%M')
end
time.to_datetime.new_offset(0).strftime('%Y-%m-%d')
end
def self.historical_data(start_date, type = :daily)
client = @mb.repl_client(credentials: :actrial, log: false)
group_sql = ''
if type == :daily
group_sql = 'GROUP BY YEAR(npp_timestamp), MONTH(npp_timestamp), DAY(npp_timestamp)'
elsif type == :weekly
group_sql = 'GROUP BY YEAR(npp_timestamp), MONTH(npp_timestamp), WEEK(npp_timestamp)'
elsif type == :monthly
group_sql = 'GROUP BY YEAR(npp_timestamp), MONTH(npp_timestamp)'
end
timestamp_sql = type == :hourly ? 'npp_timestamp' : 'MAX(npp_timestamp)'
sql = %{
SELECT #{timestamp_sql} AS `timestamp`, npp_num_articles AS `count`
FROM npp_queue_size
WHERE npp_timestamp > ?
#{group_sql}
}
statement = client.prepare(sql)
statement.execute(start_date).to_a
end
end
NPPChart.run