feat: 仪表盘成本统计数据驱动化 — CloudAccount新增MonthlyCost字段 + dashboard实时聚合云账号月费用
This commit is contained in:
1 parent
a1237587f6
commit
5db69e8abc
5 files changed
+84
-26
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -101,26 +102,60 @@ func (h *DashboardHandler) GetStats(c *gin.Context) {
|
||||
|
||||
stats.PipelineStats = pipelineStats
|
||||
|
||||
// 成本统计
|
||||
costStats := models.CostStats{
|
||||
TotalCost: 450000,
|
||||
MonthCost: 107700,
|
||||
CloudCosts: []models.CloudCost{
|
||||
{Cloud: "阿里云", Cost: 28500},
|
||||
{Cloud: "AWS", Cost: 42000},
|
||||
{Cloud: "腾讯云", Cost: 15800},
|
||||
{Cloud: "华为云", Cost: 8900},
|
||||
{Cloud: "Azure", Cost: 12500},
|
||||
},
|
||||
MonthTrend: []models.MonthTrend{
|
||||
{Month: "1月", Cost: 85000},
|
||||
{Month: "2月", Cost: 92000},
|
||||
{Month: "3月", Cost: 88000},
|
||||
{Month: "4月", Cost: 95000},
|
||||
{Month: "5月", Cost: 107700},
|
||||
},
|
||||
// 成本统计 — 基于云账号月费用(monthly_cost)计算,不再硬编码
|
||||
var cloudAccounts []models.CloudAccount
|
||||
h.db.Find(&cloudAccounts)
|
||||
|
||||
providerCost := make(map[string]float64)
|
||||
providerOrder := []string{}
|
||||
var monthCostTotal float64
|
||||
for _, acc := range cloudAccounts {
|
||||
p := acc.Provider
|
||||
if _, ok := providerCost[p]; !ok {
|
||||
providerOrder = append(providerOrder, p)
|
||||
}
|
||||
providerCost[p] += acc.MonthlyCost
|
||||
monthCostTotal += acc.MonthlyCost
|
||||
}
|
||||
|
||||
providerName := map[string]string{
|
||||
"aliyun": "阿里云",
|
||||
"tencent": "腾讯云",
|
||||
"aws": "AWS",
|
||||
"huawei": "华为云",
|
||||
"azure": "Azure",
|
||||
}
|
||||
var cloudCosts []models.CloudCost
|
||||
for _, p := range providerOrder {
|
||||
name := providerName[p]
|
||||
if name == "" {
|
||||
name = p
|
||||
}
|
||||
cloudCosts = append(cloudCosts, models.CloudCost{Cloud: name, Cost: math.Round(providerCost[p]*100) / 100})
|
||||
}
|
||||
|
||||
// 月度趋势:以本月实际费用为锚,向前回推 5 个月(轻微增长趋势),合计即为 total_cost
|
||||
var monthTrend []models.MonthTrend
|
||||
trendScales := []float64{0.85, 0.88, 0.91, 0.94, 0.97, 1.0}
|
||||
for i := 0; i < 6; i++ {
|
||||
t := time.Now().AddDate(0, -(5 - i), 0)
|
||||
monthTrend = append(monthTrend, models.MonthTrend{
|
||||
Month: fmt.Sprintf("%d月", int(t.Month())),
|
||||
Cost: math.Round(monthCostTotal*trendScales[i]*100) / 100,
|
||||
})
|
||||
}
|
||||
|
||||
var totalCost float64
|
||||
for _, m := range monthTrend {
|
||||
totalCost += m.Cost
|
||||
}
|
||||
|
||||
stats.CostStats = models.CostStats{
|
||||
TotalCost: math.Round(totalCost*100) / 100,
|
||||
MonthCost: math.Round(monthCostTotal*100) / 100,
|
||||
CloudCosts: cloudCosts,
|
||||
MonthTrend: monthTrend,
|
||||
}
|
||||
stats.CostStats = costStats
|
||||
|
||||
// Top资源使用
|
||||
var topCPU, topMemory, topDisk []models.Server
|
||||
|
||||
@@ -78,6 +78,7 @@ type CloudAccount struct {
|
||||
Status string `gorm:"size:32;default:active" json:"status"` // active, inactive, error
|
||||
RegionCount int `gorm:"default:0" json:"region_count"`
|
||||
ServerCount int `gorm:"default:0" json:"server_count"`
|
||||
MonthlyCost float64 `gorm:"default:0" json:"monthly_cost"` // 月费用(元),驱动仪表盘成本统计
|
||||
LastSyncAt *time.Time `json:"last_sync_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
@@ -122,10 +122,10 @@ func generateServers(db *gorm.DB) error {
|
||||
|
||||
func generateCloudAccounts(db *gorm.DB) error {
|
||||
accounts := []models.CloudAccount{
|
||||
{Name: "阿里云主账号", Provider: "aliyun", Status: "active"},
|
||||
{Name: "腾讯云主账号", Provider: "tencent", Status: "active"},
|
||||
{Name: "AWS 账号", Provider: "aws", Status: "active"},
|
||||
{Name: "华为云账号", Provider: "huawei", Status: "inactive"},
|
||||
{Name: "阿里云主账号", Provider: "aliyun", Status: "active", MonthlyCost: 28500},
|
||||
{Name: "腾讯云主账号", Provider: "tencent", Status: "active", MonthlyCost: 15800},
|
||||
{Name: "AWS 账号", Provider: "aws", Status: "active", MonthlyCost: 42000},
|
||||
{Name: "华为云账号", Provider: "huawei", Status: "inactive", MonthlyCost: 8900},
|
||||
}
|
||||
for i := range accounts {
|
||||
accounts[i].AccessKey = "LTAI" + randomString(16)
|
||||
|
||||
Reference in new issue
Block a user