summaryrefslogtreecommitdiffstats
path: root/extensions/OrangeFactor/web/js/orange_factor.js
blob: fa9411cf84f603b006aa4be59e185a1b33518f3f (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
92
93
/* 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. 
 */

$(function() {
    'use strict';
    var dayMs = 24 * 60 * 60 * 1000;
    var limit = 7;

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

    function displayGraph(dayCounts) {
        var max = dayCounts.reduce(function(max, count) {
            return count > max ? count : max;
        });
        $('#orange-graph')
            .attr('title', 'failures over the past week, max in a day: ' + max)
            .show();
        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();
    }

    function displayCount(count) {
        $('#orange-count').text(count + ' failures on trunk in the past week');
    }

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

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

    function orangify() {
        $('#orange-count')
            .text('Loading...')
            .show();
        var bugId = document.forms['changeform'].id.value;
        var request = {
            dataType: "json",
            xhrFields: {
                withCredentials: true
            },
            url: "https://brasstacks.mozilla.com/orangefactor/api/count?" +
                 "bugid=" + encodeURIComponent(bugId) + "&tree=trunk"
        };
        $.ajax(request)
            .done(function(data) {
                getOrangeCount(data);
            })
            .fail(function() {
                $('#orange-count').text('Please sign into OrangeFactor first');
            });
    }

    orangify();
});