-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals.lua
306 lines (229 loc) · 9.6 KB
/
signals.lua
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
local MAX_HISTORY_COUNT = 16
signals = {}
function signals.on_init()
global.signals_history = {
-- player = { -- force
-- ["some-combinator"] = { -- monitor name
-- item = { -- signal type
-- ["iron-ore"] = { -- signal name
-- samples = { -- samples
-- { tick = 0, value = 120 },
-- { tick = 60, value = 60 },
-- { tick = 120, value = 0 },
-- },
-- update_rate = 300, -- ticks
-- latest = 0, -- speeds the calculation of % filled
-- largest_seen = 120, -- represents 100% filled
-- },
-- },
-- fluid = {},
-- virtual = {},
-- },
-- },
}
end
local function get_samples_root(signal)
if not global.signals_history then
global.signals_history = {}
end
local all_history = global.signals_history
if not all_history[signal.entity.force.name] then
all_history[signal.entity.force.name] = {}
end
local force_history = all_history[signal.entity.force.name]
if not force_history[signal.title] then
force_history[signal.title] = {}
end
local title_history = force_history[signal.title]
if not title_history[signal.signal.type] then
title_history[signal.signal.type] = {}
end
local type_history = title_history[signal.signal.type]
if not type_history[signal.signal.name] then
type_history[signal.signal.name] = { samples = {} }
end
return type_history[signal.signal.name]
end
local function reduce(tbl, func)
if not tbl then return nil end
local candidate = nil
for _,v in pairs(tbl) do
if candidate == nil or func(v, candidate) then
candidate = v
end
end
return candidate
end
local function largest_value_of(t)
return reduce(t, function(new, current) return new > current end)
end
local function smallest_value_of(t)
return reduce(t, function(new, current) return new < current end)
end
-- Potentially change the update rate of a signal, if it shows signs of changing
-- more often than the currently-recorded rate.
-- A signal's update rate controls the number and size of the buckets that samples
-- are split into before rate-of-change analysis. In particular, signals from a
-- mining drill are only updated every 300 ticks, and this is the default.
-- Update rate can only ever decrease, and must show consistent faster change
-- (more than (MAX_HISTORY_COUNT / 2) + 1 samples taken less than update_rate
-- apart).
-- NB: Signal update rates can only get shorter, never longer (but will reset
-- on load).
local function check_update_rate(sample_root)
local update_rate = sample_root.update_rate or 300
local samples = sample_root.samples
local faster_update_candidates = {}
for i = 1, (#samples - 1) do
local timespan = samples[i+1].tick - samples[i].tick
local difference = samples[i+1].value - samples[i].value
if timespan < 0 then
log(string.format("WTF, there are two samples in the wrong order: %d and %d at ticks %d and %d. Ignored.",
samples[i+1].value, samples[i].value, samples[i+1].tick, samples[i].tick))
-- less than update_rate (but not 0 ticks) apart...
elseif timespan > 0 and timespan < update_rate then
-- ...and showing any difference at all...
if samples[i].value - samples[i+1].value ~= 0 then
-- then record a possible update_rate diff
table.insert(faster_update_candidates, timespan)
end
-- TODO: need a way to slow down the updates if the update rate is too fast
end
end
-- log(string.format("Have %d faster update candidates, need %d to change.",
-- #faster_update_candidates, MAX_HISTORY_COUNT / 2 + 1))
-- showing _consistent_ change...
if #faster_update_candidates > MAX_HISTORY_COUNT / 2 + 1 then
-- ...then pick the largest stored timespan between diffs
-- (still smaller than the current update_rate)
local new_rate = largest_value_of(faster_update_candidates)
log(string.format("Speeding up to sample rate %d", new_rate))
sample_root.update_rate = new_rate
end
end
local function bucketize_samples(samples, update_rate)
-- Pick the largest value for each bucket formed from
-- taking each sample tick and bucketing at
-- sample.tick - sample.tick % update_rate
local buckets = {}
local offset = samples[1].tick % update_rate
for _, sample in ipairs(samples) do
local current_bucket_tick = sample.tick - offset - (sample.tick - offset) % update_rate
local last_bucket = buckets[#buckets]
if #buckets == 0 or last_bucket.tick ~= current_bucket_tick then
table.insert(buckets, {
value = sample.value,
tick = current_bucket_tick,
})
else
if last_bucket.value < sample.value then
last_bucket.value = sample.value
end
end
end
return buckets
end
local function change_per_min_between_samples(old, new)
local diff = new.value - old.value
local duration = new.tick - old.tick
if duration == 0 then return 0 end
return diff * 60 * 60 / duration
-- ( ^ per minute )
end
local function calculate_rate_of_change(signal)
local success, sample_root = pcall(get_samples_root, signal)
if not success then error(sample_root) end
local samples = sample_root.samples
if #samples < 2 then
sample_root.rate_of_change_per_min = nil
sample_root.estimated_to_depletion = nil
return
end
check_update_rate(sample_root)
local update_rate = sample_root.update_rate or 300
-- log(string.format("Updating %s, signal %s, update rate %d ticks", signal.title, signal.signal.name, update_rate))
local buckets = bucketize_samples(samples, update_rate)
if #buckets < 3 then
sample_root.rate_of_change_per_min = nil
sample_root.estimated_to_depletion = nil
return
end
-- Weighted average: older samples' change rates have more impact than newer
local sum_weighted_change_rates = 0
local sum_weights = 0
for i = 1, #buckets - 1 do
local change_rate = change_per_min_between_samples(buckets[i], buckets[i + 1])
local weight = #buckets - i
-- log(string.format("Weight %d, difference %d, duration %d, unweighted change %f/min",
-- weight,
-- buckets[i].value - buckets[i+1].value,
-- buckets[i+1].tick - buckets[i].tick,
-- change_rate))
sum_weighted_change_rates = sum_weighted_change_rates + weight * change_rate
sum_weights = sum_weights + weight
end
local rate_of_change = sum_weighted_change_rates / sum_weights
sample_root.rate_of_change_per_min = rate_of_change
-- log(string.format("Final sum_weights: %d, sum_weighted_change_rates: %f. New change rate: %f/min",
-- sum_weights, sum_weighted_change_rates, rate_of_change))
end
function signals.add_sample(tick, live)
if not live.entity or not live.entity.valid then return end
local success, sample_root = pcall(get_samples_root, live)
if not success then error(sample_root) end
sample_root.latest = live.count
if not sample_root.largest_seen or sample_root.largest_seen < live.count then
sample_root.largest_seen = live.count
end
local samples = sample_root.samples
local new_sample = { tick = tick, value = live.count }
table.insert(samples, new_sample)
while #samples > MAX_HISTORY_COUNT do
table.remove(samples, 1)
end
calculate_rate_of_change(live)
end
function signals.rate_of_change(signal)
local success, sample_root = pcall(get_samples_root, signal)
if not success then error(sample_root) end
if sample_root.rate_of_change_per_min == nil then
return {"prodmon.insufficient-data"}
end
return {"prodmon.rate-of-change-per-min",
string.format("%.2f", sample_root.rate_of_change_per_min)}
end
function signals.estimate_to_depletion(signal)
local success, sample_root = pcall(get_samples_root, signal)
if not success then error(sample_root) end
if sample_root.rate_of_change_per_min == nil then
return "ETD/F: unknown"
end
local rate_of_change = sample_root.rate_of_change_per_min
local title = "ETD" -- estimated time to depletion
local minutes = 0
if rate_of_change == 0 then
return "ETD/F: never"
elseif sample_root.rate_of_change_per_min > 0 then
title = "ETF" -- estimated time to full
minutes = (sample_root.largest_seen - sample_root.latest) / rate_of_change
else -- rate_of_change is negative (decaying)
minutes = sample_root.latest / -rate_of_change
end
local hours = math.floor(minutes / 60)
if hours > 0 then
return string.format("%s: %d h %d m", title, hours, minutes % 60)
elseif minutes > 1 then
return string.format("%s: %d m", title, minutes)
else
return string.format("%s: <1 minute!", title)
end
end
function signals.percent_remaining(signal)
local success, sample_root = pcall(get_samples_root, signal)
if not success then error(sample_root) end
if not sample_root.largest_seen or not sample_root.latest then return "-- %" end
return sample_root.latest * 100 / sample_root.largest_seen
end
function signals.on_tick(e)
MAX_HISTORY_COUNT = settings.global["prodmon-sample-count"].value
end