diff --git a/server/api.go b/server/api.go index 04c794f..40742c3 100644 --- a/server/api.go +++ b/server/api.go @@ -50,6 +50,34 @@ func weekHistory(w http.ResponseWriter, req *http.Request) { http.Error(w, "Only one `service_name` allowed", http.StatusBadRequest) return } - entries := getWeek(keys[0]) + entries := getHistoryDay(keys[0], 7) + writeJSON(w, entries) +} + +func monthHistory(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 + } + entries := getHistoryDay(keys[0], 31) + writeJSON(w, entries) +} + +func yearHistory(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 + } + entries := getHistoryDay(keys[0], 366) writeJSON(w, entries) } diff --git a/server/data.go b/server/data.go index 9d877e1..c165596 100644 --- a/server/data.go +++ b/server/data.go @@ -18,10 +18,10 @@ func getStatus() map[string]db.PingEntry { return result } -func getWeek(serviceName string) []stat { +func getHistoryDay(serviceName string, dayCount int) []stat { currentDay := today() result := make([]stat, 0) - for i := -6; i <= 0; i++ { + for i := -dayCount + 1; i <= 0; i++ { dayData := getDay(serviceName, currentDay.AddDate(0, 0, i)) if dayData.SampleCount > 0 { result = append(result, dayData) diff --git a/server/server.go b/server/server.go index 8da3b9b..e48984a 100644 --- a/server/server.go +++ b/server/server.go @@ -10,6 +10,8 @@ func apiEndpoints() { http.HandleFunc("/api/latest", allStatus) http.HandleFunc("/api/history/day", dayHistory) http.HandleFunc("/api/history/week", weekHistory) + http.HandleFunc("/api/history/month", monthHistory) + http.HandleFunc("/api/history/year", yearHistory) } func StartServer(port int) {