Added HTTP 418 support

This commit is contained in:
v 2021-01-28 19:38:53 +00:00
parent 532302ae8e
commit d91c3f68b5
2 changed files with 14 additions and 7 deletions

View File

@ -37,8 +37,9 @@ type
ResponseType = enum ResponseType = enum
rtOk = 0 rtOk = 0
rtHttpError = 1 rtHttpError = 1
rtTimeout = 2 rtHttpTeapot = 2
rtFetchError = 3 rtTimeout = 3
rtFetchError = 4
Response = object Response = object
rtype: ResponseType rtype: ResponseType
latency: int64 # microseconds latency: int64 # microseconds
@ -50,7 +51,7 @@ type
uptimePercent: float uptimePercent: float
proc uptimeSince(sid: int, time: Time): float = proc uptimeSince(sid: int, time: Time): float =
let okPings = fromDbValue(get getDB().value("SELECT COUNT(*) FROM reqs WHERE site = ? AND status = 0", sid), int) let okPings = fromDbValue(get getDB().value("SELECT COUNT(*) FROM reqs WHERE site = ? AND (status = 0 OR status = 2)", sid), int)
let totalPings = fromDbValue(get getDB().value("SELECT COUNT(*) FROM reqs WHERE site = ?", sid), int) let totalPings = fromDbValue(get getDB().value("SELECT COUNT(*) FROM reqs WHERE site = ?", sid), int)
okPings / totalPings okPings / totalPings
@ -64,7 +65,7 @@ proc fetchLatest(row: ResultRow): Option[SiteStatus] =
proc mainPage(): string = proc mainPage(): string =
let sites = getDB().all("SELECT * FROM sites ORDER BY sid").map(fetchLatest).filter(x => isSome x).map(x => get x) let sites = getDB().all("SELECT * FROM sites ORDER BY sid").map(fetchLatest).filter(x => isSome x).map(x => get x)
let up = sites.filter(x => x.lastResponse == rtOk).len() let up = sites.filter(x => (x.lastResponse == rtOk) or (x.lastResponse == rtHttpTeapot)).len()
let vnode = buildHtml(html()): let vnode = buildHtml(html()):
head: head:
meta(charset="utf8") meta(charset="utf8")
@ -81,12 +82,14 @@ proc mainPage(): string =
of rtHttpError: text "" of rtHttpError: text ""
of rtTimeout: text "" of rtTimeout: text ""
of rtFetchError: text "" of rtFetchError: text ""
of rtHttpTeapot: text "🫖 "
text site.url text site.url
tdiv: text("Last pinged " & format(site.lastPing, "HH:mm:ss dd-MM-yyyy")) tdiv: text("Last pinged " & format(site.lastPing, "HH:mm:ss dd-MM-yyyy"))
tdiv: tdiv:
case site.lastResponse case site.lastResponse
of rtOk: text &"Latency {site.lastLatency}ms" of rtOk: text &"Latency {site.lastLatency}ms"
of rtHttpError: text "HTTP error" of rtHttpError: text "HTTP error"
of rtHttpTeapot: text &"Teapot, latency {site.lastLatency}ms"
of rtTimeout: text "Timed out" of rtTimeout: text "Timed out"
of rtFetchError: text "Fetch failed" of rtFetchError: text "Fetch failed"
tdiv: text &"{site.uptimePercent * 100}% up in last week" tdiv: text &"{site.uptimePercent * 100}% up in last week"
@ -107,7 +110,8 @@ proc pollTarget(s: string): Future[Response] {.async.} =
let ts = now().utc let ts = now().utc
let res = await client.get(s) let res = await client.get(s)
let latency = (now().utc - ts).inMicroseconds let latency = (now().utc - ts).inMicroseconds
if res.code.is4xx or res.code.is5xx: x = Response(rtype: rtHttpError, latency: latency) if res.code.int == 418: x = Response(rtype: rtHttpTeapot, latency: latency)
elif res.code.is4xx or res.code.is5xx: x = Response(rtype: rtHttpError, latency: latency)
else: x = Response(rtype: rtOk, latency: latency) else: x = Response(rtype: rtOk, latency: latency)
try: try:
discard await withTimeout(doFetch(), 10000) discard await withTimeout(doFetch(), 10000)

View File

@ -26,6 +26,9 @@ h1 {
.card.rtHttpError h2 { .card.rtHttpError h2 {
color: orange; color: orange;
} }
.card.rtHttpTeapot h2 {
color: blue;
}
.card.rtFetchError h2 { .card.rtFetchError h2 {
color: red; color: red;
} }