{% extends 'base.html' %}
{% block content %}
{% include 'nav_cat_bar.html' %}

<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-lg-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <span class="label label-success pull-right">Monthly</span>
                    <h5>用户总数</h5>
                </div>
                <div class="ibox-content">
                    <h1 class="no-margins"><a href="/juser/user_list/">{{ users.count}}</a></h1>
                    <div class="stat-percent font-bold text-success">98% <i class="fa fa-bolt"></i></div>
                    <small>All user</small>
                </div>
            </div>
        </div>
        <div class="col-lg-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <span class="label label-info pull-right">Annual</span>
                    <h5>主机总数</h5>
                </div>
                <div class="ibox-content">
                    <h1 class="no-margins"><a href="/jasset/host_list/">{{ hosts.count }}</a></h1>
                    <div class="stat-percent font-bold text-info">20% <i class="fa fa-level-up"></i></div>
                    <small>All host</small>
                </div>
            </div>
        </div>

        <div class="col-lg-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <span class="label label-primary pull-right">Today</span>
                    <h5>实时在线用户</h5>
                </div>
                <div class="ibox-content">
                    <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>
                    <small>Online user</small>
                </div>
            </div>
        </div>

        <div class="col-lg-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <span class="label label-danger pull-right">Low value</span>
                    <h5>已连接服务器</h5>
                </div>
                <div class="ibox-content">
                        <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>
                    <small>Connect host</small>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-6" id="top10" style="width:50%;height:400px;"></div>
            <div class="col-lg-6" id="usertop10" style="width:50%;height:400px;"></div>
        </div>
        <div class="row">
            <div class="col-lg-6" id="hosttop10" style="width:50%;height:400px; margin-top: 20px"></div>
        </div>
    </div>
</div>

<script>
var cate = {{ li_str|safe }};
$(function () {
    $('#top10').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: '一周数据总览',
            x: -20 //center
        },
        subtitle: {
            text: 'Source: JumpServer',
            x: -20
        },
        rangeSelector: {
            allButtonsEnabled: true,
            selected: 2
        },
        xAxis: {
            type: 'datetime',
            categories: cate
        },
        yAxis:{
            min: 0,
            title: {
                text: '登录次数'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            valueSuffix: '次'
        },
        navigation: {
            buttonOptions: {
                align: 'right'
            }
        },
        series: [
            {% for k,v in top_dic.items %}
                {
                    name: '{{ k }}',
                    data: {{ v }}
                },
            {% endfor %}
        ]
    });

    $('#usertop10').highcharts({
        title: {
            text: '一周用户登录TOP10',
            x: -20 //center
        },
        subtitle: {
            text: 'Source: JumpServer',
            x: -20
        },
        xAxis: {
            type: 'datetime',
            categories: cate
        },
        yAxis:{
            min: 0,
            title: {
                text: '登录次数'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            valueSuffix: '次'
        },
        series: [
            {% for k,v in user_dic.items %}
                {
                    name: '{{ k }}',
                    data: {{ v }}
                },
            {% endfor %}
        ]
    });

        $('#hosttop10').highcharts({
        title: {
            text: '一周主机登录TOP10',
            x: -20 //center
        },
        subtitle: {
            text: 'Source: JumpServer',
            x: -20
        },
        xAxis: {
            type: 'datetime',
            categories: cate
        },
        yAxis:{
            min: 0,
            title: {
                text: '登录次数'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            valueSuffix: '次'
        },
        series: [
            {% for k,v in host_dic.items %}
                {
                    name: '{{ k }}',
                    data: {{ v }}
                },
            {% endfor %}
        ]
    });

    function magic_number(value, id) {
        var num = $("#"+id);
        num.animate({count: value}, {
            duration: 500,
            step: function() {
                num.text(String(parseInt(this.count)));
            }
        });
    };

    function update() {
        $.getJSON('api/user/',  function(data) {
            var users = data.users;
            var hosts = data.hosts;
            magic_number(users, 'online_users');
            magic_number(hosts, 'online_hosts')
        });
    };

    setInterval(update, 5000); //5秒钟执行一次
    update();

});
</script>

{% endblock %}