[WIP] feat: local deployment, still need more testing, data migration & CI todo
This commit is contained in:
1 parent
e050065a3c
commit
0258ae1ddc
14 files changed
+792
-686
No files matched your search
Generated
+577
-603
File diff suppressed because it is too large.
Load diff
+2
-2
@@ -4,12 +4,12 @@
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"deploy": "wrangler deploy",
|
||||
"dev": "wrangler dev --config wrangler-dev.toml --test-scheduled --persist-to ../.wrangler/state",
|
||||
"dev": "wrangler dev --test-scheduled --persist-to ../.wrangler/state",
|
||||
"start": "wrangler dev"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20250410.0",
|
||||
"typescript": "^5.0.4",
|
||||
"wrangler": "^4.10.0"
|
||||
"wrangler": "^4.54.0"
|
||||
}
|
||||
}
|
||||
+71
-1
@@ -1,5 +1,5 @@
|
||||
import { Env } from '.'
|
||||
import { IncidentRecord, LatencyRecord, MonitorStateCompacted } from '../../types/config'
|
||||
import { IncidentRecord, LatencyRecord, MonitorState, MonitorStateCompacted } from '../../types/config'
|
||||
|
||||
export async function getFromStore(env: Env, key: string): Promise<string | null> {
|
||||
const stmt = env.UPTIMEFLARE_D1.prepare('SELECT value FROM uptimeflare WHERE key = ?')
|
||||
@@ -36,6 +36,64 @@ export class CompactedMonitorStateWrapper {
|
||||
return JSON.stringify(this.data)
|
||||
}
|
||||
|
||||
// Don't use this method at server-side
|
||||
uncompact(): MonitorState {
|
||||
let state: MonitorState = {
|
||||
lastUpdate: this.data.lastUpdate,
|
||||
overallUp: this.data.overallUp,
|
||||
overallDown: this.data.overallDown,
|
||||
incident: {},
|
||||
latency: {},
|
||||
}
|
||||
|
||||
Object.keys(this.data.incident).forEach((monitorId) => {
|
||||
state.incident[monitorId] = []
|
||||
const incidents = this.data.incident[monitorId]
|
||||
|
||||
if (incidents.start.length !== incidents.end.length || incidents.start.length !== incidents.error.length) {
|
||||
throw new Error('Inconsistent incident data lengths, please report an issue at https://github.com/lyc8503/UptimeFlare')
|
||||
}
|
||||
|
||||
for (let i = 0; i < incidents.start.length; i++) {
|
||||
state.incident[monitorId].push({
|
||||
start: incidents.start[i],
|
||||
end: incidents.end[i],
|
||||
error: incidents.error[i],
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
Object.keys(this.data.latency).forEach((monitorId) => {
|
||||
state.latency[monitorId] = []
|
||||
const latencies = this.data.latency[monitorId]
|
||||
const locUncompacted: string[] = []
|
||||
latencies.loc.c.forEach((count, index) => {
|
||||
for (let i = 0; i < count; i++) {
|
||||
locUncompacted.push(latencies.loc.v[index])
|
||||
}
|
||||
})
|
||||
|
||||
// @ts-expect-error
|
||||
const timeArr = new Uint32Array(Uint8Array.fromHex(latencies.time).buffer)
|
||||
// @ts-expect-error
|
||||
const pingArr = new Uint16Array(Uint8Array.fromHex(latencies.ping).buffer)
|
||||
|
||||
if (timeArr.length !== pingArr.length || timeArr.length !== locUncompacted.length) {
|
||||
throw new Error('Inconsistent latency data lengths, please report an issue at https://github.com/lyc8503/UptimeFlare.')
|
||||
}
|
||||
|
||||
for (let i = 0; i < timeArr.length; i++) {
|
||||
state.latency[monitorId].push({
|
||||
time: timeArr[i],
|
||||
ping: pingArr[i],
|
||||
loc: locUncompacted[i],
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return state
|
||||
}
|
||||
|
||||
incidentLen(monitorId: string): number {
|
||||
const incidents = this.data.incident[monitorId]
|
||||
if (!incidents) return 0
|
||||
@@ -140,6 +198,18 @@ export class CompactedMonitorStateWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
getLastLatency(monitorId: string): LatencyRecord {
|
||||
let latencies = this.data.latency[monitorId]
|
||||
|
||||
return {
|
||||
// @ts-expect-error
|
||||
time: new Uint32Array(Uint8Array.fromHex(latencies.time.slice(-8)).buffer)[0],
|
||||
// @ts-expect-error
|
||||
ping: new Uint16Array(Uint8Array.fromHex(latencies.ping.slice(-4)).buffer)[0],
|
||||
loc: latencies.loc.v[latencies.loc.v.length - 1],
|
||||
}
|
||||
}
|
||||
|
||||
unshiftLatency(monitorId: string) {
|
||||
let latencies = this.data.latency[monitorId]
|
||||
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
name = "uptimeflare_worker"
|
||||
main = "src/index.ts"
|
||||
compatibility_date = "2025-04-02"
|
||||
compatibility_flags = [ "nodejs_compat" ]
|
||||
|
||||
[[d1_databases]]
|
||||
binding = "UPTIMEFLARE_D1"
|
||||
database_name = "uptimeflare_d1"
|
||||
database_id = "00000000-0000-0000-0000-000000000000"
|
||||
@@ -3,3 +3,7 @@ main = "src/index.ts"
|
||||
compatibility_date = "2025-04-02"
|
||||
compatibility_flags = [ "nodejs_compat" ]
|
||||
|
||||
[[d1_databases]]
|
||||
binding = "UPTIMEFLARE_D1"
|
||||
database_name = "uptimeflare_d1"
|
||||
database_id = "00000000-0000-0000-0000-000000000000"
|
||||
Reference in new issue
Block a user