forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport-processor.ts
More file actions
50 lines (44 loc) · 1.61 KB
/
Copy pathimport-processor.ts
File metadata and controls
50 lines (44 loc) · 1.61 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
/**
* Copyright (c) 2023 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import statDatabase from "@db/stat-database"
import { mergeWith } from '@util/stat'
import backupProcessor from "../backup/processor"
export async function importOther(query: tt4b.imported.ProcessQuery): Promise<void> {
const { data, resolution } = query
if (resolution === 'overwrite') {
return processOverwrite(data)
}
return processAcc(data)
}
async function processOverwrite(data: tt4b.imported.Data): Promise<void> {
const { rows, focus, time } = data
const exist = await statDatabase.batchSelect(rows)
await mergeWith(rows, exist, async (row, exist) => {
focus && exist?.focus && (row.focus = exist.focus)
time && exist?.time && (row.time = exist.time)
await statDatabase.forceUpdate(row)
})
}
async function processAcc(data: tt4b.imported.Data): Promise<void> {
const { rows } = data
await Promise.all(rows.map(async row => {
const { host, date, focus = 0, time = 0 } = row
await statDatabase.accumulate(host, date, { focus, time })
}))
}
export async function previewBackup(param: tt4b.backup.RemoteQuery): Promise<tt4b.imported.Row[]> {
const remoteRows = await backupProcessor.query(param)
const rows: tt4b.imported.Row[] = remoteRows.map(rr => ({
date: rr.date,
host: rr.host,
focus: rr.focus,
time: rr.time,
}))
const exists = await statDatabase.batchSelect(rows)
await mergeWith(rows, exists, (r, exist) => { r.exist = exist })
return rows
}