Skip to content

Commit 9a955ce

Browse files
committed
系统日志采用后端分页
1 parent d45a157 commit 9a955ce

File tree

6 files changed

+100
-69
lines changed

6 files changed

+100
-69
lines changed

commons/views.py

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import datetime
23
import random
34
from django.contrib import auth
@@ -114,49 +115,59 @@ def system_log(request):
114115

115116

116117
# datatables客户端分页(一次性获取所有数据)
118+
# @admin_auth
119+
# def get_system_log(request):
120+
# mongo = MongoOps(settings.MONGODB_HOST, settings.MONGODB_PORT, settings.RECORD_DB, settings.RECORD_COLL)
121+
# start_time = request.GET.get('startTime')
122+
# end_time = request.GET.get('endTime')
123+
# try:
124+
# if start_time and end_time:
125+
# start_time = datetime.datetime.strptime(start_time, '%Y-%m-%d')
126+
# end_time = datetime.datetime.strptime(end_time, '%Y-%m-%d') + datetime.timedelta(1)
127+
# return_data = mongo.find({"datetime": {"$gt": start_time, "$lt": end_time}})
128+
# else:
129+
# return_data = mongo.find()
130+
# return JsonResponse({'code': 200, 'data': return_data, 'msg': '获取成功'}, encoder=JSONEncoder)
131+
# except Exception as e:
132+
# return JsonResponse({'code': 500, 'data': None, 'msg': '获取失败,{}'.format(e)})
133+
134+
# datatables服务端分页
117135
@admin_auth
118136
def get_system_log(request):
119137
mongo = MongoOps(settings.MONGODB_HOST, settings.MONGODB_PORT, settings.RECORD_DB, settings.RECORD_COLL)
138+
draw = int(request.GET.get('draw')) # 记录操作次數
139+
start = int(request.GET.get('start')) # 起始位置
140+
length = int(request.GET.get('length')) # 每页长度
120141
start_time = request.GET.get('startTime')
121142
end_time = request.GET.get('endTime')
143+
log_user = request.GET.get('logUser')
144+
log_path = request.GET.get('logPath')
145+
146+
# search_key = request.GET.get('search[value]') # 搜索关键字
147+
# order_column = request.GET.get('order[0][column]') # 排序字段索引
148+
# order_column = request.GET.get('order[0][dir]') # 排序规则:ase/desc
149+
150+
search_options = {}
122151
try:
152+
if log_user:
153+
search_options.update({"username": {"$regex": f".*{log_user}.*"}})
154+
155+
if log_path:
156+
search_options.update({"path": {"$regex": f".*{log_path}.*"}})
157+
123158
if start_time and end_time:
124159
start_time = datetime.datetime.strptime(start_time, '%Y-%m-%d')
125160
end_time = datetime.datetime.strptime(end_time, '%Y-%m-%d') + datetime.timedelta(1)
126-
return_data = mongo.find({"datetime": {"$gt": start_time, "$lt": end_time}})
127-
else:
128-
return_data = mongo.find()
129-
return JsonResponse({'code': 200, 'data': return_data, 'msg': '获取成功'}, encoder=JSONEncoder)
161+
search_options.update({"datetime": {"$gt": start_time, "$lt": end_time}})
162+
163+
searched_data, _ = mongo.find(search_options, start, length, sort_key='datetime', sort_method=-1)
164+
_, count = mongo.find(search_options)
165+
dic = {
166+
'draw': draw,
167+
'recordsFiltered': count,
168+
'recordsTotal': count,
169+
'data': searched_data
170+
}
171+
return HttpResponse(json.dumps(dic, cls=JSONEncoder), content_type='application/json')
130172
except Exception as e:
131-
return JsonResponse({'code': 500, 'data': None, 'msg': '获取失败,{}'.format(e)})
132-
133-
# datatables服务端分页
134-
# @admin_auth
135-
# def get_system_log(request):
136-
# mongo = MongoOps(settings.MONGODB_HOST, settings.MONGODB_PORT, settings.RECORD_DB, settings.RECORD_COLL)
137-
# draw = int(request.GET.get('draw')) # 记录操作次數
138-
# start = int(request.GET.get('start')) # 起始位置
139-
# length = int(request.GET.get('length')) # 每页长度
140-
# search_key = request.GET.get('search[value]') # 搜索关键字
141-
# # order_column = request.GET.get('order[0][column]') # 排序字段索引
142-
# # order_column = request.GET.get('order[0][dir]') # 排序规则:ase/desc
143-
#
144-
# try:
145-
# if search_key:
146-
# searched_data = mongo.find({"path": {"$regex": f".*{search_key}.*"}}, start, length, sort_key='datetime',
147-
# sort_method=-1)
148-
# count = len(searched_data)
149-
# return_data = searched_data[start:start + length]
150-
# else:
151-
# count = mongo.count()
152-
# return_data = mongo.find({}, start, length, sort_key='datetime', sort_method=-1)
153-
#
154-
# dic = {
155-
# 'draw': draw,
156-
# 'recordsFiltered': count,
157-
# 'recordsTotal': count,
158-
# 'data': return_data
159-
# }
160-
# return HttpResponse(json.dumps(dic, cls=JSONEncoder), content_type='application/json')
161-
# except Exception as e:
162-
# return JsonResponse({'code': 500, 'data': None, 'msg': '获取失败:{}'.format(e)})
173+
return JsonResponse({'code': 500, 'data': None, 'msg': '获取失败:{}'.format(e)})

