From 0174da6c7798cfb78d19ddf8c74f2314ad39b785 Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 10 Nov 2015 15:50:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9index=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jumpserver/views.py | 154 ++++++++++++++++++---------------------- templates/index.html | 162 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 209 insertions(+), 107 deletions(-) diff --git a/jumpserver/views.py b/jumpserver/views.py index 8f7d8c164..00f709ffd 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -36,42 +36,44 @@ def getDaysByNum(num): return date_li, date_str -def get_data(data, items, option): - dic = {} - li_date, li_str = getDaysByNum(7) - for item in items: - li = [] - name = item[option] - if option == 'user': - option_data = data.filter(user=name) - elif option == 'host': - option_data = data.filter(host=name) - for t in li_date: - year, month, day = t.year, t.month, t.day - times = option_data.filter(start_time__year=year, start_time__month=month, start_time__day=day).count() - li.append(times) - dic[name] = li - return dic +def get_data(x, y, z): + pass -def get_count_by_day(date_li, item): +def get_data_by_day(date_li, item): data_li = [] for d in date_li: logs = Log.objects.filter(start_time__year=d.year, start_time__month=d.month, start_time__day=d.day) if item == 'user': - count = len(set([log.user for log in logs])) + data_li.append(set([log.user for log in logs])) elif item == 'asset': - count = len(set([log.host for log in logs])) + data_li.append(set([log.host for log in logs])) elif item == 'login': - count = len(logs) + data_li.append(logs) else: - count = 0 - data_li.append(count) + pass return data_li +def get_count_by_day(date_li, item): + data_li = get_data_by_day(date_li, item) + data_count_li = [] + for data in data_li: + data_count_li.append(len(data)) + return data_count_li + + +def get_count_by_date(date_li, item): + data_li = get_data_by_day(date_li, item) + data_count_tmp = [] + for data in data_li: + data_count_tmp.extend(list(data)) + + return len(set(data_count_tmp)) + + @require_role(role='user') def index_cu(request): user_id = request.user.id @@ -102,6 +104,7 @@ def index(request): return index_cu(request) elif is_role_request(request, 'super'): + # dashboard 显示汇总 users = User.objects.all() hosts = Asset.objects.all() online = Log.objects.filter(is_finished=0) @@ -109,75 +112,52 @@ def index(request): online_user = online.values('user').distinct() active_users = User.objects.filter(is_active=1) active_hosts = Asset.objects.filter(is_active=1) - week_data = Log.objects.filter(start_time__range=[from_week, datetime.datetime.now()]) + # 一个月历史汇总 date_li, date_str = getDaysByNum(30) date_month = repr(date_str) - print date_month - active_user_month = str(get_count_by_day(date_li, 'user')) - active_asset_month = str(get_count_by_day(date_li, 'asset')) - active_login_month = str(get_count_by_day(date_li, 'login')) + active_user_per_month = str(get_count_by_day(date_li, 'user')) + active_asset_per_month = str(get_count_by_day(date_li, 'asset')) + active_login_per_month = str(get_count_by_day(date_li, 'login')) + + # 活跃用户资产图 + active_user_month = get_count_by_date(date_li, 'user') + disabled_user_count = len(users.filter(is_active=False)) + inactive_user_month = len(users) - active_user_month + active_asset_month = get_count_by_date(date_li, 'asset') + disabled_asset_count = len(hosts.filter(is_active=False)) if hosts.filter(is_active=False) else 0 + inactive_asset_month = len(hosts) - active_asset_month if len(hosts) > active_asset_month else 0 + + # 一周top10用户和主机 + week_data = Log.objects.filter(start_time__range=[from_week, datetime.datetime.now()]) + user_top_ten = week_data.values('user').annotate(times=Count('user')).order_by('-times')[:10] + host_top_ten = week_data.values('host').annotate(times=Count('host')).order_by('-times')[:10] + + for user_info in user_top_ten: + username = user_info.get('user') + last = Log.objects.filter(user=username).latest('start_time') + user_info['last'] = last + + for host_info in host_top_ten: + host = host_info.get('host') + last = Log.objects.filter(host=host).latest('start_time') + host_info['last'] = last + + # 一周top5 + week_users = week_data.values('user').distinct().count() + week_hosts = week_data.count() + + user_top_five = week_data.values('user').annotate(times=Count('user')).order_by('-times')[:5] + color = ['label-success', 'label-info', 'label-primary', 'label-default', 'label-warnning'] + + # 最后10次权限申请 + # perm apply latest 10 + # perm_apply_10 = Apply.objects.order_by('-date_add')[:10] + + # 最后10次登陆 + login_10 = Log.objects.order_by('-start_time')[:10] + login_more_10 = Log.objects.order_by('-start_time')[10:21] - elif is_role_request(request, 'admin'): - return index_cu(request) - # user = get_session_user_info(request)[2] - # users = User.objects.filter(dept=dept) - # hosts = Asset.objects.filter(dept=dept) - # online = Log.objects.filter(dept_name=dept_name, is_finished=0) - # online_host = online.values('host').distinct() - # online_user = online.values('user').distinct() - # active_users = users.filter(is_active=1) - # active_hosts = hosts.filter(is_active=1) - # week_data = Log.objects.filter(dept_name=dept_name, start_time__range=[from_week, datetime.datetime.now()]) - - # percent of dashboard - if users.count() == 0: - percent_user, percent_online_user = '0%', '0%' - else: - percent_user = format(active_users.count() / users.count(), '.0%') - percent_online_user = format(online_user.count() / users.count(), '.0%') - if hosts.count() == 0: - percent_host, percent_online_host = '0%', '0%' - else: - percent_host = format(active_hosts.count() / hosts.count(), '.0%') - percent_online_host = format(online_host.count() / hosts.count(), '.0%') - - user_top_ten = week_data.values('user').annotate(times=Count('user')).order_by('-times')[:10] - host_top_ten = week_data.values('host').annotate(times=Count('host')).order_by('-times')[:10] - user_dic, host_dic = get_data(week_data, user_top_ten, 'user'), get_data(week_data, host_top_ten, 'host') - - # a week data - week_users = week_data.values('user').distinct().count() - week_hosts = week_data.count() - - user_top_five = week_data.values('user').annotate(times=Count('user')).order_by('-times')[:5] - color = ['label-success', 'label-info', 'label-primary', 'label-default', 'label-warnning'] - - # perm apply latest 10 - # perm_apply_10 = Apply.objects.order_by('-date_add')[:10] - - # latest 10 login - login_10 = Log.objects.order_by('-start_time')[:10] - login_more_10 = Log.objects.order_by('-start_time')[10:21] - - # a week top 10 - for user_info in user_top_ten: - username = user_info.get('user') - last = Log.objects.filter(user=username).latest('start_time') - user_info['last'] = last - - top = {'user': '活跃用户数', 'host': '活跃主机数', 'times': '登录次数'} - top_dic = {} - for key, value in top.items(): - li = [] - for t in li_date: - year, month, day = t.year, t.month, t.day - if key != 'times': - times = week_data.filter(start_time__year=year, start_time__month=month, start_time__day=day).values(key).distinct().count() - else: - times = week_data.filter(start_time__year=year, start_time__month=month, start_time__day=day).count() - li.append(times) - top_dic[value] = li return render_to_response('index.html', locals(), context_instance=RequestContext(request)) diff --git a/templates/index.html b/templates/index.html index 550f2f45d..69147f9c3 100644 --- a/templates/index.html +++ b/templates/index.html @@ -14,7 +14,7 @@

