oopsie/ping/ping.go

61 lines
1.2 KiB
Go
Raw Normal View History

2024-05-09 06:12:24 +00:00
package ping
import (
"database/sql"
"lewisdale.dev/oopsie/sites"
)
type Status int16
const (
Success Status = iota
Failure
)
type Ping struct {
Site sites.Site
Timestamp string
Status Status
}
const createQuery = `CREATE TABLE IF NOT EXISTS statuses (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS ping (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
site TEXT NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
status INTEGER NOT NULL,
FOREIGN KEY (site) REFERENCES sites(url),
FOREIGN KEY (status) REFERENCES statuses(id)
);`
const seedStatusQuery = `INSERT INTO statuses (id, name) VALUES (?, ?)
2024-05-09 07:04:25 +00:00
ON CONFLICT (id) DO NOTHING;`
2024-05-09 06:12:24 +00:00
func CreateTable(db *sql.DB) {
if _, err := db.Exec(createQuery); err != nil {
panic(err)
}
seedStatuses(db)
}
func seedStatuses(db *sql.DB) {
if _, err := db.Exec(seedStatusQuery, Success, "Success"); err != nil {
panic(err)
}
if _, err := db.Exec(seedStatusQuery, Failure, "Failure"); err != nil {
panic(err)
}
}
2024-05-09 07:04:25 +00:00
const saveQuery = `INSERT INTO ping (site, status) VALUES (?, ?);`
2024-05-09 06:12:24 +00:00
2024-05-09 07:04:25 +00:00
func (p *Ping) Save(db *sql.DB) {
db.Exec(saveQuery, p.Site.Url, p.Status)
2024-05-09 06:12:24 +00:00
}