2020-04-23 07:42:16 +07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2020-04-23 14:32:56 +07:00
|
|
|
"strconv"
|
2020-04-23 12:55:30 +07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"status/db"
|
2020-04-23 07:42:16 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func writeJSON(w http.ResponseWriter, a interface{}) {
|
|
|
|
bytes, err := json.Marshal(a)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "500 Internal Server Error", http.StatusInternalServerError)
|
2020-04-23 12:55:30 +07:00
|
|
|
return
|
2020-04-23 07:42:16 +07:00
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-04-23 08:23:21 +07:00
|
|
|
_, err = w.Write(bytes)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "500 Internal Server Error", http.StatusInternalServerError)
|
|
|
|
}
|
2020-04-23 07:42:16 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func allStatus(w http.ResponseWriter, req *http.Request) {
|
|
|
|
status := getStatus()
|
|
|
|
writeJSON(w, status)
|
|
|
|
}
|
2020-04-23 12:55:30 +07:00
|
|
|
|
|
|
|
func dayHistory(w http.ResponseWriter, req *http.Request) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if len(keys) >= 2 {
|
|
|
|
http.Error(w, "Only one `service_name` allowed", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-04-23 14:32:56 +07:00
|
|
|
service_name := keys[0]
|
|
|
|
keys, ok = req.URL.Query()["interval"]
|
|
|
|
var interval = 5
|
|
|
|
if len(keys) >= 2 {
|
|
|
|
http.Error(w, "Only one `interval` allowed", http.StatusBadRequest)
|
2020-04-23 13:44:23 +07:00
|
|
|
return
|
|
|
|
}
|
2020-04-23 14:32:56 +07:00
|
|
|
if ok && len(keys) == 1 && len(keys[0]) >= 1 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entries := db.GetFromDB(service_name, time.Now().Add(time.Duration(-24)*time.Hour), time.Now())
|
|
|
|
if interval == 1 {
|
|
|
|
writeJSON(w, entries)
|
2020-04-23 13:44:23 +07:00
|
|
|
return
|
|
|
|
}
|
2020-04-23 14:32:56 +07:00
|
|
|
filteredEntries := make([]db.PingEntry, 0, len(entries)/interval)
|
|
|
|
for i := 0; i < len(entries); i += interval {
|
|
|
|
filteredEntries = append(filteredEntries, entries[i])
|
|
|
|
}
|
|
|
|
writeJSON(w, filteredEntries)
|
|
|
|
}
|
|
|
|
|
|
|
|
func weekHistory(w http.ResponseWriter, req *http.Request) {
|
|
|
|
history(w, req, 7)
|
2020-04-23 13:49:23 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func monthHistory(w http.ResponseWriter, req *http.Request) {
|
2020-04-23 14:32:56 +07:00
|
|
|
history(w, req, 31)
|
2020-04-23 13:49:23 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func yearHistory(w http.ResponseWriter, req *http.Request) {
|
2020-04-23 14:32:56 +07:00
|
|
|
history(w, req, 366)
|
|
|
|
}
|
|
|
|
|
|
|
|
func history(w http.ResponseWriter, req *http.Request, dayCount int) {
|
2020-04-23 13:49:23 +07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
if len(keys) >= 2 {
|
|
|
|
http.Error(w, "Only one `service_name` allowed", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-04-23 14:32:56 +07:00
|
|
|
entries := getHistoryDay(keys[0], dayCount)
|
2020-04-23 13:44:23 +07:00
|
|
|
writeJSON(w, entries)
|
|
|
|
}
|