forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit-database.ts
More file actions
295 lines (274 loc) · 7.45 KB
/
limit-database.ts
File metadata and controls
295 lines (274 loc) · 7.45 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
/**
* Copyright (c) 2021 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { formatTimeYMD, MILL_PER_DAY } from "@util/time"
import BaseDatabase from "./common/base-database"
import { REMAIN_WORD_PREFIX } from "./common/constant"
const KEY = REMAIN_WORD_PREFIX + 'LIMIT'
type DateRecords = {
[date: string]: {
mill: number
delay?: number
}
}
type LimitRecord = timer.limit.Rule & {
records: DateRecords
}
type ItemValue = {
/**
* ID
*/
i: number
/**
* Condition
*/
c: string[]
/**
* Name
*/
n: string
/**
* Limited time, second
*/
t: number
/**
* Limited time weekly, second
*/
wt?: number
/**
* Limited time per visit, second
*/
v?: number
/**
* Forbidden periods
*/
p?: Vector<2>[]
/**
* Enabled flag
*/
e: boolean
/**
* Latest date
*
* @deprecated use @see ItemValue.r
*/
d?: string
/**
* Wasted time, milliseconds
*
* @deprecated use @see ItemValue.r
*/
w?: number
/**
* Allow to delay
*/
ad: boolean
/**
* Effective days
*/
wd?: number[]
/**
* Delay date
*
* @deprecated use @see ItemValue.r
*/
dd?: string
/**
* Delay count
*
* @deprecated use @see ItemValue.r
*/
dc?: number
/**
* Date records
*/
r?: {
[date: string]: {
/**
* Milliseconds
*/
m: number
/**
* Delay count
*/
d?: number
}
}
}
/**
* @deprecated v2.4.1
*/
const migrateOldRecords = (item: ItemValue, records: DateRecords): void => {
const { d, w, dd, dc } = item
if (d) {
const val = records[d] || { mill: 0 }
val.mill = w ?? 0
records[d] = val
}
if (dd) {
const val = records[dd] || { mill: 0 }
val.delay = dc ?? 0
records[dd] = val
}
}
const cvtItem2Rec = (item: ItemValue): LimitRecord => {
const { i, n, c, t, v, p, e, ad, wd, wt, r } = item
const records: DateRecords = {}
if (r) {
Object.entries(r).forEach?.(([date, { m, d }]) => records[date] = { mill: m, delay: d })
} else {
migrateOldRecords(item, records)
}
return {
id: i,
name: n,
cond: c,
time: t,
weekly: wt,
visitTime: v,
periods: p?.map(i => [i?.[0], i?.[1]]),
enabled: e,
allowDelay: !!ad,
weekdays: wd,
records: records,
}
}
type Items = Record<number, ItemValue>
function migrate(exist: Items, toMigrate: any) {
const idBase = Object.keys(exist).map(parseInt).sort().reverse()?.[0] ?? 0 + 1
Object.values(toMigrate).forEach((value, idx) => {
const id = idBase + idx
const itemValue: ItemValue = value as ItemValue
const { c, n, t, e, ad, d, w, v, p } = itemValue
exist[id] = {
i: id, c, n, t, e: !!e, ad: !!ad, v, p,
r: d ? { [d]: { m: w } } : {},
}
})
}
/**
* @deprecated Compatible for old items without ID
*/
const compatibleOldItems = (items: Items): Items => {
const newItems: Items = {}
Object.entries(items).forEach(([c, v], idx) => {
const oldVal = v as Omit<ItemValue, 'i' | 'c' | 'n'>
const id = idx + 1
const newVal = { i: id, c: [c], n: 'Unnamed', ...oldVal } satisfies ItemValue
newItems[id] = newVal
})
return newItems
}
/**
* Time limit
*
* @since 0.2.2
*/
class LimitDatabase extends BaseDatabase {
private async getItems(): Promise<Items> {
let items = await this.storage.getOne<Items>(KEY) || {}
const isNew = Object.values(items).some(iv => !!iv.i)
if (!isNew) {
items = compatibleOldItems(items)
this.storage.put(KEY, items)
}
return items
}
private update(items: Items): Promise<void> {
const days10Ago = new Date(Date.now() - MILL_PER_DAY * 10)
const days10AgoStr = formatTimeYMD(days10Ago)
// Clear early date
Object.values(items).forEach(item => {
delete item.w
delete item.d
delete item.dc
delete item.dd
const records = item.r
if (!records) return
const keys2Del = Object.keys(records).filter(k => k <= days10AgoStr)
keys2Del.forEach(k => delete records[k])
})
return this.setByKey(KEY, items)
}
async all(): Promise<LimitRecord[]> {
const items = await this.getItems()
return Object.values(items).map(cvtItem2Rec)
}
async save(data: timer.limit.Rule, rewrite?: boolean): Promise<number> {
const items = await this.getItems()
let { id, name, cond, time, weekly, enabled, allowDelay, visitTime, periods, weekdays } = data
if (!id) {
const lastId = Object.values(items)
.map(e => e.i)
.filter(i => !!i)
.sort((a, b) => b - a)?.[0] ?? 0
id = lastId + 1
}
const existItem = items[id]
if (existItem && !rewrite) return id
items[id] = {
// Can be overridden by existing
d: '', w: 0,
...(existItem || {}),
i: id, n: name, c: cond, wd: weekdays,
e: enabled, ad: allowDelay,
t: time, wt: weekly, v: visitTime, p: periods,
}
await this.update(items)
return id
}
async remove(id: number): Promise<void> {
const items = await this.getItems()
delete items[id]
await this.update(items)
}
async updateWaste(date: string, toUpdate: { [id: number]: number }): Promise<void> {
const items = await this.getItems()
Object.entries(toUpdate).forEach(([k, waste]) => {
const id = parseInt(k)
const entry = items[id]
if (!entry) return
const records = entry.r = entry.r || {}
const record = records[date] = records[date] || { m: 0 }
record.m = waste
})
await this.update(items)
}
async updateDelayCount(date: string, toUpdate: timer.limit.Item[]): Promise<void> {
const items = await this.getItems()
toUpdate?.forEach(({ id, delayCount }) => {
const entry = items[id]
if (!entry) return
const records = entry.r = entry.r || {}
const record = records[date] = records[date] || { m: 0 }
record.d = delayCount
})
await this.update(items)
}
async updateDelay(id: number, allowDelay: boolean) {
const items = await this.getItems()
if (!items[id]) return
items[id].ad = allowDelay
await this.update(items)
}
async updateEnabled(id: number, enabled: boolean) {
const items = await this.getItems()
if (!items[id]) return
items[id].e = !!enabled
await this.update(items)
}
async importData(data: any): Promise<void> {
let toImport = data[KEY] as Items
// Not import
if (typeof toImport !== 'object') return
const isNew = Object.values(toImport).some(e => !!e.i)
!isNew && (toImport = compatibleOldItems(toImport))
const exists: Items = await this.getItems()
migrate(exists, toImport)
this.setByKey(KEY, exists)
}
}
export default LimitDatabase