dsh-plugin-usage 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +101 -9
- package/image-1.png +0 -0
- package/image.png +0 -0
- package/lib/client/index.js +1566 -99
- package/lib/host/fold-cache.js +117 -0
- package/lib/host/fold.js +11 -0
- package/lib/host/holidays.js +104 -0
- package/lib/host/index.js +335 -30
- package/lib/host/peak.js +107 -0
- package/lib/host/prices.js +876 -0
- package/lib/host/pricing.js +349 -0
- package/lib/host/query.js +60 -2
- package/lib/host/rpc.js +69 -36
- package/lib/host/scan.js +231 -12
- package/lib/host/settings.js +119 -76
- package/lib/host/state.js +18 -2
- package/lib/host/typert.js +221 -0
- package/package.json +11 -5
- package/screenshots.json +4 -0
- package/lib/host/client-source.js +0 -34
package/lib/host/peak.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Peak / off-peak classification for DeepSeek's two-tier list price.
|
|
3
|
+
*
|
|
4
|
+
* The rule is DeepSeek's own, restated in the base time zone so the
|
|
5
|
+
* holiday table (which is published per Chinese calendar day) and the
|
|
6
|
+
* weekday test agree on what "a day" means:
|
|
7
|
+
*
|
|
8
|
+
* 北京时间周一至周五(不含中国法定节假日)9:00-12:00、14:00-18:00 为高峰时段;
|
|
9
|
+
* 其余时段,包括周末及中国法定节假日全天均为空闲时段。
|
|
10
|
+
*
|
|
11
|
+
* Equivalences worth knowing before touching this file:
|
|
12
|
+
* - `Asia/Shanghai` has been a fixed UTC+8 offset with no DST since 1991, so
|
|
13
|
+
* `atMs + 8h` read through `getUTC*()` *is* Beijing wall-clock time. No
|
|
14
|
+
* Intl/DateTimeFormat and no time-zone database are involved.
|
|
15
|
+
* - DeepSeek's own wording is "01:00-04:00 and 06:00-10:00 UTC, Monday through
|
|
16
|
+
* Friday": those UTC windows map one-to-one onto 09:00-12:00 and
|
|
17
|
+
* 14:00-18:00 Beijing, and neither window crosses a UTC day boundary, so
|
|
18
|
+
* the weekday test is identical in both frames.
|
|
19
|
+
* - A 调休 workday (a Saturday/Sunday declared a working day) is still a
|
|
20
|
+
* weekend here: the rule above lists weekdays explicitly and carves out
|
|
21
|
+
* only public holidays, so a make-up Saturday stays off-peak.
|
|
22
|
+
*/
|
|
23
|
+
import { PUBLIC_HOLIDAYS } from './holidays.js';
|
|
24
|
+
/** Beijing-time hour windows that bill at the peak rate, `[start, end)`. */
|
|
25
|
+
export const PEAK_WINDOWS = [
|
|
26
|
+
[9, 12],
|
|
27
|
+
[14, 18],
|
|
28
|
+
];
|
|
29
|
+
/**
|
|
30
|
+
* Whether a Beijing date is a Chinese public holiday (all day off-peak).
|
|
31
|
+
*
|
|
32
|
+
* Lives here, next to the rule that needs it, so the peak test and the price
|
|
33
|
+
* display cannot disagree about what a holiday is — they did once: the fold
|
|
34
|
+
* called `isPeak()` without a calendar while `priceRow` used the full one, so a
|
|
35
|
+
* National Day call was billed at the peak rate and shown as off-peak.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} date - `YYYY-MM-DD`, Beijing.
|
|
38
|
+
* @returns {boolean} true when the generated calendar lists that date.
|
|
39
|
+
*/
|
|
40
|
+
export function holidayOf(date) {
|
|
41
|
+
const dates = PUBLIC_HOLIDAYS.dates;
|
|
42
|
+
return typeof date === 'string' && Object.prototype.hasOwnProperty.call(dates, date);
|
|
43
|
+
}
|
|
44
|
+
/** The calendar's coverage metadata (which years are known), for the UI note. */
|
|
45
|
+
export function holidayCoverage() {
|
|
46
|
+
return PUBLIC_HOLIDAYS.years;
|
|
47
|
+
}
|
|
48
|
+
/** Milliseconds in one hour. */
|
|
49
|
+
const HOUR_MS = 3_600_000;
|
|
50
|
+
/** Beijing is UTC+8, always. */
|
|
51
|
+
const BEIJING_OFFSET_MS = 8 * HOUR_MS;
|
|
52
|
+
/**
|
|
53
|
+
* Beijing wall-clock parts of an instant.
|
|
54
|
+
*
|
|
55
|
+
* @param {number} atMs - epoch milliseconds.
|
|
56
|
+
* @returns {{ year: number, month: number, day: number, weekday: number, hour: number, minute: number, date: string }}
|
|
57
|
+
* `weekday` is 0=Sunday..6=Saturday; `month` is 1-based; `date` is `YYYY-MM-DD`.
|
|
58
|
+
*/
|
|
59
|
+
export function beijingParts(atMs) {
|
|
60
|
+
const d = new Date(atMs + BEIJING_OFFSET_MS);
|
|
61
|
+
const year = d.getUTCFullYear();
|
|
62
|
+
const month = d.getUTCMonth() + 1;
|
|
63
|
+
const day = d.getUTCDate();
|
|
64
|
+
const pad = (n) => (n < 10 ? '0' + n : String(n));
|
|
65
|
+
return {
|
|
66
|
+
year,
|
|
67
|
+
month,
|
|
68
|
+
day,
|
|
69
|
+
weekday: d.getUTCDay(),
|
|
70
|
+
hour: d.getUTCHours(),
|
|
71
|
+
minute: d.getUTCMinutes(),
|
|
72
|
+
date: `${year}-${pad(month)}-${pad(day)}`,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Whether an instant falls inside a peak window, ignoring the calendar.
|
|
77
|
+
*
|
|
78
|
+
* Exported separately so a test can compare the window arithmetic against the
|
|
79
|
+
* calendar rules without needing a holiday table.
|
|
80
|
+
*
|
|
81
|
+
* @param {number} atMs - epoch milliseconds.
|
|
82
|
+
* @returns {boolean} true inside `[start, end)` of any peak window.
|
|
83
|
+
*/
|
|
84
|
+
export function inPeakWindow(atMs) {
|
|
85
|
+
const { hour, minute } = beijingParts(atMs);
|
|
86
|
+
const t = hour + minute / 60;
|
|
87
|
+
return PEAK_WINDOWS.some(([start, end]) => t >= start && t < end);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Whether an instant bills at DeepSeek's peak rate.
|
|
91
|
+
*
|
|
92
|
+
* @param {number} atMs - epoch milliseconds of the billed settlement.
|
|
93
|
+
* @param {(date: string) => boolean} [isHoliday] - holiday test; DEFAULTS to the
|
|
94
|
+
* generated calendar. It used to default to "no holidays known", which let a
|
|
95
|
+
* caller bill a public holiday at the peak rate by forgetting the argument.
|
|
96
|
+
* @returns {boolean} true for peak.
|
|
97
|
+
*/
|
|
98
|
+
export function isPeak(atMs, isHoliday = holidayOf) {
|
|
99
|
+
if (typeof atMs !== 'number' || !Number.isFinite(atMs))
|
|
100
|
+
return false;
|
|
101
|
+
const { weekday, date } = beijingParts(atMs);
|
|
102
|
+
if (weekday === 0 || weekday === 6)
|
|
103
|
+
return false;
|
|
104
|
+
if (!inPeakWindow(atMs))
|
|
105
|
+
return false;
|
|
106
|
+
return typeof isHoliday === 'function' ? !isHoliday(date) : true;
|
|
107
|
+
}
|