Debian Codegraph @

public/js/logs.js

raw symbols SCIP
1 /**
2 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
3 * Copyright (C) 2021-2025 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 Logs {
22 constructor() {
23 this._page = null;
24 this._table = null;
25 this._scroll = null;
26 this._filter = null;
27 this._element = document.getElementById("main-block");
28 this._set_btn = null;
29 this._set_dlg = null;
30 this._fetching = false;
31 this._sort = { column: "", direction: "" };
32 }
33
34 display() {
35 this._gen_settings_button();
36 this._make_page_container();
37 this._make_scroll_container();
38 this._make_table();
39 this._scroll.append(this._table.element());
40 this._page.append(this._scroll);
41 this._element.append(this._page);
42 this._ensure_settins_button();
43 this._table.focus();
44 }
45
46 update() {
47 this._filter = Common.getFilterFromURL(new URL(document.location));
48 this._update_table();
49 this._update_settings_button();
50 }
51
52 title() {
53 return "Logs";
54 }
55
56 _fetch_list() {
57 this._table.display_status("wait");
58 this._fetching = true;
59
60 let pos = this._table.last_row_index() + 1;
61
62 let uparams = new URLSearchParams();
63 uparams.set("position", pos);
64 if (this._sort.column && this._sort.direction) {
65 uparams.set("order", this._sort.column);
66 uparams.set("direction", this._sort.direction);
67 }
68 if (this._filter) {
69 [ "success", "source" ].forEach(function(nm) {
70 if (this._filter[nm]) uparams.append("filter[]", `${nm}:${this._filter[nm]}`);
71 }, this);
72 }
73
74 let that = this;
75 return window.fetch("logs.php?" + uparams.toString(), {
76 method: "GET",
77 cache: "no-store",
78 headers: HTTP_HEADERS,
79 credentials: "same-origin"
80 }).then(function(resp) {
81 if (!resp.ok)
82 throw new Error("Failed to fetch the logs");
83 return resp.json();
84 }).then(function(data) {
85 that._table.display_status(null);
86 Common.checkResult(data);
87 if (data.sorted_by) {
88 let cname = data.sorted_by.column;
89 let dir = data.sorted_by.direction;
90 if (that._sort.column !== cname || that._sort.direction !== dir) {
91 that._sort.column = cname;
92 that._sort.direction = dir;
93 that._table.set_sorted(cname, dir);
94 }
95 }
96 let d = { more: data.more };
97 d.rows = data.items.map(function(it) {
98 return new ITableRow(that._make_row_data(it));
99 });
100 let fr = new ITableFrame(d, pos);
101 that._table.add_frame(fr);
102 return fr;
103 }).catch(function(err) {
104 Common.displayError(err);
105 that._table.display_status("error");
106 }).finally(function() {
107 that._fetching = false;
108 });
109 }
110
111 _update_table() {
112 this._table.clear();
113 let that = this;
114 let fr_cnt = -1;
115 let again = function() {
116 let fc = that._table.frames_count()
117 if (fr_cnt < fc && that._scroll.scrollHeight <= that._scroll.clientHeight * 1.5) {
118 fr_cnt = fc;
119 that._fetch_list().then(function(frame) {
120 if (frame && frame.more()) {
121 again();
122 }
123 else {
124 that._table.focus();
125 }
126 });
127 }
128 else {
129 that._table.focus();
130 }
131 };
132 again();
133 }
134
135 _make_page_container() {
136 this._page = document.createElement("div");
137 this._page.classList.add("page-container");
138 }
139
140 _make_scroll_container() {
141 const el = document.createElement("div");
142 el.classList.add("table-wrapper");
143 el.addEventListener("scroll", event => {
144 if (!this._fetching && el.scrollTop + el.clientHeight >= el.scrollHeight * 0.95) {
145 if (this._table.frames_count() === 0 || this._table.more()) {
146 this._fetch_list();
147 }
148 }
149 });
150 this._scroll = el;
151 }
152
153 _gen_settings_button() {
154 if (!this._set_btn) {
155 let btn = document.createElement("span");
156 btn.setAttribute("class", "options-button");
157 btn.appendChild(document.createTextNode("\u{2699}"));
158 btn.addEventListener("click", function(event) {
159 event.preventDefault();
160 this._display_settings_dialog();
161 }.bind(this));
162 this._set_btn = btn;
163 }
164 }
165
166 _ensure_settins_button() {
167 let title_el = document.querySelector("h1");
168 if (!title_el.contains(this._set_btn)) {
169 title_el.appendChild(this._set_btn);
170 }
171 }
172
173 _update_settings_button() {
174 if (this._set_btn) {
175 this._set_btn.classList[ this._filter ? "add" : "remove" ]("active");
176 }
177 }
178
179 _make_table() {
180 this._table = new ITable({
181 class: "main-table small-cards",
182 onclick: function(row) {
183 let data = row.userdata();
184 if (data) {
185 this._display_item_dialog(data);
186 }
187 }.bind(this),
188 onsort: function(col) {
189 let dir = col.sorted() && "toggle" || "descent";
190 this._table.set_sorted(col.name(), dir);
191 this._sort.column = col.name();
192 this._sort.direction = col.sorted();
193 this._update_table();
194 }.bind(this),
195 onfocus: function(el) {
196 scroll_to_element(el, this._scroll);
197 }.bind(this)
198 });
199 [
200 { content: "", class: "cell-status" },
201 { content: "Domain", name: "domain" },
202 { content: "Source" },
203 { content: "Event time", sortable: true, name: "event_time" },
204 { content: "Message" }
205 ].forEach(function(col) {
206 let c = this._table.add_column(col);
207 if (c.name() === this._sort.column) {
208 c.sort(this._sort.direction);
209 }
210 }, this);
211 }
212
213 _make_row_data(d) {
214 let rd = { cells: [], userdata: { id: d.id } };
215 rd.cells.push(new LogsResultCell(d.success));
216 rd.cells.push({ content: d.domain, label: "Domain" });
217 rd.cells.push({ content: d.source, label: "Source" });
218 rd.cells.push({ content: (new Date(d.event_time)).toUIString(), label: "Event time" });
219 rd.cells.push({ content: d.message, label: "Message" });
220 return rd;
221 }
222
223 _display_item_dialog(data) {
224 let dlg = new LogItemDialog(data);
225 this._element.appendChild(dlg.element());
226 let that = this;
227 dlg.show().finally(function() {
228 dlg.element().remove();
229 that._table.focus();
230 });
231 }
232
233 _display_settings_dialog() {
234 let dlg = this._set_dlg;
235 if (!this._set_dlg) {
236 dlg = new ReportLogFilterDialog({ filter: this._filter });
237 this._set_dlg = dlg;
238 }
239 this._element.appendChild(dlg.element());
240 dlg.show().then(function(d) {
241 if (d) {
242 const url = new URL(document.location);
243 url.searchParams.delete("filter[]");
244 for (let k in d) {
245 if (d[k]) url.searchParams.append("filter[]", `${k}:${d[k]}`);
246 }
247 window.history.replaceState(null, "", url);
248 const f = Common.getFilterFromURL(url, this._filter);
249 if (f !== undefined) {
250 this._filter = f;
251 this._update_table();
252 this._update_settings_button();
253 }
254 }
255 }.bind(this)).finally(function() {
256 this._table.focus();
257 }.bind(this));
258 }
259 }
260
261 class LogsResultCell extends ITableCell {
262 constructor(success, props) {
263 props = props || {};
264 let ca = (props.class || "").split(" ");
265 ca.push(success && "state-green" || "state-red");
266 props.class = ca.filter(function(s) { return s.length > 0; }).join(" ");
267 super(success, props);
268 }
269
270 element() {
271 if (!this._element) {
272 super.element().setAttribute("data-label", "Result");
273 }
274 return this._element;
275 }
276
277 value(target) {
278 if (target === "dom") {
279 let div = document.createElement("div");
280 div.setAttribute("class", "state-background status-indicator");
281 if (!this.title) {
282 div.setAttribute("title", this._content && "Ok" || "Failed");
283 }
284 return div;
285 }
286 return this._content;
287 }
288 }
289
290 class LogItemDialog extends ModalDialog {
291 constructor(data) {
292 super({ title: "Log record", buttons: [ "close" ] });
293 this._data = data;
294 this._table = null;
295 this._res_el = null;
296 this._dom_el = null;
297 this._time_el = null; // event_time
298 this._rid_el = null; // report_id
299 this._file_el = null; // filename
300 this._sou_el = null; // source
301 this._msg_el = null; // message
302 }
303
304 _gen_content() {
305 this._table = document.createElement("div");
306 this._table.setAttribute("class", "left-titled");
307 this._content.appendChild(this._table);
308
309 this._time_el = this._insert_row("Event time");
310 this._res_el = this._insert_row("Result");
311 this._res_el.setAttribute("class", "state-text");
312 this._dom_el = this._insert_row("Domain");
313 this._rid_el = this._insert_row("Report Id");
314 this._file_el = this._insert_row("File name");
315 this._sou_el = this._insert_row("Source");
316 this._msg_el = this._insert_row("Message");
317
318 this._fetch_data();
319 }
320
321 _insert_row(text) {
322 let t_el = document.createElement("span");
323 t_el.appendChild(document.createTextNode(text + ": "));
324 this._table.appendChild(t_el);
325 let v_el = document.createElement("span");
326 this._table.appendChild(v_el);
327 return v_el;
328 }
329
330 _fetch_data() {
331 this.display_status("wait", "Getting data...");
332 let uparams = new URLSearchParams();
333 uparams.set("id", this._data.id);
334
335 window.fetch("logs.php?" + uparams.toString(), {
336 method: "GET",
337 cache: "no-store",
338 headers: HTTP_HEADERS,
339 credentials: "same-origin"
340 }).then(resp => {
341 if (!resp.ok) throw new Error("Failed to fetch the log item");
342 return resp.json();
343 }).then(data => {
344 Common.checkResult(data);
345 this._data.domain = data.domain;
346 this._data.report_id = data.report_id;
347 this._data.event_time = new Date(data.event_time);
348 this._data.filename = data.filename;
349 this._data.source = data.source;
350 this._data.success = data.success;
351 this._data.message = data.message;
352 this._update_ui();
353 }).catch(err => {
354 Common.displayError(err);
355 this.display_status("error", err.message);
356 }).finally(() => {
357 this.display_status("wait", null);
358 });
359 }
360
361 _update_ui() {
362 this._time_el.textContent = this._data.event_time.toUIString();
363 if (this._data.success) {
364 this._res_el.textContent = "Ok";
365 this._res_el.parentElement.classList.add("state-green");
366 }
367 else {
368 this._res_el.textContent = "Failed";
369 this._res_el.parentElement.classList.add("state-red");
370 }
371 this._dom_el.textContent = this._data.domain || "n/a";
372 this._rid_el.textContent = this._data.report_id || "n/a";
373 this._file_el.textContent = this._data.filename || "n/a";
374 this._sou_el.textContent = this._data.source;
375 this._msg_el.textContent = this._data.message || "n/a";
376 }
377 }
378
379 class ReportLogFilterDialog extends VerticalDialog {
380 constructor(params) {
381 params ||= {};
382 super({ title: "Filter settings", buttons: [ "apply", "reset" ] });
383 this._data = params;
384 this._ui_data = [
385 { name: "success", title: "Result" },
386 { name: "source", title: "Source" }
387 ];
388 }
389
390 show() {
391 this._update_ui();
392 return super.show();
393 }
394
395 _gen_content() {
396 this._inputs = document.createElement("fieldset");
397 this._inputs.name = "filter";
398 this._inputs.classList.add("round-border", "titled-input");
399 let lg = document.createElement("legend");
400 lg.append("Filter by");
401 this._inputs.appendChild(lg);
402 this._content.appendChild(this._inputs);
403 this._ui_data.forEach(function(ud) {
404 const el = document.createElement("select");
405 el.name = ud.name;
406 ud.element = el;
407 this._insert_input_row(ud.title, el);
408 }, this);
409 const frm = this._content.parentElement;
410 [ [ "", "Any" ], [ "true", "Success" ], [ "false", "Failure" ] ].forEach(function(it) {
411 const op = document.createElement("option");
412 op.setAttribute("value", it[0]);
413 op.append(it[1]);
414 frm.success.appendChild(op);
415 });
416 [
417 [ "", "Any" ], [ "uploaded_file", "Uploaded file" ], [ "email", "Mailbox" ],
418 [ "directory", "Directory" ], [ "remotefs", "Remote FS" ]
419 ].forEach(function(it) {
420 const op = document.createElement("option");
421 op.setAttribute("value", it[0]);
422 op.append(it[1]);
423 frm.source.appendChild(op);
424 });
425 }
426
427 _update_ui() {
428 let f = this._data.filter;
429 this._ui_data.forEach(function(ud) {
430 ud.element.value = f && f[ud.name] || "";
431 });
432 }
433
434 _submit() {
435 this._result = {};
436 this._data.filter = {};
437 this._ui_data.forEach(function(ud) {
438 const el = ud.element;
439 const val = el.options[el.selectedIndex].value;
440 this._result[ud.name] = val;
441 this._data.filter[ud.name] = val;
442 }, this);
443 this.hide();
444 }
445 }