|
|
@ -3,6 +3,7 @@ package server
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"status/db"
|
|
|
|
"status/db"
|
|
|
@ -36,39 +37,46 @@ func dayHistory(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.Error(w, "Only one `service_name` allowed", http.StatusBadRequest)
|
|
|
|
http.Error(w, "Only one `service_name` allowed", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entries := db.GetFromDB(keys[0], time.Now().Add(time.Duration(-24)*time.Hour), time.Now())
|
|
|
|
service_name := keys[0]
|
|
|
|
writeJSON(w, entries)
|
|
|
|
keys, ok = req.URL.Query()["interval"]
|
|
|
|
}
|
|
|
|
var interval = 5
|
|
|
|
|
|
|
|
if len(keys) >= 2 {
|
|
|
|
func weekHistory(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.Error(w, "Only one `interval` allowed", http.StatusBadRequest)
|
|
|
|
keys, ok := req.URL.Query()["service_name"]
|
|
|
|
|
|
|
|
if !ok || len(keys) == 0 || len(keys[0]) < 1 {
|
|
|
|
|
|
|
|
http.Error(w, "`service_name` not provided", http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(keys) >= 2 {
|
|
|
|
if ok && len(keys) == 1 && len(keys[0]) >= 1 {
|
|
|
|
http.Error(w, "Only one `service_name` allowed", http.StatusBadRequest)
|
|
|
|
var err error
|
|
|
|
|
|
|
|
interval, err = strconv.Atoi(keys[0])
|
|
|
|
|
|
|
|
if err != nil || interval < 1 || interval > 1440 {
|
|
|
|
|
|
|
|
http.Error(w, "Invalid `interval` provided. Must be integer in [1, 1440]", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entries := getHistoryDay(keys[0], 7)
|
|
|
|
|
|
|
|
writeJSON(w, entries)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entries := db.GetFromDB(service_name, time.Now().Add(time.Duration(-24)*time.Hour), time.Now())
|
|
|
|
func monthHistory(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if interval == 1 {
|
|
|
|
keys, ok := req.URL.Query()["service_name"]
|
|
|
|
writeJSON(w, entries)
|
|
|
|
if !ok || len(keys) == 0 || len(keys[0]) < 1 {
|
|
|
|
|
|
|
|
http.Error(w, "`service_name` not provided", http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(keys) >= 2 {
|
|
|
|
filteredEntries := make([]db.PingEntry, 0, len(entries)/interval)
|
|
|
|
http.Error(w, "Only one `service_name` allowed", http.StatusBadRequest)
|
|
|
|
for i := 0; i < len(entries); i += interval {
|
|
|
|
return
|
|
|
|
filteredEntries = append(filteredEntries, entries[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entries := getHistoryDay(keys[0], 31)
|
|
|
|
writeJSON(w, filteredEntries)
|
|
|
|
writeJSON(w, entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func weekHistory(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
|
|
|
history(w, req, 7)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func monthHistory(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
|
|
|
history(w, req, 31)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func yearHistory(w http.ResponseWriter, req *http.Request) {
|
|
|
|
func yearHistory(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
|
|
|
history(w, req, 366)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func history(w http.ResponseWriter, req *http.Request, dayCount int) {
|
|
|
|
keys, ok := req.URL.Query()["service_name"]
|
|
|
|
keys, ok := req.URL.Query()["service_name"]
|
|
|
|
if !ok || len(keys) == 0 || len(keys[0]) < 1 {
|
|
|
|
if !ok || len(keys) == 0 || len(keys[0]) < 1 {
|
|
|
|
http.Error(w, "`service_name` not provided", http.StatusBadRequest)
|
|
|
|
http.Error(w, "`service_name` not provided", http.StatusBadRequest)
|
|
|
@ -78,6 +86,6 @@ func yearHistory(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.Error(w, "Only one `service_name` allowed", http.StatusBadRequest)
|
|
|
|
http.Error(w, "Only one `service_name` allowed", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entries := getHistoryDay(keys[0], 366)
|
|
|
|
entries := getHistoryDay(keys[0], dayCount)
|
|
|
|
writeJSON(w, entries)
|
|
|
|
writeJSON(w, entries)
|
|
|
|
}
|
|
|
|
}
|
|
|
|