可以清理数据库节点的数据表

This commit is contained in:
GoEdgeLab
2021-01-19 22:05:46 +08:00
parent 600caad2f8
commit 62d42e8e30
22 changed files with 236 additions and 18 deletions

View File

@@ -0,0 +1,56 @@
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/actions"
"github.com/iwind/TeaGo/maps"
)
type CleanPopupAction struct {
actionutils.ParentAction
}
func (this *CleanPopupAction) Init() {
this.Nav("", "", "")
}
func (this *CleanPopupAction) RunGet(params struct {
NodeId int64
}) {
this.Data["nodeId"] = params.NodeId
this.Show()
}
func (this *CleanPopupAction) 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.ErrorPage(err)
return
}
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

@@ -62,7 +62,7 @@ func (this *CreatePopupAction) RunPost(params struct {
} }
// 创建日志 // 创建日志
defer this.CreateLog(oplogs.LevelInfo, "创建数据库节点 %d", createResp.NodeId) defer this.CreateLog(oplogs.LevelInfo, "创建数据库节点 %d", createResp.DbNodeId)
this.Success() this.Success()
} }

View File

@@ -16,7 +16,7 @@ func (this *DeleteAction) RunPost(params struct {
// 创建日志 // 创建日志
defer this.CreateLog(oplogs.LevelInfo, "删除数据库节点 %d", params.NodeId) defer this.CreateLog(oplogs.LevelInfo, "删除数据库节点 %d", params.NodeId)
_, err := this.RPC().DBNodeRPC().DeleteDBNode(this.AdminContext(), &pb.DeleteDBNodeRequest{NodeId: params.NodeId}) _, err := this.RPC().DBNodeRPC().DeleteDBNode(this.AdminContext(), &pb.DeleteDBNodeRequest{DbNodeId: params.NodeId})
if err != nil { if err != nil {
this.ErrorPage(err) this.ErrorPage(err)
return return

View File

@@ -0,0 +1,27 @@
package db
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 {
NodeId int64
Table string
}) {
defer this.CreateLogInfo("删除数据库节点 %d 数据表 %s", 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

@@ -1,6 +1,7 @@
package db package db
import ( import (
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
@@ -32,7 +33,7 @@ func (this *IndexAction) RunGet(params struct{}) {
return return
} }
nodeMaps := []maps.Map{} nodeMaps := []maps.Map{}
for _, node := range listResp.Nodes { for _, node := range listResp.DbNodes {
nodeMaps = append(nodeMaps, maps.Map{ nodeMaps = append(nodeMaps, maps.Map{
"id": node.Id, "id": node.Id,
"isOn": node.IsOn, "isOn": node.IsOn,
@@ -40,6 +41,11 @@ func (this *IndexAction) RunGet(params struct{}) {
"host": node.Host, "host": node.Host,
"port": node.Port, "port": node.Port,
"database": node.Database, "database": node.Database,
"status": maps.Map{
"isOk": node.Status.IsOk,
"error": node.Status.Error,
"size": numberutils.FormatBytes(node.Status.Size),
},
}) })
} }

View File

@@ -18,6 +18,9 @@ func init() {
GetPost("/createPopup", new(CreatePopupAction)). GetPost("/createPopup", new(CreatePopupAction)).
GetPost("/updatePopup", new(UpdatePopupAction)). GetPost("/updatePopup", new(UpdatePopupAction)).
Post("/delete", new(DeleteAction)). Post("/delete", new(DeleteAction)).
GetPost("/cleanPopup", new(CleanPopupAction)).
Post("/deleteTable", new(DeleteTableAction)).
Post("/truncateTable", new(TruncateTableAction)).
EndAll() EndAll()
}) })

View File

@@ -0,0 +1,27 @@
package db
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 {
NodeId int64
Table string
}) {
defer this.CreateLogInfo("清空数据库节点 %d 数据表 %s 数据", 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

@@ -19,13 +19,13 @@ func (this *UpdatePopupAction) Init() {
func (this *UpdatePopupAction) RunGet(params struct { func (this *UpdatePopupAction) RunGet(params struct {
NodeId int64 NodeId int64
}) { }) {
nodeResp, err := this.RPC().DBNodeRPC().FindEnabledDBNode(this.AdminContext(), &pb.FindEnabledDBNodeRequest{NodeId: params.NodeId}) nodeResp, err := this.RPC().DBNodeRPC().FindEnabledDBNode(this.AdminContext(), &pb.FindEnabledDBNodeRequest{DbNodeId: params.NodeId})
if err != nil { if err != nil {
this.ErrorPage(err) this.ErrorPage(err)
return return
} }
node := nodeResp.Node node := nodeResp.DbNode
if node == nil { if node == nil {
this.NotFound("dbNode", params.NodeId) this.NotFound("dbNode", params.NodeId)
return return
@@ -78,7 +78,7 @@ func (this *UpdatePopupAction) RunPost(params struct {
Require("请输入连接数据库的用户名") Require("请输入连接数据库的用户名")
_, err := this.RPC().DBNodeRPC().UpdateDBNode(this.AdminContext(), &pb.UpdateDBNodeRequest{ _, err := this.RPC().DBNodeRPC().UpdateDBNode(this.AdminContext(), &pb.UpdateDBNodeRequest{
NodeId: params.NodeId, DbNodeId: params.NodeId,
IsOn: params.IsOn, IsOn: params.IsOn,
Name: params.Name, Name: params.Name,
Description: params.Description, Description: params.Description,

View File

@@ -1,4 +1,4 @@
package profile package database
import ( import (
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils" "github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"

View File

@@ -1,4 +1,4 @@
package profile package database
import ( import (
"encoding/json" "encoding/json"

View File

@@ -1,4 +1,4 @@
package profile package database
import ( import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"

View File

@@ -1,4 +1,4 @@
package profile package database
import ( import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"

View File

@@ -1,4 +1,4 @@
package profile package database
import ( import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders" "github.com/TeaOSLab/EdgeAdmin/internal/configloaders"

View File

@@ -1,4 +1,4 @@
package profile package database
import ( import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"

View File

@@ -1,4 +1,4 @@
package profile package database
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package profile package database
import ( import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders" "github.com/TeaOSLab/EdgeAdmin/internal/configloaders"

View File

@@ -1,4 +1,4 @@
package profile package database
import ( import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders" "github.com/TeaOSLab/EdgeAdmin/internal/configloaders"

View File

@@ -0,0 +1,23 @@
{$layout "layout_popup"}
<div class="ui message" v-if="isLoading">正在加载中...</div>
<table class="ui table selectable">
<thead>
<tr>
<th>表名</th>
<th>占用空间</th>
<th>用途</th>
<th class="two op">操作</th>
</tr>
</thead>
<tr v-for="table in tables">
<td>{{table.name}}</td>
<td>{{table.size}}</td>
<td>{{table.comment}}</td>
<td>
<a href="" v-if="table.canDelete" @click.prevent="deleteTable(table.name)">删除</a><span v-if="table.canDelete"> &nbsp;</span>
<a href="" v-if="table.canClean" @click.prevent="truncateTable(table.name)">清空</a>
</td>
</tr>
</table>

View File

@@ -0,0 +1,52 @@
Tea.context(function () {
this.tables = []
this.isLoading = true
this.$delay(function () {
this.reload()
})
this.reload = function () {
this.isLoading = true
this.$post("$")
.params({ nodeId: this.nodeId })
.success(function (resp) {
this.tables = resp.data.tables;
})
.done(function () {
this.isLoading = false
})
}
this.deleteTable = function (tableName) {
let that = this
teaweb.confirm("html:确定要删除此数据表吗?<br/>删除后数据不能恢复!", function () {
that.$post(".deleteTable")
.params({
nodeId: that.nodeId,
table: tableName
})
.success(function () {
teaweb.success("操作成功", function () {
that.reload()
})
})
})
}
this.truncateTable = function (tableName) {
let that = this
teaweb.confirm("html:确定要清空此数据表吗?<br/>清空后数据不能恢复!", function () {
that.$post(".truncateTable")
.params({
nodeId: that.nodeId,
table: tableName
})
.success(function () {
teaweb.success("操作成功", function () {
that.reload()
})
})
})
}
})

View File

@@ -12,16 +12,30 @@
<th>节点名称</th> <th>节点名称</th>
<th>连接地址</th> <th>连接地址</th>
<th>数据库名</th> <th>数据库名</th>
<th>用量</th>
<th class="center width10">状态</th> <th class="center width10">状态</th>
<th class="two op">操作</th> <th class="three op">操作</th>
</tr> </tr>
</thead> </thead>
<tr v-for="node in nodes"> <tr v-for="node in nodes">
<td>{{node.name}}</td> <td>{{node.name}}</td>
<td>{{node.host}}:{{node.port}}</td> <td>{{node.host}}:{{node.port}}</td>
<td>{{node.database}}</td> <td>{{node.database}}</td>
<td class="center"><label-on :v-is-on="node.isOn"></label-on></td> <td>
<span v-if="node.status.isOk">{{node.status.size}}</span>
<span v-else class="disabled">-</span>
</td>
<td class="center">
<div v-if="node.isOn">
<span v-if="node.status.isOk" class="green">连接正常</span>
<a href="" title="点击查看具体错误" v-else @click.prevent="showError(node.status.error)"><span class="red" style="border-bottom: 1px #db2828 dashed">连接错误</span></a>
</div>
<span v-else>
<label-on :v-is-on="node.isOn"></label-on>
</span>
</td>
<td> <td>
<a href="" @click.prevent="cleanNode(node.id)" v-if="node.isOn && node.status.isOk">清理</a><span v-else class="disabled">清理 </span> &nbsp;
<a href="" @click.prevent="updateNode(node.id)">修改</a> &nbsp; <a href="" @click.prevent="updateNode(node.id)">修改</a> &nbsp;
<a href="" @click.prevent="deleteNode(node.id)">删除</a> <a href="" @click.prevent="deleteNode(node.id)">删除</a>
</td> </td>

View File

@@ -34,4 +34,14 @@ Tea.context(function () {
.refresh() .refresh()
}) })
} }
// 清理节点
this.cleanNode = function (nodeId) {
teaweb.popup("/db/cleanPopup?nodeId=" + nodeId)
}
// 显示错误信息
this.showError = function (err) {
teaweb.popupTip("<span style=\"color:#db2828\">错误信息:" + err + "</span>")
}
}) })

View File

@@ -13,7 +13,7 @@
<input type="text" name="days" v-model="config.days" style="width:6em" maxlength="6"/> <input type="text" name="days" v-model="config.days" style="width:6em" maxlength="6"/>
<span class="ui label"></span> <span class="ui label"></span>
</div> </div>
<p class="comment">天数包括当天。如果填0表示不自动清理。</p> <p class="comment">天数包括当天。如果填0表示不自动清理。此设置也会同时作用于日志数据库。</p>
</td> </td>
</tr> </tr>
</table> </table>