Compare commits

...

4 Commits

Author SHA1 Message Date
hysyeah
4626f3bf9f bfl: myapps api add rawAppName field (#2431) 2026-01-21 17:35:25 +08:00
hys
6d45890365 fix: myapps api add rawAppName field 2026-01-21 17:23:13 +08:00
dkeven
c82262e0c9 chore(bfl): update image version to v0.4.38 2026-01-08 20:33:54 +08:00
dkeven
c74c4b6da4 fix(bfl): add backend cluster API handler for stats report (#2381) 2026-01-08 20:14:50 +08:00
5 changed files with 77 additions and 1 deletions

View File

@@ -266,7 +266,7 @@ spec:
containers:
- name: api
image: beclab/bfl:v0.4.37
image: beclab/bfl:v0.4.38
imagePullPolicy: IfNotPresent
securityContext:
runAsUser: 1000

View File

@@ -2,6 +2,7 @@ package v1
import (
"fmt"
"math"
"strconv"
"time"
@@ -9,7 +10,9 @@ import (
"bytetrade.io/web3os/bfl/pkg/api"
"bytetrade.io/web3os/bfl/pkg/api/response"
"bytetrade.io/web3os/bfl/pkg/apis"
"bytetrade.io/web3os/bfl/pkg/apis/backend/v1/metrics"
"bytetrade.io/web3os/bfl/pkg/apis/iam/v1alpha1/operator"
monitov1alpha1 "bytetrade.io/web3os/bfl/pkg/apis/monitor/v1alpha1"
"bytetrade.io/web3os/bfl/pkg/apiserver/runtime"
"bytetrade.io/web3os/bfl/pkg/app_service/v1"
"bytetrade.io/web3os/bfl/pkg/client/clientset/v1alpha1"
@@ -371,3 +374,68 @@ func (h *Handler) myapps(req *restful.Request, resp *restful.Response) {
response.Success(resp, api.NewListResult(list))
}
func (h *Handler) getClusterMetric(req *restful.Request, resp *restful.Response) {
prome, err := metrics.NewPrometheus(metrics.PrometheusEndpoint)
if err != nil {
response.HandleError(resp, err)
return
}
opts := metrics.QueryOptions{
Level: metrics.LevelCluster,
}
metricsResult := prome.GetNamedMetrics(req.Request.Context(), []string{
"cluster_cpu_usage",
"cluster_cpu_total",
"cluster_disk_size_usage",
"cluster_disk_size_capacity",
"cluster_memory_total",
"cluster_memory_usage_wo_cache",
"cluster_net_bytes_transmitted",
"cluster_net_bytes_received",
}, time.Now(), opts)
var clusterMetrics monitov1alpha1.ClusterMetrics
for _, m := range metricsResult {
switch m.MetricName {
case "cluster_cpu_usage":
clusterMetrics.CPU.Usage = metrics.GetValue(&m)
case "cluster_cpu_total":
clusterMetrics.CPU.Total = metrics.GetValue(&m)
case "cluster_disk_size_usage":
clusterMetrics.Disk.Usage = metrics.GetValue(&m)
case "cluster_disk_size_capacity":
clusterMetrics.Disk.Total = metrics.GetValue(&m)
case "cluster_memory_total":
clusterMetrics.Memory.Total = metrics.GetValue(&m)
case "cluster_memory_usage_wo_cache":
clusterMetrics.Memory.Usage = metrics.GetValue(&m)
case "cluster_net_bytes_transmitted":
clusterMetrics.Net.Transmitted = metrics.GetValue(&m)
case "cluster_net_bytes_received":
clusterMetrics.Net.Received = metrics.GetValue(&m)
}
}
roundToGB := func(v float64) float64 { return math.Round((v/1000000000.00)*100.00) / 100.00 }
fmtMetricsValue(&clusterMetrics.CPU, "Cores", func(v float64) float64 { return v })
fmtMetricsValue(&clusterMetrics.Memory, "GB", roundToGB)
fmtMetricsValue(&clusterMetrics.Disk, "GB", roundToGB)
response.Success(resp, clusterMetrics)
}
func fmtMetricsValue(v *monitov1alpha1.MetricV, unit string, unitFunc func(float64) float64) {
v.Unit = unit
v.Usage = unitFunc(v.Usage)
v.Total = unitFunc(v.Total)
v.Ratio = math.Round((v.Usage / v.Total) * 100)
}

View File

@@ -52,6 +52,12 @@ func AddContainer(c *restful.Container) error {
Metadata(restfulspec.KeyOpenAPITags, tags).
Returns(http.StatusOK, "", response.Response{}))
ws.Route(ws.GET("/cluster").
To(handler.getClusterMetric).
Doc("get the cluster current metrics ( cpu, memory, disk ).").
Metadata(restfulspec.KeyOpenAPITags, tags).
Returns(http.StatusOK, "", response.Response{}))
ws.Route(ws.GET("/config-system").
To(handler.HandleGetSysConfig).
Doc("get user locale.").

View File

@@ -240,6 +240,7 @@ func (c *Client) getAppListFromData(apps []map[string]interface{}) ([]*AppInfo,
res = append(res, &AppInfo{
ID: genAppID(appSpec),
Name: stringOrEmpty(appSpec["name"]),
RawAppName: stringOrEmpty(appSpec["rawAppName"]),
Namespace: stringOrEmpty(appSpec["namespace"]),
DeploymentName: stringOrEmpty(appSpec["deployment"]),
Owner: stringOrEmpty(appSpec["owner"]),

View File

@@ -11,6 +11,7 @@ import (
type AppInfo struct {
ID string `json:"id"`
Name string `json:"name"`
RawAppName string `json:"rawAppName"`
Namespace string `json:"namespace"`
DeploymentName string `json:"deployment"`
Owner string `json:"owner"`