summaryrefslogtreecommitdiff
path: root/static/js/stats.js
blob: 00e00a0f650483b58d4324d9eef5cbe8395eeccf (plain)
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
var CurrentStats = ['msgs', 'today'];

function refreshStats() {
    $('a.statname, a.timescale').addClass('disabled');
    $('#statschart').empty().append('<img src="/static/spinner.gif">');

    var onSuccess = function(json) {
        var dataString = "Date,Count\n";
        $.each(json, function(i, v) {
            dataString += v.created_on + "," + v.count + "\n";
        });
        $('#statschart').empty();
        new Dygraph($('#statschart')[0],
                    dataString,
                    { showRoller: false}
                   );
        
        $('a.statname, a.timescale').removeClass('disabled');
    };

    var onError = function(resp, textStatus, errorThrown) {
        $('a.statname, a.timescale').removeClass('disabled');
    };

    $.ajax({
        type: 'GET',
        timeout: 5000,
        url: '/stats',
        data: { 'stat': CurrentStats[0], 'ts': CurrentStats[1] },
        cache: false,
        dataType: 'json',
        success: onSuccess,
        error: onError
    });   
}

function makeStatsWindow() {
    var statBox= $('<div class="stats" align="center">');    
    var choices = ['msgs', 'new users'];
    $(choices).map(function(i, c) {
        return link = $('<a>')
            .text(c)
            .attr('href', '#')
            .addClass('statname') 
            .addClass(i == 0 ? 'active' : 'inactive')
            .click(function() {
                if ($(this).hasClass('disabled') || 
                    CurrentStats[0] == c) { return };
                $('.statname').removeClass('active');
                $(this).addClass('active');
                CurrentStats[0] = c;
                refreshStats();
            });
    }).appendTo(statBox);
    $('<div id="statschart" style="width:375px; height:300px;">').appendTo(statBox);
    var timescales = ['today', 'all time'];
    $(timescales).map(function(i, c) {
        return link = $('<a>')
            .text(c)
            .attr('href', '#')
            .addClass('timescale') 
            .addClass(i == 0 ? 'active' : 'inactive')            
            .click(function() {
                if ($(this).hasClass('disabled') || 
                    CurrentStats[1] == c) { return };
                $('.timescale').removeClass('active');
                $(this).addClass('active');
                CurrentStats[1] = c;
                refreshStats();
            });
    })//.appendTo(statBox);
    return statBox;
}

function initStats() {
    makeStatsWindow().appendTo('body');
    refreshStats();
}