2022-02-03 07:25:36 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-02-03 11:35:42 +01:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2022-02-03 07:25:36 +01:00
|
|
|
"strings"
|
2022-02-28 11:08:43 +01:00
|
|
|
"time"
|
2022-02-03 07:25:36 +01:00
|
|
|
|
|
|
|
"github.com/ClusterCockpit/cc-backend/auth"
|
|
|
|
"github.com/ClusterCockpit/cc-backend/config"
|
2022-02-03 11:35:42 +01:00
|
|
|
"github.com/ClusterCockpit/cc-backend/schema"
|
2022-02-03 07:25:36 +01:00
|
|
|
"github.com/ClusterCockpit/cc-backend/templates"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InfoType map[string]interface{}
|
|
|
|
|
|
|
|
type Route struct {
|
|
|
|
Route string
|
|
|
|
Template string
|
|
|
|
Title string
|
|
|
|
Filter bool
|
|
|
|
Setup func(i InfoType, r *http.Request) InfoType
|
|
|
|
}
|
|
|
|
|
2022-02-03 11:35:42 +01:00
|
|
|
func buildFilterPresets(query url.Values) map[string]interface{} {
|
|
|
|
filterPresets := map[string]interface{}{}
|
|
|
|
|
|
|
|
if query.Get("cluster") != "" {
|
|
|
|
filterPresets["cluster"] = query.Get("cluster")
|
|
|
|
}
|
|
|
|
if query.Get("partition") != "" {
|
|
|
|
filterPresets["partition"] = query.Get("partition")
|
|
|
|
}
|
|
|
|
if query.Get("project") != "" {
|
|
|
|
filterPresets["project"] = query.Get("project")
|
|
|
|
filterPresets["projectMatch"] = "eq"
|
|
|
|
}
|
2022-02-03 16:00:08 +01:00
|
|
|
if query.Get("user") != "" {
|
|
|
|
filterPresets["user"] = query.Get("user")
|
|
|
|
filterPresets["userMatch"] = "eq"
|
|
|
|
}
|
2022-02-03 11:35:42 +01:00
|
|
|
if query.Get("state") != "" && schema.JobState(query.Get("state")).Valid() {
|
|
|
|
filterPresets["state"] = query.Get("state")
|
|
|
|
}
|
|
|
|
if rawtags, ok := query["tag"]; ok {
|
|
|
|
tags := make([]int, len(rawtags))
|
|
|
|
for i, tid := range rawtags {
|
|
|
|
var err error
|
|
|
|
tags[i], err = strconv.Atoi(tid)
|
|
|
|
if err != nil {
|
|
|
|
tags[i] = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filterPresets["tags"] = tags
|
|
|
|
}
|
|
|
|
if query.Get("numNodes") != "" {
|
|
|
|
parts := strings.Split(query.Get("numNodes"), "-")
|
|
|
|
if len(parts) == 2 {
|
|
|
|
a, e1 := strconv.Atoi(parts[0])
|
|
|
|
b, e2 := strconv.Atoi(parts[1])
|
|
|
|
if e1 == nil && e2 == nil {
|
|
|
|
filterPresets["numNodes"] = map[string]int{"from": a, "to": b}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if query.Get("jobId") != "" {
|
|
|
|
filterPresets["jobId"] = query.Get("jobId")
|
|
|
|
}
|
|
|
|
if query.Get("arrayJobId") != "" {
|
|
|
|
if num, err := strconv.Atoi(query.Get("arrayJobId")); err == nil {
|
|
|
|
filterPresets["arrayJobId"] = num
|
|
|
|
}
|
|
|
|
}
|
2022-02-28 11:08:43 +01:00
|
|
|
if query.Get("startTime") != "" {
|
|
|
|
parts := strings.Split(query.Get("startTime"), "-")
|
|
|
|
if len(parts) == 2 {
|
|
|
|
a, e1 := strconv.ParseInt(parts[0], 10, 64)
|
|
|
|
b, e2 := strconv.ParseInt(parts[1], 10, 64)
|
|
|
|
if e1 == nil && e2 == nil {
|
|
|
|
filterPresets["startTime"] = map[string]string{
|
|
|
|
"from": time.Unix(a, 0).Format(time.RFC3339),
|
|
|
|
"to": time.Unix(b, 0).Format(time.RFC3339),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 11:35:42 +01:00
|
|
|
|
|
|
|
return filterPresets
|
|
|
|
}
|
|
|
|
|
2022-02-03 09:39:04 +01:00
|
|
|
func setupRoutes(router *mux.Router, routes []Route) {
|
2022-02-03 07:25:36 +01:00
|
|
|
for _, route := range routes {
|
2022-02-03 11:35:42 +01:00
|
|
|
route := route
|
|
|
|
router.HandleFunc(route.Route, func(rw http.ResponseWriter, r *http.Request) {
|
2022-02-03 07:25:36 +01:00
|
|
|
conf, err := config.GetUIConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-03 13:45:41 +01:00
|
|
|
title := route.Title
|
2022-02-15 10:03:09 +01:00
|
|
|
infos := route.Setup(map[string]interface{}{}, r)
|
2022-02-03 10:25:52 +01:00
|
|
|
if id, ok := infos["id"]; ok {
|
2022-03-03 13:45:41 +01:00
|
|
|
title = strings.Replace(route.Title, "<ID>", id.(string), 1)
|
2022-02-03 11:35:42 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 10:03:09 +01:00
|
|
|
username, isAdmin := "", true
|
|
|
|
if user := auth.GetUser(r.Context()); user != nil {
|
|
|
|
username = user.Username
|
|
|
|
isAdmin = user.HasRole(auth.RoleAdmin)
|
|
|
|
}
|
2022-02-11 16:51:49 +01:00
|
|
|
|
2022-02-03 11:35:42 +01:00
|
|
|
page := templates.Page{
|
2022-03-03 13:45:41 +01:00
|
|
|
Title: title,
|
2022-02-15 10:03:09 +01:00
|
|
|
User: templates.User{Username: username, IsAdmin: isAdmin},
|
2022-02-03 11:35:42 +01:00
|
|
|
Config: conf,
|
|
|
|
Infos: infos,
|
|
|
|
}
|
|
|
|
|
|
|
|
if route.Filter {
|
|
|
|
page.FilterPresets = buildFilterPresets(r.URL.Query())
|
2022-02-03 10:25:52 +01:00
|
|
|
}
|
2022-02-03 07:25:36 +01:00
|
|
|
|
2022-02-03 11:35:42 +01:00
|
|
|
templates.Render(rw, r, route.Template, &page)
|
2022-02-03 07:25:36 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|