This commit is contained in:
unknown
2026-02-04 20:27:13 +08:00
commit 3b042d1dad
9410 changed files with 1488147 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
package db
import (
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/db/dbnodeutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type CleanAction struct {
actionutils.ParentAction
}
func (this *CleanAction) Init() {
this.Nav("", "", "clean")
}
func (this *CleanAction) RunGet(params struct {
NodeId int64
}) {
_, err := dbnodeutils.InitNode(this.Parent(), params.NodeId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["nodeId"] = params.NodeId
this.Show()
}
func (this *CleanAction) RunPost(params struct {
NodeId int64
Must *actions.Must
}) {
tablesResp, err := this.RPC().DBNodeRPC().FindAllDBNodeTables(this.AdminContext(), &pb.FindAllDBNodeTablesRequest{
DbNodeId: params.NodeId,
})
if err != nil {
this.Fail("查询数据时出错了:" + err.Error())
}
tableMaps := []maps.Map{}
for _, table := range tablesResp.DbNodeTables {
if !table.IsBaseTable || (!table.CanClean && !table.CanDelete) {
continue
}
tableMaps = append(tableMaps, maps.Map{
"name": table.Name,
"rows": table.Rows,
"size": numberutils.FormatBytes(table.DataLength + table.IndexLength),
"canDelete": table.CanDelete,
"canClean": table.CanClean,
"comment": table.Comment,
})
}
this.Data["tables"] = tableMaps
this.Success()
}

View File

@@ -0,0 +1,68 @@
package db
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/iwind/TeaGo/actions"
)
type CreatePopupAction struct {
actionutils.ParentAction
}
func (this *CreatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreatePopupAction) RunGet(params struct{}) {
this.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
Name string
Host string
Port int32
Database string
Username string
Password string
Description string
IsOn bool
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入节点名称").
Field("host", params.Host).
Require("请输入主机地址").
Field("port", params.Port).
Gt(0, "请输入正确的数据库端口").
Lt(65535, "请输入正确的数据库端口").
Field("database", params.Database).
Require("请输入数据库名称").
Field("username", params.Username).
Require("请输入连接数据库的用户名")
createResp, err := this.RPC().DBNodeRPC().CreateDBNode(this.AdminContext(), &pb.CreateDBNodeRequest{
IsOn: params.IsOn,
Name: params.Name,
Description: params.Description,
Host: params.Host,
Port: params.Port,
Database: params.Database,
Username: params.Username,
Password: params.Password,
Charset: "", // 暂时不能修改
})
if err != nil {
this.ErrorPage(err)
return
}
// 创建日志
defer this.CreateLogInfo(codes.DBNode_LogCreateDBNode, createResp.DbNodeId)
this.Success()
}

View File

@@ -0,0 +1,34 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package dbnodeutils
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"
"github.com/iwind/TeaGo/types"
)
// InitNode 初始化指标信息
func InitNode(parent *actionutils.ParentAction, nodeId int64) (*pb.DBNode, error) {
client, err := rpc.SharedRPC()
if err != nil {
return nil, err
}
resp, err := client.DBNodeRPC().FindEnabledDBNode(parent.AdminContext(), &pb.FindEnabledDBNodeRequest{DbNodeId: nodeId})
if err != nil {
return nil, err
}
var node = resp.DbNode
if node == nil {
return nil, errors.New("not found db node with id '" + types.String(nodeId) + "'")
}
parent.Data["node"] = maps.Map{
"id": node.Id,
"name": node.Name,
}
return node, nil
}

View File

