63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package webui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
)
|
|
|
|
func enrichSnapshotForViewer(snapshot []byte) []byte {
|
|
if len(snapshot) == 0 {
|
|
return snapshot
|
|
}
|
|
var root map[string]any
|
|
if err := json.Unmarshal(snapshot, &root); err != nil {
|
|
return snapshot
|
|
}
|
|
hardware, _ := root["hardware"].(map[string]any)
|
|
if len(hardware) == 0 {
|
|
return snapshot
|
|
}
|
|
storage, _ := hardware["storage"].([]any)
|
|
if len(storage) == 0 {
|
|
return snapshot
|
|
}
|
|
changed := false
|
|
for _, item := range storage {
|
|
row, _ := item.(map[string]any)
|
|
if len(row) == 0 {
|
|
continue
|
|
}
|
|
if _, exists := row["block_format"]; exists {
|
|
continue
|
|
}
|
|
logical, okLogical := jsonNumberToInt64(row["logical_block_size_bytes"])
|
|
metadata, okMetadata := jsonNumberToInt64(row["metadata_bytes_per_block"])
|
|
if !okLogical || !okMetadata || logical <= 0 || metadata < 0 {
|
|
continue
|
|
}
|
|
row["block_format"] = strconv.FormatInt(logical, 10) + "+" + strconv.FormatInt(metadata, 10)
|
|
changed = true
|
|
}
|
|
if !changed {
|
|
return snapshot
|
|
}
|
|
out, err := json.Marshal(root)
|
|
if err != nil {
|
|
return snapshot
|
|
}
|
|
return out
|
|
}
|
|
|
|
func jsonNumberToInt64(v any) (int64, bool) {
|
|
switch x := v.(type) {
|
|
case float64:
|
|
return int64(x), true
|
|
case int64:
|
|
return x, true
|
|
case int:
|
|
return int64(x), true
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|