mirror of
				https://github.com/ClusterCockpit/cc-backend
				synced 2025-10-31 07:55:06 +01:00 
			
		
		
		
	Update nodestate API and db adapter
This commit is contained in:
		| @@ -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) | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -20,7 +20,6 @@ CREATE TABLE "node" ( | ||||
|     CHECK (health_state IN ( | ||||
|         'full', 'partial', 'failed' | ||||
|     )), | ||||
|     meta_data TEXT,          -- JSON | ||||
|     UNIQUE (hostname, cluster) | ||||
| ); | ||||
|  | ||||
|   | ||||
| @@ -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"). | ||||
|   | ||||
| @@ -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), | ||||
| 		} | ||||
| 	}) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user