aboutsummaryrefslogtreecommitdiffstats
path: root/interface/js/app/stats.js
blob: e2724af4756009bae535a350792e0b31071a0c33 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 The MIT License (MIT)

 Copyright (C) 2017 Vsevolod Stakhov <vsevolod@highsecure.ru>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */

/* global d3:false */

define(["jquery", "d3pie"],
    function ($) {
        "use strict";
        // @ ms to date
        function msToTime(seconds) {
            /* eslint-disable no-bitwise */
            var years = seconds / 31536000 >> 0; // 3600*24*365
            var months = seconds % 31536000 / 2628000 >> 0; // 3600*24*365/12
            var days = seconds % 31536000 % 2628000 / 86400 >> 0; // 24*3600
            var hours = seconds % 31536000 % 2628000 % 86400 / 3600 >> 0;
            var minutes = seconds % 31536000 % 2628000 % 86400 % 3600 / 60 >> 0;
            /* eslint-enable no-bitwise */
            var out = null;
            if (years > 0) {
                if (months > 0) {
                    out = years + "yr " + months + "mth";
                } else {
                    out = years + "yr " + days + "d";
                }
            } else if (months > 0) {
                out = months + "mth " + days + "d";
            } else if (days > 0) {
                out = days + "d " + hours + "hr";
            } else if (hours > 0) {
                out = hours + "hr " + minutes + "min";
            } else {
                out = minutes + "min";
            }
            return out;
        }

        function displayStatWidgets(checked_server) {
            var widgets = $("#statWidgets");
            $(widgets).empty().hide();

            var servers = JSON.parse(sessionStorage.getItem("Credentials"));
            var data = {};

            if (servers && servers[checked_server]) {
                data = servers[checked_server].data;
            }
            var stat_w = [];

            $.each(data, function (i, item) {
                var widgetsOrder = ["scanned", "no action", "greylist", "add header", "rewrite subject", "reject", "learned"];

                function widget(k, v, cls) {
                    var c = (typeof cls === "undefined") ? "" : cls;
                    var titleAtt = d3.format(",")(v) + " " + k;
                    return '<div class="card stat-box d-inline-block text-center bg-light shadow-sm mr-3 px-3">' +
                      '<div class="widget overflow-hidden p-2' + c + '" title="' + titleAtt +
                      '"><strong class="d-block mt-2 mb-1 font-weight-bold">' +
                    d3.format(".3~s")(v) + "</strong>" + k + "</div></div>";
                }

                if (i === "auth" || i === "error") return; // Skip to the next iteration
                if (i === "uptime" || i === "version") {
                    var cls = "border-right ";
                    var val = item;
                    if (i === "uptime") {
                        cls = "";
                        val = msToTime(item);
                    }
                    $('<div class="' + cls + 'float-left px-3"><strong class="d-block mt-2 mb-1 font-weight-bold">' +
                      val + "</strong>" + i + "</div>")
                        .appendTo(widgets);
                } else if (i === "actions") {
                    $.each(item, function (action, count) {
                        stat_w[widgetsOrder.indexOf(action)] = widget(action, count);
                    });
                } else {
                    stat_w[widgetsOrder.indexOf(i)] = widget(i, item, " text-capitalize");
                }
            });
            $.each(stat_w, function (i, item) {
                $(item).appendTo(widgets);
            });
            $("#statWidgets > div:not(.stat-box)")
                .wrapAll('<div class="card stat-box text-center bg-light shadow-sm float-right">' +
                  '<div class="widget overflow-hidden p-2 text-capitalize"></div></div>');
            $("#statWidgets").find("div.float-right").appendTo("#statWidgets");

            $("#clusterTable tbody").empty();
            $("#selSrv").empty();
            $.each(servers, function (key, val) {
                var row_class = "danger";
                var glyph_status = "fas fa-times";
                var short_id = "???";
                if (!("config_id" in val.data)) {
                    val.data.config_id = "";
                }
                if (val.status) {
                    row_class = "success";
                    glyph_status = "fas fa-check";
                    short_id = val.data.config_id.substring(0, 8);
                }

                $("#clusterTable tbody").append("<tr class=\"" + row_class + "\">" +
                "<td class=\"align-middle\"><input type=\"radio\" class=\"form-check m-auto\" name=\"clusterName\" value=\"" + key + "\"></td>" +
                "<td>" + key + "</td>" +
                "<td>" + val.host + "</td>" +
                "<td class=\"text-center\"><span class=\"icon\"><i class=\"" + glyph_status + "\"></i></span></td>" +
                "<td>" + short_id + "</td></tr>");

                $("#selSrv").append($("<option value=\"" + key + "\">" + key + "</option>"));

                if (checked_server === key) {
                    $("#clusterTable tbody [value=\"" + key + "\"]").prop("checked", true);
                    $("#selSrv [value=\"" + key + "\"]").prop("selected", true);
                } else if (!val.status) {
                    $("#clusterTable tbody [value=\"" + key + "\"]").prop("disabled", true);
                    $("#selSrv [value=\"" + key + "\"]").prop("disabled", true);
                }
            });
            $(widgets).show();
        }

        function getChart(rspamd, pie, checked_server) {
            var creds = JSON.parse(sessionStorage.getItem("Credentials"));
            if (!creds || !creds[checked_server]) return null;

            var data = creds[checked_server].data.actions;
            var new_data = [{
                color: "#66CC00",
                label: "no action",
                data: data["no action"],
                value: data["no action"]
            }, {
                color: "#BF8040",
                label: "soft reject",
                data: data["soft reject"],
                value: data["soft reject"]
            }, {
                color: "#FFAD00",
                label: "add header",
                data: data["add header"],
                value: data["add header"]
            }, {
                color: "#FF6600",
                label: "rewrite subject",
                data: data["rewrite subject"],
                value: data["rewrite subject"]
            }, {
                color: "#436EEE",
                label: "greylist",
                data: data.greylist,
                value: data.greylist
            }, {
                color: "#FF0000",
                label: "reject",
                data: data.reject,
                value: data.reject
            }];

            return rspamd.drawPie(pie, "chart", new_data);
        }
        // Public API
        var ui = {
            statWidgets: function (rspamd, graphs, checked_server) {
                rspamd.query("stat", {
                    success: function (neighbours_status) {
                        var neighbours_sum = {
                            version: neighbours_status[0].data.version,
                            uptime: 0,
                            scanned: 0,
                            learned: 0,
                            actions: {
                                "no action": 0,
                                "add header": 0,
                                "rewrite subject": 0,
                                "greylist": 0,
                                "reject": 0,
                                "soft reject": 0,
                            }
                        };
                        var status_count = 0;
                        for (var e in neighbours_status) {
                            if (neighbours_status[e].status === true) {
                                // Remove alert status
                                localStorage.removeItem(e + "_alerted");

                                var data = neighbours_status[e].data;
                                for (var action in neighbours_sum.actions) {
                                    if ({}.hasOwnProperty.call(neighbours_sum.actions, action)) {
                                        neighbours_sum.actions[action] += data.actions[action];
                                    }
                                }
                                var items = ["learned", "scanned", "uptime"];
                                for (var i in items) {
                                    if ({}.hasOwnProperty.call(items, i)) {
                                        neighbours_sum[items[i]] += data[items[i]];
                                    }
                                }
                                status_count++;
                            }
                        }
                        neighbours_sum.uptime = Math.floor(neighbours_sum.uptime / status_count);
                        var to_Credentials = {};
                        to_Credentials["All SERVERS"] = {
                            name: "All SERVERS",
                            url: "",
                            host: "",
                            checked: true,
                            data: neighbours_sum,
                            status: true
                        };
                        neighbours_status.forEach(function (elmt) {
                            to_Credentials[elmt.name] = elmt;
                        });
                        sessionStorage.setItem("Credentials", JSON.stringify(to_Credentials));
                        displayStatWidgets(checked_server);
                        graphs.chart = getChart(rspamd, graphs.chart, checked_server);
                    },
                    errorMessage: "Cannot receive stats data",
                    errorOnceId: "alerted_stats_",
                    server: "All SERVERS"
                });
            },
        };

        return ui;
    }
);