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: `