-
Notifications
You must be signed in to change notification settings - Fork 10
/
html.go
149 lines (144 loc) · 4.33 KB
/
html.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package boot
import (
"encoding/json"
"github.com/dream-mo/prom-elastic-alert/conf"
redisx "github.com/dream-mo/prom-elastic-alert/utils/redis"
"github.com/dream-mo/prom-elastic-alert/utils/xelastic"
"github.com/dream-mo/prom-elastic-alert/utils/xtime"
"html/template"
"net/http"
)
func RenderAlertMessage(writer http.ResponseWriter, request *http.Request) {
q := request.URL.Query()
key := q.Get("key")
if key == "" {
_, _ = writer.Write([]byte(""))
return
} else {
var message AlertSampleMessage
v, e := redisx.Client.Get(ctx, key).Result()
if e != nil {
_, _ = writer.Write([]byte(""))
} else {
err := json.Unmarshal([]byte(v), &message)
if err != nil {
_, _ = writer.Write([]byte(""))
} else {
t, _ := template.New("index.html").Funcs(template.FuncMap{
"json": func(v any) string {
res, _ := json.Marshal(v)
return string(res)
},
"showTime": func(v map[string]any) string {
m := v["_source"].(map[string]any)
return xtime.TimeFormatISO8601(xtime.Parse(m["@timestamp"].(string)))
},
}).Parse(htmlPage)
body := conf.BuildFindByIdsDSLBody(message.Ids)
client := xelastic.NewElasticClient(message.ES, message.ES.Version)
hits, _, _ := client.FindByDSL(message.Index, body, nil)
hitsStr, _ := json.Marshal(hits)
_ = t.Execute(writer, map[string]any{
"hitsStr": string(hitsStr),
"hits": hits,
})
}
}
}
}
var htmlPage = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>日志告警详细信息</title>
<link type="text/css" href="http://summerstyle.github.io/jsonTreeViewer/libs/jsonTree/jsonTree.css" rel="stylesheet"/>
<link type="text/css" href="https://www.layuicdn.com/layui-v2.7.4/css/layui.css" rel="stylesheet"/>
</head>
<style type="text/css">
table {
table-layout: fixed;
}
table td {
word-wrap: break-word;
}
.jsontree_node {
width: 95%;
word-wrap: break-word;
display: inline-block;
}
</style>
<body>
<div style="margin: 10px">
<blockquote class="layui-elem-quote">告警日志-取样文档</blockquote>
<table class="layui-table" lay-size="sm">
<colgroup>
<col width="110">
<col width="700">
<col width="60">
</colgroup>
<thead>
<tr>
<th>@timestamp</th>
<th>内容</th>
<th style="text-align: center">操作</th>
</tr>
</thead>
<tbody>
{{range $i, $v := .hits}}
<tr>
<td>{{showTime $v}}</td>
<td>{{json $v}}</td>
<td align="center">
<button onclick='jsonViewer({{$i}})' class="layui-btn layui-btn-xs">查看格式化日志</button>
</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</body>
<script type="text/javascript" src="http://summerstyle.github.io/jsonTreeViewer/libs/jsonTree/jsonTree.js"></script>
<script type="text/javascript" src="https://www.layuicdn.com/layui-v2.7.4/layui.js"></script>
<script>
let listString = {{.hitsStr}}
function renderJson(jsonContent, wrapperId) {
// init wrapper
let wrapper = document.getElementById(wrapperId);
if (typeof jsonContent === 'string') {
try {
console.log(jsonContent)
var data = JSON.parse(jsonContent)
} catch (e) {
console.log(e)
var data = {"message": jsonContent}
}
} else {
data = jsonContent;
}
let tree = jsonTree.create(data, wrapper);
tree.expand()
}
function jsonViewer(index) {
let lists = JSON.parse(listString)
let jsonContent = lists[index];
let indexVal = index + 1
let title = '【第' + indexVal + '条】日志内容'
let warp = 'wrapper_' + indexVal
layer.open({
id: warp,
type: 1,
title: title,
skin: 'layui-layer-rim',
area: ['80%', '80%'],
content: '<div style="overflow-x: scroll" ' + 'id="' + warp + '"></div>',
shadeClose: true,
maxmin: true,
fixed: false
});
renderJson(jsonContent, warp)
}
</script>
</html>
`