From beb92967e53851a90c86e1bd2fd9d9f05438cb35 Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Sun, 28 Sep 2025 08:26:44 +0200 Subject: [PATCH] Update nodestate API and db adapter --- internal/api/node.go | 15 ++++++++++-- .../migrations/sqlite3/10_node-table.up.sql | 1 - internal/repository/node.go | 24 +++++++++++++++++-- internal/repository/userConfig.go | 5 ++-- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/internal/api/node.go b/internal/api/node.go index 385b2da..aa5016d 100644 --- a/internal/api/node.go +++ b/internal/api/node.go @@ -8,6 +8,7 @@ import ( "fmt" "net/http" "strings" + "time" "github.com/ClusterCockpit/cc-backend/internal/repository" "github.com/ClusterCockpit/cc-lib/schema" @@ -68,13 +69,23 @@ func (api *RestApi) updateNodeStates(rw http.ResponseWriter, r *http.Request) { // Parse request body req := UpdateNodeStatesRequest{} if err := decode(r.Body, &req); err != nil { - handleError(fmt.Errorf("parsing request body failed: %w", err), http.StatusBadRequest, rw) + handleError(fmt.Errorf("parsing request body failed: %w", err), + http.StatusBadRequest, rw) return } repo := repository.GetNodeRepository() for _, node := range req.Nodes { state := determineState(node.States) - repo.UpdateNodeState(node.Name, req.Cluster, &state) + nodeState := schema.Node{ + TimeStamp: time.Now().Unix(), NodeState: state, + Hostname: node.Name, Cluster: req.Cluster, + CpusAllocated: node.CpusAllocated, CpusTotal: node.CpusTotal, + MemoryAllocated: node.MemoryAllocated, MemoryTotal: node.MemoryTotal, + GpusAllocated: node.GpusAllocated, GpusTotal: node.GpusTotal, + HealthState: schema.MonitoringStateFull, + } + + repo.InsertNodeState(&nodeState) } } diff --git a/internal/repository/migrations/sqlite3/10_node-table.up.sql b/internal/repository/migrations/sqlite3/10_node-table.up.sql index 03a9bf6..4265eb8 100644 --- a/internal/repository/migrations/sqlite3/10_node-table.up.sql +++ b/internal/repository/migrations/sqlite3/10_node-table.up.sql @@ -20,7 +20,6 @@ CREATE TABLE "node" ( CHECK (health_state IN ( 'full', 'partial', 'failed' )), - meta_data TEXT, -- JSON UNIQUE (hostname, cluster) ); diff --git a/internal/repository/node.go b/internal/repository/node.go index d7db2f4..53af28d 100644 --- a/internal/repository/node.go +++ b/internal/repository/node.go @@ -144,8 +144,10 @@ func (r *NodeRepository) GetNode(id int64, withMeta bool) (*schema.Node, error) } const NamedNodeInsert string = ` -INSERT INTO node (hostname, cluster, subcluster, node_state, health_state) - VALUES (:hostname, :cluster, :subcluster, :node_state, :health_state);` +INSERT INTO node (time_stamp, hostname, cluster, subcluster, node_state, health_state, + cpus_allocated, cpus_total, memory_allocated, memory_total, gpus_allocated, gpus_total) + VALUES (:time_stamp, :hostname, :cluster, :subcluster, :node_state, :health_state, + :cpus_allocated, :cpus_total, :memory_allocated, :memory_total, :gpus_allocated, :gpus_total);` func (r *NodeRepository) AddNode(node *schema.Node) (int64, error) { var err error @@ -164,6 +166,24 @@ func (r *NodeRepository) AddNode(node *schema.Node) (int64, error) { return node.ID, nil } +func (r *NodeRepository) InsertNodeState(nodeState *schema.Node) error { + subcluster, err := archive.GetSubClusterByNode(nodeState.Cluster, nodeState.Hostname) + if err != nil { + cclog.Errorf("Error while getting subcluster for node '%s' in cluster '%s': %v", nodeState.Hostname, nodeState.Cluster, err) + return err + } + + nodeState.SubCluster = subcluster + + _, err = r.DB.NamedExec(NamedNodeInsert, nodeState) + if err != nil { + cclog.Errorf("Error while adding node '%v' to database", nodeState.Hostname) + return err + } + + return nil +} + func (r *NodeRepository) UpdateNodeState(hostname string, cluster string, nodeState *schema.NodeState) error { var id int64 if err := sq.Select("id").From("node"). diff --git a/internal/repository/userConfig.go b/internal/repository/userConfig.go index 83f3140..9df6af3 100644 --- a/internal/repository/userConfig.go +++ b/internal/repository/userConfig.go @@ -11,6 +11,7 @@ import ( "time" "github.com/ClusterCockpit/cc-backend/internal/config" + "github.com/ClusterCockpit/cc-backend/web" cclog "github.com/ClusterCockpit/cc-lib/ccLogger" "github.com/ClusterCockpit/cc-lib/lrucache" "github.com/ClusterCockpit/cc-lib/schema" @@ -25,7 +26,7 @@ var ( type UserCfgRepo struct { DB *sqlx.DB Lookup *sqlx.Stmt - uiDefaults map[string]any + uiDefaults web.WebConfig cache *lrucache.Cache lock sync.RWMutex } @@ -42,7 +43,7 @@ func GetUserCfgRepo() *UserCfgRepo { userCfgRepoInstance = &UserCfgRepo{ DB: db.DB, Lookup: lookupConfigStmt, - uiDefaults: config.Keys.UiDefaults, + uiDefaults: web.UIDefaults, cache: lrucache.New(1024), } })