summaryrefslogtreecommitdiffstats
path: root/extensions/OrangeFactor/web/js/orange_factor.js
blob: da993580dfa40b46945afe1edee5896168e4ab14 (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
80
81
82
83
84
85
86
87
88
89
90
91
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0. 
 */

YAHOO.namespace('OrangeFactor');

var OrangeFactor = YAHOO.OrangeFactor;

OrangeFactor.dayMs = 24 * 60 * 60 * 1000,
OrangeFactor.limit = 7;

OrangeFactor.getOrangeCount = function (data) {
    data = data.oranges;
    var total = 0,
        days = [],
        date = OrangeFactor.getCurrentDateMs() - OrangeFactor.limit * OrangeFactor.dayMs;
    for(var i = 0; i < OrangeFactor.limit; i++) {
        var iso = OrangeFactor.dateString(new Date(date));
        var count = data[iso] ? data[iso].orangecount : 0;
        days.push(count);
        total += count;
        date += OrangeFactor.dayMs;
    }
    OrangeFactor.displayGraph(days);
    OrangeFactor.displayCount(total);
}

OrangeFactor.displayGraph = function (dayCounts) {
    var max = dayCounts.reduce(function(max, count) {
        return count > max ? count : max;
    });
    var graphContainer = YAHOO.util.Dom.get('orange-graph');
    Dom.removeClass(graphContainer, 'bz_default_hidden');
    YAHOO.util.Dom.setAttribute(graphContainer, 'title',
                                'failures over the past week, max in a day: ' + max);
    var  opts = {
        "percentage_lines":[0.25, 0.5, 0.75],
        "fill_between_percentage_lines": false,
        "left_padding": 0,
        "right_padding": 0,
        "top_padding": 0,
        "bottom_padding": 0,
        "background": "#D0D0D0",
        "stroke": "#000000",
        "percentage_fill_color": "#CCCCFF",
        "scale_from_zero": true,
    };
    new Sparkline('orange-graph', dayCounts, opts).draw();
}

OrangeFactor.displayCount = function (count) {
    var countContainer = YAHOO.util.Dom.get('orange-count');
    countContainer.innerHTML = encodeURIComponent(count) + 
                               ' failures on trunk in the past week';
}

OrangeFactor.dateString = function (date) {
    function norm(part) {
        return JSON.stringify(part).length == 2 ? part : '0' + part;
    }
    return date.getFullYear()
           + "-" + norm(date.getMonth() + 1)
           + "-" + norm(date.getDate());
}

OrangeFactor.getCurrentDateMs = function () {
    var d = new Date;
    return d.getTime();
}

OrangeFactor.orangify = function () {
    var bugId = document.forms['changeform'].id.value;
    var url = "https://brasstacks.mozilla.com/orangefactor/api/count?" +
              "bugid=" + encodeURIComponent(bugId) + 
              "&tree=trunk" +
              "&callback=OrangeFactor.getOrangeCount";
    var script = document.createElement('script');
    Dom.setAttribute(script, 'src', url);
    Dom.setAttribute(script, 'type', 'text/javascript');
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(script);
    var countContainer = YAHOO.util.Dom.get('orange-count');
    Dom.removeClass(countContainer, 'bz_default_hidden');
    countContainer.innerHTML = 'Loading...';a
}

YAHOO.util.Event.onDOMReady(OrangeFactor.orangify);