阶段性提交

This commit is contained in:
刘祥超
2021-06-27 21:59:06 +08:00
parent bd34a9bd37
commit 963be2bc63
42 changed files with 956 additions and 139 deletions

View File

@@ -99,6 +99,8 @@ func (this *BindHTTPFirewallPopupAction) RunPost(params struct {
Must *actions.Must
}) {
defer this.CreateLogInfo("绑定IP名单 %d 到WAF策略 %d", params.ListId, params.HttpFirewallPolicyId)
// List类型
listResp, err := this.RPC().IPListRPC().FindEnabledIPList(this.AdminContext(), &pb.FindEnabledIPListRequest{IpListId: params.ListId})
if err != nil {

View File

@@ -18,6 +18,8 @@ func (this *UnbindHTTPFirewallAction) RunPost(params struct {
HttpFirewallPolicyId int64
ListId int64
}) {
defer this.CreateLogInfo("接触绑定IP名单 %d WAF策略 %d", params.ListId, params.HttpFirewallPolicyId)
// List类型
listResp, err := this.RPC().IPListRPC().FindEnabledIPList(this.AdminContext(), &pb.FindEnabledIPListRequest{IpListId: params.ListId})
if err != nil {

View File

@@ -0,0 +1,25 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package metrics
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
type ChartsAction struct {
actionutils.ParentAction
}
func (this *ChartsAction) Init() {
this.Nav("", "", "chart")
}
func (this *ChartsAction) RunGet(params struct {
ItemId int64
}) {
err := InitItem(this.Parent(), params.ItemId)
if err != nil {
this.ErrorPage(err)
return
}
this.Show()
}

View File

@@ -0,0 +1,83 @@
// 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/rpc/pb"
"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.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
Name string
Category string
KeysJSON []byte
PeriodJSON []byte
Value string
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")
createResp, err := this.RPC().MetricItemRPC().CreateMetricItem(this.AdminContext(), &pb.CreateMetricItemRequest{
Code: "", // TODO 未来实现
Category: params.Category,
Name: params.Name,
Keys: keys,
Period: period,
PeriodUnit: periodUnit,
Value: params.Value,
})
if err != nil {
this.ErrorPage(err)
return
}
defer this.CreateLogInfo("创建统计指标 %d", createResp.MetricItemId)
this.Success()
}

View File

@@ -0,0 +1,26 @@
// 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"
)
type DeleteAction struct {
actionutils.ParentAction
}
func (this *DeleteAction) RunPost(params struct {
ItemId int64
}) {
defer this.CreateLogInfo("删除统计指标")
_, err := this.RPC().MetricItemRPC().DeleteMetricItem(this.AdminContext(), &pb.DeleteMetricItemRequest{MetricItemId: params.ItemId})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,61 @@
// 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/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,
"isOn": item.IsOn,
"period": item.Period,
"periodUnit": item.PeriodUnit,
"keys": item.Keys,
"value": item.Value,
"category": item.Category,
})
}
this.Data["items"] = itemMaps
this.Show()
}

View File

@@ -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("/charts", new(ChartsAction)).
EndAll()
})
}

View File

@@ -0,0 +1,25 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package metrics
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
type ItemAction struct {
actionutils.ParentAction
}
func (this *ItemAction) Init() {
this.Nav("", "", "item")
}
func (this *ItemAction) RunGet(params struct {
ItemId int64
}) {
err := InitItem(this.Parent(), params.ItemId)
if err != nil {
this.ErrorPage(err)
return
}
this.Show()
}

View File

@@ -2,16 +2,83 @@
package metrics
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type UpdateAction struct {
actionutils.ParentAction
}
func (this *UpdateAction) Init() {
this.Nav("", "", "")
this.Nav("", "", "update")
}
func (this *UpdateAction) RunGet(params struct{}) {
func (this *UpdateAction) RunGet(params struct {
ItemId int64
}) {
err := InitItem(this.Parent(), params.ItemId)
if err != nil {
this.ErrorPage(err)
return
}
this.Show()
}
func (this *UpdateAction) RunPost(params struct {
ItemId int64
Name string
KeysJSON []byte
PeriodJSON []byte
Value string
IsOn 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")
_, 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,
})
if err != nil {
this.ErrorPage(err)
return
}
defer this.CreateLogInfo("修改统计指标 %d", params.ItemId)
this.Success()
}

View File

@@ -0,0 +1,37 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package metrics
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/iwind/TeaGo/maps"
)
func InitItem(parent *actionutils.ParentAction, itemId int64) error {
client, err := rpc.SharedRPC()
if err != nil {
return err
}
resp, err := client.MetricItemRPC().FindEnabledMetricItem(parent.AdminContext(), &pb.FindEnabledMetricItemRequest{MetricItemId: itemId})
if err != nil {
return err
}
var item = resp.MetricItem
if item == nil {
return errors.New("not found")
}
parent.Data["item"] = maps.Map{
"id": item.Id,
"name": item.Name,
"isOn": item.IsOn,
"keys": item.Keys,
"value": item.Value,
"period": item.Period,
"periodUnit": item.PeriodUnit,
"category": item.Category,
}
return nil
}