mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 13:10:26 +08:00 
			
		
		
		
	可以清理数据库节点的数据表
This commit is contained in:
		
							
								
								
									
										56
									
								
								internal/web/actions/default/db/cleanPopup.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								internal/web/actions/default/db/cleanPopup.go
									
									
									
									
									
										Normal 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()
 | 
			
		||||
}
 | 
			
		||||
@@ -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()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -16,7 +16,7 @@ func (this *DeleteAction) RunPost(params struct {
 | 
			
		||||
	// 创建日志
 | 
			
		||||
	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 {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										27
									
								
								internal/web/actions/default/db/deleteTable.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								internal/web/actions/default/db/deleteTable.go
									
									
									
									
									
										Normal 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()
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
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"
 | 
			
		||||
@@ -32,7 +33,7 @@ func (this *IndexAction) RunGet(params struct{}) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	nodeMaps := []maps.Map{}
 | 
			
		||||
	for _, node := range listResp.Nodes {
 | 
			
		||||
	for _, node := range listResp.DbNodes {
 | 
			
		||||
		nodeMaps = append(nodeMaps, maps.Map{
 | 
			
		||||
			"id":       node.Id,
 | 
			
		||||
			"isOn":     node.IsOn,
 | 
			
		||||
@@ -40,6 +41,11 @@ func (this *IndexAction) RunGet(params struct{}) {
 | 
			
		||||
			"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),
 | 
			
		||||
			},
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,9 @@ func init() {
 | 
			
		||||
			GetPost("/createPopup", new(CreatePopupAction)).
 | 
			
		||||
			GetPost("/updatePopup", new(UpdatePopupAction)).
 | 
			
		||||
			Post("/delete", new(DeleteAction)).
 | 
			
		||||
			GetPost("/cleanPopup", new(CleanPopupAction)).
 | 
			
		||||
			Post("/deleteTable", new(DeleteTableAction)).
 | 
			
		||||
			Post("/truncateTable", new(TruncateTableAction)).
 | 
			
		||||
 | 
			
		||||
			EndAll()
 | 
			
		||||
	})
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										27
									
								
								internal/web/actions/default/db/truncateTable.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								internal/web/actions/default/db/truncateTable.go
									
									
									
									
									
										Normal 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()
 | 
			
		||||
}
 | 
			
		||||
@@ -19,13 +19,13 @@ func (this *UpdatePopupAction) Init() {
 | 
			
		||||
func (this *UpdatePopupAction) RunGet(params struct {
 | 
			
		||||
	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 {
 | 
			
		||||
		this.ErrorPage(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	node := nodeResp.Node
 | 
			
		||||
	node := nodeResp.DbNode
 | 
			
		||||
	if node == nil {
 | 
			
		||||
		this.NotFound("dbNode", params.NodeId)
 | 
			
		||||
		return
 | 
			
		||||
@@ -78,7 +78,7 @@ func (this *UpdatePopupAction) RunPost(params struct {
 | 
			
		||||
		Require("请输入连接数据库的用户名")
 | 
			
		||||
 | 
			
		||||
	_, err := this.RPC().DBNodeRPC().UpdateDBNode(this.AdminContext(), &pb.UpdateDBNodeRequest{
 | 
			
		||||
		NodeId:      params.NodeId,
 | 
			
		||||
		DbNodeId:    params.NodeId,
 | 
			
		||||
		IsOn:        params.IsOn,
 | 
			
		||||
		Name:        params.Name,
 | 
			
		||||
		Description: params.Description,
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package profile
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package profile
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package profile
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package profile
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package profile
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package profile
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package profile
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package profile
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
package profile
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										23
									
								
								web/views/@default/db/cleanPopup.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								web/views/@default/db/cleanPopup.html
									
									
									
									
									
										Normal 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">  </span>
 | 
			
		||||
            <a href="" v-if="table.canClean" @click.prevent="truncateTable(table.name)">清空</a>
 | 
			
		||||
        </td>
 | 
			
		||||
    </tr>
 | 
			
		||||
</table>
 | 
			
		||||
							
								
								
									
										52
									
								
								web/views/@default/db/cleanPopup.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								web/views/@default/db/cleanPopup.js
									
									
									
									
									
										Normal 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()
 | 
			
		||||
                    })
 | 
			
		||||
                })
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
})
 | 
			
		||||
@@ -12,16 +12,30 @@
 | 
			
		||||
			<th>节点名称</th>
 | 
			
		||||
			<th>连接地址</th>
 | 
			
		||||
			<th>数据库名</th>
 | 
			
		||||
            <th>用量</th>
 | 
			
		||||
			<th class="center width10">状态</th>
 | 
			
		||||
			<th class="two op">操作</th>
 | 
			
		||||
			<th class="three op">操作</th>
 | 
			
		||||
		</tr>
 | 
			
		||||
	</thead>
 | 
			
		||||
	<tr v-for="node in nodes">
 | 
			
		||||
		<td>{{node.name}}</td>
 | 
			
		||||
		<td>{{node.host}}:{{node.port}}</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>
 | 
			
		||||
            <a href="" @click.prevent="cleanNode(node.id)" v-if="node.isOn && node.status.isOk">清理</a><span v-else class="disabled">清理 </span>  
 | 
			
		||||
			<a href="" @click.prevent="updateNode(node.id)">修改</a>  
 | 
			
		||||
			<a href="" @click.prevent="deleteNode(node.id)">删除</a>
 | 
			
		||||
		</td>
 | 
			
		||||
 
 | 
			
		||||
@@ -34,4 +34,14 @@ Tea.context(function () {
 | 
			
		||||
				.refresh()
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    // 清理节点
 | 
			
		||||
    this.cleanNode = function (nodeId) {
 | 
			
		||||
        teaweb.popup("/db/cleanPopup?nodeId=" + nodeId)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
	// 显示错误信息
 | 
			
		||||
    this.showError = function (err) {
 | 
			
		||||
	    teaweb.popupTip("<span style=\"color:#db2828\">错误信息:" + err + "</span>")
 | 
			
		||||
    }
 | 
			
		||||
})
 | 
			
		||||
@@ -13,7 +13,7 @@
 | 
			
		||||
                    <input type="text" name="days" v-model="config.days" style="width:6em" maxlength="6"/>
 | 
			
		||||
                    <span class="ui label">天</span>
 | 
			
		||||
                </div>
 | 
			
		||||
                <p class="comment">天数包括当天。如果填0表示不自动清理。</p>
 | 
			
		||||
                <p class="comment">天数包括当天。如果填0表示不自动清理。此设置也会同时作用于日志数据库。</p>
 | 
			
		||||
            </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
    </table>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user