Skip to content

Commit

Permalink
aukera: Move activity window to a dedicated endpoint
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 701887299
  • Loading branch information
Aukera Team authored and copybara-github committed Dec 2, 2024
1 parent e186d03 commit da6bfcd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
27 changes: 27 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 5 additions & 8 deletions schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package schedule

import (
"fmt"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -60,20 +59,18 @@ 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
m, err := window.Windows(auklib.ConfDir, r)
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()
}
Expand Down
13 changes: 13 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand All @@ -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
}

Expand Down
5 changes: 2 additions & 3 deletions window/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -602,6 +602,5 @@ func ActiveHoursWindow(m Map) (Map, error) {
} else {
activeWindow.Schedule.State = "closed"
}
m.Add(activeWindow)
return m, nil
return &activeWindow, nil
}

0 comments on commit da6bfcd

Please sign in to comment.