/
run.rb
164 lines (147 loc) · 5.14 KB
/
run.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
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
require_relative 'processing.rb'
require 'gruff'
require 'logger'
require 'json'
def token
return File.new("token.secret").read.strip
end
def goal_names
return JSON.parse(File.new("goal_names.secret").read.strip)
end
def main
goal_names().each do |goal|
data = download_data(goal, token())
data.reverse_each do |entry|
print_datapoint(entry, Logger::INFO)
$logger.info ""
end
process_data_and_generate_graph(data, goal)
end
end
def process_data_and_generate_graph(data, goal_name)
split = split_into_days(data)
step = 15
now = Time.now
outliers = get_outliers(split, current_date: now.to_date, percent_to_remove: 10)
data_for_graph = []
# first day is guaranted to have 0 datapoint added by beeminder, so [0][0] is safe
# -1 is done to counteract first incrementation in a loop
# incrementation is at the beginning of loop to allow for breaks in alter part
processed_date = split[0][0].timestamp.to_date.next_day(-1)
split.each do |dataset_for_a_day|
processed_date = processed_date.next_day(1)
progress = get_data_series_for_graph(dataset_for_a_day, processed_date, now, step)
total_value = progress[-1].to_i
location_in_outliers = outliers.index(total_value)
if !location_in_outliers.nil? && processed_date != now.to_date
# current day is not discarded, even if it is an outlier
outliers.delete_at location_in_outliers
$logger.info "IGNORED AS OUTLIER (#{total_value})"
else
$logger.debug progress.inspect
data_for_graph << progress
end
end
if outliers.length > 0
$logger.warn "NOT REMOVED OUTLIERS: #{outliers}"
end
generate_graph(data_for_graph, split, goal_name)
end
def get_special_color_for_today(days_count)
colors = []
(1..(days_count-1)).each do |_|
colors << '#bbbbbb'
end
colors << 'black'
end
def get_outliers(dataset_split_into_days, current_date, percent_to_remove: 10)
processed_date = dataset_split_into_days[0][0].timestamp.to_date.next_day(-1)
day_counts = []
dataset_split_into_days.each do |dataset_for_a_day|
processed_date = processed_date.next_day(1)
day_count = 0
if processed_date == current_date
# do not discard current date even if it is an outlier
next
end
dataset_for_a_day.each do |datapoint|
if datapoint.timestamp.to_date == datapoint.updated_at.to_date
day_count += datapoint.value
end
end
day_counts << day_count.to_i
end
return get_outliers_from_number_array(day_counts, percent_to_remove)
end
def get_outliers_from_number_array(array_of_numbers, percent_to_declare_as_outliers)
array_of_numbers.sort!
outlier_count = array_of_numbers.length * percent_to_declare_as_outliers / 100
outliers_on_each_side = outlier_count / 2
returned = []
array_of_numbers.each_with_index do |number, index|
if index < outliers_on_each_side || index > (array_of_numbers.length - 1 - outliers_on_each_side)
returned << number
end
end
return returned
end
def print_datapoint(entry, importance)
$logger.add(importance) { entry.timestamp } # last second of the affected day
$logger.add(importance) { entry.updated_at } # time of posting/updating entry (may be on a later date)
$logger.add(importance) { entry.value } # datapoint value
end
def get_initialized_graph(displayed_lines)
size = 2000
g = Gruff::Line.new(size)
g.hide_legend = true
g.hide_dots = true
g.line_width = 1
g.theme = {
colors: get_special_color_for_today(displayed_lines),
marker_color: 'grey',
font_color: 'black',
background_colors: 'white'
}
return g
end
def get_data_series_for_graph(dataset_for_a_day, day_date, current_time, resolution_in_minutes)
progress = transform_dataset_for_a_day_in_cumulative_progress(dataset_for_a_day, resolution_in_minutes)
if day_date.to_date == current_time.to_date
# do not paint line of what has yet to happen
# especially as it would be a flat demotivating one
wanted_minutes = current_time.hour * 60 + current_time.min
wanted_steps = wanted_minutes / resolution_in_minutes + 1 #+1 because we want also current that started but is not yet finished
return progress[0, wanted_steps]
end
return progress
end
def generate_graph(processed_data_for_graph, data_split_into_days, goal_name)
g = get_initialized_graph(processed_data_for_graph.length)
processed_data_for_graph.each do |data_for_day|
g.data :day, data_for_day
end
$logger.info "GENERATING"
# https://github.com/topfunky/gruff
# https://makandracards.com/makandra/8745-plot-graphs-in-ruby
# probably I should switch to https://plot.ly/python/
g.title = "percentile #{percentile_of_day_compared_to_other(data_split_into_days)} for #{goal_name}"
g.write('percentile_feedback_' + goal_name + '.png')
pid = Process.fork
if pid.nil? then
# In child
`eog percentile_feedback_#{goal_name}.png`
exit
else
# In parent
Process.detach(pid)
end
end
$logger = Logger.new(STDOUT)
$logger.formatter = proc do |severity, datetime, _progname, msg|
"#{severity} [#{datetime.strftime('%Y-%m-%d %H:%M:%S.%6N')} ##{Process.pid}]: #{msg}\n"
end
$logger.level = Logger::WARN
if __FILE__ == $0
$logger.level = Logger::INFO
main
end