[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
|
||||
})
|
||||
}
|
||||
+9
-13
@@ -1,8 +1,7 @@
|
||||
import Head from 'next/head'
|
||||
|
||||
import { Inter } from 'next/font/google'
|
||||
import { MonitorState, MonitorTarget } from '@/types/config'
|
||||
import { KVNamespace } from '@cloudflare/workers-types'
|
||||
import { MonitorTarget } from '@/types/config'
|
||||
import { maintenances, pageConfig, workerConfig } from '@/uptime.config'
|
||||
import OverallStatus from '@/components/OverallStatus'
|
||||
import Header from '@/components/Header'
|
||||
@@ -11,25 +10,22 @@ import { Center, Text } from '@mantine/core'
|
||||
import MonitorDetail from '@/components/MonitorDetail'
|
||||
import Footer from '@/components/Footer'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { getFromStore } from '@/worker/src/store'
|
||||
import { CompactedMonitorStateWrapper, getFromStore } from '@/worker/src/store'
|
||||
|
||||
export const runtime = 'experimental-edge'
|
||||
const inter = Inter({ subsets: ['latin'] })
|
||||
|
||||
export default function Home({
|
||||
state: stateStr,
|
||||
compactedStateStr,
|
||||
monitors,
|
||||
}: {
|
||||
state: string
|
||||
compactedStateStr: string
|
||||
monitors: MonitorTarget[]
|
||||
tooltip?: string
|
||||
statusPageLink?: string
|
||||
}) {
|
||||
const { t } = useTranslation('common')
|
||||
let state
|
||||
if (stateStr !== undefined) {
|
||||
state = JSON.parse(stateStr) as MonitorState
|
||||
}
|
||||
let state = new CompactedMonitorStateWrapper(compactedStateStr).uncompact()
|
||||
|
||||
// Specify monitorId in URL hash to view a specific monitor (can be used in iframe)
|
||||
const monitorId = window.location.hash.substring(1)
|
||||
@@ -55,7 +51,7 @@ export default function Home({
|
||||
<main className={inter.className}>
|
||||
<Header />
|
||||
|
||||
{state == undefined ? (
|
||||
{state.lastUpdate === 0 ? (
|
||||
<Center>
|
||||
<Text fw={700}>{t('Monitor State not defined')}</Text>
|
||||
</Center>
|
||||
@@ -73,8 +69,8 @@ export default function Home({
|
||||
}
|
||||
|
||||
export async function getServerSideProps() {
|
||||
// Read state as string from KV, to avoid hitting server-side cpu time limit
|
||||
const state = (await getFromStore(process.env as any, 'state')) as unknown as MonitorState
|
||||
// Read state as string from storage, to avoid hitting server-side cpu time limit
|
||||
const compactedStateStr = await getFromStore(process.env as any, 'state')
|
||||
|
||||
// Only present these values to client
|
||||
const monitors = workerConfig.monitors.map((monitor) => {
|
||||
@@ -90,5 +86,5 @@ export async function getServerSideProps() {
|
||||
}
|
||||
})
|
||||
|
||||
return { props: { state, monitors } }
|
||||
return { props: { compactedStateStr, monitors } }
|
||||
}
|
||||
Reference in new issue
Block a user