From 7c20a70973acc6c8b86cecd5f87b1d75d0496654 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Thu, 25 Aug 2022 11:36:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8F=E8=A7=88=E8=AE=BF=E9=97=AE=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=97=B6=E8=87=AA=E5=8A=A8=E7=94=A8=E7=82=B9=E7=AC=A6?= =?UTF-8?q?=E5=8F=B7=E6=A0=87=E8=AE=B0=E6=9C=89=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E5=88=86=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../actions/default/servers/logs/hasLogs.go | 67 ++++++++++++++++ .../web/actions/default/servers/logs/init.go | 1 + web/public/js/components/common/dot.js | 3 + .../server/http-access-log-partitions-box.js | 45 ++++++++++- web/views/@default/servers/logs/index.html | 12 +-- web/views/@default/servers/logs/index.js | 56 ++++++++++++++ .../@default/servers/server/log/history.html | 12 +-- .../@default/servers/server/log/history.js | 77 +++++++++++++++---- .../@default/servers/server/log/today.html | 12 +-- .../@default/servers/server/log/today.js | 43 +++++++++++ 10 files changed, 292 insertions(+), 36 deletions(-) create mode 100644 internal/web/actions/default/servers/logs/hasLogs.go create mode 100644 web/public/js/components/common/dot.js diff --git a/internal/web/actions/default/servers/logs/hasLogs.go b/internal/web/actions/default/servers/logs/hasLogs.go new file mode 100644 index 00000000..37a1f143 --- /dev/null +++ b/internal/web/actions/default/servers/logs/hasLogs.go @@ -0,0 +1,67 @@ +// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn . + +package logs + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + timeutil "github.com/iwind/TeaGo/utils/time" + "regexp" + "strings" +) + +// HasLogsAction 检查某个分区是否有日志 +type HasLogsAction struct { + actionutils.ParentAction +} + +func (this *HasLogsAction) RunPost(params struct { + ClusterId int64 + NodeId int64 + Day string + Hour string + Keyword string + Ip string + Domain string + HasError int + HasWAF int + Partition int32 `default:"-1"` + + RequestId string + ServerId int64 +}) { + if len(params.Day) == 0 { + params.Day = timeutil.Format("Y-m-d") + } + + var day = params.Day + + if len(day) > 0 && regexp.MustCompile(`\d{4}-\d{2}-\d{2}`).MatchString(day) { + day = strings.ReplaceAll(day, "-", "") + } + + resp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{ + Partition: params.Partition, + RequestId: params.RequestId, + NodeClusterId: params.ClusterId, + NodeId: params.NodeId, + ServerId: params.ServerId, + HasError: params.HasError > 0, + HasFirewallPolicy: params.HasWAF > 0, + Day: day, + HourFrom: params.Hour, + HourTo: params.Hour, + Keyword: params.Keyword, + Ip: params.Ip, + Domain: params.Domain, + Size: 1, + }) + if err != nil { + this.ErrorPage(err) + return + } + + this.Data["hasLogs"] = len(resp.HttpAccessLogs) > 0 + + this.Success() +} diff --git a/internal/web/actions/default/servers/logs/init.go b/internal/web/actions/default/servers/logs/init.go index b2b109e6..bc00fb67 100644 --- a/internal/web/actions/default/servers/logs/init.go +++ b/internal/web/actions/default/servers/logs/init.go @@ -18,6 +18,7 @@ func init() { Get("", new(IndexAction)). GetPost("/settings", new(SettingsAction)). Post("/partitionData", new(PartitionDataAction)). + Post("/hasLogs", new(HasLogsAction)). EndAll() }) } diff --git a/web/public/js/components/common/dot.js b/web/public/js/components/common/dot.js new file mode 100644 index 00000000..97ab0a1e --- /dev/null +++ b/web/public/js/components/common/dot.js @@ -0,0 +1,3 @@ +Vue.component("dot", { + template: '' +}) \ No newline at end of file diff --git a/web/public/js/components/server/http-access-log-partitions-box.js b/web/public/js/components/server/http-access-log-partitions-box.js index 8c84a78d..f2524d14 100644 --- a/web/public/js/components/server/http-access-log-partitions-box.js +++ b/web/public/js/components/server/http-access-log-partitions-box.js @@ -1,5 +1,5 @@ Vue.component("http-access-log-partitions-box", { - props: ["v-partition", "v-day"], + props: ["v-partition", "v-day", "v-query"], mounted: function () { let that = this Tea.action("/servers/logs/partitionData") @@ -11,13 +11,18 @@ Vue.component("http-access-log-partitions-box", { resp.data.partitions.reverse().forEach(function (v) { that.partitions.push({ code: v, - isDisabled: false + isDisabled: false, + hasLogs: false }) }) if (that.partitions.length > 0) { if (that.vPartition == null || that.vPartition < 0) { that.selectedPartition = that.partitions[0].code } + + if (that.partitions.length > 1) { + that.checkLogs() + } } }) .post() @@ -25,7 +30,8 @@ Vue.component("http-access-log-partitions-box", { data: function () { return { partitions: [], - selectedPartition: this.vPartition + selectedPartition: this.vPartition, + checkingPartition: 0 } }, methods: { @@ -48,12 +54,43 @@ Vue.component("http-access-log-partitions-box", { p.isDisabled = true } }) + }, + checkLogs: function () { + let that = this + let index = this.checkingPartition + let params = { + partition: index + } + let query = this.vQuery + if (query == null || query.length == 0) { + return + } + query.split("&").forEach(function (v) { + let param = v.split("=") + params[param[0]] = decodeURIComponent(param[1]) + }) + Tea.action("/servers/logs/hasLogs") + .params(params) + .post() + .success(function (response) { + if (response.data.hasLogs) { + // 因为是倒序,所以这里需要使用总长度减去index + that.partitions[that.partitions.length - 1 - index].hasLogs = true + } + + index++ + if (index >= that.partitions.length) { + return + } + that.checkingPartition = index + that.checkLogs() + }) } }, template: `
` diff --git a/web/views/@default/servers/logs/index.html b/web/views/@default/servers/logs/index.html index 2b1f2b54..6d503975 100644 --- a/web/views/@default/servers/logs/index.html +++ b/web/views/@default/servers/logs/index.html @@ -2,9 +2,9 @@ {$template "/datepicker"} - 所有日志 - 错误日志 - WAF日志 + 所有日志 + 错误日志 + WAF日志 | 设置 @@ -29,7 +29,7 @@ - + 看起来你的访问日志查询非常慢({{slowQueryCost}}s),建议 1)指定具体域名查询;2)通过添加新的 [日志节点] 来分散存储访问日志。 @@ -43,10 +43,10 @@
- 上一页 + 上一页 上一页   |   - 下一页 + 下一页 下一页 diff --git a/web/views/@default/servers/logs/index.js b/web/views/@default/servers/logs/index.js index f462f8a5..93ca39c5 100644 --- a/web/views/@default/servers/logs/index.js +++ b/web/views/@default/servers/logs/index.js @@ -19,4 +19,60 @@ Tea.context(function () { accessLog.wafInfo = null } }) + + this.query = function (args) { + // 初始化时页面尚未设置Vue变量,所以使用全局的变量获取 + let that = TEA.ACTION.data + + if (that.clusterId == null) { + that.clusterId = 0 + } + if (that.nodeId == null) { + that.nodeId = 0 + } + if (that.serverId == null) { + that.serverId = 0 + } + if (that.day == null) { + that.day = "" + } + if (that.keyword == null) { + that.keyword = "" + } + if (that.ip == null) { + that.ip = "" + } + if (that.domain == null) { + that.domain = "" + } + if (that.hour == null) { + that.hour = "" + } + if (that.pageSize == null) { + that.pageSize = 0 + } + let query = 'clusterId=' + that.clusterId + '&nodeId=' + that.nodeId + '&serverId=' + that.serverId + '&day=' + that.day + '&keyword=' + encodeURIComponent(that.keyword) + '&ip=' + that.ip + '&domain=' + that.domain + '&hour=' + that.hour + '&pageSize=' + that.pageSize + + if (args != null && args.length > 0) { + query += "&" + args + } + return query + } + + this.allQuery = function () { + if (this.query == null) { + // 尚未初始化完成 + return + } + let query = this.query() + if (this.hasError == 1) { + query += "&hasError=1" + } + if (this.hasWAF == 1) { + query += "&hasWAF=1" + } + return query + } + + this.currentQuery = this.allQuery() }) \ No newline at end of file diff --git a/web/views/@default/servers/server/log/history.html b/web/views/@default/servers/server/log/history.html index 2fc7cd4a..14425eb7 100644 --- a/web/views/@default/servers/server/log/history.html +++ b/web/views/@default/servers/server/log/history.html @@ -15,9 +15,9 @@ {$template "/left_menu_with_menu"}
- 所有日志 - 错误日志 - WAF日志 + 所有日志 + 错误日志 + WAF日志
@@ -38,7 +38,7 @@
- +

暂时还没有访问日志。

@@ -50,10 +50,10 @@
- 上一页 + 上一页 上一页   |   - 下一页 + 下一页 下一页
diff --git a/web/views/@default/servers/server/log/history.js b/web/views/@default/servers/server/log/history.js index b24aa0d2..67a00fa2 100644 --- a/web/views/@default/servers/server/log/history.js +++ b/web/views/@default/servers/server/log/history.js @@ -1,22 +1,71 @@ Tea.context(function () { - this.$delay(function () { - let that = this - teaweb.datepicker("day-input", function (day) { - that.day = day - }) - }) + this.$delay(function () { + let that = this + teaweb.datepicker("day-input", function (day) { + that.day = day + }) + }) - let that = this - this.accessLogs.forEach(function (accessLog) { - if (typeof (that.regions[accessLog.remoteAddr]) == "string") { - accessLog.region = that.regions[accessLog.remoteAddr] - } else { - accessLog.region = "" - } + let that = this + this.accessLogs.forEach(function (accessLog) { + if (typeof (that.regions[accessLog.remoteAddr]) == "string") { + accessLog.region = that.regions[accessLog.remoteAddr] + } else { + accessLog.region = "" + } if (accessLog.firewallRuleSetId > 0 && typeof (that.wafInfos[accessLog.firewallRuleSetId]) == "object") { accessLog.wafInfo = that.wafInfos[accessLog.firewallRuleSetId] } else { accessLog.wafInfo = null } - }) + }) + + this.query = function (args) { + // 初始化时页面尚未设置Vue变量,所以使用全局的变量获取 + let that = TEA.ACTION.data + + if (that.serverId == null) { + that.serverId = 0 + } + if (that.keyword == null) { + that.keyword = "" + } + if (that.ip == null) { + that.ip = "" + } + if (that.domain == null) { + that.domain = "" + } + if (that.pageSize == null) { + that.pageSize = "" + } + if (that.day == null) { + that.day = "" + } + if (that.hour == null) { + that.hour = "" + } + let query = 'serverId=' + that.serverId + '&day=' + that.day + '&keyword=' + encodeURIComponent(that.keyword) + '&ip=' + that.ip + '&domain=' + that.domain + '&hour=' + that.hour + '&pageSize=' + that.pageSize + if (args != null && args.length > 0) { + query += "&" + args + } + return query + } + + this.allQuery = function () { + if (this.query == null) { + // 尚未初始化完成 + return + } + let query = this.query() + if (this.hasError == 1) { + query += "&hasError=1" + } + if (this.hasWAF == 1) { + query += "&hasWAF=1" + } + return query + } + + this.currentQuery = this.allQuery() }) \ No newline at end of file diff --git a/web/views/@default/servers/server/log/today.html b/web/views/@default/servers/server/log/today.html index 512eecc2..5010994d 100644 --- a/web/views/@default/servers/server/log/today.html +++ b/web/views/@default/servers/server/log/today.html @@ -13,9 +13,9 @@ {$template "/left_menu_with_menu"}
- 所有日志 - 错误日志 - WAF日志 + 所有日志 + 错误日志 + WAF日志
@@ -26,7 +26,7 @@
- +

今天暂时还没有访问日志。

@@ -38,10 +38,10 @@
- 上一页 + 上一页 上一页   |   - 下一页 + 下一页 下一页
diff --git a/web/views/@default/servers/server/log/today.js b/web/views/@default/servers/server/log/today.js index 37bd321a..03491a6e 100644 --- a/web/views/@default/servers/server/log/today.js +++ b/web/views/@default/servers/server/log/today.js @@ -12,4 +12,47 @@ Tea.context(function () { accessLog.wafInfo = null } }) + + this.query = function (args) { + // 初始化时页面尚未设置Vue变量,所以使用全局的变量获取 + let that = TEA.ACTION.data + + if (that.serverId == null) { + that.serverId = 0 + } + if (that.keyword == null) { + that.keyword = "" + } + if (that.ip == null) { + that.ip = "" + } + if (that.domain == null) { + that.domain = "" + } + if (that.pageSize == null) { + that.pageSize = "" + } + let query = 'serverId=' + that.serverId + '&keyword=' + encodeURIComponent(that.keyword) + '&ip=' + that.ip + '&domain=' + that.domain + '&pageSize=' + that.pageSize + if (args != null && args.length > 0) { + query += "&" + args + } + return query + } + + this.allQuery = function () { + if (this.query == null) { + // 尚未初始化完成 + return + } + let query = this.query() + if (this.hasError == 1) { + query += "&hasError=1" + } + if (this.hasWAF == 1) { + query += "&hasWAF=1" + } + return query + } + + this.currentQuery = this.allQuery() }) \ No newline at end of file