Output data as JSON
Build and copy to prod / build-and-copy (push) Successful in 37s

This commit is contained in:
Lewis Dale committed 2024-05-12 16:54:00 +01:00
1 parent 4df6d17739
commit 72122553dd
2 files changed
+39 -1

No files matched your search

+28
View File
@@ -20,6 +20,12 @@ type Ping struct {
Status Status
}
type PingResponse struct {
Site sites.Site
Timestamp string
Status string
}
const createQuery = `CREATE TABLE IF NOT EXISTS statuses (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
@@ -73,3 +79,25 @@ func SendPing(db *sql.DB, site sites.Site) {
p.Save(db)
}
func List(db *sql.DB) []PingResponse {
rows, err := db.Query(`SELECT sites.url as url, sites.name, ping.timestamp as timestamp, statuses.name as status FROM ping
JOIN sites ON ping.site = sites.url
JOIN statuses ON ping.status = statuses.id
ORDER BY timestamp DESC`)
if err != nil {
panic(err)
}
defer rows.Close()
pings := make([]PingResponse, 0)
for rows.Next() {
p := PingResponse{}
rows.Scan(&p.Site.Url, &p.Site.Name, &p.Timestamp, &p.Status)
pings = append(pings, p)
}
return pings
}