-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmpo.js
More file actions
581 lines (542 loc) · 19.2 KB
/
tmpo.js
File metadata and controls
581 lines (542 loc) · 19.2 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
$.ajaxSetup({
timeout: 10 * 60 * 1000, // msecs
cache: false
})
class Tmpo {
constructor(sid, token, progress_cb = (() => { return }),
resample=true, purge=true, debug=false) {
this.sid = sid
this.token = token
this.resample = resample
this.debug = debug
this.dbPromise = idb.open("flukso", 3, (upgradeDB) => {
switch (upgradeDB.oldVersion) {
case 0: // called at DB creation time
upgradeDB.createObjectStore("device")
upgradeDB.createObjectStore("sensor")
upgradeDB.createObjectStore("tmpo")
case 1:
upgradeDB.createObjectStore("cache")
case 2:
upgradeDB.deleteObjectStore("device")
upgradeDB.deleteObjectStore("sensor")
}
})
this.progress = this._progress_state_init()
this.progress_cb = progress_cb
}
async sync() {
this.progress = this._progress_state_init("waiting")
const last = await this._last_block()
try {
await this._tmpo_sensor_sync(last)
this.progress.sync.state = "completed"
this.progress.clean.state = "completed"
this._progress_cb_handler()
if (this.resample) {
await this._cache_update()
this.progress.resample.state = "completed"
this._progress_cb_handler()
}
if (this.purge) {
this._tmpo_purge()
}
return true
} catch(err) {
this.progress.sync.state = "error"
this.progress.error = err
this._progress_cb_handler()
return false
}
}
async _tmpo_sensor_sync(last = { rid: 0, lvl: 0, bid: 0 }) {
let prom = Array()
const url = `https://www.flukso.net/api/sensor/${this.sid}/tmpo/sync`
const data = {
version: "1.0",
token: this.token,
rid: last.rid,
lvl: last.lvl,
bid: last.bid
}
const blocks = await $.ajax({
dataType: "json",
url: url,
data: data
})
this._log("tmpo", `sync call for sensor ${this.sid} successful`)
for (const block of blocks) {
this.progress.sync.state = "running";
this.progress.sync.todo++;
prom.push(this._tmpo_block_sync(block))
}
return Promise.all(prom)
}
async _tmpo_block_sync(block) {
const rid = block.rid
const lvl = block.lvl
const bid = block.bid
const key = this._bid2key(this.sid, rid, lvl, bid)
const url = `https://www.flukso.net/api/sensor/${this.sid}/tmpo/${rid}/${lvl}/${bid}`
const data = {
version: "1.0",
token: this.token
}
const response = await $.ajax({
dataType: "json",
url: url,
data: data
})
this._log("tmpo", `sync call for block ${key} successful`)
const db = await this.dbPromise
const tx = db.transaction("tmpo", "readwrite")
tx.objectStore("tmpo").put(response, key)
await tx.complete
this._log("tmpo", `block ${key} stored locally`)
await this._tmpo_clean(block)
this.progress.sync.todo--;
this._progress_cb_handler()
}
async _tmpo_clean(last) {
const range = IDBKeyRange.bound(this.sid, `${this.sid}~`)
const that = this // for 'process' function scoping
const db = await this.dbPromise
const tx = db.transaction("tmpo", "readonly")
const store = tx.objectStore("tmpo")
const cursor = store.openKeyCursor(range)
let keys2delete = Array()
return await cursor.then(function process(cursor) {
if (last.lvl == 8) { return }
if (!cursor) { return }
const block = that._key2bid(cursor.key)
if (block.rid == last.rid &&
block.lvl < last.lvl &&
block.bid < last.bid + Math.pow(2, last.lvl)) {
keys2delete.push(cursor.key)
}
return cursor.continue().then(process)
})
.then(async () => {
let list2str = keys2delete.toString()
this._log("clean", `cleaning sensor ${this.sid} blocks [${list2str}]`)
for (const key of keys2delete) {
this.progress.clean.state = "running";
this.progress.clean.todo++;
await this._tmpo_delete_block(key)
this.progress.clean.todo--;
this._progress_cb_handler()
}
})
}
async _tmpo_purge() {
// purge all block20's except for the last one
let rid
({ rid } = await this._last_block())
const range = IDBKeyRange.bound(`${this.sid}-${rid}-20`, `${this.sid}-${rid}-20~`)
const that = this // for 'process' function scoping
const db = await this.dbPromise
const tx = db.transaction("tmpo", "readonly")
const store = tx.objectStore("tmpo")
const cursor = store.openKeyCursor(range)
let keys2delete = Array()
cursor.then(function process(cursor) {
if (!cursor) { return }
keys2delete.push(cursor.key)
return cursor.continue().then(process)
})
.then(async () => {
keys2delete.sort()
keys2delete.pop()
let list2str = keys2delete.toString()
this._log("purge", `purging sensor ${this.sid} blocks [${list2str}]`)
for (const key of keys2delete) {
this._tmpo_delete_block(key)
}
})
}
async _tmpo_delete_block(key) {
const db = await this.dbPromise
const tx = db.transaction("tmpo", "readwrite")
tx.objectStore("tmpo").delete(key)
await tx.complete
this._log("del", `block ${key} deleted`)
}
async _cache_update(rid=null) {
this.progress.resample.state = "running"
this._progress_cb_handler()
const day2secs = 86400
const tz_offset = -7200
if (rid == null) {
({ rid } = await this._last_block())
}
let cblock = null
let head = 0
const last = await this._cache_last_block()
if (last.cid > 0 && last.rid == rid) {
cblock = await this._cache_block_load(last.rid, last.cid)
head = (Math.floor(cblock.state.last / 256) + 1) * 256
}
const serieslist = await this._serieslist(
{ rid: rid, head: head, subsample: 8 })
for (const { t, v } of serieslist) {
for (let [i, _] of t.entries()) {
if (!cblock) {
cblock = new CachedBlock(t[i], tz_offset)
}
if (!cblock.push(t[i], v[i])) {
this._cache_block_store(rid, cblock)
cblock = new CachedBlock(t[i], tz_offset)
cblock.push(t[i], v[i])
}
}
}
if (cblock) {
cblock.close()
this._cache_block_store(rid, cblock)
}
}
_cache_block_store(rid, cblock) {
const key = this._cache2key(this.sid, rid, cblock.head)
this.dbPromise.then((db) => {
const tx = db.transaction("cache", "readwrite")
tx.objectStore("cache").put(cblock, key)
return tx.complete
})
.then(() => {
this._log("cache", `cblock ${key} stored`)
})
}
async _cache_block_load(rid, cid) {
const db = await this.dbPromise
const key = this._cache2key(this.sid, rid, cid)
const tx = db.transaction("cache")
const store = tx.objectStore("cache")
let cblock = await store.get(key)
Object.setPrototypeOf(cblock, CachedBlock.prototype)
return cblock
}
async _cache_last_block() {
// get all cache blocks of a single sensor
const range = IDBKeyRange.bound(this.sid, `${this.sid}~`)
const that = this
let last = {
rid: 0,
cid: 0
}
const db = await this.dbPromise
const tx = db.transaction("cache", "readonly")
const store = tx.objectStore("cache")
const cursor = store.openKeyCursor(range)
return await cursor.then(function process(cursor) {
if (!cursor) { return }
const cache = that._key2cache(cursor.key)
if (cache.cid > last.cid) {
last = cache
}
return cursor.continue().then(process)
})
.then(() => {
return last
})
}
async _cache_serieslist(
{ rid=null, head=0, tail=Number.POSITIVE_INFINITY, resample=60,
series="avg" } = { }) {
if (rid == null) {
({ rid } = await this._cache_last_block())
}
const cblocklist = await this._cache_blocklist(rid, head, tail)
const cblocklist2string = JSON.stringify(cblocklist)
this._log("series", `sensor ${this.sid} using cache blocks: ${cblocklist2string}`)
const serieslist = await Promise.all(
cblocklist.map(async (cache) => {
const cblock = await this._cache_block_load(cache.rid, cache.cid)
return this._cache_block2series(cblock, head, tail, resample, series)
})
)
return serieslist
}
async _cache_blocklist(rid, head, tail) {
const that = this
// get all cache blocks of a single sensor/rid
const range = IDBKeyRange.bound(`${this.sid}-${rid}`, `${this.sid}-${rid}~`)
const db = await this.dbPromise
const tx = db.transaction("cache", "readonly")
const store = tx.objectStore("cache")
const cursor = store.openKeyCursor(range)
let cblocklist = []
return await cursor.then(function process(cursor) {
if (!cursor) { return }
const cache = that._key2cache(cursor.key)
if (head < that._cblocktail(cache.cid) && tail > cache.cid) {
cblocklist.push(cache)
}
return cursor.continue().then(process)
})
.then(() => {
return cblocklist.sort((a, b) => a.cid - b.cid)
})
}
_cache_block2series(cblock, head, tail, resample=60, series="avg") {
const start = Math.max(cblock.head,
Math.ceil((head / resample) * resample))
const stop = Math.min(cblock.head + cblock.day2secs - resample,
Math.floor((tail / resample) * resample))
const i_start = (start - cblock.head) / resample
const i_stop = (stop - cblock.head) / resample
const size = i_stop - i_start + 1
let t = Array(size)
for (let i = 0; i < size; i++) {
t[i] = start + i * resample
}
let v = cblock.series[series][resample].slice(i_start, i_stop + 1)
return {t: t, v: v}
}
_cache2key(sid, rid, cid) {
return `${sid}-${rid}-${cid}`
}
_key2cache(key) {
const cache = key.split("-")
return {
rid: Number(cache[1]),
cid: Number(cache[2])
}
}
_cblocktail(cid) {
return cid + 86400
}
async series(params, sync=false) {
if (sync) {
await this.sync()
}
let serieslist = []
if (params.resample) {
serieslist = await this._cache_serieslist(params)
} else {
serieslist = await this._serieslist(params)
}
return this._serieslist_concat(serieslist)
}
async _serieslist(
{ rid=null, head=0, tail=Number.POSITIVE_INFINITY, subsample=0 } = { }) {
if (rid == null) {
({ rid } = await this._last_block())
}
const blocklist = await this._blocklist(rid, head, tail)
const blocklist2string = JSON.stringify(blocklist)
this._log("series", `sensor ${this.sid} using blocks: ${blocklist2string}`)
const serieslist = await Promise.all(
blocklist.map(async (block) => {
const content = await this._block_content(block.rid, block.lvl, block.bid)
const key = this._bid2key(this.sid, block.rid, block.lvl, block.bid)
return this._block2series(key, content, head, tail, subsample)
})
)
return serieslist
}
async _last_block() {
// get all tmpo blocks of a single sensor
const range = IDBKeyRange.bound(this.sid, `${this.sid}~`)
const that = this
let last = {
rid: 0,
lvl: 0,
bid: 0
}
const db = await this.dbPromise
const tx = db.transaction("tmpo", "readonly")
const store = tx.objectStore("tmpo")
const cursor = store.openKeyCursor(range)
return await cursor.then(function process(cursor) {
if (!cursor) { return }
const block = that._key2bid(cursor.key)
if (block.bid > last.bid) {
last = block
}
return cursor.continue().then(process)
})
.then(() => {
const last_key = that._bid2key(that.sid, last.rid, last.lvl, last.bid)
that._log("last", `last block: ${last_key}`)
return last
})
}
async _blocklist(rid, head, tail) {
const that = this
// get all tmpo blocks of a single sensor/rid
const range = IDBKeyRange.bound(`${this.sid}-${rid}`, `${this.sid}-${rid}~`)
const db = await this.dbPromise
const tx = db.transaction("tmpo", "readonly")
const store = tx.objectStore("tmpo")
const cursor = store.openKeyCursor(range)
let blocklist = []
return await cursor.then(function process(cursor) {
if (!cursor) { return }
const block = that._key2bid(cursor.key)
if (head < that._blocktail(block.lvl, block.bid) && tail > block.bid) {
blocklist.push(block)
}
return cursor.continue().then(process)
})
.then(() => {
return blocklist.sort((a, b) => a.bid - b.bid)
})
}
_block2series(key, content, head, tail, subsample=0) {
if (content == null) {
this._log("tmpo", `block2series for block ${key} failed: empty block`)
return {t: [], v: []}
}
let [t_accu, v_accu] = content.h.head
let t_accu_last = 0
let t = []
let v = []
for (let i in content.t) {
t_accu += content.t[i]
v_accu += content.v[i]
if (t_accu >= head && t_accu <= tail
&& t_accu >= t_accu_last + subsample) {
t.push(t_accu)
v.push(v_accu)
t_accu_last = t_accu
}
}
return {t: t, v: v}
}
_serieslist_concat(list) {
let accu = { t: [], v: [] }
for (let { t, v } of list) {
accu.t = accu.t.concat(t)
accu.v = accu.v.concat(v)
}
return accu
}
async _block_content(rid, lvl, bid) {
const db = await this.dbPromise
const key = this._bid2key(this.sid, rid, lvl, bid)
const tx = db.transaction("tmpo")
const store = tx.objectStore("tmpo")
return store.get(key)
}
_blocktail(lvl, bid) {
return bid + Math.pow(2, lvl)
}
_bid2key(sid, rid, lvl, bid) {
return `${sid}-${rid}-${lvl}-${bid}`
}
_key2bid(key) {
const block = key.split("-")
return {
rid: Number(block[1]),
lvl: Number(block[2]),
bid: Number(block[3])
}
}
_progress_state_init(init_state="waiting") {
return {
// possible states: waiting/running/completed/error
sync: { state: init_state, todo: 0 },
clean: { state: init_state, todo: 0 },
resample: { state: init_state, todo: 0 },
time: { start: Date.now(), runtime: 0 },
error: null
}
}
_progress_cb_handler() {
this.progress.time.runtime =
Date.now() - this.progress.time.start
this.progress_cb(this.progress)
}
_log(topic, message) {
if (this.debug) {
const time = Date.now()
console.log(`${time} [${topic}] ${message}`)
}
}
}
class CachedBlock {
constructor(timestamp, tz_offset) {
this.day2secs = 86400
const x = Math.floor((timestamp - tz_offset) / this.day2secs)
this.head = x * this.day2secs + tz_offset
this.series = {
"avg": [],
"min": [],
"max": []
}
for (const metric of ["avg", "min", "max"]) {
this.series[metric][60] = Array(1440)
this.series[metric][900] = Array(96)
this.series[metric][3600] = Array(24)
this.series[metric][86400] = Array(1)
}
this.state = {
last: this.head,
minute: null,
samples: []
}
}
push(t, v) {
if (t >= this.head + this.day2secs) {
this.gen_minute()
this.close()
return false
}
if (!this.state.minute) {
this.state.minute = Math.floor(t / 60) * 60
}
if (t < this.state.minute + 60) {
this.state.last = t
this.state.samples.push(v)
} else {
this.gen_minute()
this.push(t, v)
}
return true
}
gen_minute() {
const i = (this.state.minute - this.head) / 60
const len = this.state.samples.length
this.series.avg[60][i] =
this.state.samples.reduce((a, b) => a + b, 0) / len
this.series.min[60][i] = Math.min(...this.state.samples)
this.series.max[60][i] = Math.max(...this.state.samples)
this.state.minute = null
this.state.samples = []
}
close() {
const resampling = [
{ entries: 96, samples: 15, source: 60, sink: 900 },
{ entries: 24, samples: 4, source: 900, sink: 3600 },
{ entries: 1, samples: 24, source: 3600, sink: 86400 }
]
for (const r of resampling) {
this.resample(r)
}
}
resample({ entries, samples, source, sink }) {
for (const i of Array(entries).keys()) {
let slice = {
"avg": null,
"min": null,
"max": null
}
slice.avg = this.series.avg[source]
.slice(i * samples, (i + 1) * samples)
const len = Object.keys(slice.avg).length
if (len == 0) {
continue
}
this.series.avg[sink][i] =
slice.avg.reduce((a, b) => a + b, 0) / len
slice.min = this.series.min[source]
.slice(i * samples, (i + 1) * samples)
this.series.min[sink][i] =
Math.min(...slice.min.filter(x => x != undefined))
slice.max = this.series.max[source]
.slice(i * samples, (i + 1) * samples)
this.series.max[sink][i] =
Math.max(...slice.max.filter(x => x != undefined))
}
}
}