-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add legacy inventory metrics (#119)
Signed-off-by: Artur Troian <troian.ap@gmail.com>
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package v1 | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3" | ||
) | ||
|
||
type ResourcesMetric struct { | ||
CPU uint64 `json:"cpu"` | ||
GPU uint64 `json:"gpu"` | ||
Memory uint64 `json:"memory"` | ||
StorageEphemeral uint64 `json:"storage_ephemeral"` | ||
} | ||
|
||
type NodeMetrics struct { | ||
Name string `json:"name"` | ||
Allocatable ResourcesMetric `json:"allocatable"` | ||
Available ResourcesMetric `json:"available"` | ||
} | ||
|
||
type Metrics struct { | ||
Nodes []NodeMetrics `json:"nodes"` | ||
TotalAllocatable MetricTotal `json:"total_allocatable"` | ||
TotalAvailable MetricTotal `json:"total_available"` | ||
} | ||
|
||
type MetricTotal struct { | ||
CPU uint64 `json:"cpu"` | ||
GPU uint64 `json:"gpu"` | ||
Memory uint64 `json:"memory"` | ||
StorageEphemeral uint64 `json:"storage_ephemeral"` | ||
Storage map[string]int64 `json:"storage,omitempty"` | ||
} | ||
|
||
type StorageStatus struct { | ||
Class string `json:"class"` | ||
Size int64 `json:"size"` | ||
} | ||
|
||
// InventoryMetrics stores active, pending and available units | ||
type InventoryMetrics struct { | ||
Active []MetricTotal `json:"active,omitempty"` | ||
Pending []MetricTotal `json:"pending,omitempty"` | ||
Available struct { | ||
Nodes []NodeMetrics `json:"nodes,omitempty"` | ||
Storage []StorageStatus `json:"storage,omitempty"` | ||
} `json:"available,omitempty"` | ||
Error error `json:"error,omitempty"` | ||
} | ||
|
||
func (inv *MetricTotal) AddResources(res dtypes.ResourceUnit) { | ||
cpu := sdk.NewIntFromUint64(inv.CPU) | ||
gpu := sdk.NewIntFromUint64(inv.GPU) | ||
mem := sdk.NewIntFromUint64(inv.Memory) | ||
ephemeralStorage := sdk.NewIntFromUint64(inv.StorageEphemeral) | ||
|
||
if res.CPU != nil { | ||
cpu = cpu.Add(res.CPU.Units.Val.MulRaw(int64(res.Count))) | ||
} | ||
|
||
if res.GPU != nil { | ||
gpu = gpu.Add(res.GPU.Units.Val.MulRaw(int64(res.Count))) | ||
} | ||
|
||
if res.Memory != nil { | ||
mem = mem.Add(res.Memory.Quantity.Val.MulRaw(int64(res.Count))) | ||
} | ||
|
||
for _, storage := range res.Storage { | ||
if storageClass, found := storage.Attributes.Find("class").AsString(); !found { | ||
ephemeralStorage = ephemeralStorage.Add(storage.Quantity.Val.MulRaw(int64(res.Count))) | ||
} else { | ||
val := sdk.NewIntFromUint64(uint64(inv.Storage[storageClass])) | ||
val = val.Add(storage.Quantity.Val.MulRaw(int64(res.Count))) | ||
inv.Storage[storageClass] = val.Int64() | ||
} | ||
} | ||
|
||
inv.CPU = cpu.Uint64() | ||
inv.GPU = gpu.Uint64() | ||
inv.Memory = mem.Uint64() | ||
inv.StorageEphemeral = ephemeralStorage.Uint64() | ||
} |