实现实时展示访问日志

This commit is contained in:
GoEdgeLab
2020-10-10 19:22:17 +08:00
parent a507d53b03
commit 8d24063e80
7 changed files with 122 additions and 5 deletions

View File

@@ -0,0 +1,22 @@
Vue.component("http-access-log-box", {
props: ["v-access-log"],
data: function () {
return {
accessLog: this.vAccessLog
}
},
methods: {
formatCost: function (seconds) {
var s = (seconds * 1000).toString();
var pieces = s.split(".");
if (pieces.length < 2) {
return s;
}
return pieces[0] + "." + pieces[1].substr(0, 3);
}
},
template: `<div :style="{'color': (accessLog.status >= 400) ? '#dc143c' : ''}">
{{accessLog.remoteAddr}} [{{accessLog.timeLocal}}] <em>&quot;{{accessLog.requestMethod}} {{accessLog.requestScheme}}://{{accessLog.host}}{{accessLog.requestURI}} <a :href="accessLog.scheme + '://' + accessLog.host + accessLog.requestURI" target="_blank" title="新窗口打开" class="disabled"><i class="external icon tiny"></i> </a> {{accessLog.proto}}&quot; </em> {{accessLog.status}} <span v-if="accessLog.attrs != null && accessLog.attrs['cache_cached'] == '1'">[cached]</span> <span v-if="accessLog.attrs != null && accessLog.attrs['waf_action'] != null && accessLog.attrs['waf_action'].length > 0">[waf {{accessLog.attrs['waf_action']}}]</span> - 耗时:{{formatCost(accessLog.requestTime)}} ms
</div>`
})

View File

@@ -2,5 +2,12 @@
{$template "/left_menu"}
<div class="right-box">
<div class="ui message">此功能暂未开放,敬请期待</div>
<p class="comment" v-if="isLoaded && accessLogs.length == 0">今天暂时还没有访问日志</p>
<table class="ui table selectable" v-if="accessLogs.length > 0">
<!-- 这里之所以需要添加 :key是因为要不然不会刷新显示 -->
<tr v-for="accessLog in accessLogs" :key="accessLog.requestId">
<td><http-access-log-box :v-access-log="accessLog"></http-access-log-box></td>
</tr>
</table>
</div>

View File

@@ -0,0 +1,38 @@
Tea.context(function () {
this.$delay(function () {
this.load()
})
this.hasMore = false
this.accessLogs = []
this.isLoaded = false
this.load = function () {
this.$post("$")
.params({
serverId: this.serverId,
requestId: this.requestId
})
.success(function (resp) {
this.accessLogs = resp.data.accessLogs.concat(this.accessLogs)
let max = 100
if (this.accessLogs.length > max) {
this.accessLogs = this.accessLogs.slice(0, max)
}
this.hasMore = resp.data.hasMore
this.requestId = resp.data.requestId
})
.done(function () {
if (!this.isLoaded) {
this.$delay(function () {
this.isLoaded = true
})
}
// 自动刷新
this.$delay(function () {
this.load()
}, 5000)
})
}
})