增加消息管理

This commit is contained in:
GoEdgeLab
2020-10-20 20:18:12 +08:00
parent 957b5b620c
commit 10faba32ea
15 changed files with 274 additions and 3 deletions

2
go.mod
View File

@@ -8,7 +8,7 @@ require (
github.com/TeaOSLab/EdgeCommon v0.0.0-00010101000000-000000000000
github.com/go-sql-driver/mysql v1.5.0
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/iwind/TeaGo v0.0.0-20201010005321-430e836dee8a
github.com/iwind/TeaGo v0.0.0-20201020081413-7cf62d6f420f
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c // indirect
google.golang.org/grpc v1.32.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776

2
go.sum
View File

@@ -50,6 +50,8 @@ github.com/iwind/TeaGo v0.0.0-20200923021120-f5d76441fe9e h1:/xn7wUvlwaoA5IkdBUc
github.com/iwind/TeaGo v0.0.0-20200923021120-f5d76441fe9e/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/iwind/TeaGo v0.0.0-20201010005321-430e836dee8a h1:sO6uDbQOEe6/tIB3o31vn6eD/JmkKGErKgcOA/Cpb+Q=
github.com/iwind/TeaGo v0.0.0-20201010005321-430e836dee8a/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/iwind/TeaGo v0.0.0-20201020081413-7cf62d6f420f h1:6Ws2H+eorfVUoMO2jta6A9nIdh8oi5/5LXo/LkAxR+E=
github.com/iwind/TeaGo v0.0.0-20201020081413-7cf62d6f420f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=

View File

@@ -175,6 +175,10 @@ func (this *RPCClient) SysSettingRPC() pb.SysSettingServiceClient {
return pb.NewSysSettingServiceClient(this.pickConn())
}
func (this *RPCClient) MessageRPC() pb.MessageServiceClient {
return pb.NewMessageServiceClient(this.pickConn())
}
// 构造Admin上下文
func (this *RPCClient) Context(adminId int64) context.Context {
ctx := context.Background()

View File

@@ -0,0 +1,22 @@
package messages
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type BadgeAction struct {
actionutils.ParentAction
}
func (this *BadgeAction) RunPost(params struct{}) {
countResp, err := this.RPC().MessageRPC().CountUnreadMessages(this.AdminContext(), &pb.CountUnreadMessagesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["count"] = countResp.Count
this.Success()
}

View File

@@ -0,0 +1,11 @@
package messages
import "github.com/iwind/TeaGo/actions"
type Helper struct {
}
func (this *Helper) BeforeAction(actionPtr actions.ActionWrapper) {
action := actionPtr.Object()
action.Data["teaMenu"] = "message"
}

View File

@@ -0,0 +1,65 @@
package messages
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct{}) {
countResp, err := this.RPC().MessageRPC().CountUnreadMessages(this.AdminContext(), &pb.CountUnreadMessagesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
count := countResp.Count
page := this.NewPage(count)
this.Data["page"] = page.AsHTML()
listResp, err := this.RPC().MessageRPC().ListUnreadMessages(this.AdminContext(), &pb.ListUnreadMessagesRequest{
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
messages := []maps.Map{}
for _, message := range listResp.Messages {
clusterMap := maps.Map{}
if message.Cluster != nil {
clusterMap = maps.Map{
"id": message.Cluster.Id,
"name": message.Cluster.Name,
}
}
nodeMap := maps.Map{}
messages = append(messages, maps.Map{
"id": message.Id,
"isRead": message.IsRead,
"body": message.Body,
"level": message.Level,
"datetime": timeutil.FormatTime("Y-m-d H:i:s", message.CreatedAt),
"params": string(message.ParamsJSON),
"type": message.Type,
"cluster": clusterMap,
"node": nodeMap,
})
}
this.Data["messages"] = messages
this.Show()
}

View File

@@ -0,0 +1,20 @@
package messages
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(new(helpers.UserMustAuth)).
Helper(new(Helper)).
Prefix("/messages").
GetPost("", new(IndexAction)).
Post("/badge", new(BadgeAction)).
Post("/readAll", new(ReadAllAction)).
Post("/readPage", new(ReadPageAction)).
EndAll()
})
}

View File

@@ -0,0 +1,20 @@
package messages
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type ReadAllAction struct {
actionutils.ParentAction
}
func (this *ReadAllAction) RunPost(params struct{}) {
_, err := this.RPC().MessageRPC().UpdateAllMessagesRead(this.AdminContext(), &pb.UpdateAllMessagesReadRequest{})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,25 @@
package messages
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type ReadPageAction struct {
actionutils.ParentAction
}
func (this *ReadPageAction) RunPost(params struct {
MessageIds []int64
}) {
_, err := this.RPC().MessageRPC().UpdateMessagesRead(this.AdminContext(), &pb.UpdateMessagesReadRequest{
MessageIds: params.MessageIds,
IsRead: true,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -13,6 +13,7 @@ import (
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/index"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/log"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/logout"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/messages"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/components"

View File

@@ -0,0 +1,38 @@
Vue.component("message-row", {
props: ["v-message"],
data: function () {
let paramsJSON = this.vMessage.params
let params = null
if (paramsJSON != null && paramsJSON.length > 0) {
params = JSON.parse(paramsJSON)
}
return {
message: this.vMessage,
params: params
}
},
template: `<div>
<table class="ui table selectable">
<tr :class="{error: message.level == 'error'}">
<td>
<strong>{{message.datetime}}</strong>
<span v-if="message.cluster != null && message.cluster.id != null">
<span> | </span>
<a :href="'/clusters/cluster?clusterId=' + message.cluster.id">集群:{{message.cluster.name}}</a>
</span>
</td>
</tr>
<tr :class="{error: message.level == 'error'}">
<td>
{{message.body}}
<div v-if="message.type == 'HealthCheckFail'" style="margin-top: 0.8em">
<a :href="'/clusters/cluster/node?clusterId=' + message.cluster.id + '&nodeId=' + param.node.id" v-for="param in params" class="ui label tiny">{{param.node.name}}</a>
</div>
</td>
</tr>
</table>
<div class="margin"></div>
</div>`
})

View File

@@ -28,7 +28,7 @@
<div class="right menu">
<a href="" class="item" v-if="globalChangedClusters.length > 0" @click.prevent="syncClustersConfigs()"><i class="icon refresh"></i>{{globalChangedClusters.length}}个集群服务已变更,点此同步</a>
<a href="/monitor/messages" class="item" v-if="teaBadge > 0"><span :class="{'blink':teaBadge > 0}"><i class="icon bell"></i>告警({{teaBadge}}) </span></a>
<a href="/messages" class="item" :class="{active:teaMenu == 'message'}"><span :class="{'blink':globalMessageBadge > 0}"><i class="icon bell"></i>消息({{globalMessageBadge}}) </span></a>
<a href="/settings/profile" class="item" :class="{active: teaMenu == 'settings'}">
<i class="icon user" v-if="teaUserAvatar.length == 0"></i>
<img class="avatar" alt="" :src="teaUserAvatar" v-if="teaUserAvatar.length > 0"/>

View File

@@ -1,6 +1,7 @@
Tea.context(function () {
this.moreOptionsVisible = false
this.globalChangedClusters = []
this.globalMessageBadge = 0
this.teaDemoEnabled = false
if (typeof this.leftMenuItemIsDisabled == "undefined") {
@@ -14,6 +15,9 @@ Tea.context(function () {
// 检查变更
this.checkClusterChanges()
// 检查消息
this.checkMessages()
})
/**
@@ -58,7 +62,27 @@ Tea.context(function () {
this.checkClusterChanges()
}, delay)
})
};
}
/**
* 检查消息
*/
this.checkMessages = function () {
this.$post("/messages/badge")
.params({})
.success(function (resp) {
this.globalMessageBadge = resp.data.count
})
.done(function () {
let delay = 6000
if (this.globalMessageBadge > 0) {
delay = 30000
}
this.$delay(function () {
this.checkMessages()
}, delay)
})
}
/**
* 同步集群配置

View File

@@ -0,0 +1,13 @@
{$layout}
<first-menu v-if="messages.length > 0">
<a href="" class="item" @click.prevent="updatePageRead()">[设置当前页为已读]</a>
<a href="" class="item" @click.prevent="updateAllRead()">[设置全部为已读]</a>
</first-menu>
<div class="margin"></div>
<p class="comment" v-if="messages.length == 0">暂时还没有消息。</p>
<message-row v-for="message in messages" :v-message="message" :key="message.id"></message-row>
<div class="page" v-html="page"></div>

View File

@@ -0,0 +1,26 @@
Tea.context(function () {
this.updateAllRead = function () {
let that = this
teaweb.confirm("确定要设置所有的未读消息为已读吗?", function () {
that.$post("/messages/readAll")
.success(function () {
window.location = "/messages"
})
})
}
this.updatePageRead = function () {
let that = this
teaweb.confirm("确定要设置当前页的未读消息为已读吗?", function () {
let messageIds = []
that.messages.forEach(function (v) {
messageIds.push(v.id)
})
that.$post("/messages/readPage")
.params({
messageIds: messageIds
})
.refresh()
})
}
})