logs/all.log

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
INFO 2019-08-19 15:58:40 _log Connected (version 2.0, client OpenSSH_5.3)
2-
INFO 2019-08-19 15:58:41 _log Authentication (publickey) failed.
3-
INFO 2019-08-19 15:58:41 _log Authentication (password) successful!
1+
INFO 2019-09-04 15:36:35 _log Connected (version 2.0, client OpenSSH_5.3)
2+
INFO 2019-09-04 15:36:35 _log Authentication (publickey) successful!

task/utils/ans_module_websocket.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ def disconnect(self, code):
4848
def run_model(self, group_ids, host_ids, selected_module_name, custom_model_name, module_args):
4949
gen_resource = GenResource()
5050

51+
group_names = None
5152
if group_ids == ['custom'] or group_ids == ['all']:
5253
resource = gen_resource.gen_host_list(host_ids)
5354
else:
54-
resource = gen_resource.gen_group_dict(group_ids)
55+
resource, group_names = gen_resource.gen_group_dict(group_ids)
5556

5657
host_list = [ServerAssets.objects.get(id=host_id).assets.asset_management_ip for host_id in host_ids]
5758

@@ -66,12 +67,13 @@ def run_model(self, group_ids, host_ids, selected_module_name, custom_model_name
6667
self.redis_instance.set(unique_key, 1)
6768
ans = ANSRunner(resource, become='yes', become_method='sudo', become_user='root', sock=self)
6869
ans.run_module(host_list=host_list, module_name=module_name, module_args=module_args)
70+
ans_server = group_names if group_names else host_list
6971

7072
module_record.delay(ans_user=UserProfile.objects.get(id=self.ans_info['run_user']),
7173
ans_remote_ip=self.ans_info['remote_ip'],
7274
ans_module=module_name,
7375
ans_args=module_args,
74-
ans_server=host_list, ans_result=ans.get_module_results)
76+
ans_server=ans_server, ans_result=ans.get_module_results)
7577
except Exception as e:
7678
self.send('<code style="color: #FF0000">\nansible执行模块出错:{}\n</code>'.format(str(e)))
7779
finally:

