Files
waf-platform/EdgeAdmin/web/views/@default/servers/server/log/index.js
2026-02-08 02:00:51 +08:00

111 lines
2.6 KiB
JavaScript

Tea.context(function () {
this.$delay(function () {
this.load()
})
this.hasMore = false
this.accessLogs = []
this.isLoaded = false
this.mergeAccessLogs = function (newAccessLogs, regions, wafInfos) {
let mergedLogs = newAccessLogs.concat(this.accessLogs)
let result = []
let seenRequestIds = {}
let that = this
mergedLogs.forEach(function (accessLog) {
if (accessLog == null) {
return
}
let requestId = accessLog.requestId || ""
if (requestId.length > 0) {
if (seenRequestIds[requestId] === true) {
return
}
seenRequestIds[requestId] = true
}
that.formatTime(accessLog)
let region = regions[accessLog.remoteAddr]
if (typeof (region) == "string") {
accessLog.region = region
} else if (typeof accessLog.region != "string") {
accessLog.region = ""
}
let wafInfo = wafInfos[accessLog.firewallRuleSetId]
if (accessLog.firewallRuleSetId > 0 && typeof (wafInfo) == "object") {
accessLog.wafInfo = wafInfo
} else if (typeof accessLog.wafInfo == "undefined") {
accessLog.wafInfo = null
}
result.push(accessLog)
})
return result
}
this.load = function () {
// 如果有弹窗时,暂时不更新
if (teaweb.hasPopup()) {
this.$delay(function () {
this.load()
}, 5000)
return
}
this.$post("$")
.params({
serverId: this.serverId,
requestId: this.requestId,
keyword: this.keyword,
ip: this.ip,
domain: this.domain,
clusterId: this.clusterId,
nodeId: this.nodeId
})
.success(function (resp) {
let newAccessLogs = resp.data.accessLogs || []
this.accessLogs = this.mergeAccessLogs(newAccessLogs, resp.data.regions || {}, resp.data.wafInfos || {})
let max = 100
if (this.accessLogs.length > max) {
this.accessLogs = this.accessLogs.slice(0, max)
}
this.hasMore = resp.data.hasMore
if (typeof resp.data.requestId == "string" && resp.data.requestId.length > 0) {
this.requestId = resp.data.requestId
}
})
.done(function () {
if (!this.isLoaded) {
this.$delay(function () {
this.isLoaded = true
})
}
// 自动刷新
this.$delay(function () {
this.load()
}, 5000)
})
}
this.formatTime = function (accessLog) {
let elapsedSeconds = Math.ceil(new Date().getTime() / 1000) - accessLog.timestamp
if (elapsedSeconds >= 0) {
if (elapsedSeconds < 60) {
accessLog.humanTime = elapsedSeconds + "秒前"
} else if (elapsedSeconds < 3600) {
accessLog.humanTime = Math.ceil(elapsedSeconds / 60) + "分钟前"
} else if (elapsedSeconds < 3600 * 24) {
accessLog.humanTime = Math.ceil(elapsedSeconds / 3600) + "小时前"
}
}
}
})