{{ users.count}}

-
{{ percent_user }}
+{#
{{ percent_user }}
#} All user
@@ -27,7 +27,7 @@

{{ hosts.count }}

-
{{ percent_host }}
+{#
{{ percent_host }}
#} All host
@@ -40,8 +40,8 @@
实时在线用户
-

10

-
{{ percent_online_user }}
+

{{ online_user | length }}

+{#
{{ percent_online_user }}
#} Online user
@@ -54,8 +54,8 @@
已连接服务器
-

10

-
{{ percent_online_host }}
+

{{ online_host | length }}

+{#
{{ percent_online_host }}
#} Connected host
@@ -151,6 +151,51 @@ + +
+
+
一周Top10资产
+ +
+
+

一周Top10资产

+ 登录次数及最近一次登录记录. +
+
+ {% if host_top_ten %} + {% for data in host_top_ten %} +
+
+
+ + {{ data.host }} +
+ {{ data.times }}次 +
+
+

最近一次登录用户

+

{{ data.last.user }}

+

于{{ data.last.start_time |date:"Y-m-d H:i:s" }}

+
+
+
+ {% endfor %} + {% else %} +

(暂无)

+ {% endif %} +
+
@@ -162,7 +207,7 @@

登录记录

- 最近十次登录记录. + 最近十次登录记录.
@@ -237,7 +282,7 @@

一周Top10用户

- 一周Top10用户登录次数及最近一次登录记录. + 用户登录次数及最近一次登录记录.
{% if user_top_ten %} @@ -251,7 +296,7 @@ {{ data.times }}次
-

最近一次登录

+

最近一次登录主机

{{ data.last.host }}

于{{ data.last.start_time |date:"Y-m-d H:i:s" }}

@@ -272,6 +317,7 @@
+