summaryrefslogtreecommitdiffstats
path: root/web/gui/src/dashboard.js/chart-registry.js
blob: 542f4e016947de74e5b47543d745ca89d8ef7b6c (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
94
// *** src/dashboard.js/chart-registry.js

// Chart Registry

// When multiple charts need the same chart, we avoid downloading it
// multiple times (and having it in browser memory multiple time)
// by using this registry.

// Every time we download a chart definition, we save it here with .add()
// Then we try to get it back with .get(). If that fails, we download it.

NETDATA.fixHost = function (host) {
    while (host.slice(-1) === '/') {
        host = host.substring(0, host.length - 1);
    }

    return host;
};

NETDATA.chartRegistry = {
    charts: {},

    globalReset: function () {
        this.charts = {};
    },

    add: function (host, id, data) {
        if (typeof this.charts[host] === 'undefined') {
            this.charts[host] = {};
        }

        //console.log('added ' + host + '/' + id);
        this.charts[host][id] = data;
    },

    get: function (host, id) {
        if (typeof this.charts[host] === 'undefined') {
            return null;
        }

        if (typeof this.charts[host][id] === 'undefined') {
            return null;
        }

        //console.log('cached ' + host + '/' + id);
        return this.charts[host][id];
    },

    downloadAll: function (host, callback) {
        host = NETDATA.fixHost(host);

        let self = this;

        function got_data(h, data, callback) {
            if (data !== null) {
                self.charts[h] = data.charts;

                // update the server timezone in our options
                if (typeof data.timezone === 'string') {
                    NETDATA.options.server_timezone = data.timezone;
                }
            } else {
                NETDATA.error(406, h + '/api/v1/charts');
            }

            if (typeof callback === 'function') {
                callback(data);
            }
        }

        if (netdataSnapshotData !== null) {
            got_data(host, netdataSnapshotData.charts, callback);
        } else {
            $.ajax({
                url: host + '/api/v1/charts',
                async: true,
                cache: false,
                xhrFields: {withCredentials: true} // required for the cookie
            })
                .done(function (data) {
                    data = NETDATA.xss.checkOptional('/api/v1/charts', data);
                    got_data(host, data, callback);
                })
                .fail(function () {
                    NETDATA.error(405, host + '/api/v1/charts');

                    if (typeof callback === 'function') {
                        callback(null);
                    }
                });
        }
    }
};