2021-10-26 10:24:43 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-03-01 16:00:44 +01:00
|
|
|
"errors"
|
2021-12-08 10:12:19 +01:00
|
|
|
"fmt"
|
2021-10-26 10:24:43 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-12-08 10:12:19 +01:00
|
|
|
"path/filepath"
|
2021-10-26 10:24:43 +02:00
|
|
|
"sync"
|
2021-12-08 10:12:19 +01:00
|
|
|
"time"
|
2021-11-26 10:32:36 +01:00
|
|
|
|
2022-01-27 09:40:59 +01:00
|
|
|
"github.com/ClusterCockpit/cc-backend/auth"
|
|
|
|
"github.com/ClusterCockpit/cc-backend/graph/model"
|
2022-03-01 16:00:44 +01:00
|
|
|
"github.com/ClusterCockpit/cc-backend/schema"
|
2022-01-20 10:00:55 +01:00
|
|
|
"github.com/iamlouk/lrucache"
|
2021-12-08 10:12:19 +01:00
|
|
|
"github.com/jmoiron/sqlx"
|
2021-10-26 10:24:43 +02:00
|
|
|
)
|
|
|
|
|
2021-12-08 10:12:19 +01:00
|
|
|
var db *sqlx.DB
|
2022-01-20 10:00:55 +01:00
|
|
|
var lookupConfigStmt *sqlx.Stmt
|
2021-10-26 10:24:43 +02:00
|
|
|
var lock sync.RWMutex
|
2021-12-08 10:12:19 +01:00
|
|
|
var uiDefaults map[string]interface{}
|
2022-03-08 10:33:56 +01:00
|
|
|
var cache *lrucache.Cache = lrucache.New(1024)
|
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-10-26 10:24:43 +02:00
|
|
|
|
2021-12-08 10:12:19 +01:00
|
|
|
Clusters = []*model.Cluster{}
|
|
|
|
for _, de := range entries {
|
2022-03-01 16:00:44 +01:00
|
|
|
raw, err := os.ReadFile(filepath.Join(jobArchive, de.Name(), "cluster.json"))
|
2021-12-08 10:12:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-26 10:24:43 +02:00
|
|
|
|
2021-12-08 10:12:19 +01:00
|
|
|
var cluster model.Cluster
|
2022-03-01 16:00:44 +01:00
|
|
|
|
|
|
|
// Disabled because of the historic 'measurement' field.
|
|
|
|
// dec := json.NewDecoder(bytes.NewBuffer(raw))
|
|
|
|
// dec.DisallowUnknownFields()
|
|
|
|
// if err := dec.Decode(&cluster); err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
|
|
|
|
if err := json.Unmarshal(raw, &cluster); err != nil {
|
2021-12-08 10:12:19 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-01 16:00:44 +01:00
|
|
|
if len(cluster.Name) == 0 || len(cluster.MetricConfig) == 0 || len(cluster.Partitions) == 0 {
|
|
|
|
return errors.New("cluster.name, cluster.metricConfig and cluster.Partitions should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mc := range cluster.MetricConfig {
|
|
|
|
if len(mc.Name) == 0 {
|
|
|
|
return errors.New("cluster.metricConfig.name should not be empty")
|
|
|
|
}
|
|
|
|
if mc.Timestep < 1 {
|
|
|
|
return errors.New("cluster.metricConfig.timestep should not be smaller than one")
|
|
|
|
}
|
|
|
|
|
|
|
|
// For backwards compability...
|
|
|
|
if mc.Scope == "" {
|
|
|
|
mc.Scope = schema.MetricScopeNode
|
|
|
|
}
|
|
|
|
if !mc.Scope.Valid() {
|
|
|
|
return errors.New("cluster.metricConfig.scope must be a valid scope ('node', 'scocket', ...)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 10:12:19 +01:00
|
|
|
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() {
|
2022-03-01 16:00:44 +01:00
|
|
|
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-10-26 10:24:43 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 10:12:19 +01:00
|
|
|
if authEnabled {
|
|
|
|
_, err := db.Exec(`
|
|
|
|
CREATE TABLE IF NOT EXISTS configuration (
|
|
|
|
username varchar(255),
|
2022-01-20 10:00:55 +01:00
|
|
|
confkey varchar(255),
|
2021-12-08 10:12:19 +01:00
|
|
|
value varchar(255),
|
2022-01-20 10:00:55 +01:00
|
|
|
PRIMARY KEY (username, confkey),
|
2021-12-08 10:12:19 +01:00
|
|
|
FOREIGN KEY (username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE NO ACTION);`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-20 10:00:55 +01:00
|
|
|
|
|
|
|
lookupConfigStmt, err = db.Preparex(`SELECT confkey, value FROM configuration WHERE configuration.username = ?`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-26 10:24:43 +02:00
|
|
|
}
|
2021-12-08 10:12:19 +01:00
|
|
|
|
|
|
|
return nil
|
2021-10-26 10:24:43 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
user := auth.GetUser(r.Context())
|
|
|
|
if user == nil {
|
2022-01-20 10:00:55 +01:00
|
|
|
lock.RLock()
|
|
|
|
copy := make(map[string]interface{}, len(uiDefaults))
|
|
|
|
for k, v := range uiDefaults {
|
|
|
|
copy[k] = v
|
|
|
|
}
|
|
|
|
lock.RUnlock()
|
|
|
|
return copy, nil
|
2021-12-08 10:12:19 +01:00
|
|
|
}
|
2021-10-26 10:24:43 +02:00
|
|
|
|
2022-01-20 10:00:55 +01:00
|
|
|
data := cache.Get(user.Username, func() (interface{}, time.Duration, int) {
|
|
|
|
config := make(map[string]interface{}, len(uiDefaults))
|
|
|
|
for k, v := range uiDefaults {
|
|
|
|
config[k] = v
|
|
|
|
}
|
2021-12-08 10:12:19 +01:00
|
|
|
|
2022-01-20 10:00:55 +01:00
|
|
|
rows, err := lookupConfigStmt.Query(user.Username)
|
|
|
|
if err != nil {
|
|
|
|
return err, 0, 0
|
2021-12-08 10:12:19 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 10:00:55 +01:00
|
|
|
size := 0
|
2022-03-03 14:54:37 +01:00
|
|
|
defer rows.Close()
|
2022-01-20 10:00:55 +01:00
|
|
|
for rows.Next() {
|
|
|
|
var key, rawval string
|
|
|
|
if err := rows.Scan(&key, &rawval); err != nil {
|
|
|
|
return err, 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var val interface{}
|
|
|
|
if err := json.Unmarshal([]byte(rawval), &val); err != nil {
|
|
|
|
return err, 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
size += len(key)
|
|
|
|
size += len(rawval)
|
|
|
|
config[key] = val
|
2021-12-08 10:12:19 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 10:00:55 +01:00
|
|
|
return config, 24 * time.Hour, size
|
|
|
|
})
|
|
|
|
if err, ok := data.(error); ok {
|
|
|
|
return nil, err
|
2021-12-08 10:12:19 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 10:00:55 +01:00
|
|
|
return data.(map[string]interface{}), nil
|
2021-12-08 10:12:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
var val interface{}
|
|
|
|
if err := json.Unmarshal([]byte(value), &val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-20 10:00:55 +01:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
2021-12-08 10:12:19 +01:00
|
|
|
uiDefaults[key] = val
|
|
|
|
return nil
|
2021-10-26 10:24:43 +02:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:54:37 +01:00
|
|
|
if _, ok := uiDefaults[key]; !ok {
|
|
|
|
return errors.New("this configuration key does not exist")
|
|
|
|
}
|
|
|
|
|
2022-01-20 10:00:55 +01:00
|
|
|
if _, err := db.Exec(`REPLACE INTO configuration (username, confkey, value) VALUES (?, ?, ?)`,
|
2021-12-08 10:12:19 +01:00
|
|
|
user.Username, key, value); err != nil {
|
2021-10-26 10:24:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:54:37 +01:00
|
|
|
cache.Del(user.Username)
|
2021-10-26 10:24:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-07 09:47:41 +01:00
|
|
|
func GetPartition(cluster, partition string) *model.Partition {
|
|
|
|
for _, c := range Clusters {
|
|
|
|
if c.Name == cluster {
|
|
|
|
for _, p := range c.Partitions {
|
|
|
|
if p.Name == partition {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-26 10:32:36 +01:00
|
|
|
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
|
|
|
|
}
|