@@ -0,0 +1,26 @@
package db
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 {
NodeId int64
}) {
// 创建日志
defer this.CreateLogInfo(codes.DBNode_LogDeleteDBNode, params.NodeId)
_, err := this.RPC().DBNodeRPC().DeleteDBNode(this.AdminContext(), &pb.DeleteDBNodeRequest{DbNodeId: params.NodeId})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,28 @@
package db
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type DeleteTableAction struct {
actionutils.ParentAction
}
func (this *DeleteTableAction) RunPost(params struct {
NodeId int64
Table string
}) {
defer this.CreateLogInfo(codes.DBNode_LogDeleteTable, params.NodeId, params.Table)
_, err := this.RPC().DBNodeRPC().DeleteDBNodeTable(this.AdminContext(), &pb.DeleteDBNodeTableRequest{
DbNodeId: params.NodeId,
DbNodeTable: params.Table,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,27 @@
package db
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/iwind/TeaGo/actions"
"net/http"
)
type Helper struct {
helpers.LangHelper
}
func (this *Helper) BeforeAction(action *actions.ActionObject) {
if action.Request.Method != http.MethodGet {
return
}
action.Data["teaMenu"] = "db"
var selectedTabbar = action.Data["mainTab"]
var tabbar = actionutils.NewTabbar()
tabbar.Add(this.Lang(action, codes.DBNode_TabNodes), "", "/db", "", selectedTabbar == "db")
actionutils.SetTabbar(action, tabbar)
}

View File

@@ -0,0 +1,57 @@
package db
import (
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
"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("db", "db", "")
}
func (this *IndexAction) RunGet(params struct{}) {
countResp, err := this.RPC().DBNodeRPC().CountAllEnabledDBNodes(this.AdminContext(), &pb.CountAllEnabledDBNodesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
count := countResp.Count
page := this.NewPage(count)
listResp, err := this.RPC().DBNodeRPC().ListEnabledDBNodes(this.AdminContext(), &pb.ListEnabledDBNodesRequest{
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
nodeMaps := []maps.Map{}
for _, node := range listResp.DbNodes {
nodeMaps = append(nodeMaps, maps.Map{
"id": node.Id,
"isOn": node.IsOn,
"name": node.Name,
"host": node.Host,
"port": node.Port,
"database": node.Database,
"status": maps.Map{
"isOk": node.Status.IsOk,
"error": node.Status.Error,
"size": numberutils.FormatBytes(node.Status.Size),
"version": node.Status.Version,
},
})
}
this.Data["nodes"] = nodeMaps
this.Data["page"] = page.AsHTML()
this.Show()
}

View File

@@ -0,0 +1,29 @@
package db
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/settingutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeSetting)).
Helper(new(Helper)).
Helper(settingutils.NewAdvancedHelper("dbNodes")).
Prefix("/db").
Get("", new(IndexAction)).
GetPost("/createPopup", new(CreatePopupAction)).
GetPost("/update", new(UpdateAction)).
Post("/delete", new(DeleteAction)).
GetPost("/clean", new(CleanAction)).
Post("/deleteTable", new(DeleteTableAction)).
Post("/truncateTable", new(TruncateTableAction)).
Get("/node", new(NodeAction)).
Get("/logs", new(LogsAction)).
Post("/status", new(StatusAction)).
EndAll()
})
}

View File

@@ -0,0 +1,86 @@
package db
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/db/dbnodeutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
)
type LogsAction struct {
actionutils.ParentAction
}
func (this *LogsAction) Init() {
this.Nav("", "", "log")
}
func (this *LogsAction) RunGet(params struct {
NodeId int64
DayFrom string
DayTo string
Keyword string
Level string
}) {
_, err := dbnodeutils.InitNode(this.Parent(), params.NodeId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["nodeId"] = params.NodeId
this.Data["dayFrom"] = params.DayFrom
this.Data["dayTo"] = params.DayTo
this.Data["keyword"] = params.Keyword
this.Data["level"] = params.Level
countResp, err := this.RPC().NodeLogRPC().CountNodeLogs(this.AdminContext(), &pb.CountNodeLogsRequest{
Role: nodeconfigs.NodeRoleDatabase,
NodeId: params.NodeId,
DayFrom: params.DayFrom,
DayTo: params.DayTo,
Keyword: params.Keyword,
Level: params.Level,
})
if err != nil {
this.ErrorPage(err)
return
}
count := countResp.Count
page := this.NewPage(count, 20)
logsResp, err := this.RPC().NodeLogRPC().ListNodeLogs(this.AdminContext(), &pb.ListNodeLogsRequest{
NodeId: params.NodeId,
Role: nodeconfigs.NodeRoleDatabase,
DayFrom: params.DayFrom,
DayTo: params.DayTo,
Keyword: params.Keyword,
Level: params.Level,
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
logs := []maps.Map{}
for _, log := range logsResp.NodeLogs {
logs = append(logs, maps.Map{
"tag": log.Tag,
"description": log.Description,
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt),
"level": log.Level,
"isToday": timeutil.FormatTime("Y-m-d", log.CreatedAt) == timeutil.Format("Y-m-d"),
})
}
this.Data["logs"] = logs
this.Data["page"] = page.AsHTML()
this.Show()
}

View File

@@ -0,0 +1,42 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package db
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/db/dbnodeutils"
"github.com/iwind/TeaGo/maps"
"strings"
)
type NodeAction struct {
actionutils.ParentAction
}
func (this *NodeAction) Init() {
this.Nav("", "", "node")
}
func (this *NodeAction) RunGet(params struct {
NodeId int64
}) {
node, err := dbnodeutils.InitNode(this.Parent(), params.NodeId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["node"] = maps.Map{
"id": node.Id,
"isOn": node.IsOn,
"name": node.Name,
"database": node.Database,
"host": node.Host,
"port": node.Port,
"username": node.Username,
"password": strings.Repeat("*", len(node.Password)),
"description": node.Description,
}
this.Show()
}

View File

@@ -0,0 +1,38 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package db
import (
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
)
type StatusAction struct {
actionutils.ParentAction
}
func (this *StatusAction) RunPost(params struct {
NodeId int64
}) {
statusResp, err := this.RPC().DBNodeRPC().CheckDBNodeStatus(this.AdminContext(), &pb.CheckDBNodeStatusRequest{DbNodeId: params.NodeId})
if err != nil {
this.ErrorPage(err)
return
}
var status = statusResp.DbNodeStatus
if status != nil {
this.Data["status"] = maps.Map{
"isOk": status.IsOk,
"error": status.Error,
"size": numberutils.FormatBytes(status.Size),
"version": status.Version,
}
} else {
this.Data["status"] = nil
}
this.Success()
}

View File

@@ -0,0 +1,28 @@
package db
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type TruncateTableAction struct {
actionutils.ParentAction
}
func (this *TruncateTableAction) RunPost(params struct {
NodeId int64
Table string
}) {
defer this.CreateLogInfo(codes.DBNode_LogTruncateTable, params.NodeId, params.Table)
_, err := this.RPC().DBNodeRPC().TruncateDBNodeTable(this.AdminContext(), &pb.TruncateDBNodeTableRequest{
DbNodeId: params.NodeId,
DbNodeTable: params.Table,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,98 @@
package db
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/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 {
NodeId int64
}) {
nodeResp, err := this.RPC().DBNodeRPC().FindEnabledDBNode(this.AdminContext(), &pb.FindEnabledDBNodeRequest{DbNodeId: params.NodeId})
if err != nil {
this.ErrorPage(err)
return
}
node := nodeResp.DbNode
if node == nil {
this.NotFound("dbNode", params.NodeId)
return
}
this.Data["node"] = maps.Map{
"id": node.Id,
"isOn": node.IsOn,
"name": node.Name,
"description": node.Description,
"host": node.Host,
"port": node.Port,
"username": node.Username,
"password": node.Password,
"database": node.Database,
}
this.Show()
}
func (this *UpdateAction) RunPost(params struct {
NodeId int64
Name string
Host string
Port int32
Database string
Username string
Password string
Description string
IsOn bool
Must *actions.Must
}) {
// 创建日志
defer this.CreateLogInfo(codes.DBNode_LogUpdateDBNode, params.NodeId)
params.Must.
Field("name", params.Name).
Require("请输入节点名称").
Field("host", params.Host).
Require("请输入主机地址").
Field("port", params.Port).
Gt(0, "请输入正确的数据库端口").
Lt(65535, "请输入正确的数据库端口").
Field("database", params.Database).
Require("请输入数据库名称").
Field("username", params.Username).
Require("请输入连接数据库的用户名")
_, err := this.RPC().DBNodeRPC().UpdateDBNode(this.AdminContext(), &pb.UpdateDBNodeRequest{
DbNodeId: params.NodeId,
IsOn: params.IsOn,
Name: params.Name,
Description: params.Description,
Host: params.Host,
Port: params.Port,
Database: params.Database,
Username: params.Username,
Password: params.Password,
Charset: "", // 暂时不能修改
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}