[API节点]显示API节点运行日志 [用户]增加可用功能控制

This commit is contained in:
GoEdgeLab
2020-12-30 22:00:21 +08:00
parent 1f6ecccc65
commit 8d8db37d9d
15 changed files with 220 additions and 0 deletions

View File

@@ -23,6 +23,7 @@ func init() {
Get("", new(IndexAction)).
GetPost("/update", new(UpdateAction)).
Get("/install", new(InstallAction)).
Get("/logs", new(LogsAction)).
EndAll()
})

View File

@@ -0,0 +1,71 @@
package node
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 LogsAction struct {
actionutils.ParentAction
}
func (this *LogsAction) Init() {
this.Nav("", "node", "log")
this.SecondMenu("nodes")
}
func (this *LogsAction) RunGet(params struct {
NodeId int64
}) {
apiNodeResp, err := this.RPC().APINodeRPC().FindEnabledAPINode(this.AdminContext(), &pb.FindEnabledAPINodeRequest{NodeId: params.NodeId})
if err != nil {
this.ErrorPage(err)
return
}
apiNode := apiNodeResp.Node
if apiNode == nil {
this.NotFound("apiNode", params.NodeId)
return
}
this.Data["node"] = maps.Map{
"id": apiNode.Id,
"name": apiNode.Name,
}
countResp, err := this.RPC().NodeLogRPC().CountNodeLogs(this.AdminContext(), &pb.CountNodeLogsRequest{
Role: "api",
NodeId: params.NodeId,
})
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: "api",
Offset: page.Offset,
Size: page.Size,
})
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,78 @@
package users
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/users/userutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
)
type FeaturesAction struct {
actionutils.ParentAction
}
func (this *FeaturesAction) Init() {
this.Nav("", "", "feature")
}
func (this *FeaturesAction) RunGet(params struct {
UserId int64
}) {
err := userutils.InitUser(this.Parent(), params.UserId)
if err != nil {
this.ErrorPage(err)
return
}
featuresResp, err := this.RPC().UserRPC().FindAllUserFeatureDefinitions(this.AdminContext(), &pb.FindAllUserFeatureDefinitionsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
allFeatures := featuresResp.Features
userFeaturesResp, err := this.RPC().UserRPC().FindUserFeatures(this.AdminContext(), &pb.FindUserFeaturesRequest{UserId: params.UserId})
if err != nil {
this.ErrorPage(err)
return
}
userFeatureCodes := []string{}
for _, userFeature := range userFeaturesResp.Features {
userFeatureCodes = append(userFeatureCodes, userFeature.Code)
}
featureMaps := []maps.Map{}
for _, feature := range allFeatures {
featureMaps = append(featureMaps, maps.Map{
"name": feature.Name,
"code": feature.Code,
"description": feature.Description,
"isChecked": lists.ContainsString(userFeatureCodes, feature.Code),
})
}
this.Data["features"] = featureMaps
this.Show()
}
func (this *FeaturesAction) RunPost(params struct {
UserId int64
Codes []string
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo("设置用户 %d 的功能列表", params.UserId)
_, err := this.RPC().UserRPC().UpdateUserFeatures(this.AdminContext(), &pb.UpdateUserFeaturesRequest{
UserId: params.UserId,
FeatureCodes: params.Codes,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -17,6 +17,7 @@ func init() {
Get("/user", new(UserAction)).
GetPost("/update", new(UpdateAction)).
Post("/delete", new(DeleteAction)).
GetPost("/features", new(FeaturesAction)).
EndAll()
})
}

View File

@@ -2,6 +2,7 @@
<menu-item href="/api">节点列表</menu-item>
<span class="item">|</span>
<menu-item :href="'/api/node?nodeId=' + node.id" code="index">"{{node.name}}"详情</menu-item>
<menu-item :href="'/api/node/logs?nodeId=' + node.id" code="log">运行日志</menu-item>
<menu-item :href="'/api/node/install?nodeId=' + node.id" code="install">安装节点</menu-item>
<menu-item :href="'/api/node/update?nodeId=' + node.id" code="update">修改节点</menu-item>
</first-menu>

View File

@@ -0,0 +1,5 @@
pre.log-box {
margin: 0;
padding: 0;
}
/*# sourceMappingURL=logs.css.map */

View File

@@ -0,0 +1 @@
{"version":3,"sources":["logs.less"],"names":[],"mappings":"AAAA,GAAG;EACF,SAAA;EACA,UAAA","file":"logs.css"}

View File

@@ -0,0 +1,20 @@
{$layout}
{$template "menu"}
<p class="comment" v-if="logs.length == 0">暂时还没有日志。</p>
<table class="ui table selectable" v-if="logs.length > 0">
<thead>
<tr>
</tr>
</thead>
<tr v-for="log in logs">
<td>
<pre class="log-box"><span :class="{red:log.level == 'error', yellow:log.level == 'warning'}"><span v-if="!log.isToday">[{{log.createdTime}}]</span><strong v-if="log.isToday">[{{log.createdTime}}]</strong>[{{log.tag}}]{{log.description}}</span></pre>
</td>
</tr>
</table>
<div class="page" v-html="page"></div>

View File

@@ -0,0 +1,4 @@
pre.log-box {
margin: 0;
padding: 0;
}

View File

@@ -3,4 +3,5 @@
<span class="item">|</span>
<menu-item :href="'/users/user?userId=' + user.id" code="index">{{user.fullname}}&nbsp; <span class="small">({{user.username}})</span></menu-item>
<menu-item :href="'/users/update?userId=' + user.id" code="update">修改</menu-item>
<menu-item :href="'/users/features?userId=' + user.id" code="feature">功能</menu-item>
</first-menu>

View File

@@ -0,0 +1,4 @@
.feature-boxes .feature-box {
margin-bottom: 0.7em;
}
/*# sourceMappingURL=features.css.map */

View File

@@ -0,0 +1 @@
{"version":3,"sources":["features.less"],"names":[],"mappings":"AAAA,cACC;EACC,oBAAA","file":"features.css"}

View File

@@ -0,0 +1,24 @@
{$layout}
{$template "user_menu"}
<div class="margin"></div>
<form class="ui form" data-tea-success="success" data-tea-action="$">
<csrf-token></csrf-token>
<input type="hidden" name="userId" :value="user.id"/>
<table class="ui table definition selectable">
<tr>
<td class="title">功能列表</td>
<td>
<div class="feature-boxes">
<div class="feature-box" v-for="feature in features">
<checkbox name="codes" :v-value="feature.code" v-model="feature.isChecked">{{feature.name}}</checkbox>
<p class="comment">{{feature.description}}</p>
</div>
</div>
</td>
</tr>
</table>
<submit-btn></submit-btn>
</form>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyReloadSuccess("保存成功")
})

View File

@@ -0,0 +1,5 @@
.feature-boxes {
.feature-box {
margin-bottom: 0.7em;
}
}