Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aukera: Move activity window to a dedicated endpoint #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,35 @@ func Label(port int, names ...string) ([]window.Schedule, error) {
return readSchedules(urls)
}

// ActiveHours gets the built-in Active Hours maintenance window.
// This window is active (open) during the times set by the user or machine admin as the hours
// during which user activity is expected.
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
}
Loading