More output on the home page
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 32s
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 32s
This commit is contained in:
parent
72122553dd
commit
a7a55b6a90
2
main.go
2
main.go
@ -29,7 +29,7 @@ func main() {
|
|||||||
ping.SendPing(db, failureSite)
|
ping.SendPing(db, failureSite)
|
||||||
|
|
||||||
http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
||||||
pings := ping.List(db)
|
pings := ping.ListGroupedBySite(db)
|
||||||
|
|
||||||
if output, err := json.Marshal(pings); err != nil {
|
if output, err := json.Marshal(pings); err != nil {
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
|
40
ping/ping.go
40
ping/ping.go
@ -101,3 +101,43 @@ func List(db *sql.DB) []PingResponse {
|
|||||||
|
|
||||||
return pings
|
return pings
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SiteResponse struct {
|
||||||
|
created_at string
|
||||||
|
Name string
|
||||||
|
Url string
|
||||||
|
Pings []Ping
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListGroupedBySite(db *sql.DB) []SiteResponse {
|
||||||
|
|
||||||
|
responses := make([]SiteResponse, 0)
|
||||||
|
|
||||||
|
for _, site := range sites.List(db) {
|
||||||
|
s := SiteResponse{Url: site.Url, Name: site.Name, created_at: site.Created_at}
|
||||||
|
s.Pings = listForSite(db, site)
|
||||||
|
responses = append(responses, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return responses
|
||||||
|
}
|
||||||
|
|
||||||
|
func listForSite(db *sql.DB, site sites.Site) []Ping {
|
||||||
|
rows, err := db.Query(`SELECT ping.timestamp, ping.status FROM ping WHERE site = ? ORDER BY timestamp DESC`, site.Url)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
pings := make([]Ping, 0)
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
p := Ping{}
|
||||||
|
rows.Scan(&p.Timestamp, &p.Status)
|
||||||
|
p.Site = site
|
||||||
|
pings = append(pings, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
return pings
|
||||||
|
}
|
||||||
|
@ -18,7 +18,7 @@ func CreateTable(db *sql.DB) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Site struct {
|
type Site struct {
|
||||||
created_at uint64
|
Created_at string
|
||||||
Name string
|
Name string
|
||||||
Url string
|
Url string
|
||||||
}
|
}
|
||||||
@ -32,3 +32,22 @@ SET
|
|||||||
func (s *Site) Save(db *sql.DB) {
|
func (s *Site) Save(db *sql.DB) {
|
||||||
db.Exec(upsertQuery, s.Url, s.Name)
|
db.Exec(upsertQuery, s.Url, s.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func List(db *sql.DB) []Site {
|
||||||
|
rows, err := db.Query(`SELECT url, name, created_at FROM sites ORDER BY created_at DESC`)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
sites := make([]Site, 0)
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
s := Site{}
|
||||||
|
rows.Scan(&s.Url, &s.Name, &s.Created_at)
|
||||||
|
sites = append(sites, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sites
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user