Debian Codegraph @

public/js/status.js

raw symbols SCIP
1 /**
2 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
3 * Copyright (C) 2020-2024 Aleksey Andreev (liuch)
4 *
5 * Available at:
6 * https://github.com/liuch/dmarc-srg
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation, either version 3 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 class Status {
22 constructor() {
23 this._data = {};
24 this._hint = null;
25 this._use_filter = null;
26 }
27
28 update(params) {
29 return this._fetch(params || {}).then(function(data) {
30 return data;
31 }).catch(function(err) {
32 Common.displayError(err);
33 });
34 }
35
36 reset() {
37 this._data.emails = null;
38 this._data.error_code = 0;
39 this._update_block();
40 }
41
42 error() {
43 return this._data.error_code && this._data.error_code !== 0 || false;
44 }
45
46 _fetch(params) {
47 let url = new URL("status.php", document.location);
48 let fields = [ "state", "user" ];
49 let s_list = params.settings || [];
50 if (params.page !== "list") this._use_filter = false;
51 if (this._use_filter === null || (params.settings && params.settings.length)) {
52 fields.push("settings");
53 if (this._use_filter === null) s_list.push("status.emails-filter-when-list-filtered");
54 }
55 url.searchParams.set("fields", fields.join(","));
56 if (s_list.length) url.searchParams.set("settings", s_list.join(","));
57
58 return window.fetch(url, {
59 method: "GET",
60 cache: "no-store",
61 headers: HTTP_HEADERS,
62 credentials: "same-origin"
63 }).then(function(resp) {
64 if (!resp.ok) throw new Error("Failed to fetch the status");
65 return resp.json();
66 }).then(function(data) {
67 User.name = data.user && data.user.name || null;
68 User.level = data.user && data.user.level || null;
69 User.auth_type = data.auth_type;
70 this._data = {
71 state: data.state,
72 error_code: data.error_code,
73 message: data.message,
74 emails: null
75 };
76 this._update_block();
77 if (data.error_code === -2) LoginDialog.start();
78 if (!this.error()) {
79 if (data.settings) {
80 let uf = data.settings["status.emails-filter-when-list-filtered"] || null;
81 this._use_filter = (uf === "yes");
82 }
83 this._fetch_statistics();
84 }
85 return data;
86 }.bind(this)).catch(function(err) {
87 this._data = {
88 state: "Err",
89 error_code: -100,
90 message: err.message
91 };
92 this._update_block();
93 throw err;
94 }.bind(this));
95 }
96
97 _fetch_statistics() {
98 let url = new URL("status.php", document.location);
99 url.searchParams.set("fields", "emails");
100 if (this._use_filter) {
101 (new URL(document.location)).searchParams.getAll("filter[]").forEach(function(fval) {
102 url.searchParams.append("filter[]", fval);
103 });
104 }
105 window.fetch(url, {
106 method: "GET",
107 cache: "no-store",
108 headers: HTTP_HEADERS,
109 credentials: "same-origin"
110 }).then(function(resp) {
111 if (!resp.ok) throw new Error("Failed to fetch statistics");
112 return resp.json();
113 }).then(function(data) {
114 Common.checkResult(data);
115 this._data.emails = data.emails;
116 this._update_block();
117 }.bind(this)).catch(function(err) {
118 Common.displayError(err);
119 this._update_block();
120 Notification.add({ text: err.message || "Error!", type: "error" });
121 }.bind(this));
122 }
123
124 _update_block() {
125 if (this._hint) this._hint.reset();
126 this._ensure_element_created();
127 if (this._data.error_code) {
128 let p = { text: "[" + this._data.error_code + "] " + this._data.message, type: "error" };
129 if (this._data.error_code === -2)
130 p.name = "auth";
131 Notification.add(p);
132 }
133 if (!this._data.emails) {
134 this._data.emails = {
135 days: 0,
136 total: -1,
137 spf_aligned: 0,
138 dkim_aligned: 0,
139 dkim_spf_aligned: 0
140 };
141 }
142
143 const data = this._data.emails;
144 const tf = (data.total !== undefined && data.total !== -1);
145 const vals = this._format_statistic_values();
146 Status._element_list.forEach((id, pos) => {
147 this._set_element_value(id, null, vals[pos], tf && Status._element_data[id].color || null);
148 });
149
150 const el = document.getElementById("stat-block");
151 if (typeof(data.days) === "string") {
152 el.setAttribute("title", "Statistics for " + data.days);
153 }
154 else {
155 el.removeAttribute("title");
156 }
157 }
158
159 _formatted_statistic(val, total) {
160 if (total === -1)
161 return "?";
162 if (!total)
163 return "-";
164 if (val === 0)
165 return "0";
166 let rval = Math.round(val / total * 100);
167 return (val > 0 && rval === 0 && "+" || "" ) + rval + "%";
168 }
169
170 _set_element_value(c_el, v_el, value, cname) {
171 if (typeof(c_el) == "string") c_el = document.getElementById("stat-" + c_el);
172 if (cname) {
173 c_el.setAttribute("class", "state-" + cname);
174 } else {
175 c_el.removeAttribute("class");
176 }
177 (v_el || c_el.querySelector(".stat-val")).textContent = value;
178 }
179
180 _format_statistic_values() {
181 const de = this._data.emails;
182 const total = de.total;
183 const vals = [ (total === -1 || total === undefined) && "?" || Common.abbrNumber(total, 10000) ];
184 [
185 de.dkim_spf_aligned,
186 de.dkim_aligned + de.spf_aligned,
187 (total || 0) - de.dkim_spf_aligned - de.dkim_aligned - de.spf_aligned
188 ].forEach(v => vals.push(this._formatted_statistic(v, total)));
189 return vals;
190 }
191
192 _ensure_element_created() {
193 let block = document.getElementById("stat-block");
194 if (block && block.children.length === 0) {
195 const ul = block.appendChild(document.createElement("ul"));
196 Status._element_list.forEach(function(id) {
197 let li = document.createElement("li");
198 let div = document.createElement("div");
199 div.setAttribute("id", "stat-" + id);
200 let val = document.createElement("span");
201 val.setAttribute("class", "stat-val state-text");
202 val.appendChild(document.createTextNode("?"));
203 let msg = document.createElement("span");
204 msg.setAttribute("class", "stat-msg");
205 msg.appendChild(document.createTextNode(Status._element_data[id].text));
206 div.appendChild(val);
207 div.appendChild(msg);
208 li.appendChild(div);
209 ul.appendChild(li);
210 });
211 const hli = ul.appendChild(document.createElement("li"));
212 hli.classList.add("hint");
213 if (!this._hint) this._hint = new HintButton({ content: this._make_hint_content.bind(this) });
214 hli.append(this._hint.element());
215 }
216 }
217
218 _make_hint_content() {
219 const data = this._data.emails;
220 const el = document.createElement("div");
221 const hd = el.appendChild(document.createElement("h4"));
222 const tf = (data.total !== undefined && data.total !== -1);
223 hd.textContent = tf && ("Statistics for " + data.days) || "n/a";
224 const ul = el.appendChild(document.createElement("ul"));
225 const vals = this._format_statistic_values();
226 Status._element_list.forEach((id, pos) => {
227 const li = ul.appendChild(document.createElement("li"));
228 li.appendChild(document.createElement("span")).textContent = Status._element_data[id].text + ": ";
229 const vl = li.appendChild(document.createElement("span"));
230 vl.classList.add("stat-val", "state-text");
231 this._set_element_value(li, vl, vals[pos], tf && Status._element_data[id].color || null);
232 });
233 return el;
234 }
235 }
236
237 Status.instance = function() {
238 if (!this._instance)
239 this._instance = new Status();
240 return this._instance;
241 }
242
243 Status._element_list = [ "processed", "passed", "forwarded", "failed" ];
244
245 Status._element_data = {
246 processed: {
247 text: "Emails processed",
248 color: "blue"
249 },
250 passed: {
251 text: "Fully aligned",
252 color: "green"
253 },
254 forwarded: {
255 text: "Partially aligned",
256 color: "green"
257 },
258 failed: {
259 text: "Not aligned",
260 color: "red"
261 }
262 };