From 600caad2f87992198aee535af673163205d4d17f Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Tue, 19 Jan 2021 16:14:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=9F=90=E4=BA=9B?= =?UTF-8?q?=E8=A1=A8=E5=8F=AF=E4=BB=A5=E6=89=8B=E5=B7=A5=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/rpc/rpc_client.go | 4 ++ internal/utils/numberutils/utils.go | 19 ++++++- .../default/settings/database/clean.go | 35 ++++++++++++- .../default/settings/database/deleteTable.go | 23 +++++++++ .../actions/default/settings/database/init.go | 2 + .../settings/database/truncateTable.go | 23 +++++++++ .../@default/settings/database/clean.html | 24 ++++++++- web/views/@default/settings/database/clean.js | 49 +++++++++++++++++++ 8 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 internal/web/actions/default/settings/database/deleteTable.go create mode 100644 internal/web/actions/default/settings/database/truncateTable.go create mode 100644 web/views/@default/settings/database/clean.js diff --git a/internal/rpc/rpc_client.go b/internal/rpc/rpc_client.go index 28884b9b..756e4262 100644 --- a/internal/rpc/rpc_client.go +++ b/internal/rpc/rpc_client.go @@ -107,6 +107,10 @@ func (this *RPCClient) DBNodeRPC() pb.DBNodeServiceClient { return pb.NewDBNodeServiceClient(this.pickConn()) } +func (this *RPCClient) DBRPC() pb.DBServiceClient { + return pb.NewDBServiceClient(this.pickConn()) +} + func (this *RPCClient) OriginRPC() pb.OriginServiceClient { return pb.NewOriginServiceClient(this.pickConn()) } diff --git a/internal/utils/numberutils/utils.go b/internal/utils/numberutils/utils.go index b7cd4b35..ca022a98 100644 --- a/internal/utils/numberutils/utils.go +++ b/internal/utils/numberutils/utils.go @@ -1,6 +1,9 @@ package numberutils -import "strconv" +import ( + "fmt" + "strconv" +) func FormatInt64(value int64) string { return strconv.FormatInt(value, 10) @@ -9,3 +12,17 @@ func FormatInt64(value int64) string { func FormatInt(value int) string { return strconv.Itoa(value) } + +func FormatBytes(bytes int64) string { + if bytes < 1024 { + return FormatInt64(bytes) + "B" + } else if bytes < 1024*1024 { + return fmt.Sprintf("%.2fK", float64(bytes)/1024) + } else if bytes < 1024*1024*1024 { + return fmt.Sprintf("%.2fM", float64(bytes)/1024/1024) + } else if bytes < 1024*1024*1024*1024 { + return fmt.Sprintf("%.2fG", float64(bytes)/1024/1024/1024) + } else { + return fmt.Sprintf("%.2fP", float64(bytes)/1024/1024/1024/1024) + } +} diff --git a/internal/web/actions/default/settings/database/clean.go b/internal/web/actions/default/settings/database/clean.go index 3bec1f33..f8cf05ec 100644 --- a/internal/web/actions/default/settings/database/clean.go +++ b/internal/web/actions/default/settings/database/clean.go @@ -1,6 +1,12 @@ package profile -import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" +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/actions" + "github.com/iwind/TeaGo/maps" +) type CleanAction struct { actionutils.ParentAction @@ -13,3 +19,30 @@ func (this *CleanAction) Init() { func (this *CleanAction) RunGet(params struct{}) { this.Show() } + +func (this *CleanAction) RunPost(params struct { + Must *actions.Must +}) { + tablesResp, err := this.RPC().DBRPC().FindAllDBTables(this.AdminContext(), &pb.FindAllDBTablesRequest{}) + if err != nil { + this.ErrorPage(err) + return + } + + tableMaps := []maps.Map{} + for _, table := range tablesResp.DbTables { + 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() +} diff --git a/internal/web/actions/default/settings/database/deleteTable.go b/internal/web/actions/default/settings/database/deleteTable.go new file mode 100644 index 00000000..329f8ef1 --- /dev/null +++ b/internal/web/actions/default/settings/database/deleteTable.go @@ -0,0 +1,23 @@ +package profile + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" +) + +type DeleteTableAction struct { + actionutils.ParentAction +} + +func (this *DeleteTableAction) RunPost(params struct { + Table string +}) { + defer this.CreateLogInfo("删除数据表 %s", params.Table) + + _, err := this.RPC().DBRPC().DeleteDBTable(this.AdminContext(), &pb.DeleteDBTableRequest{DbTable: params.Table}) + if err != nil { + this.ErrorPage(err) + return + } + this.Success() +} diff --git a/internal/web/actions/default/settings/database/init.go b/internal/web/actions/default/settings/database/init.go index e497799a..2d932834 100644 --- a/internal/web/actions/default/settings/database/init.go +++ b/internal/web/actions/default/settings/database/init.go @@ -17,6 +17,8 @@ func init() { GetPost("/update", new(UpdateAction)). GetPost("/clean", new(CleanAction)). GetPost("/cleanSetting", new(CleanSettingAction)). + GetPost("/truncateTable", new(TruncateTableAction)). + GetPost("/deleteTable", new(DeleteTableAction)). EndAll() }) } diff --git a/internal/web/actions/default/settings/database/truncateTable.go b/internal/web/actions/default/settings/database/truncateTable.go new file mode 100644 index 00000000..282f0ab7 --- /dev/null +++ b/internal/web/actions/default/settings/database/truncateTable.go @@ -0,0 +1,23 @@ +package profile + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" +) + +type TruncateTableAction struct { + actionutils.ParentAction +} + +func (this *TruncateTableAction) RunPost(params struct { + Table string +}) { + defer this.CreateLogInfo("清空数据表 %s 数据", params.Table) + + _, err := this.RPC().DBRPC().TruncateDBTable(this.AdminContext(), &pb.TruncateDBTableRequest{DbTable: params.Table}) + if err != nil { + this.ErrorPage(err) + return + } + this.Success() +} diff --git a/web/views/@default/settings/database/clean.html b/web/views/@default/settings/database/clean.html index e896c845..e9d30ee8 100644 --- a/web/views/@default/settings/database/clean.html +++ b/web/views/@default/settings/database/clean.html @@ -1,2 +1,24 @@ {$layout} -{$template "menu"} \ No newline at end of file +{$template "menu"} + +
正在加载中...
+ + + + + + + + + + + + + + + + +
表名占用空间用途操作
{{table.name}}{{table.size}}{{table.comment}} + 删除   + 清空 +
\ No newline at end of file diff --git a/web/views/@default/settings/database/clean.js b/web/views/@default/settings/database/clean.js new file mode 100644 index 00000000..7de0838e --- /dev/null +++ b/web/views/@default/settings/database/clean.js @@ -0,0 +1,49 @@ +Tea.context(function () { + this.tables = [] + this.isLoading = true + + this.$delay(function () { + this.reload() + }) + + this.reload = function () { + this.isLoading = true + this.$post("$") + .success(function (resp) { + this.tables = resp.data.tables; + }) + .done(function () { + this.isLoading = false + }) + } + + this.deleteTable = function (tableName) { + let that = this + teaweb.confirm("html:确定要删除此数据表吗?
删除后数据不能恢复!", function () { + that.$post(".deleteTable") + .params({ + table: tableName + }) + .success(function () { + teaweb.success("操作成功", function () { + that.reload() + }) + }) + }) + } + + this.truncateTable = function (tableName) { + let that = this + teaweb.confirm("html:确定要清空此数据表吗?
清空后数据不能恢复!", function () { + that.$post(".truncateTable") + .params({ + table: tableName + }) + .success(function () { + teaweb.success("操作成功", function () { + that.reload() + }) + }) + }) + } +}) \ No newline at end of file