-
Notifications
You must be signed in to change notification settings - Fork 4
/
data-utils.js
593 lines (498 loc) · 19 KB
/
data-utils.js
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
581
582
583
584
585
586
587
588
589
590
591
592
import '@logseq/libs'
import {
getDateForPage,
getDateForPageWithoutBrackets,
getDayInText,
getScheduledDeadlineDateDay,
getScheduledDeadlineDateDayTime,
} from 'logseq-dateutils'
import {logseq as packageInfo} from './package.json'
function loggerWrapper(console_) {
const prefix = `#${packageInfo.id}: `
return {
debug: msg => { console_.debug(prefix + msg) },
info: msg => { console_.info(prefix + msg) },
log: msg => { console_.log(prefix + msg) },
warn: msg => { console_.warn(prefix + msg) },
error: msg => { console_.error(prefix + msg) },
}
}
export const logger = console = loggerWrapper(console)
export class Metric {
date // String formatted as: 1970-01-01T00:00:00.000Z
value
constructor(obj) {
this.date = obj.date
this.value = obj.value
}
}
export class DataUtils {
constructor(_logseq) {
this.logseq = _logseq
}
async findBlock(tree, name) {
let found = null
tree.forEach(async function (value) {
if(value.content && value.content.split('\n')[0] === name) {
found = value.uuid
return
}
})
if(found)
return found
else return null
}
async enterMetric(name, childName, entry) {
const DATA_PAGE = this.logseq.settings.data_page_name
let page = await this.logseq.Editor.getPage(DATA_PAGE)
if(!page) {
page = await this.logseq.Editor.createPage(DATA_PAGE, {}, { redirect: false })
if(page) {
console.log(`Created page ${DATA_PAGE}`)
}
else {
console.warn(`Failed to create page ${DATA_PAGE}`)
return
}
}
else {
console.log(`Loaded page ${DATA_PAGE}`)
}
let tree = await this.logseq.Editor.getPageBlocksTree(DATA_PAGE)
console.debug(`Loaded tree with ${tree.length} blocks`)
var blockId
if(tree.length == 0) {
console.debug(`Page is empty. Inserting block ${name}`)
blockId = (await this.logseq.Editor.appendBlockInPage(DATA_PAGE, name))?.uuid
}
else {
blockId = await this.findOrCreateBlock(tree, name)
if(blockId === null) {
console.debug("Can not locate block to insert metric")
return
}
}
if(childName)
{
let parentBlock = await this.logseq.Editor.getBlock(blockId, { includeChildren: true })
if(parentBlock?.children?.length === 0) {
let block = await this.logseq.Editor.insertBlock(blockId, childName, {
before: false, sibling: false, isPageBlock: false
})
blockId = block?.uuid
}
else {
let childId = await this.findOrCreateBlock(parentBlock?.children, childName)
if(childId === null) {
console.warn("Can not locate block to insert metric")
return
}
blockId = childId
}
}
let metricBlock = await this.logseq.Editor.insertBlock(blockId, entry, {
before: false, sibling: false, isPageBlock: false
})
if(!metricBlock) {
console.warn(`Failed to insert metric: ${entry}`)
}
else {
console.log(`Metric inserted successfully: ${entry}`)
let formattedName = name
if(childName)
formattedName += " / " + childName
logseq.UI.showMsg(`Inserted data point for metric ${formattedName}.`)
}
}
async findOrCreateBlock(tree, name) {
console.log(`findOrCreateBlock ${name}, ${tree.length}`)
let found = null
tree.forEach(async function (value) {
if(value.content === name) {
console.debug(`Iteration name match ${value.content}, ${value.children}`)
found = value.uuid
return
}
})
if(found)
return found
console.debug(`Block not found, inserting block at ${tree[tree.length - 1].uuid}`)
let block = await this.logseq.Editor.insertBlock(tree[tree.length - 1].uuid, name, {
before: false, sibling: true, isPageBlock: false
})
if(!block) {
console.warn(`Failed to create block ${name}`)
return null
}
console.log(`Created block ${name} with uuid ${block.uuid}`)
return block?.uuid
}
prepareMetricsForLineChart(metrics, cumulativeMode) {
metrics = this.sortMetricsByDate(this.filterInvalidMetrics(metrics))
let data = []
let sum = 0
metrics.forEach(metric => {
var date, value
try {
let y = parseFloat(metric.value)
sum += y
date = new Date(metric.date)
value = { x: date, y: cumulativeMode ? sum : y }
} catch {
console.debug(`Invalid meric. date: ${metric.date}, value: ${metric.value}`)
}
data.push(value)
})
return data
}
async loadLineChart(metricName, cumulativeMode) {
// Scenarios:
// 1. Single dataset using data from immediate children
// 2. Multiple datasets where data comes from grandchildren
// Modes:
// 1. Standard - data points plotted normally along y-axis
// 2. Cumulative - values are ordered chronologically and the cumulative sum is plotted
// Return value:
// datasets: [ { data: [ { x: (date), y: (float) } }, { ... } ] } ]
const datasets = []
const childNames = await this.loadMetricNames(metricName)
if(childNames.length > 0) {
for(const { label } of childNames) {
const metrics = await this.loadMetrics(metricName, label)
datasets.push({
data: this.prepareMetricsForLineChart(metrics, cumulativeMode),
label: label,
})
}
}
else {
const metrics = await this.loadMetrics(metricName)
datasets.push( { data: this.prepareMetricsForLineChart(metrics, cumulativeMode) } )
}
return datasets
}
// Remove any metrics that have non-numeric values or invalid dates
filterInvalidMetrics(metrics) {
var filtered = []
metrics.forEach(metric => {
if(isNaN(parseFloat(metric.value)))
return
if(isNaN(new Date(metric.date).valueOf()))
return
filtered.push(metric)
})
return filtered
}
// Chart.js requires data points to be sorted along the y-axis
sortMetricsByDate(metrics) {
var sorted = metrics.sort((a, b) => {
return new Date(a.date) - new Date(b.date)
})
return sorted
}
parseMetric(content) {
let parsed = null
try {
parsed = JSON.parse(content)
if(parsed.value && parsed.date)
return new Metric(parsed)
}
finally {
return parsed
}
}
async loadMetrics(metricName, childName) {
const DATA_PAGE = this.logseq.settings.data_page_name
var block
const tree = await this.logseq.Editor.getPageBlocksTree(DATA_PAGE)
let blockId = await this.findBlock(tree, metricName)
if(!blockId) return []
if(childName && childName.length > 0) {
block = await this.logseq.Editor.getBlock(blockId, { includeChildren: true })
blockId = await this.findBlock(block?.children, childName)
}
if(!blockId) return []
block = await this.logseq.Editor.getBlock(blockId, { includeChildren: true })
let metrics = []
var metric
if(childName && childName.length > 0) {
block?.children?.forEach( (child) => {
metric = this.parseMetric(child.content)
if(metric)
metrics.push(metric)
})
}
else {
block?.children?.forEach( (child) => {
// Child block may be an entry or it may be a label for a child metric
// Try to parse the content to see if it's a valid metric
metric = this.parseMetric(child.content)
if(metric) {
metrics.push(metric)
}
else {
child.children.forEach((grandchild) => {
metric = this.parseMetric(grandchild.content)
if(metric)
metrics.push(metric)
})
}
})
}
console.debug(`Loaded ${metrics.length} metrics`)
return metrics
}
async loadChildMetrics(metricName) {
console.log(`Loading child metrics for ${metricName}`)
var metrics = {}
const DATA_PAGE = this.logseq.settings.data_page_name
const tree = await this.logseq.Editor.getPageBlocksTree(DATA_PAGE)
console.debug(`Loaded tree: ${JSON.stringify(tree)}`)
let blockId = await this.findBlock(tree, metricName)
if(!blockId) return metrics
var block = await this.logseq.Editor.getBlock(blockId, { includeChildren: true })
block?.children?.forEach( (child) => {
let parsed = this.parseMetric(child.content)
if(parsed) {
// Only include child metrics
}
else if(child.content.length > 0) {
metrics[child.content] = []
child.children.forEach((grandchild) => {
parsed = this.parseMetric(grandchild.content)
if(parsed)
metrics[child.content].push(parsed)
})
}
})
return metrics
}
// Returns list of top-level metric names if `parent` is null.
// Returns list of names of child metrics if `parent` is non null.
async loadMetricNames(parent) {
const DATA_PAGE = this.logseq.settings.data_page_name
const tree = await this.logseq.Editor.getPageBlocksTree(DATA_PAGE)
let names = []
if(parent) {
let blockId = await this.findBlock(tree, parent)
let block = await this.logseq.Editor.getBlock(blockId, { includeChildren: true })
block?.children?.forEach((child) => {
try {
JSON.parse(child.content)
}
catch {
if(child.content.indexOf("{{renderer") === -1) {
names.push({
id: names.length,
uuid: child.uuid,
label: child.content
})
}
}
})
}
else {
tree.forEach(async function (value) {
if(value.content.indexOf("{{renderer") === -1) {
names.push({
id: names.length,
uuid: value.uuid,
label: value.content
})
}
})
}
return names
}
async propertiesQuery(properties, start, end) {
return Promise.all(properties.map(async (prop) => {
try {
const results = await this.logseq.DB.datascriptQuery(`
[:find (pull ?b [*])
:where
[?b :block/page ?p]
[?p :block/journal? true]
[?b :block/properties ?prop]
[?p :block/journal-day ?day]
[(get ?prop :${prop})]
[(>= ?day ${start})]
[(<= ?day ${end})]
]
`)
if(!results)
return { data: [] }
const metrics = []
for(const [ result ] of results) {
const value = parseFloat(result.properties[prop])
if(!isNaN(value)) {
const page = await this.logseq.Editor.getPage(result.page.id)
if(page) {
const day = page.journalDay.toString()
const date = new Date(day.slice(0, 4) + "-" + day.slice(4, 6) + "-" + day.slice(6) + " 00:00:00")
metrics.push(new Metric({ date: date, value: value }))
}
}
}
return metrics
}
catch(e) {
console.log(e)
return []
}
}))
}
async propertiesQueryLineChart(properties, cumulativeMode, start, end) {
let metrics = await this.propertiesQuery(properties, start, end)
return properties.map( (prop, idx) => {
return {
label: prop,
data: this.prepareMetricsForLineChart(metrics[idx], cumulativeMode)
}
})
}
backToSunday(date) {
let dayOfWeek = date.getDay();
let result = new Date(date)
result.setDate(result.getDate() - dayOfWeek);
return result
}
backToFirstOfMonth(date) {
}
async propertiesQueryBarChart(properties, bucketSizeDays, start, end) {
let datasets = await this.propertiesQuery(properties, start, end)
if(datasets.length == 0)
return []
// flatten into single array
let single = []
datasets.forEach(propArray => {
propArray.forEach(val => {
single.push(val)
})
})
if(single.length == 0)
return []
single.sort((a, b) => {
return a.date - b.date
})
let startTime = single[0].date.getTime()
let endTime = single[single.length - 1].date.getTime()
let bucketSizeMillis = 24 * 60 * 60 * 1000
if(bucketSizeDays == "week") {
bucketSizeMillis *= 7
startTime = this.backToSunday(single[0].date).getTime()
}
else if(bucketSizeDays == "month") {
}
else {
bucketSizeMillis *= parseInt(bucketSizeDays)
}
var results = []
datasets.forEach(dataset => {
let buckets = {}
// create the buckets
let numBuckets = Math.floor((endTime - startTime) / bucketSizeMillis) + 1
for(var i = 0; i < numBuckets; i++) {
buckets[i.toString()] = []
}
// populate the buckets
dataset.forEach((metric, idx) => {
let bucket = Math.floor((metric.date.getTime() - startTime) / bucketSizeMillis)
buckets[bucket.toString()].push(metric)
})
// calculate sum and average
var metrics = { }
for (let key in buckets) {
let bucket = buckets[key]
let bucketStart = parseInt(key) * bucketSizeMillis + startTime
let sum = 0;
let average = 0;
for(var i = 0; i < bucket.length; i++) {
sum += bucket[i].value
}
average = bucket.length > 0 ? sum / bucket.length : 0
metrics[key] = {
sum: sum,
average: average,
bucketTime: bucketStart
}
}
results.push(metrics)
})
return results
}
async addToJournal(name, child, metricObj) {
const config = await logseq.App.getUserConfigs()
const pageName = getDateForPageWithoutBrackets(new Date(metricObj.date), config.preferredDateFormat)
let page = await logseq.Editor.getPage(pageName)
if(!page) { // See if the page exists
console.log(`Creating page ${pageName}`)
page = await logseq.Editor.createPage(pageName, {}, { createFirstBlock: true, journal: true, redirect: false })
}
let fullName = name
let property = this.clearPropertyName(name)
if(child) {
fullName += " / " + child
// it seems like now @logseq/libs v0.0.14 have a bug: we cannot add property in the form "test/sub"
// so as a temporal solution used "test___sub": it can be renamed manually in Logseq
property += "___" + this.clearPropertyName(child)
}
const text = this.logseq.settings.journal_title.replaceAll("${metric}", fullName)
return await logseq.Editor.appendBlockInPage(
page.uuid,
text,
{
properties: {
[property]: metricObj.value
}
}
)
}
clearName(name) {
// Idea: restric metric names with the same rules as Logseq restricts property names
// Property names restrictions from Logseq itself:
/**
* Property name begins with a non-numeric character and can contain alphanumeric characters
* and . * + ! - _ ? $ % & = < >.
* If -, + or . are the first character, the second character (if any) must be non-numeric.
*/
// But this message is wrong for Logseq v0.8.16:
// - ' is also allowed in property names
// - property name can begins with numeric character
// - property name can continues with numeric character after -, + or .
// - property name can contain non-keyboard characters like ≈, §, ⌘, smiles, etc.
// So the real rule is:
// Property name cannot contain keyboard characters :;,^@#()/\{}[]|"`~ and space
// Here, for metric (non-property) name we can restrict all these characters
// Except of space: because it is very usefull for naming
// But spaces should be cleaned in conversion to property
const restrictedChars = /[:;,^@#~"`/|\(){}[\]]/g
return name.replaceAll(restrictedChars, "")
}
clearPropertyName(name) {
return this.clearName(name).replaceAll(" ", "-").toLowerCase()
}
interpretUserDate(value) {
value = value.trim()
var date = new Date();
if(value.toLowerCase() == "today") {
date = new Date();
}
else if(value.toLowerCase() == "yesterday") {
date.setDate(date.getDate() - 1)
}
else if(value.endsWith("d") && value.startsWith("-")) {
var days = parseInt(value.slice(1, -1));
if(isNaN(days))
days = 0;
let d = new Date();
date.setDate(date.getDate() - days)
}
else {
return value;
}
return date.getFullYear() + "-" + this.pad(date.getMonth()+1) + "-" + this.pad(date.getDate());
}
pad(d) {
return (d < 10) ? '0' + d.toString() : d.toString();
}
}