/* DokuWiki Monitor Plugin Script file */ /* 29.08.2025 - 1.0.5 - initial release */ /* Authors: Sascha Leib */ const Monitor = { init: function() { //console.info('Monitor.init()'); // find the plugin basedir: this._baseDir = document.currentScript.src.substring(0, document.currentScript.src.indexOf('/exe/')) + '/plugins/monitor/'; // read the page language from the DOM: this._lang = document.getRootNode().documentElement.lang || this._lang; // get the time offset: this._timeDiff = Monitor.t._getTimeOffset(); // init the sub-objects: Monitor.t._callInit(this); }, _baseDir: null, _lang: 'en', _today: (new Date()).toISOString().slice(0, 10), _timeDiff: '', /* internal tools */ t: { /* helper function to call inits of sub-objects */ _callInit: function(obj) { //console.info('Monitor.t._callInit(obj=',obj,')'); /* call init / _init on each sub-object: */ Object.keys(obj).forEach( (key,i) => { const sub = obj[key]; let init = null; if (typeof sub === 'object' && sub.init) { init = sub.init; } // bind to object if (typeof init == 'function') { const init2 = init.bind(sub); init2(obj); } }); }, /* helper function to calculate the time difference to UTC: */ _getTimeOffset: function() { const now = new Date(); let offset = now.getTimezoneOffset(); // in minutes const sign = Math.sign(offset); // +1 or -1 offset = Math.abs(offset); // always positive let hours = 0; while (offset >= 60) { hours += 1; offset -= 60; } return ( hours > 0 ? sign * hours + ' h' : '') + (offset > 0 ? ` ${offset} min` : ''); } } }; /* everything specific to the "Today" tab is self-contained here: */ Monitor.live = { init: function() { //console.info('Monitor.live.init()'); // set the title: const tDiff = (Monitor._timeDiff != '' ? ` (${Monitor._timeDiff})` : ' (UTC)' ); Monitor.live.status.setTitle(`Showing visits for ${tDiff}`); // init sub-objects: Monitor.t._callInit(this); }, data: { init: function() { //console.info('Monitor.live.data.init()'); // call sub-inits: Monitor.t._callInit(this); // load the first log file: Monitor.live.data.loadLogFile('srv', Monitor.live.data._onServerLogLoaded); }, _onServerLogLoaded: function() { console.info('Monitor.live.data._onServerLogLoaded()'); Monitor.live.data.loadLogFile('log', Monitor.live.data._onClientLogLoaded); }, _onClientLogLoaded: function() { console.info('Monitor.live.data._onClientLogLoaded()'); // Monitor.live.data.loadLogFile('tck'); console.log(Monitor.live.data.model._visitors); }, model: { _visitors: [], // find an already existing visitor record: findVisitor: function(id) { // shortcut to make code more readable: const model = Monitor.live.data.model; // loop over all visitors already registered: for (let i=0; i { if (line.trim() === '') return; // skip empty lines const cols = line.split('\t'); // assign the columns to an object: const data = {}; cols.forEach( (colVal,i) => { colName = columns[i] || `col${i}`; const colValue = (colName == 'ts' ? new Date(colVal) : colVal); data[colName] = colValue; }); // register the visit in the model: switch(type) { case 'srv': Monitor.live.data.model.registerVisit(data); break; case 'log': Monitor.live.data.model.updateVisit(data); break; default: console.warn(`Unknown log type ${type}.`); return; } }); if (onLoaded) { onLoaded(); // callback after loading is finished. } } catch (error) { Monitor.live.status.setError(`Error while loading the ${typeName} log file: ${error.message}.`); } finally { Monitor.live.status.hideBusy("Done."); } } }, status: { setText: function(txt) { const el = document.getElementById('monitor__today__status'); if (el && Monitor.live.status._errorCount <= 0) { el.innerText = txt; } }, setTitle: function(html) { const el = document.getElementById('monitor__today__title'); if (el) { el.innerHTML = html; } }, setError: function(txt) { console.error(txt); Monitor.live.status._errorCount += 1; const el = document.getElementById('monitor__today__status'); if (el) { el.innerText = "An error occured. See the browser log for details!"; el.classList.add('error'); } }, _errorCount: 0, showBusy: function(txt = null) { Monitor.live.status._busyCount += 1; const el = document.getElementById('monitor__today__busy'); if (el) { el.style.display = 'inline-block'; } if (txt) Monitor.live.status.setText(txt); }, _busyCount: 0, hideBusy: function(txt = null) { const el = document.getElementById('monitor__today__busy'); Monitor.live.status._busyCount -= 1; if (Monitor.live.status._busyCount <= 0) { if (el) el.style.display = 'none'; if (txt) Monitor.live.status.setText(txt); } } } }; /* check if the nustat admin panel is open: */ if (document.getElementById('monitor__admin')) { Monitor.init(); }