-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathindex.ts
More file actions
266 lines (238 loc) · 7.81 KB
/
index.ts
File metadata and controls
266 lines (238 loc) · 7.81 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
/**
* Copyright (c) 2021 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { escapeRegExp } from "@util/pattern"
import { isNotZeroResult } from "@util/stat"
import { formatTimeYMD } from "@util/time"
import { log } from "../../common/logger"
import BaseDatabase from "../common/base-database"
import { REMAIN_WORD_PREFIX } from "../common/constant"
import { filter } from "./filter"
export type StatCondition = {
/**
* Date
* {y}{m}{d}
*/
date?: Date | [Date, Date?]
/**
* Host name for query
*/
host?: string
/**
* Focus range, milliseconds
*
* @since 0.0.9
*/
focusRange?: Vector<2>
/**
* Time range
*
* @since 0.0.9
*/
timeRange?: [number, number?]
/**
* Whether to enable full host search, default is false
*
* @since 0.0.8
*/
fullHost?: boolean
/**
* Whether to exclusive virtual sites
*
* @since 1.6.1
*/
exclusiveVirtual?: boolean
}
function increase(a: timer.core.Result, b: timer.core.Result) {
const res: timer.core.Result = {
focus: (a?.focus ?? 0) + (b?.focus ?? 0),
time: (a?.time ?? 0) + (b?.time ?? 0),
}
const run = (a?.run ?? 0) + (b?.run ?? 0)
run && (res.run = run)
return res
}
function createZeroResult(): timer.core.Result {
return { focus: 0, time: 0 }
}
function mergeMigration(exist: timer.core.Result | undefined, another: any) {
exist = exist || createZeroResult()
return increase(exist, { focus: another.focus ?? 0, time: another.time ?? 0, run: another.run ?? 0 })
}
/**
* Generate the key in local storage by host and date
*
* @param host host
* @param date date
*/
function generateKey(host: string, date: Date | string) {
const str = typeof date === 'object' ? formatTimeYMD(date as Date) : date
return str + host
}
function migrate(exists: { [key: string]: timer.core.Result }, data: any): timer.stat.ResultSet {
const result: timer.stat.ResultSet = {}
Object.entries(data)
.filter(([key]) => /^20\d{2}[01]\d[0-3]\d.*/.test(key))
.forEach(([key, value]) => {
if (typeof value !== "object") return
const exist = exists[key]
const merged = mergeMigration(exist, value)
merged && isNotZeroResult(merged) && (result[key] = mergeMigration(exist, value))
})
return result
}
class StatDatabase extends BaseDatabase {
async refresh(): Promise<{ [key: string]: unknown }> {
const result = await this.storage.get()
const items: timer.stat.ResultSet = {}
Object.entries(result)
.filter(([key]) => !key.startsWith(REMAIN_WORD_PREFIX))
.forEach(([key, value]) => items[key] = value)
return items
}
/**
* @param host host
* @since 0.1.3
*/
async accumulate(host: string, date: Date | string, item: timer.core.Result): Promise<timer.core.Result> {
const key = generateKey(host, date)
let exist = await this.storage.getOne<timer.core.Result>(key)
exist = increase(exist || createZeroResult(), item)
await this.setByKey(key, exist)
return exist
}
/**
* Batch accumulate
*
* @param data data: {host=>waste_per_day}
* @param date date
* @since 0.1.8
*/
async accumulateBatch(data: timer.stat.ResultSet, date: Date | string): Promise<timer.stat.ResultSet> {
const hosts = Object.keys(data)
if (!hosts.length) return {}
const dateStr = typeof date === 'string' ? date : formatTimeYMD(date)
const keys: { [host: string]: string } = {}
hosts.forEach(host => keys[host] = generateKey(host, dateStr))
const items = await this.storage.get(Object.values(keys))
const toUpdate: timer.stat.ResultSet = {}
const afterUpdated: timer.stat.ResultSet = {}
Object.entries(keys).forEach(([host, key]) => {
const item = data[host]
const exist: timer.core.Result = increase(items[key] as timer.core.Result || createZeroResult(), item)
toUpdate[key] = afterUpdated[host] = exist
})
await this.storage.set(toUpdate)
return afterUpdated
}
filter = filter
/**
* Select
*
* @param condition condition
*/
async select(condition?: StatCondition): Promise<timer.core.Row[]> {
log("select:{condition}", condition)
const filterResults = await this.filter(condition)
return filterResults.map(({ date, host, value }) => {
const { focus, time, run } = value
return { date, host, focus, time, run }
})
}
/**
* Count by condition
*
* @param condition
* @returns count
* @since 1.0.2
*/
async count(condition: StatCondition): Promise<number> {
log("select:{condition}", condition)
const filterResults = await this.filter(condition)
return filterResults.length || 0
}
/**
* Get by host and date
*
* @since 0.0.5
*/
async get(host: string, date: Date | string): Promise<timer.core.Result> {
const key = generateKey(host, date)
const exist = await this.storage.getOne<timer.core.Result>(key)
return exist || createZeroResult()
}
/**
* Delete the record
*
* @param host host
* @param date date
* @since 0.0.5
*/
async deleteByUrlAndDate(host: string, date: Date | string): Promise<void> {
const key = generateKey(host, date)
return this.storage.remove(key)
}
/**
* Delete by key
*
* @param rows site rows, the host and date mustn't be null
* @since 0.0.9
*/
async delete(rows: timer.core.RowKey[]): Promise<void> {
const keys: string[] = rows.filter(({ date, host }) => !!host && !!date).map(({ host, date }) => generateKey(host, date))
return this.storage.remove(keys)
}
/**
* Force update data
*
* @since 1.4.3
*/
forceUpdate({ host, date, time, focus, run }: timer.core.Row): Promise<void> {
const key = generateKey(host, date)
const result: timer.core.Result = { time, focus }
run && (result.run = run)
return this.storage.put(key, result)
}
/**
* @param host host
* @param start start date, inclusive
* @param end end date, inclusive
* @since 0.0.7
*/
async deleteByUrlBetween(host: string, start?: Date, end?: Date): Promise<string[]> {
const startStr = start ? formatTimeYMD(start) : undefined
const endStr = end ? formatTimeYMD(end) : undefined
const dateFilter = (date: string) => (startStr ? startStr <= date : true) && (endStr ? date <= endStr : true)
const items = await this.refresh()
// Key format: 20201112www.google.com
const keyReg = RegExp('\\d{8}' + escapeRegExp(host))
const keys: string[] = Object.keys(items)
.filter(key => keyReg.test(key) && dateFilter(key.substring(0, 8)))
await this.storage.remove(keys)
return keys.map(k => k.substring(0, 8))
}
/**
* Delete the record
*
* @param host host
* @since 0.0.5
*/
async deleteByUrl(host: string): Promise<string[]> {
const items = await this.refresh()
// Key format: 20201112www.google.com
const keyReg = RegExp('\\d{8}' + escapeRegExp(host))
const keys: string[] = Object.keys(items).filter(key => keyReg.test(key))
await this.storage.remove(keys)
return keys.map(k => k.substring(0, 8))
}
async importData(data: any): Promise<void> {
if (typeof data !== "object") return
const items = await this.storage.get()
const toSave = migrate(items, data)
this.storage.set(toSave)
}
}
export default StatDatabase