|
5 | 5 | * https://opensource.org/licenses/MIT |
6 | 6 | */ |
7 | 7 |
|
8 | | -import { log } from "../common/logger" |
| 8 | +import { log } from "../../common/logger" |
9 | 9 | import { formatTime } from "@util/time" |
10 | | -import BaseDatabase from "./common/base-database" |
11 | | -import { DATE_FORMAT, REMAIN_WORD_PREFIX } from "./common/constant" |
| 10 | +import BaseDatabase from "../common/base-database" |
| 11 | +import { DATE_FORMAT, REMAIN_WORD_PREFIX } from "../common/constant" |
12 | 12 | import { createZeroResult, mergeResult, isNotZeroResult } from "@util/stat" |
13 | 13 | import { judgeVirtualFast } from "@util/pattern" |
| 14 | +import { filter } from "./filter" |
14 | 15 |
|
15 | 16 | export type StatCondition = { |
16 | 17 | /** |
@@ -48,58 +49,6 @@ export type StatCondition = { |
48 | 49 | exlcusiveVirtual?: boolean |
49 | 50 | } |
50 | 51 |
|
51 | | -type _StatCondition = StatCondition & { |
52 | | - // Use exact date condition |
53 | | - useExactDate?: boolean |
54 | | - // date str |
55 | | - exactDateStr?: string |
56 | | - startDateStr?: string |
57 | | - endDateStr?: string |
58 | | - // time range |
59 | | - timeStart?: number |
60 | | - timeEnd?: number |
61 | | - focusStart?: number |
62 | | - focusEnd?: number |
63 | | -} |
64 | | - |
65 | | -function processDateCondition(cond: _StatCondition, paramDate: Date | Date[]) { |
66 | | - if (!paramDate) return |
67 | | - |
68 | | - if (paramDate instanceof Date) { |
69 | | - cond.useExactDate = true |
70 | | - cond.exactDateStr = formatTime(paramDate as Date, DATE_FORMAT) |
71 | | - } else { |
72 | | - let startDate: Date = undefined |
73 | | - let endDate: Date = undefined |
74 | | - const dateArr = paramDate as Date[] |
75 | | - dateArr && dateArr.length >= 2 && (endDate = dateArr[1]) |
76 | | - dateArr && dateArr.length >= 1 && (startDate = dateArr[0]) |
77 | | - cond.useExactDate = false |
78 | | - startDate && (cond.startDateStr = formatTime(startDate, DATE_FORMAT)) |
79 | | - endDate && (cond.endDateStr = formatTime(endDate, DATE_FORMAT)) |
80 | | - } |
81 | | -} |
82 | | - |
83 | | -function processParamTimeCondition(cond: _StatCondition, paramTime: number[]) { |
84 | | - if (!paramTime) return |
85 | | - paramTime.length >= 2 && (cond.timeEnd = paramTime[1]) |
86 | | - paramTime.length >= 1 && (cond.timeStart = paramTime[0]) |
87 | | -} |
88 | | - |
89 | | -function processParamFocusCondition(cond: _StatCondition, paramFocus: number[]) { |
90 | | - if (!paramFocus) return |
91 | | - paramFocus.length >= 2 && (cond.focusEnd = paramFocus[1]) |
92 | | - paramFocus.length >= 1 && (cond.focusStart = paramFocus[0]) |
93 | | -} |
94 | | - |
95 | | -function processCondition(condition: StatCondition): _StatCondition { |
96 | | - const result: _StatCondition = { ...condition } |
97 | | - processDateCondition(result, condition.date) |
98 | | - processParamTimeCondition(result, condition.timeRange) |
99 | | - processParamFocusCondition(result, condition.focusRange) |
100 | | - return result |
101 | | -} |
102 | | - |
103 | 52 | function mergeMigration(exist: timer.stat.Result | undefined, another: any) { |
104 | 53 | exist = exist || createZeroResult() |
105 | 54 | return mergeResult(exist, { focus: another.focus || 0, time: another.time || 0 }) |
@@ -181,78 +130,33 @@ class StatDatabase extends BaseDatabase { |
181 | 130 | return afterUpdated |
182 | 131 | } |
183 | 132 |
|
| 133 | + filter = filter |
| 134 | + |
184 | 135 | /** |
185 | 136 | * Select |
186 | 137 | * |
187 | 138 | * @param condition condition |
188 | 139 | */ |
189 | 140 | async select(condition?: StatCondition): Promise<timer.stat.Row[]> { |
190 | 141 | log("select:{condition}", condition) |
191 | | - condition = condition || {} |
192 | | - const _cond: _StatCondition = processCondition(condition) |
193 | | - const items = await this.refresh() |
194 | | - let result: timer.stat.Row[] = [] |
195 | | - |
196 | | - for (let key in items) { |
197 | | - const date = key.substring(0, 8) |
198 | | - const host = key.substring(8) |
199 | | - const val: timer.stat.Result = items[key] |
200 | | - if (this.filterBefore(date, host, val, _cond)) { |
201 | | - const { focus, time } = val |
202 | | - result.push({ date, host, focus, time, mergedHosts: [], virtual: judgeVirtualFast(host) }) |
203 | | - } |
204 | | - } |
205 | | - |
206 | | - log('Result of select: ', result) |
207 | | - return result |
208 | | - } |
209 | | - |
210 | | - private filterHost(host: string, condition: _StatCondition): boolean { |
211 | | - const paramHost = (condition.host || '').trim() |
212 | | - const exlcusiveVirtual = condition.exlcusiveVirtual |
213 | | - if (!paramHost) return true |
214 | | - if (!!condition.fullHost && host !== paramHost) return false |
215 | | - if (!condition.fullHost && !host.includes(paramHost)) return false |
216 | | - if (exlcusiveVirtual && judgeVirtualFast(host)) return false |
217 | | - return true |
218 | | - } |
219 | | - |
220 | | - private filterDate(date: string, condition: _StatCondition): boolean { |
221 | | - if (condition.useExactDate) { |
222 | | - if (condition.exactDateStr !== date) return false |
223 | | - } else { |
224 | | - const { startDateStr, endDateStr } = condition |
225 | | - if (startDateStr && startDateStr > date) return false |
226 | | - if (endDateStr && endDateStr < date) return false |
227 | | - } |
228 | | - return true |
229 | | - } |
230 | | - |
231 | | - private filterNumberRange(val: number, range: number[]): boolean { |
232 | | - const start = range[0] |
233 | | - const end = range[1] |
234 | | - if (start !== null && start !== undefined && start > val) return false |
235 | | - if (end !== null && end !== undefined && end < val) return false |
236 | | - return true |
| 142 | + const filterResults = await this.filter(condition) |
| 143 | + return filterResults.map(({ date, host, value }) => { |
| 144 | + const { focus, time } = value |
| 145 | + return { date, host, focus, time, mergedHosts: [], virtual: judgeVirtualFast(host) } |
| 146 | + }) |
237 | 147 | } |
238 | 148 |
|
239 | 149 | /** |
240 | | - * Filter by query parameters |
| 150 | + * Count by condition |
241 | 151 | * |
242 | | - * @param date date of item |
243 | | - * @param host host of item |
244 | | - * @param val val of item |
245 | | - * @param condition query parameters |
246 | | - * @return true if valid, or false |
| 152 | + * @param condition |
| 153 | + * @returns count |
| 154 | + * @since 1.0.2 |
247 | 155 | */ |
248 | | - private filterBefore(date: string, host: string, val: timer.stat.Result, condition: _StatCondition): boolean { |
249 | | - const { focus, time } = val |
250 | | - const { timeStart, timeEnd, focusStart, focusEnd } = condition |
251 | | - |
252 | | - return this.filterHost(host, condition) |
253 | | - && this.filterDate(date, condition) |
254 | | - && this.filterNumberRange(time, [timeStart, timeEnd]) |
255 | | - && this.filterNumberRange(focus, [focusStart, focusEnd]) |
| 156 | + async count(condition: StatCondition): Promise<number> { |
| 157 | + log("select:{condition}", condition) |
| 158 | + const filterResults = await this.filter(condition) |
| 159 | + return filterResults.length || 0 |
256 | 160 | } |
257 | 161 |
|
258 | 162 | /** |
@@ -331,30 +235,6 @@ class StatDatabase extends BaseDatabase { |
331 | 235 | return this.deleteByUrlBetween(host) |
332 | 236 | } |
333 | 237 |
|
334 | | - /** |
335 | | - * Count by condition |
336 | | - * |
337 | | - * @param condition |
338 | | - * @returns count |
339 | | - * @since 1.0.2 |
340 | | - */ |
341 | | - async count(condition: StatCondition): Promise<number> { |
342 | | - condition = condition || {} |
343 | | - const _cond: _StatCondition = processCondition(condition) |
344 | | - const items = await this.refresh() |
345 | | - let count = 0 |
346 | | - |
347 | | - for (let key in items) { |
348 | | - const date = key.substring(0, 8) |
349 | | - const host = key.substring(8) |
350 | | - const val: timer.stat.Result = items[key] |
351 | | - if (this.filterBefore(date, host, val, _cond)) { |
352 | | - count++ |
353 | | - } |
354 | | - } |
355 | | - return count |
356 | | - } |
357 | | - |
358 | 238 | async importData(data: any): Promise<void> { |
359 | 239 | if (typeof data !== "object") return |
360 | 240 | const items = await this.storage.get() |
|
0 commit comments