task/utils/gen_resource.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,17 @@ def gen_group_dict(self, group_ids):
4949
:return:
5050
"""
5151
resource = {}
52+
group_names = []
5253
for group_id in group_ids:
5354
group_values = {}
5455
group_obj = AnsibleInventory.objects.prefetch_related('ans_group_hosts').get(id=group_id)
56+
group_names.append(group_obj.ans_group_name)
5557
host_ids = [host.id for host in group_obj.ans_group_hosts.all()]
5658
group_values['hosts'] = self.gen_host_list(host_ids)
5759
if group_obj.ans_group_vars:
5860
group_values['group_vars'] = eval(group_obj.ans_group_vars)
5961
resource[group_obj.ans_group_name] = group_values
60-
return resource
62+
return resource, group_names
6163

6264
@staticmethod
6365
def gen_host_dict(group_ids):

templates/system_log.html

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
.key {
4141
color: red;
4242
}
43+
.col-lg-12 .col-sm-2 {
44+
padding-left: 5px;
45+
padding-right: 5px;
46+
}
4347
</style>
4448
<link rel="stylesheet" href="{% static 'jquery-confirm/dist/jquery-confirm.min.css' %}">
4549
{% endblock %}
@@ -49,14 +53,27 @@
4953
<div class="row">
5054
<div class="col-lg-12">
5155
<br>
56+
<div class="col-sm-2">
57+
<div class="form-group">
58+
<div class="input-group">
59+
<input type="text" id="systemLogUser" class="form-control pull-right"
60+
data-date-format="yyyy-mm-dd" placeholder="操作用户">
61+
</div>
62+
</div>
63+
</div>
64+
<div class="col-sm-2">
65+
<div class="form-group">
66+
<div class="input-group">
67+
<input type="text" id="systemLogPath" class="form-control pull-right"
68+
data-date-format="yyyy-mm-dd" placeholder="请求路径">
69+
</div>
70+
</div>
71+
</div>
5272
<div class="col-sm-2">
5373
<div class="form-group">
5474
<div class="input-group">
5575
<input type="text" id="systemLogStartTime" class="form-control pull-right"
5676
data-date-format="yyyy-mm-dd" placeholder="开始时间">
57-
<span class="input-group-addon">
58-
<span class="glyphicon glyphicon-calendar"></span>
59-
</span>
6077
</div>
6178
</div>
6279
</div>
@@ -65,12 +82,12 @@
6582
<div class="input-group">
6683
<input type="text" id="systemLogEndTime" class="form-control pull-right"
6784
data-date-format="yyyy-mm-dd" placeholder="结束时间">
68-
<span class="input-group-addon" onclick="searchUserLog()">
69-
<span class="glyphicon glyphicon-search"
70-
style="cursor: pointer"></span></span>
7185
</div>
7286
</div>
7387
</div>
88+
<div class="col-sm-2">
89+
<button type="button" class="btn btn-block btn-primary" style="width: unset" onclick="searchUserLog()">搜索</button>
90+
</div>
7491
</div>
7592
<div class="col-xs-12">
7693
<!-- /.box -->
@@ -134,7 +151,9 @@ <h4 class="modal-title">请求数据</h4>
134151
$(function () {
135152
table = $('#example1').DataTable({
136153
"processing": true,
137-
// "serverSide": true,------------服务端分页时使用
154+
"serverSide": true, // 服务端分页时使用
155+
searching: false,
156+
ordering: false,
138157
ajax: {
139158
url: "{% url 'get_system_log' %}",
140159
"dataSrc": function (d) {
@@ -161,7 +180,7 @@ <h4 class="modal-title">请求数据</h4>
161180
{data: 'ip'},
162181
{data: 'datetime'},
163182
],
164-
"order": [[6, "desc"]]
183+
// "order": [[6, "desc"]]
165184
});
166185

167186
//Date picker
@@ -183,15 +202,25 @@ <h4 class="modal-title">请求数据</h4>
183202
function searchUserLog() {
184203
let startTime = $('#systemLogStartTime').val();
185204
let endTime = $('#systemLogEndTime').val();
186-
if (startTime.length === 0 || endTime.length === 0) {
205+
let logUser = $('#systemLogUser').val();
206+
let logPath = $('#systemLogPath').val();
207+
if ((startTime.length === 0 && endTime.length !== 0) || (startTime.length !== 0 && endTime.length === 0)) {
208+
$.alert({
209+
title: 'Tips',
210+
type: 'red',
211+
content: '起始时间和截止时间必须同时选择或者同时不选!'
212+
});
213+
return false;
214+
}
215+
if (startTime > endTime) {
187216
$.alert({
188217
title: 'Tips',
189218
type: 'red',
190-
content: '请选择起始时间!'
219+
content: '起始时间不能大于截止时间!'
191220
});
192221
return false;
193222
}
194-
table.settings()[0].ajax.data = {"startTime": startTime, "endTime": endTime};
223+
table.settings()[0].ajax.data = {"startTime": startTime, "endTime": endTime, "logUser": logUser, "logPath": logPath};
195224
table.ajax.reload(function () {
196225
$.alert({
197226
title: 'Tips',

utils/db/mongo_ops.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# -*- coding: utf-8 -*-
22
"""
33
-------------------------------------------------
4-
File Name: get_mongo_log
4+
File Name: mongo_ops
55
Description:
6-
Author: Administrator
6+
Author: pythonzm
77
date: 2018/6/3
88
-------------------------------------------------
99
Change Activity:
@@ -89,10 +89,8 @@ def find(self, query_dict=None, skip=0, limit=0, sort_key=None, sort_method=1):
8989
r = self.coll.find().skip(skip).limit(limit)
9090

9191
results = [i for i in r]
92-
return results
93-
94-
def count(self):
95-
return self.coll.count()
92+
count = len(results)
93+
return results, count
9694

9795
def delete(self, condition, del_all=True):
9896
"""
@@ -118,13 +116,3 @@ def json_res(self, data):
118116
"""
119117
res = JSONEncoder().encode(data)
120118
return res
121-
122-
123-
if __name__ == '__main__':
124-
start_time = '2019-07-08'
125-
end_time = '2019-07-09'
126-
s = datetime.strptime(start_time, '%Y-%m-%d')
127-
e = datetime.strptime(end_time, '%Y-%m-%d')
128-
m = MongoOps('10.1.7.198', 27017, 'records', 'ops')
129-
rs = m.find({"datetime": {"$gt": s, "$lt": e}})
130-
print(rs)

0 commit comments

Comments
 (0)