Update nodestate API and db adapter

This commit is contained in:
2025-09-28 08:26:44 +02:00
parent 015583f1cd
commit beb92967e5
4 changed files with 38 additions and 7 deletions

View File

@@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
"time"
"github.com/ClusterCockpit/cc-backend/internal/repository" "github.com/ClusterCockpit/cc-backend/internal/repository"
"github.com/ClusterCockpit/cc-lib/schema" "github.com/ClusterCockpit/cc-lib/schema"
@@ -68,13 +69,23 @@ func (api *RestApi) updateNodeStates(rw http.ResponseWriter, r *http.Request) {
// Parse request body // Parse request body
req := UpdateNodeStatesRequest{} req := UpdateNodeStatesRequest{}
if err := decode(r.Body, &req); err != nil { 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 return
} }
repo := repository.GetNodeRepository() repo := repository.GetNodeRepository()
for _, node := range req.Nodes { for _, node := range req.Nodes {
state := determineState(node.States) 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)
} }
} }

View File

@@ -20,7 +20,6 @@ CREATE TABLE "node" (
CHECK (health_state IN ( CHECK (health_state IN (
'full', 'partial', 'failed' 'full', 'partial', 'failed'
)), )),
meta_data TEXT, -- JSON
UNIQUE (hostname, cluster) UNIQUE (hostname, cluster)
); );

View File

@@ -144,8 +144,10 @@ func (r *NodeRepository) GetNode(id int64, withMeta bool) (*schema.Node, error)
} }
const NamedNodeInsert string = ` const NamedNodeInsert string = `
INSERT INTO node (hostname, cluster, subcluster, node_state, health_state) INSERT INTO node (time_stamp, hostname, cluster, subcluster, node_state, health_state,
VALUES (: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) { func (r *NodeRepository) AddNode(node *schema.Node) (int64, error) {
var err error var err error
@@ -164,6 +166,24 @@ func (r *NodeRepository) AddNode(node *schema.Node) (int64, error) {
return node.ID, nil 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 { func (r *NodeRepository) UpdateNodeState(hostname string, cluster string, nodeState *schema.NodeState) error {
var id int64 var id int64
if err := sq.Select("id").From("node"). if err := sq.Select("id").From("node").

View File

@@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/ClusterCockpit/cc-backend/internal/config" "github.com/ClusterCockpit/cc-backend/internal/config"
"github.com/ClusterCockpit/cc-backend/web"
cclog "github.com/ClusterCockpit/cc-lib/ccLogger" cclog "github.com/ClusterCockpit/cc-lib/ccLogger"
"github.com/ClusterCockpit/cc-lib/lrucache" "github.com/ClusterCockpit/cc-lib/lrucache"
"github.com/ClusterCockpit/cc-lib/schema" "github.com/ClusterCockpit/cc-lib/schema"
@@ -25,7 +26,7 @@ var (
type UserCfgRepo struct { type UserCfgRepo struct {
DB *sqlx.DB DB *sqlx.DB
Lookup *sqlx.Stmt Lookup *sqlx.Stmt
uiDefaults map[string]any uiDefaults web.WebConfig
cache *lrucache.Cache cache *lrucache.Cache
lock sync.RWMutex lock sync.RWMutex
} }
@@ -42,7 +43,7 @@ func GetUserCfgRepo() *UserCfgRepo {
userCfgRepoInstance = &UserCfgRepo{ userCfgRepoInstance = &UserCfgRepo{
DB: db.DB, DB: db.DB,
Lookup: lookupConfigStmt, Lookup: lookupConfigStmt,
uiDefaults: config.Keys.UiDefaults, uiDefaults: web.UIDefaults,
cache: lrucache.New(1024), cache: lrucache.New(1024),
} }
}) })