MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / processClusterResourcesWithCache

Method processClusterResourcesWithCache

pkg/api/cluster.go:299–449  ·  view source on GitHub ↗

processClusterResourcesWithCache processes cluster resources with specified cache TTL

(cluster *Cluster, ttl time.Duration)

Source from the content-addressed store, hash-verified

297
298// processClusterResourcesWithCache processes cluster resources with specified cache TTL
299func (c *Client) processClusterResourcesWithCache(cluster *Cluster, ttl time.Duration) error {
300 var resourcesResp map[string]interface{}
301 if ttl == 0 {
302 // Use non-cached call for fresh data
303 if err := c.Get("/cluster/resources", &resourcesResp); err != nil {
304 return fmt.Errorf("failed to get cluster resources: %w", err)
305 }
306 } else {
307 // Use cached call with specified TTL
308 if err := c.GetWithCache("/cluster/resources", &resourcesResp, ttl); err != nil {
309 return fmt.Errorf("failed to get cluster resources: %w", err)
310 }
311 }
312
313 resourcesData, ok := resourcesResp["data"].([]interface{})
314 if !ok {
315 return fmt.Errorf("invalid cluster resources response format")
316 }
317
318 // Create a map for quick node lookup
319 nodeMap := make(map[string]*Node, len(cluster.Nodes))
320 for i := range cluster.Nodes {
321 nodeMap[cluster.Nodes[i].Name] = cluster.Nodes[i]
322 // Initialize VMs slice if nil
323 if cluster.Nodes[i].VMs == nil {
324 cluster.Nodes[i].VMs = make([]*VM, 0)
325 }
326 }
327
328 // Process resources in a single pass
329 for _, item := range resourcesData {
330 resource, ok := item.(map[string]interface{})
331 if !ok {
332 continue
333 }
334
335 resType := getString(resource, "type")
336 nodeName := getString(resource, "node")
337
338 switch resType {
339 case "node":
340 // Handle node resources - populate all available data from cluster resources
341 if node, exists := nodeMap[nodeName]; exists {
342 // Update all metrics available in cluster resources
343 if cpuUsage := getFloat(resource, "cpu"); cpuUsage >= 0 {
344 node.CPUUsage = cpuUsage
345 }
346
347 // Memory (convert bytes to GB)
348 if memUsed := getFloat(resource, "mem"); memUsed > 0 {
349 node.MemoryUsed = memUsed / 1073741824
350 }
351
352 if memMax := getFloat(resource, "maxmem"); memMax > 0 {
353 node.MemoryTotal = memMax / 1073741824
354 }
355
356 // CPU count (available as maxcpu in cluster resources)

Calls 9

GetMethod · 0.95
GetWithCacheMethod · 0.95
getStringFunction · 0.85
getFloatFunction · 0.85
getIntFunction · 0.85
getBoolFunction · 0.85
IsSharedMethod · 0.80
AddStorageMethod · 0.80
DebugMethod · 0.65

Tested by

no test coverage detected