diff --git a/client/client.go b/client/client.go index 5440739..29b1f11 100644 --- a/client/client.go +++ b/client/client.go @@ -60,6 +60,33 @@ func Label(port int, names ...string) ([]window.Schedule, error) { return readSchedules(urls) } +// ActiveHours gets the built-in Active Hours maintenance window. +func ActiveHours(port int) (*window.Window, error) { + if !Test(fmt.Sprintf("%s:%d", urlBase, port)) { + return nil, fmt.Errorf("service not available") + } + url := fmt.Sprintf("%s:%d/active_hours", urlBase, port) + var win *window.Window + response, err := http.Get(url) + if err != nil { + return nil, err + } + defer response.Body.Close() + if response.StatusCode != http.StatusOK { + return win, fmt.Errorf( + "active_hours request failed for url %s (%d)", url, response.StatusCode) + } + j, err := io.ReadAll(response.Body) + if err != nil { + return nil, err + } + + if err := json.Unmarshal(j, win); err != nil { + return nil, err + } + return win, nil +} + func readSchedules(urls []string) ([]window.Schedule, error) { var sched []window.Schedule for _, url := range urls { diff --git a/schedule/schedule.go b/schedule/schedule.go index 2ab1285..522a9e7 100644 --- a/schedule/schedule.go +++ b/schedule/schedule.go @@ -17,7 +17,6 @@ package schedule import ( "fmt" - "runtime" "strings" "time" @@ -60,6 +59,11 @@ func findNearest(schedules []window.Schedule) window.Schedule { return next } +// ActiveHours returns the built-in Active Hours maintenance window. +func ActiveHours() (*window.Window, error) { + return window.ActiveHoursWindow() +} + // Schedule calculates schedule per label and returns label whose names match the given string(s). func Schedule(names ...string) ([]window.Schedule, error) { var r window.Reader @@ -67,13 +71,6 @@ func Schedule(names ...string) ([]window.Schedule, error) { if err != nil { return nil, err } - switch runtime.GOOS { - case "windows": - m, err = window.ActiveHoursWindow(m) - if err != nil { - return nil, err - } - } if len(names) == 0 { names = m.Keys() } diff --git a/server/server.go b/server/server.go index 921fd52..0c0647d 100644 --- a/server/server.go +++ b/server/server.go @@ -53,6 +53,18 @@ func serve(w http.ResponseWriter, r *http.Request) { sendHTTPResponse(w, http.StatusOK, b) } +func serveActiveHours(w http.ResponseWriter, r *http.Request) { + s, err := schedule.ActiveHours() + if err != nil { + sendHTTPResponse(w, http.StatusInternalServerError, []byte(err.Error())) + } + b, err := json.Marshal(&s) + if err != nil { + sendHTTPResponse(w, http.StatusInternalServerError, []byte(err.Error())) + } + sendHTTPResponse(w, http.StatusOK, b) +} + func respondOk(w http.ResponseWriter, r *http.Request) { sendHTTPResponse(w, http.StatusOK, []byte("OK")) } @@ -62,6 +74,7 @@ func muxRouter() http.Handler { rtr.HandleFunc("/status", respondOk) rtr.HandleFunc("/schedule", serve) rtr.HandleFunc("/schedule/{label}", serve) + rtr.HandleFunc("/active_hours", serveActiveHours) return rtr } diff --git a/window/window.go b/window/window.go index 0584810..ae5a0eb 100644 --- a/window/window.go +++ b/window/window.go @@ -579,7 +579,7 @@ func reportConfFileMetric(path, result string) { } // ActiveHoursWindow retrieves the built-in Active Hours maintenance windows if available. -func ActiveHoursWindow(m Map) (Map, error) { +func ActiveHoursWindow() (*Window, error) { activeStartTime, activeEndTime, err := auklib.ActiveHours() if err != nil { return nil, err @@ -602,6 +602,5 @@ func ActiveHoursWindow(m Map) (Map, error) { } else { activeWindow.Schedule.State = "closed" } - m.Add(activeWindow) - return m, nil + return &activeWindow, nil }