log_view.hbs
8.39 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<div class="pageheader">
<div class="media">
<div class="pageicon pull-left">
<i class="fa fa-th-list"></i>
</div>
<div class="media-body">
<ul class="breadcrumb">
<li><a href=""><i class="glyphicon glyphicon-home"></i></a></li>
<li>监控中心</li>
</ul>
<h4>运行日志</h4>
</div>
</div>
<!-- media -->
</div>
<!-- pageheader -->
<div class="contentpanel">
<div class="row">
<div class="col-sm-4 col-md-3">
<h4 class="md-title mb5">项目列表</h4>
<select id="projects" data-placeholder="Choose a Project..." class="width100p">
<option value="">Choose One</option>
{{#each projects}}
<option value="{{_id}}" {{#if checked}}selected{{/if}}>{{subname}}</option>
{{/each}}
</select>
<div class="mb20"></div>
<h4 class="md-title mb5">运行环境</h4>
{{#each env}}
<div class="radio"><label><input type="radio" {{#if checked}}checked=""{{/if}} value="{{value}}"
name="env"> {{name}}</label></div>
{{/each}}
<div class="mb20"></div>
<h4 class="md-title mb5">Host列表</h4>
<select id="hosts" data-placeholder="Choose a Host..." class="width100p">
<option value="">Choose One</option>
{{#each hosts}}
<option value="{{this}}" {{#equals this @root.ip}}selected{{/equals}}>{{this}}</option>
{{/each}}
</select>
<div class="mb20"></div>
<button class="btn btn-success" id="load-btn">加载</button>
<br>
</div><!-- col-sm-4 -->
<div class="col-sm-8 col-md-9">
<div class="panel">
<div class="panel-heading">
<div class="pull-right">
<a class="btn btn-success btn-rounded mr20 realtime-btn">开启实时加载</a>
</div>
<h4 class="panel-title">日志</h4>
<p>可通过滚动鼠标加载</p>
</div><!-- panel-heading -->
<div class="panel-body yoho-log-dark">
<div class="results-list ">
</div><!-- results-list -->
</div><!-- panel-body -->
</div><!-- panel -->
</div><!-- col-sm-8 -->
</div><!-- row -->
</div>
<script>
$(document).on('ready pjax:success', function() {
$('#projects').select2();
$('#hosts').select2();
var pid = $('#projects').val();
var env = $('input[name="env"]:checked').val();
var projects = {{{json projects}}};
var $logs = $('.yoho-log-dark .results-list');
var $logWrap = $('.yoho-log-dark');
var oldTime, newTime;
var influxName, ip;
var loading = false;
function changeIp(pid, env) {
if (pid && env) {
for (var i = 0; i < projects.length; i++) {
var p = projects[i];
if (p._id === pid) {
var hosts = p.deploy[env].target;
$('#hosts').select2("destroy");
$('#hosts').empty();
$('#hosts').append('<option value="">全部</option>');
for (var j = 0; j < hosts.length; j++) {
$('#hosts').append('<option value="' + hosts[j] + '">' + hosts[j] + '</option>');
}
$('#hosts').select2();
}
}
}
}
$('#projects').on('change', function(e) {
pid = e.val;
changeIp(pid, env);
});
$('input[name="env"]').change(function(e) {
env = e.target.value;
changeIp(pid, env);
});
$('#load-btn').click(function() {
ip = $('#hosts').val();
for (var i = 0; i < projects.length; i++) {
var p = projects[i];
if (p._id === pid) {
if (p.monitor && p.monitor.influx && p.monitor.influx.name) {
influxName = p.monitor.influx.name;
} else {
$.gritter.add({
title: '警告',
text: '未配置监控信息!',
time: 2000,
class_name: 'growl-danger'
});
}
}
}
if (influxName) {
$logs.empty();
$.get('/monitor/log/query', {
influxName: influxName,
ip: ip
}, function(data) {
$(data).each(function(i) {
if (i == 0) {
newTime = this.time;
} else if (i == data.length - 1) {
oldTime = this.time;
}
appendLog(this, 0);
});
$logWrap.scrollTop($logWrap[0].scrollHeight);
});
}
});
function layoutResize() {
$('.yoho-log-dark').height($('body').height() - 340);
}
$(window).resize(function() {
layoutResize();
});
layoutResize();
function appendLog(l, i) {
if (l) {
var html = '<p><span class="timer">' + l.time.replace('T', ' ') + '</span>' +
(ip ? '' : '<span class="host">' + l.host + '</span>')
+ '<span class="pid">[' + l.pid + ']</span><span class="level">[' + l.level + ']</span><span class="message">- ' + l.message + '</span></p>';
if (i === 0) {
$logs.prepend(html);
} else {
$logs.append(html);
}
}
}
function loadOld() {
if (loading) {
return;
} else {
loading = true;
var scH = $logWrap[0].scrollHeight;
$.get('/monitor/log/query', {
influxName: influxName,
ip: ip,
oldTime: oldTime
}, function(data) {
loading = false;
if (data && data.length > 0) {
oldTime = data[data.length - 1].time;
}
$(data).each(function() {
appendLog(this, 0);
});
var scH2 = $logWrap[0].scrollHeight;
$logWrap.scrollTop(scH2 - scH);
});
}
}
function loadNew() {
if (loading) {
return;
} else {
loading = true;
var scH = $logWrap[0].scrollHeight;
$.get('/monitor/log/query', {
influxName: influxName,
ip: ip,
newTime: newTime
}, function(data) {
loading = false;
if (data && data.length > 0) {
newTime = data[0].time;
}
$(data).each(function() {
appendLog(this);
});
$logWrap.scrollTop(scH);
});
}
}
var scrollH, scrollTop = 0;
$logWrap.scroll(function() {
scrollH = $(this)[0].scrollHeight;
scrollTop = $(this)[0].scrollTop;
if (scrollTop < 10) {
loadOld();
} else if (scrollTop + $logWrap.height() + 40 >= scrollH) {
loadNew();
}
});
var sc;
$('.realtime-btn').click(function() {
if ($(this).hasClass('active')) {
clearInterval(sc);
$(this).removeClass('active');
$(this).text('开启实时加载');
} else {
$(this).addClass('active');
$(this).text('关闭实时加载');
sc = setInterval(loadNew, 2000);
}
});
});
</script>