Compare commits
8 Commits
ae54bee23b
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 439c481c60 | |||
| 0a58f85ca3 | |||
| 141c0e5100 | |||
| 9f3de791f9 | |||
| 8d3db70e01 | |||
| 831fed82f7 | |||
| 43887bb447 | |||
| 0b9b790986 |
+18
-12
@@ -4,28 +4,34 @@
|
|||||||
<script>
|
<script>
|
||||||
function onload() {
|
function onload() {
|
||||||
const results = [
|
const results = [
|
||||||
{{ range $v := query
|
{{ range $v := query
|
||||||
`
|
`
|
||||||
SELECT
|
SELECT
|
||||||
start AS start
|
start AS start
|
||||||
, CASE
|
, CASE
|
||||||
WHEN ms < 750 OR ok THEN
|
WHEN ms < 50 AND ok THEN
|
||||||
'okay'
|
'okay'
|
||||||
ELSE
|
WHEN ms < 750 AND ok THEN
|
||||||
'errored'
|
'slow'
|
||||||
|
ELSE
|
||||||
|
'bad'
|
||||||
END AS ok
|
END AS ok
|
||||||
|
, ms AS ms
|
||||||
FROM
|
FROM
|
||||||
"pinger_pings"
|
"pinger_pings"
|
||||||
ORDER BY start DESC
|
ORDER BY start DESC
|
||||||
LIMIT 1000
|
LIMIT 1000
|
||||||
`
|
`
|
||||||
-}}
|
-}}
|
||||||
[
|
[
|
||||||
{{.ok}}==="okay"
|
{{.ok}}==="okay"
|
||||||
? "🟢"
|
? "🟢"
|
||||||
: "🔴",
|
: {{.ok}}==="slow"
|
||||||
`${Math.round((new Date() - 1000*{{.start}})/1000.0)}s ago`,
|
? "🟠"
|
||||||
new Date(1000*{{.start}}),
|
: "🔴",
|
||||||
|
`${Math.round((new Date() - 1000*{{.start}})/1000.0)}s ago`,
|
||||||
|
new Date(1000*{{.start}}),
|
||||||
|
"{{.ms}}ms",
|
||||||
],
|
],
|
||||||
{{ end }}
|
{{ end }}
|
||||||
]
|
]
|
||||||
|
|||||||
+32
-3
@@ -42,6 +42,7 @@ func run(ctx context.Context) error {
|
|||||||
defer wg.Wait()
|
defer wg.Wait()
|
||||||
for _, foo := range []func(context.Context) error{
|
for _, foo := range []func(context.Context) error{
|
||||||
s.pingAndRecord,
|
s.pingAndRecord,
|
||||||
|
s.collectGarbage,
|
||||||
s.listen,
|
s.listen,
|
||||||
} {
|
} {
|
||||||
foo := foo
|
foo := foo
|
||||||
@@ -62,10 +63,10 @@ func (s S) init(ctx context.Context) error {
|
|||||||
, addr TEXT NOT NULL UNIQUE
|
, addr TEXT NOT NULL UNIQUE
|
||||||
)`,
|
)`,
|
||||||
`CREATE TABLE IF NOT EXISTS "pinger_pings" (
|
`CREATE TABLE IF NOT EXISTS "pinger_pings" (
|
||||||
start TIMESTAMP NOT NULL
|
start TIMESTAMP NOT NULL
|
||||||
, ms NUMBER NOT NULL
|
, ms NUMBER NOT NULL
|
||||||
, ok BOOLEAN NOT NULL DEFAULT false
|
, ok BOOLEAN NOT NULL DEFAULT false
|
||||||
, to_id NUMBER
|
, to_id NUMBER
|
||||||
, FOREIGN KEY (to_id) REFERENCES "pinger_to"(id)
|
, FOREIGN KEY (to_id) REFERENCES "pinger_to"(id)
|
||||||
)`,
|
)`,
|
||||||
//`ALTER TABLE "pinger_pings" SET UNLOGGED`,
|
//`ALTER TABLE "pinger_pings" SET UNLOGGED`,
|
||||||
@@ -141,6 +142,32 @@ func (s S) listen(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s S) collectGarbage(ctx context.Context) error {
|
||||||
|
c := time.NewTicker(time.Hour)
|
||||||
|
defer c.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-c.C:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if _, err := s.ExecContext(ctx, `
|
||||||
|
DELETE FROM "pinger_pings"
|
||||||
|
WHERE
|
||||||
|
start < $1
|
||||||
|
AND (
|
||||||
|
ms < 100
|
||||||
|
OR ok
|
||||||
|
)
|
||||||
|
`, time.Now().Add(-1*2*24*time.Hour).Unix()); err != nil {
|
||||||
|
log.Println("!", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s S) pingAndRecord(ctx context.Context) error {
|
func (s S) pingAndRecord(ctx context.Context) error {
|
||||||
ctx, can := context.WithCancel(ctx)
|
ctx, can := context.WithCancel(ctx)
|
||||||
defer can()
|
defer can()
|
||||||
@@ -183,7 +210,7 @@ func (s S) pingAndRecord(ctx context.Context) error {
|
|||||||
go func() {
|
go func() {
|
||||||
for record := range records {
|
for record := range records {
|
||||||
if _, err := s.ExecContext(ctx, `
|
if _, err := s.ExecContext(ctx, `
|
||||||
INSERT INTO "pinger_pings"
|
INSERT INTO "pinger_pings"
|
||||||
(start, ms, ok, to_id)
|
(start, ms, ok, to_id)
|
||||||
VALUES
|
VALUES
|
||||||
($1, $2, $3, $4)
|
($1, $2, $3, $4)
|
||||||
@@ -224,6 +251,8 @@ func (s S) pingAndRecord(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
|
} else {
|
||||||
|
log.Println("!", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user