[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
+7
-28
@@ -1,6 +1,5 @@
|
||||
import { workerConfig } from '@/uptime.config'
|
||||
import { MonitorState } from '@/types/config'
|
||||
import { NextRequest } from 'next/server'
|
||||
import { CompactedMonitorStateWrapper, getFromStore } from '@/worker/src/store'
|
||||
|
||||
export const runtime = 'edge'
|
||||
|
||||
@@ -31,8 +30,7 @@ export default async function handler(req: NextRequest): Promise<Response> {
|
||||
try {
|
||||
const url = new URL(req.url)
|
||||
|
||||
const defaultMonitorId = workerConfig.monitors[0]?.id
|
||||
const monitorId = url.searchParams.get('id') ?? defaultMonitorId
|
||||
const monitorId = url.searchParams.get('id')
|
||||
const label = url.searchParams.get('label') ?? monitorId ?? 'UptimeFlare'
|
||||
|
||||
const upMsg = url.searchParams.get('up') ?? 'UP'
|
||||
@@ -47,31 +45,12 @@ export default async function handler(req: NextRequest): Promise<Response> {
|
||||
})
|
||||
}
|
||||
|
||||
const { UPTIMEFLARE_STATE } = process.env as unknown as {
|
||||
UPTIMEFLARE_STATE: KVNamespace
|
||||
}
|
||||
const compactedState = new CompactedMonitorStateWrapper(
|
||||
await getFromStore(process.env as any, 'state')
|
||||
)
|
||||
|
||||
const stateStr = await UPTIMEFLARE_STATE?.get('state')
|
||||
if (!stateStr) {
|
||||
return new Response(JSON.stringify(errorBadge(label, 'unavailable')), {
|
||||
headers: jsonHeaders,
|
||||
status: 503,
|
||||
})
|
||||
}
|
||||
|
||||
const state = JSON.parse(stateStr) as MonitorState
|
||||
const monitorIncidentHistory = state.incident?.[monitorId]
|
||||
const hasLatencyData = Boolean(state.latency?.[monitorId]?.length)
|
||||
|
||||
if (!monitorIncidentHistory || monitorIncidentHistory.length === 0 || !hasLatencyData) {
|
||||
return new Response(JSON.stringify(errorBadge(label, 'unknown')), {
|
||||
headers: jsonHeaders,
|
||||
status: 404,
|
||||
})
|
||||
}
|
||||
|
||||
const latestIncident = monitorIncidentHistory.slice(-1)[0]
|
||||
const isUp = latestIncident.end !== undefined
|
||||
const lastIncident = compactedState.getIncident(monitorId, compactedState.incidentLen(monitorId) - 1)
|
||||
const isUp = lastIncident?.end !== null
|
||||
|
||||
const badge: BadgePayload = {
|
||||
schemaVersion: 1,
|
||||
|
||||
+23
-27
@@ -1,53 +1,49 @@
|
||||
import { workerConfig } from '@/uptime.config'
|
||||
import { MonitorState } from '@/types/config'
|
||||
import { NextRequest } from 'next/server'
|
||||
import { CompactedMonitorStateWrapper, getFromStore } from '@/worker/src/store'
|
||||
|
||||
export const runtime = 'edge'
|
||||
|
||||
export default async function handler(req: NextRequest): Promise<Response> {
|
||||
const { UPTIMEFLARE_STATE } = process.env as unknown as {
|
||||
UPTIMEFLARE_STATE: KVNamespace
|
||||
}
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
}
|
||||
|
||||
const stateStr = await UPTIMEFLARE_STATE?.get('state')
|
||||
if (!stateStr) {
|
||||
export default async function handler(req: NextRequest): Promise<Response> {
|
||||
const compactedState = new CompactedMonitorStateWrapper(await getFromStore(process.env as any, 'state'))
|
||||
|
||||
if (compactedState.data.lastUpdate === 0) {
|
||||
return new Response(JSON.stringify({ error: 'No data available' }), {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
},
|
||||
headers
|
||||
})
|
||||
}
|
||||
const state = JSON.parse(stateStr) as unknown as MonitorState
|
||||
|
||||
let monitors: any = {}
|
||||
|
||||
for (let monitor of workerConfig.monitors) {
|
||||
const isUp = state.incident[monitor.id].slice(-1)[0].end !== undefined
|
||||
const lastIncident = compactedState.getIncident(monitor.id, compactedState.incidentLen(monitor.id) - 1)
|
||||
|
||||
const isUp = lastIncident?.end !== null
|
||||
const latency = compactedState.getLastLatency(monitor.id)
|
||||
monitors[monitor.id] = {
|
||||
up: isUp,
|
||||
latency: state.latency[monitor.id].slice(-1)[0].ping,
|
||||
location: state.latency[monitor.id].slice(-1)[0].loc,
|
||||
message: isUp ? 'OK' : state.incident[monitor.id].slice(-1)[0].error.slice(-1)[0],
|
||||
latency: latency.ping,
|
||||
location: latency.loc,
|
||||
message: isUp ? 'OK' : lastIncident?.error[lastIncident.error.length - 1],
|
||||
}
|
||||
}
|
||||
|
||||
let ret = {
|
||||
up: state.overallUp,
|
||||
down: state.overallDown,
|
||||
updatedAt: state.lastUpdate,
|
||||
up: compactedState.data.overallUp,
|
||||
down: compactedState.data.overallDown,
|
||||
updatedAt: compactedState.data.lastUpdate,
|
||||
monitors: monitors,
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify(ret), {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
},
|
||||
headers
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user