diff --git a/cmd/daemon.go b/cmd/daemon.go index ed6b941..669773f 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -46,7 +46,7 @@ func main() { slog.Info("at least one process exited, shutting down") } - err = shiftNotifier.Stop() + err := shiftNotifier.Stop() if err != nil { slog.Error("failed stopping notifier", "error", err.Error()) } diff --git a/internal/shiftnotifier/service.go b/internal/shiftnotifier/service.go index 092d6a1..6765bb4 100644 --- a/internal/shiftnotifier/service.go +++ b/internal/shiftnotifier/service.go @@ -55,7 +55,8 @@ func New(config *Config, angelAPI angelapi.Service, messenger matrixmessenger.Me } http.HandleFunc("/data", s.serveJSONData) - http.HandleFunc("/", s.serveHumanData) + http.HandleFunc("/", s.serveHumanPortrait) + http.HandleFunc("/landscape", s.serveHumanLandscape) return s } diff --git a/internal/shiftnotifier/template/landscape.html b/internal/shiftnotifier/template/landscape.html new file mode 100644 index 0000000..90aa1c6 --- /dev/null +++ b/internal/shiftnotifier/template/landscape.html @@ -0,0 +1,104 @@ + + + + {{ if .refresh_seconds }} + + {{ end }} + + + + + +

Upcoming Troll Changes for {{ .shift_time }}

+ +
+ {{ range $location, $diffs := .data.DiffsInLocations }} +
+ 📍 {{ $location }}

+ Arriving Trolls 🔜:
+ {{ if $diffs.UsersArriving }} + + {{ else }} +   none
+ {{ end }} +
+ + Staying Trolls 🔄:
+ {{ if $diffs.UsersWorking }} + + {{ else }} +   none
+ {{ end }} +
+ + Leaving Trolls 🔚:
+ {{ if $diffs.UsersLeaving }} + + {{ else }} +   none
+ {{ end }} +

+ {{ $diffs.ExpectedUsers }} Trolls expected.

+ {{ if $diffs.OpenUsers }} + 🚨 Open positions:
+ + {{ end }} + +
+ {{ end }} +
+ + + \ No newline at end of file diff --git a/internal/shiftnotifier/webview.go b/internal/shiftnotifier/webview.go index daf1487..e44aea6 100644 --- a/internal/shiftnotifier/webview.go +++ b/internal/shiftnotifier/webview.go @@ -6,8 +6,14 @@ import ( "log/slog" "net/http" "strings" + "text/template" + + _ "embed" ) +//go:embed template/landscape.html +var landscapeTemplate string + func (service *service) requireToken(r *http.Request) error { t := r.URL.Query().Get("token") if strings.TrimSpace(t) != service.config.Token { @@ -34,7 +40,7 @@ func (service *service) serveJSONData(w http.ResponseWriter, r *http.Request) { _, _ = w.Write(data) } -func (service *service) serveHumanData(w http.ResponseWriter, r *http.Request) { +func (service *service) serveHumanPortrait(w http.ResponseWriter, r *http.Request) { err := service.requireToken(r) if err != nil { w.WriteHeader(http.StatusUnauthorized) @@ -65,3 +71,49 @@ body { _, _ = w.Write([]byte(html)) } + +func (service *service) serveHumanLandscape(w http.ResponseWriter, r *http.Request) { + err := service.requireToken(r) + if err != nil { + w.WriteHeader(http.StatusUnauthorized) + _, _ = w.Write([]byte("unauthorized")) + return + } + + if service.latestDiffs == nil { + _, _ = w.Write([]byte("no data")) + return + } + + service.latestDiffs.DiffsInLocations["test"] = shiftDiff{ + ExpectedUsers: 200, + OpenUsers: map[string]int64{"Gulasch": 3, "Drucker": 7}, + UsersLeaving: []shiftUser{ + { + Nickname: "cubic", + ShiftName: "Tschunk", + }, + { + Nickname: "2222222", + ShiftName: "Tschunkfsdfadd", + }, + }, + } + + tmpl, err := template.New("landscape").Parse(landscapeTemplate) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + _, _ = w.Write([]byte(err.Error())) + return + } + err = tmpl.Execute(w, map[string]any{ + "data": service.latestDiffs, + "refresh_seconds": r.URL.Query().Get("refresh_seconds"), + "shift_time": service.latestDiffs.ReferenceTime.Format("Mon, 15:04"), + }) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + _, _ = w.Write([]byte(err.Error())) + return + } +}