cc-backend/config/config.go

171 lines
3.8 KiB
Go
Raw Normal View History

package config
import (
"context"
"encoding/json"
2021-12-08 10:12:19 +01:00
"fmt"
"log"
"net/http"
"os"
2021-12-08 10:12:19 +01:00
"path/filepath"
"sync"
2021-12-08 10:12:19 +01:00
"time"
2021-11-26 10:32:36 +01:00
2021-12-08 10:12:19 +01:00
"github.com/ClusterCockpit/cc-jobarchive/auth"
2021-11-26 10:32:36 +01:00
"github.com/ClusterCockpit/cc-jobarchive/graph/model"
2021-12-08 10:12:19 +01:00
"github.com/jmoiron/sqlx"
)
2021-12-08 10:12:19 +01:00
var db *sqlx.DB
var lock sync.RWMutex
2021-12-08 10:12:19 +01:00
var uiDefaults map[string]interface{}
2021-11-26 10:32:36 +01:00
var Clusters []*model.Cluster
2021-12-08 10:12:19 +01:00
func Init(usersdb *sqlx.DB, authEnabled bool, uiConfig map[string]interface{}, jobArchive string) error {
db = usersdb
uiDefaults = uiConfig
entries, err := os.ReadDir(jobArchive)
if err != nil {
return err
}
2021-12-08 10:12:19 +01:00
Clusters = []*model.Cluster{}
for _, de := range entries {
bytes, err := os.ReadFile(filepath.Join(jobArchive, de.Name(), "cluster.json"))
if err != nil {
return err
}
2021-12-08 10:12:19 +01:00
var cluster model.Cluster
if err := json.Unmarshal(bytes, &cluster); err != nil {
return err
}
if cluster.FilterRanges.StartTime.To.IsZero() {
cluster.FilterRanges.StartTime.To = time.Unix(0, 0)
}
2021-12-17 15:49:22 +01:00
if cluster.Name != de.Name() {
return fmt.Errorf("the file '%s/cluster.json' contains the clusterId '%s'", de.Name(), cluster.Name)
2021-12-08 10:12:19 +01:00
}
Clusters = append(Clusters, &cluster)
}
2021-12-08 10:12:19 +01:00
if authEnabled {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS configuration (
username varchar(255),
key varchar(255),
value varchar(255),
PRIMARY KEY (username, key),
FOREIGN KEY (username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE NO ACTION);`)
if err != nil {
return err
}
}
2021-12-08 10:12:19 +01:00
return nil
}
2021-12-08 10:12:19 +01:00
// Return the personalised UI config for the currently authenticated
// user or return the plain default config.
func GetUIConfig(r *http.Request) (map[string]interface{}, error) {
lock.RLock()
config := make(map[string]interface{}, len(uiDefaults))
for k, v := range uiDefaults {
config[k] = v
}
2021-12-08 10:12:19 +01:00
lock.RUnlock()
2021-12-08 10:12:19 +01:00
user := auth.GetUser(r.Context())
if user == nil {
return config, nil
}
2021-12-08 10:12:19 +01:00
rows, err := db.Query(`SELECT key, value FROM configuration WHERE configuration.username = ?`, user.Username)
if err != nil {
2021-12-08 10:12:19 +01:00
return nil, err
}
for rows.Next() {
var key, rawval string
if err := rows.Scan(&key, &rawval); err != nil {
return nil, err
}
var val interface{}
if err := json.Unmarshal([]byte(rawval), &val); err != nil {
return nil, err
}
config[key] = val
}
return config, nil
}
// If the context does not have a user, update the global ui configuration without persisting it!
// If there is a (authenticated) user, update only his configuration.
func UpdateConfig(key, value string, ctx context.Context) error {
user := auth.GetUser(ctx)
if user == nil {
lock.RLock()
defer lock.RUnlock()
var val interface{}
if err := json.Unmarshal([]byte(value), &val); err != nil {
return err
}
uiDefaults[key] = val
return nil
}
2021-12-08 10:12:19 +01:00
if _, err := db.Exec(`REPLACE INTO configuration (username, key, value) VALUES (?, ?, ?)`,
user.Username, key, value); err != nil {
log.Printf("db.Exec: %s\n", err.Error())
return err
}
return nil
}
2021-12-08 10:12:19 +01:00
// http.HandlerFunc compatible function that serves the current configuration as JSON.
// TODO: Use templates and stuff instead of this...
func ServeConfig(rw http.ResponseWriter, r *http.Request) {
2021-12-08 10:12:19 +01:00
config, err := GetUIConfig(r)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(rw).Encode(config); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
2021-11-26 10:32:36 +01:00
func GetClusterConfig(cluster string) *model.Cluster {
for _, c := range Clusters {
2021-12-17 15:49:22 +01:00
if c.Name == cluster {
2021-11-26 10:32:36 +01:00
return c
}
}
return nil
}
func GetMetricConfig(cluster, metric string) *model.MetricConfig {
for _, c := range Clusters {
2021-12-17 15:49:22 +01:00
if c.Name == cluster {
2021-11-26 10:32:36 +01:00
for _, m := range c.MetricConfig {
if m.Name == metric {
return m
}
}
}
}
return nil
}