Commit 10bcae7c authored by halcyon's avatar halcyon

dashboard

parent 95bb96eb
__author__ = 'guanghongwei' __author__ = 'guanghongwei'
import json
from django.http import HttpResponse
from juser.models import User, UserGroup from juser.models import User, UserGroup
from jasset.models import Asset, BisGroup from jasset.models import Asset, BisGroup
from jlog.models import Log
def user_perm_group_api(user): def user_perm_group_api(user):
...@@ -46,3 +51,10 @@ def asset_perm_api(asset): ...@@ -46,3 +51,10 @@ def asset_perm_api(asset):
user_permed_list.extend(user_group.user_set.all()) user_permed_list.extend(user_group.user_set.all())
return user_permed_list return user_permed_list
def api_user(request):
hosts = Log.objects.filter(is_finished=0).count()
users = Log.objects.filter(is_finished=0).values('user').distinct().count()
ret = {'users': users, 'hosts': hosts}
json_data = json.dumps(ret)
return HttpResponse(json_data)
...@@ -6,7 +6,7 @@ urlpatterns = patterns('', ...@@ -6,7 +6,7 @@ urlpatterns = patterns('',
# url(r'^$', 'jumpserver.views.home', name='home'), # url(r'^$', 'jumpserver.views.home', name='home'),
# url(r'^blog/', include('blog.urls')), # url(r'^blog/', include('blog.urls')),
(r'^$', 'jumpserver.views.index'), (r'^$', 'jumpserver.views.index'),
(r'^api/user/$', 'jumpserver.views.api_user'), (r'^api/user/$', 'jumpserver.api.api_user'),
(r'^skin_config/$', 'jumpserver.views.skin_config'), (r'^skin_config/$', 'jumpserver.views.skin_config'),
(r'^install/$', 'jumpserver.views.install'), (r'^install/$', 'jumpserver.views.install'),
(r'^base/$', 'jumpserver.views.base'), (r'^base/$', 'jumpserver.views.base'),
......
...@@ -103,10 +103,6 @@ def index(request): ...@@ -103,10 +103,6 @@ def index(request):
return render_to_response('index.html', locals(), context_instance=RequestContext(request)) return render_to_response('index.html', locals(), context_instance=RequestContext(request))
def api_user(request):
users = Log.objects.filter(is_finished=0).count()
ret = {'users': users}
return HttpResponse(json.dumps(ret))
def skin_config(request): def skin_config(request):
......
...@@ -35,10 +35,10 @@ ...@@ -35,10 +35,10 @@
<div class="ibox float-e-margins"> <div class="ibox float-e-margins">
<div class="ibox-title"> <div class="ibox-title">
<span class="label label-primary pull-right">Today</span> <span class="label label-primary pull-right">Today</span>
<h5>在线用户</h5> <h5>实时在线用户</h5>
</div> </div>
<div class="ibox-content"> <div class="ibox-content">
<h1 class="no-margins"><a href="/jlog/log_list/online/">{{ online_user.count }}</a></h1> <h1 class="no-margins"><a href="/jlog/log_list/online/"> <span id="online_users"></span></a></h1>
<div class="stat-percent font-bold text-navy">44% <i class="fa fa-level-up"></i></div> <div class="stat-percent font-bold text-navy">44% <i class="fa fa-level-up"></i></div>
<small>Online user</small> <small>Online user</small>
</div> </div>
...@@ -52,7 +52,7 @@ ...@@ -52,7 +52,7 @@
<h5>已连接服务器</h5> <h5>已连接服务器</h5>
</div> </div>
<div class="ibox-content"> <div class="ibox-content">
<h1 class="no-margins"><a href="/jlog/log_list/online/">{{ online_host.count }}</a></h1> <h1 class="no-margins"><a href="/jlog/log_list/online/"> <span id="online_hosts"></span></a></h1>
<div class="stat-percent font-bold text-danger">38% <i class="fa fa-level-down"></i></div> <div class="stat-percent font-bold text-danger">38% <i class="fa fa-level-down"></i></div>
<small>Connect host</small> <small>Connect host</small>
</div> </div>
...@@ -208,85 +208,27 @@ $(function () { ...@@ -208,85 +208,27 @@ $(function () {
] ]
}); });
function magic_number(value, id) {
$(document).ready(function() { var num = $("#"+id);
Highcharts.setOptions({ num.animate({count: value}, {
global: { duration: 500,
useUTC: false step: function() {
num.text(String(parseInt(this.count)));
} }
}); });
};
var chart; function update() {
$('#dynamic').highcharts({ $.getJSON('api/user/', function(data) {
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
var series = this.series[0];
setInterval(function () {
jQuery.getJSON('api/user/', function (data) {
var users = data.users; var users = data.users;
var x = (new Date()).getTime(), // current time var hosts = data.hosts;
y = users; magic_number(users, 'online_users');
console.log(users) magic_number(hosts, 'online_hosts')
series.addPoint([x, y], true, true);
}); });
}, 3000); };
}
}
},
title: {
text: '实时在线用户统计'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
min: 0,
title: {
text: '用户数量'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
Highcharts.numberFormat(this.y);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: '实时在线用户数量',
data: (function() {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i++) { setInterval(update, 1000); //5秒钟执行一次
data.push({ update();
x: time + i * 1000,
y: 0
});
}
return data;
})()
}]
});
});
}); });
</script> </script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment