generated from yunsii/starter-vike-cloudflare-react-worker
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstore.ts
175 lines (152 loc) · 4.56 KB
/
store.ts
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
import { getHistoryDates } from './datetime'
import type { Monitor } from '#src/types'
import { config } from '#src/config'
import { aMiB, memorySizeOf } from '#src/helpers/memory'
import { ensureWorkerEnv } from '#src/worker/_helpers'
export const DATA_KEY = 'data-v1'
export const REMOTE_MONITORS_KEY = 'remote-monitors-v1'
export interface MonitorLastCheck {
/** Timestamp(ms) */
time?: number
operational: boolean
status: number
statusText: string
}
export interface DataV1LastCheck {
/** Timestamp(ms) */
time: number
location: string
/** Requests has been initiated, subrequest including required/check/notify request */
subrequests: {
total: number
/** Monitor notified subrequests has been initiated */
notified: number
/** Monitor required subrequests has been initiated */
required: number
}
/** Monitor check subrequests has been initiated, store with monitor id */
checks: {
ids: string[]
allOperational: boolean
}
}
export interface MonitorDailyChecksItem {
date: string
fails: number
/**
* Stats for operational monitor
*
* Key: location, No value if config.settings.collectResponseTimes=false
*/
stats: Record<string, {
count: number
totalMs: number
// totalMs / count
// avgMs: number
}>
}
export interface MonitorAllData {
checks: MonitorDailyChecksItem[]
firstCheck: string
lastCheck: MonitorLastCheck
}
export interface DataV1 {
/** Key: monitor id */
monitorHistoryData?: Record<string, MonitorAllData>
lastUpdate?: DataV1LastCheck
}
export async function upsertRemoteMonitors(env: Env, value: Monitor[] | null) {
ensureWorkerEnv()
if (value === null) {
await env.KV_STORE.delete(REMOTE_MONITORS_KEY)
return
}
await env.KV_STORE.put(REMOTE_MONITORS_KEY, JSON.stringify(value))
}
export async function upsertData(env: Env, value: DataV1 | null, allMonitors: Monitor[]) {
ensureWorkerEnv()
if (value === null) {
await env.KV_STORE.delete(DATA_KEY)
return
}
const cleanedValue = await cleanDataV1(value, allMonitors)
await env.KV_STORE.put(DATA_KEY, JSON.stringify(cleanedValue))
}
export async function cleanDataV1(value: DataV1, allMonitors: Monitor[]) {
const { bytes } = memorySizeOf(JSON.stringify(value))
// https://developers.cloudflare.com/kv/platform/limits/
// Value max size 25 MiB, in case of exceptions, we clean data when bytes bigger than 24 MiB.
if (bytes < 24 * aMiB) {
return value
}
const { monitorHistoryData, ...rest } = value
if (!monitorHistoryData) {
return value
}
const historyDates = getHistoryDates()
return {
...rest,
...(Object.keys(monitorHistoryData).filter((item) => {
// Remove monitor data from state if missing in monitors config
return allMonitors.some((monitorItem) => monitorItem.id === item)
}).reduce<Record<string, MonitorAllData>>((previous, current) => {
const { checks, ...restHistoryData } = monitorHistoryData[current]
return {
...previous,
[current]: {
...restHistoryData,
// Remove dates older than config.settings.displayDays
checks: checks.filter((item) => {
return historyDates.includes(item.date)
}),
},
}
}, {})),
}
}
async function getStore(env: Env) {
ensureWorkerEnv()
// https://developers.cloudflare.com/kv/api/read-key-value-pairs/
let kvData = await env.KV_STORE.get<DataV1>(DATA_KEY, 'json')
if (!kvData) {
kvData = {}
}
return { kvData }
}
export async function getAllMonitors(env: Env, useRemoteMonitors = true) {
if (useRemoteMonitors) {
const remoteMonitors = await env.KV_STORE.get<Monitor[]>(REMOTE_MONITORS_KEY, 'json')
return [...config.monitors, ...(remoteMonitors || [])]
}
return config.monitors
}
export async function getCoreData(env: Env, useRemoteMonitors?: boolean) {
const [allMonitors, { kvData }] = await Promise.all([getAllMonitors(env, useRemoteMonitors), getStore(env)])
return {
allMonitors,
kvData,
}
}
export async function prepareData(env: Env) {
const { allMonitors, kvData } = await getCoreData(env)
const lastCheckedMonitorIds = kvData.lastUpdate?.checks.ids || []
const uncheckMonitors = lastCheckedMonitorIds.length === 0
? allMonitors
: allMonitors.filter((item) => {
return !lastCheckedMonitorIds.includes(item.id)
})
if (uncheckMonitors.length === 0) {
return {
kvData,
allMonitors,
uncheckMonitors: allMonitors,
lastCheckedMonitorIds: [],
}
}
return {
kvData,
allMonitors,
uncheckMonitors,
lastCheckedMonitorIds,
}
}