实现基本的图表管理

This commit is contained in:
刘祥超
2021-07-03 15:44:49 +08:00
parent 534f10b8a7
commit f297f4ec52
42 changed files with 1010 additions and 99 deletions

View File

@@ -0,0 +1,35 @@
// 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"
)
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
}
this.Show()
}

View File

@@ -0,0 +1,38 @@
// 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")
}
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.FindAllMetricChartTypeName(chart.Type),
}
return chart, nil
}

View File

@@ -0,0 +1,65 @@
// 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/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
Must *actions.Must
CSRF *actionutils.CSRF
}) {
var chartId int64
defer func() {
this.CreateLogInfo("创建指标图表 %d", 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,
})
if err != nil {
this.ErrorPage(err)
return
}
chartId = createResp.MetricChartId
this.Success()
}

View File

@@ -0,0 +1,26 @@
// 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/rpc/pb"
)
type DeleteAction struct {
actionutils.ParentAction
}
func (this *DeleteAction) RunPost(params struct {
ChartId int64
}) {
defer this.CreateLogInfo("删除指标图表 %d", params.ChartId)
_, err := this.RPC().MetricChartRPC().DeleteMetricChart(this.AdminContext(), &pb.DeleteMetricChartRequest{MetricChartId: params.ChartId})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -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.FindAllMetricChartTypeName(chart.Type),
"isOn": chart.IsOn,
"widthDiv": chart.WidthDiv,
})
}
this.Data["charts"] = chartMaps
this.Show()
}

View File

@@ -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()
})
}

View File

@@ -0,0 +1,76 @@
// 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/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
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo("修改指标图表 %d", 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,
IsOn: params.IsOn,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}