Initial commit (code only without large binaries)
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package charts
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/metrics/charts/chartutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/metrics/metricutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type ChartAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *ChartAction) Init() {
|
||||
this.Nav("", "", "chart,chartIndex")
|
||||
}
|
||||
|
||||
func (this *ChartAction) RunGet(params struct {
|
||||
ChartId int64
|
||||
}) {
|
||||
chart, err := chartutils.InitChart(this.Parent(), params.ChartId)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = metricutils.InitItem(this.Parent(), chart.MetricItem.Id)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
var itemMap = this.Data["item"].(maps.Map)
|
||||
itemMap["valueTypeName"] = serverconfigs.FindMetricValueName(itemMap.GetString("category"), itemMap.GetString("value"))
|
||||
|
||||
this.Show()
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package chartutils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
// InitChart 初始化指标图表信息
|
||||
func InitChart(parent *actionutils.ParentAction, chartId int64) (*pb.MetricChart, error) {
|
||||
client, err := rpc.SharedRPC()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := client.MetricChartRPC().FindEnabledMetricChart(parent.AdminContext(), &pb.FindEnabledMetricChartRequest{MetricChartId: chartId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var chart = resp.MetricChart
|
||||
if chart == nil {
|
||||
return nil, errors.New("metric chart not found")
|
||||
}
|
||||
if len(chart.IgnoredKeys) == 0 {
|
||||
chart.IgnoredKeys = []string{}
|
||||
}
|
||||
parent.Data["chart"] = maps.Map{
|
||||
"id": chart.Id,
|
||||
"name": chart.Name,
|
||||
"isOn": chart.IsOn,
|
||||
"widthDiv": chart.WidthDiv,
|
||||
"maxItems": chart.MaxItems,
|
||||
"type": chart.Type,
|
||||
"typeName": serverconfigs.FindMetricChartTypeName(chart.Type),
|
||||
"ignoreEmptyKeys": chart.IgnoreEmptyKeys,
|
||||
"ignoredKeys": chart.IgnoredKeys,
|
||||
}
|
||||
return chart, nil
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package charts
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
)
|
||||
|
||||
type CreatePopupAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *CreatePopupAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *CreatePopupAction) RunGet(params struct {
|
||||
ItemId int64
|
||||
}) {
|
||||
this.Data["itemId"] = params.ItemId
|
||||
this.Data["types"] = serverconfigs.FindAllMetricChartTypes()
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *CreatePopupAction) RunPost(params struct {
|
||||
ItemId int64
|
||||
Name string
|
||||
Type string
|
||||
WidthDiv int32
|
||||
MaxItems int32
|
||||
IgnoreEmptyKeys bool
|
||||
IgnoredKeys []string
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
var chartId int64
|
||||
defer func() {
|
||||
this.CreateLogInfo(codes.MetricChart_LogCreateMetricChart, chartId)
|
||||
}()
|
||||
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入图表名称").
|
||||
Field("type", params.Type).
|
||||
Require("请选择图表类型")
|
||||
|
||||
createResp, err := this.RPC().MetricChartRPC().CreateMetricChart(this.AdminContext(), &pb.CreateMetricChartRequest{
|
||||
MetricItemId: params.ItemId,
|
||||
Name: params.Name,
|
||||
Type: params.Type,
|
||||
WidthDiv: params.WidthDiv,
|
||||
MaxItems: params.MaxItems,
|
||||
ParamsJSON: nil,
|
||||
IgnoreEmptyKeys: params.IgnoreEmptyKeys,
|
||||
IgnoredKeys: params.IgnoredKeys,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
chartId = createResp.MetricChartId
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package charts
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
type DeleteAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *DeleteAction) RunPost(params struct {
|
||||
ChartId int64
|
||||
}) {
|
||||
defer this.CreateLogInfo(codes.MetricChart_LogDeleteMetricChart, params.ChartId)
|
||||
|
||||
_, err := this.RPC().MetricChartRPC().DeleteMetricChart(this.AdminContext(), &pb.DeleteMetricChartRequest{MetricChartId: params.ChartId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package charts
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/metrics/metricutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "", "chart")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct {
|
||||
ItemId int64
|
||||
}) {
|
||||
_, err := metricutils.InitItem(this.Parent(), params.ItemId)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
countResp, err := this.RPC().MetricChartRPC().CountEnabledMetricCharts(this.AdminContext(), &pb.CountEnabledMetricChartsRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var count = countResp.Count
|
||||
page := this.NewPage(count)
|
||||
this.Data["page"] = page.AsHTML()
|
||||
|
||||
chartsResp, err := this.RPC().MetricChartRPC().ListEnabledMetricCharts(this.AdminContext(), &pb.ListEnabledMetricChartsRequest{
|
||||
MetricItemId: params.ItemId,
|
||||
Offset: page.Offset,
|
||||
Size: page.Size,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var charts = chartsResp.MetricCharts
|
||||
var chartMaps = []maps.Map{}
|
||||
for _, chart := range charts {
|
||||
chartMaps = append(chartMaps, maps.Map{
|
||||
"id": chart.Id,
|
||||
"name": chart.Name,
|
||||
"type": chart.Type,
|
||||
"typeName": serverconfigs.FindMetricChartTypeName(chart.Type),
|
||||
"isOn": chart.IsOn,
|
||||
"widthDiv": chart.WidthDiv,
|
||||
})
|
||||
}
|
||||
this.Data["charts"] = chartMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package charts
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
"github.com/iwind/TeaGo"
|
||||
)
|
||||
|
||||
func init() {
|
||||
TeaGo.BeforeStart(func(server *TeaGo.Server) {
|
||||
server.
|
||||
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
|
||||
Data("teaMenu", "servers").
|
||||
Data("teaSubMenu", "metric").
|
||||
Prefix("/servers/metrics/charts").
|
||||
Get("", new(IndexAction)).
|
||||
GetPost("/createPopup", new(CreatePopupAction)).
|
||||
GetPost("/update", new(UpdateAction)).
|
||||
Post("/delete", new(DeleteAction)).
|
||||
Get("/chart", new(ChartAction)).
|
||||
EndAll()
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package charts
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/metrics/charts/chartutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/metrics/metricutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
)
|
||||
|
||||
type UpdateAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *UpdateAction) Init() {
|
||||
this.Nav("", "", "chart,chartUpdate")
|
||||
}
|
||||
|
||||
func (this *UpdateAction) RunGet(params struct {
|
||||
ChartId int64
|
||||
}) {
|
||||
chart, err := chartutils.InitChart(this.Parent(), params.ChartId)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = metricutils.InitItem(this.Parent(), chart.MetricItem.Id)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Data["types"] = serverconfigs.FindAllMetricChartTypes()
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *UpdateAction) RunPost(params struct {
|
||||
ChartId int64
|
||||
Name string
|
||||
Type string
|
||||
WidthDiv int32
|
||||
MaxItems int32
|
||||
IsOn bool
|
||||
IgnoreEmptyKeys bool
|
||||
IgnoredKeys []string
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
defer this.CreateLogInfo(codes.MetricChart_LogUpdateMetricChart, params.ChartId)
|
||||
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入图表名称").
|
||||
Field("type", params.Type).
|
||||
Require("请选择图表类型")
|
||||
|
||||
_, err := this.RPC().MetricChartRPC().UpdateMetricChart(this.AdminContext(), &pb.UpdateMetricChartRequest{
|
||||
MetricChartId: params.ChartId,
|
||||
Name: params.Name,
|
||||
Type: params.Type,
|
||||
WidthDiv: params.WidthDiv,
|
||||
MaxItems: params.MaxItems,
|
||||
ParamsJSON: nil,
|
||||
IgnoreEmptyKeys: params.IgnoreEmptyKeys,
|
||||
IgnoredKeys: params.IgnoredKeys,
|
||||
IsOn: params.IsOn,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type CreatePopupAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *CreatePopupAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *CreatePopupAction) RunGet(params struct {
|
||||
Category string
|
||||
}) {
|
||||
this.Data["category"] = params.Category
|
||||
this.Data["valueDefinitions"] = serverconfigs.FindAllMetricValueDefinitions(params.Category)
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *CreatePopupAction) RunPost(params struct {
|
||||
Name string
|
||||
Category string
|
||||
KeysJSON []byte
|
||||
PeriodJSON []byte
|
||||
Value string
|
||||
IsPublic bool
|
||||
ExpiresPeriod int32
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入指标名称")
|
||||
|
||||
if len(params.Category) == 0 {
|
||||
this.Fail("请选择指标类型")
|
||||
}
|
||||
|
||||
// 统计对象
|
||||
if len(params.KeysJSON) == 0 {
|
||||
this.FailField("keys", "请选择指标统计的对象")
|
||||
}
|
||||
var keys = []string{}
|
||||
err := json.Unmarshal(params.KeysJSON, &keys)
|
||||
if err != nil {
|
||||
this.FailField("keys", "解析指标对象失败")
|
||||
}
|
||||
if len(keys) == 0 {
|
||||
this.FailField("keys", "请选择指标统计的对象")
|
||||
}
|
||||
|
||||
var periodMap = maps.Map{}
|
||||
err = json.Unmarshal(params.PeriodJSON, &periodMap)
|
||||
if err != nil {
|
||||
this.FailField("period", "解析统计周期失败")
|
||||
}
|
||||
var period = periodMap.GetInt32("period")
|
||||
var periodUnit = periodMap.GetString("unit")
|
||||
|
||||
if params.ExpiresPeriod < 0 {
|
||||
params.ExpiresPeriod = 0
|
||||
}
|
||||
|
||||
createResp, err := this.RPC().MetricItemRPC().CreateMetricItem(this.AdminContext(), &pb.CreateMetricItemRequest{
|
||||
Code: "", // TODO 未来实现
|
||||
Category: params.Category,
|
||||
Name: params.Name,
|
||||
Keys: keys,
|
||||
Period: period,
|
||||
PeriodUnit: periodUnit,
|
||||
ExpiresPeriod: params.ExpiresPeriod,
|
||||
Value: params.Value,
|
||||
IsPublic: params.IsPublic,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
defer this.CreateLogInfo(codes.MetricItem_LogCreateMetricItem, createResp.MetricItemId)
|
||||
this.Success()
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
type DeleteAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *DeleteAction) RunPost(params struct {
|
||||
ItemId int64
|
||||
}) {
|
||||
defer this.CreateLogInfo(codes.MetricItem_LogDeleteMetricItem)
|
||||
|
||||
_, err := this.RPC().MetricItemRPC().DeleteMetricItem(this.AdminContext(), &pb.DeleteMetricItemRequest{MetricItemId: params.ItemId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct {
|
||||
Category string
|
||||
}) {
|
||||
if len(params.Category) == 0 {
|
||||
params.Category = "http"
|
||||
}
|
||||
this.Data["category"] = params.Category
|
||||
|
||||
countResp, err := this.RPC().MetricItemRPC().CountAllEnabledMetricItems(this.AdminContext(), &pb.CountAllEnabledMetricItemsRequest{Category: params.Category})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var count = countResp.Count
|
||||
page := this.NewPage(count)
|
||||
this.Data["page"] = page.AsHTML()
|
||||
|
||||
itemsResp, err := this.RPC().MetricItemRPC().ListEnabledMetricItems(this.AdminContext(), &pb.ListEnabledMetricItemsRequest{
|
||||
Category: params.Category,
|
||||
Offset: page.Offset,
|
||||
Size: page.Size,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var itemMaps = []maps.Map{}
|
||||
for _, item := range itemsResp.MetricItems {
|
||||
itemMaps = append(itemMaps, maps.Map{
|
||||
"id": item.Id,
|
||||
"name": item.Name,
|
||||
"code": item.Code,
|
||||
"isOn": item.IsOn,
|
||||
"period": item.Period,
|
||||
"periodUnit": item.PeriodUnit,
|
||||
"periodUnitName": serverconfigs.FindMetricPeriodUnitName(item.PeriodUnit),
|
||||
"expiresPeriod": item.ExpiresPeriod,
|
||||
"keys": item.Keys,
|
||||
"value": item.Value,
|
||||
"valueName": serverconfigs.FindMetricValueName(item.Category, item.Value),
|
||||
"category": item.Category,
|
||||
"isPublic": item.IsPublic,
|
||||
})
|
||||
}
|
||||
this.Data["items"] = itemMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
"github.com/iwind/TeaGo"
|
||||
)
|
||||
|
||||
func init() {
|
||||
TeaGo.BeforeStart(func(server *TeaGo.Server) {
|
||||
server.
|
||||
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
|
||||
Data("teaMenu", "servers").
|
||||
Data("teaSubMenu", "metric").
|
||||
Prefix("/servers/metrics").
|
||||
Get("", new(IndexAction)).
|
||||
GetPost("/createPopup", new(CreatePopupAction)).
|
||||
GetPost("/update", new(UpdateAction)).
|
||||
Post("/delete", new(DeleteAction)).
|
||||
Get("/item", new(ItemAction)).
|
||||
Get("/stats", new(StatsAction)).
|
||||
EndAll()
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/metrics/metricutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type ItemAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *ItemAction) Init() {
|
||||
this.Nav("", "", "item")
|
||||
}
|
||||
|
||||
func (this *ItemAction) RunGet(params struct {
|
||||
ItemId int64
|
||||
}) {
|
||||
_, err := metricutils.InitItem(this.Parent(), params.ItemId)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 使用此指标的集群
|
||||
clustersResp, err := this.RPC().NodeClusterMetricItemRPC().FindAllNodeClustersWithMetricItemId(this.AdminContext(), &pb.FindAllNodeClustersWithMetricItemIdRequest{MetricItemId: params.ItemId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var clusterMaps = []maps.Map{}
|
||||
for _, cluster := range clustersResp.NodeClusters {
|
||||
clusterMaps = append(clusterMaps, maps.Map{
|
||||
"id": cluster.Id,
|
||||
"name": cluster.Name,
|
||||
})
|
||||
}
|
||||
this.Data["clusters"] = clusterMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package metricutils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
// InitItem 初始化指标信息
|
||||
func InitItem(parent *actionutils.ParentAction, itemId int64) (*pb.MetricItem, error) {
|
||||
client, err := rpc.SharedRPC()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := client.MetricItemRPC().FindEnabledMetricItem(parent.AdminContext(), &pb.FindEnabledMetricItemRequest{MetricItemId: itemId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var item = resp.MetricItem
|
||||
if item == nil {
|
||||
return nil, errors.New("metric item not found")
|
||||
}
|
||||
|
||||
countChartsResp, err := client.MetricChartRPC().CountEnabledMetricCharts(parent.AdminContext(), &pb.CountEnabledMetricChartsRequest{MetricItemId: item.Id})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var countCharts = countChartsResp.Count
|
||||
|
||||
parent.Data["item"] = maps.Map{
|
||||
"id": item.Id,
|
||||
"name": item.Name,
|
||||
"code": item.Code,
|
||||
"isOn": item.IsOn,
|
||||
"keys": item.Keys,
|
||||
"value": item.Value,
|
||||
"valueName": serverconfigs.FindMetricValueName(item.Category, item.Value),
|
||||
"period": item.Period,
|
||||
"periodUnit": item.PeriodUnit,
|
||||
"periodUnitName": serverconfigs.FindMetricPeriodUnitName(item.PeriodUnit),
|
||||
"category": item.Category,
|
||||
"isPublic": item.IsPublic,
|
||||
"countCharts": countCharts,
|
||||
"expiresPeriod": item.ExpiresPeriod,
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/metrics/metricutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type StatsAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *StatsAction) Init() {
|
||||
this.Nav("", "", "stat")
|
||||
}
|
||||
|
||||
func (this *StatsAction) RunGet(params struct {
|
||||
ItemId int64
|
||||
}) {
|
||||
item, err := metricutils.InitItem(this.Parent(), params.ItemId)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
countResp, err := this.RPC().MetricStatRPC().CountMetricStats(this.AdminContext(), &pb.CountMetricStatsRequest{
|
||||
MetricItemId: params.ItemId,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var count = countResp.Count
|
||||
|
||||
page := this.NewPage(count)
|
||||
this.Data["page"] = page.AsHTML()
|
||||
|
||||
statsResp, err := this.RPC().MetricStatRPC().ListMetricStats(this.AdminContext(), &pb.ListMetricStatsRequest{
|
||||
MetricItemId: params.ItemId,
|
||||
Offset: page.Offset,
|
||||
Size: page.Size,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var statMaps = []maps.Map{}
|
||||
for _, stat := range statsResp.MetricStats {
|
||||
// 占比
|
||||
var ratio float32
|
||||
if stat.SumTotal > 0 {
|
||||
ratio = stat.Value * 100 / stat.SumTotal
|
||||
}
|
||||
|
||||
statMaps = append(statMaps, maps.Map{
|
||||
"id": stat.Id,
|
||||
"time": serverconfigs.HumanMetricTime(item.PeriodUnit, stat.Time),
|
||||
"keys": stat.Keys,
|
||||
"value": stat.Value,
|
||||
"cluster": maps.Map{"id": stat.NodeCluster.Id, "name": stat.NodeCluster.Name},
|
||||
"node": maps.Map{"id": stat.Node.Id, "name": stat.Node.Name},
|
||||
"server": maps.Map{"id": stat.Server.Id, "name": stat.Server.Name},
|
||||
"ratio": fmt.Sprintf("%.2f", ratio),
|
||||
})
|
||||
}
|
||||
this.Data["stats"] = statMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/metrics/metricutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type UpdateAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *UpdateAction) Init() {
|
||||
this.Nav("", "", "update")
|
||||
}
|
||||
|
||||
func (this *UpdateAction) RunGet(params struct {
|
||||
ItemId int64
|
||||
}) {
|
||||
item, err := metricutils.InitItem(this.Parent(), params.ItemId)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Data["valueDefinitions"] = serverconfigs.FindAllMetricValueDefinitions(item.Category)
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *UpdateAction) RunPost(params struct {
|
||||
ItemId int64
|
||||
Name string
|
||||
KeysJSON []byte
|
||||
PeriodJSON []byte
|
||||
ExpiresPeriod int32
|
||||
Value string
|
||||
IsOn bool
|
||||
IsPublic bool
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入指标名称")
|
||||
|
||||
// 统计对象
|
||||
if len(params.KeysJSON) == 0 {
|
||||
this.FailField("keys", "请选择指标统计的对象")
|
||||
}
|
||||
var keys = []string{}
|
||||
err := json.Unmarshal(params.KeysJSON, &keys)
|
||||
if err != nil {
|
||||
this.FailField("keys", "解析指标对象失败")
|
||||
}
|
||||
if len(keys) == 0 {
|
||||
this.FailField("keys", "请选择指标统计的对象")
|
||||
}
|
||||
|
||||
var periodMap = maps.Map{}
|
||||
err = json.Unmarshal(params.PeriodJSON, &periodMap)
|
||||
if err != nil {
|
||||
this.FailField("period", "解析统计周期失败")
|
||||
}
|
||||
var period = periodMap.GetInt32("period")
|
||||
var periodUnit = periodMap.GetString("unit")
|
||||
|
||||
if params.ExpiresPeriod < 0 {
|
||||
params.ExpiresPeriod = 0
|
||||
}
|
||||
|
||||
_, err = this.RPC().MetricItemRPC().UpdateMetricItem(this.AdminContext(), &pb.UpdateMetricItemRequest{
|
||||
MetricItemId: params.ItemId,
|
||||
Name: params.Name,
|
||||
Keys: keys,
|
||||
Period: period,
|
||||
PeriodUnit: periodUnit,
|
||||
Value: params.Value,
|
||||
IsOn: params.IsOn,
|
||||
IsPublic: params.IsPublic,
|
||||
ExpiresPeriod: params.ExpiresPeriod,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
defer this.CreateLogInfo(codes.MetricItem_LogUpdateMetricItem, params.ItemId)
|
||||
this.Success()
|
||||
}
|
||||
Reference in New Issue
Block a user