xref: /plugin/botmon/script.js (revision b8245a63508117568027cae9e68e0e511ffe1d72)
1"use strict";
2/* DokuWiki BotMon Plugin Script file */
3/* 12.09.2025 - 0.3.0 - beta */
4/* Author: Sascha Leib <ad@hominem.info> */
5
6// enumeration of user types:
7const BM_USERTYPE = Object.freeze({
8	'UNKNOWN': 'unknown',
9	'KNOWN_USER': 'user',
10	'PROBABLY_HUMAN': 'human',
11	'LIKELY_BOT': 'likely_bot',
12	'KNOWN_BOT': 'known_bot'
13});
14
15// enumeration of log types:
16const BM_LOGTYPE = Object.freeze({
17	'SERVER': 'srv',
18	'CLIENT': 'log',
19	'TICKER': 'tck'
20});
21
22/* BotMon root object */
23const BotMon = {
24
25	init: function() {
26		//console.info('BotMon.init()');
27
28		// find the plugin basedir:
29		this._baseDir = document.currentScript.src.substring(0, document.currentScript.src.indexOf('/exe/'))
30			+ '/plugins/botmon/';
31
32		// read the page language from the DOM:
33		this._lang = document.getRootNode().documentElement.lang || this._lang;
34
35		// get the time offset:
36		this._timeDiff = BotMon.t._getTimeOffset();
37
38		// init the sub-objects:
39		BotMon.t._callInit(this);
40	},
41
42	_baseDir: null,
43	_lang: 'en',
44	_datestr: (new Date()).toISOString().slice(0, 10),
45	_timeDiff: '',
46
47	/* internal tools */
48	t: {
49		/* helper function to call inits of sub-objects */
50		_callInit: function(obj) {
51			//console.info('BotMon.t._callInit(obj=',obj,')');
52
53			/* call init / _init on each sub-object: */
54			Object.keys(obj).forEach( (key,i) => {
55				const sub = obj[key];
56				let init = null;
57				if (typeof sub === 'object' && sub.init) {
58					init = sub.init;
59				}
60
61				// bind to object
62				if (typeof init == 'function') {
63					const init2 = init.bind(sub);
64					init2(obj);
65				}
66			});
67		},
68
69		/* helper function to calculate the time difference to UTC: */
70		_getTimeOffset: function() {
71			const now = new Date();
72			let offset = now.getTimezoneOffset(); // in minutes
73			const sign = Math.sign(offset); // +1 or -1
74			offset = Math.abs(offset); // always positive
75
76			let hours = 0;
77			while (offset >= 60) {
78				hours += 1;
79				offset -= 60;
80			}
81			return ( hours > 0 ? sign * hours + ' h' : '') + (offset > 0 ? ` ${offset} min` : '');
82		},
83
84		/* helper function to create a new element with all attributes and text content */
85		_makeElement: function(name, atlist = undefined, text = undefined) {
86			var r = null;
87			try {
88				r = document.createElement(name);
89				if (atlist) {
90					for (let attr in atlist) {
91						r.setAttribute(attr, atlist[attr]);
92					}
93				}
94				if (text) {
95					r.textContent = text.toString();
96				}
97			} catch(e) {
98				console.error(e);
99			}
100			return r;
101		},
102
103		/* helper to convert an ip address string to a normalised format: */
104		_ip2Num: function(ip) {
105			if (!ip) {
106				return 'null';
107			} else if (ip.indexOf(':') > 0) { /* IP6 */
108				return (ip.split(':').map(d => ('0000'+d).slice(-4) ).join(''));
109			} else { /* IP4 */
110				return Number(ip.split('.').map(d => ('000'+d).slice(-3) ).join(''));
111			}
112		},
113
114		/* helper function to format a Date object to show only the time. */
115		/* returns String */
116		_formatTime: function(date) {
117
118			if (date) {
119				return date.getHours() + ':' + ('0'+date.getMinutes()).slice(-2) + ':' + ('0'+date.getSeconds()).slice(-2);
120			} else {
121				return null;
122			}
123
124		},
125
126		/* helper function to show a time difference in seconds or minutes */
127		/* returns String */
128		_formatTimeDiff: function(dateA, dateB) {
129
130			// if the second date is ealier, swap them:
131			if (dateA > dateB) dateB = [dateA, dateA = dateB][0];
132
133			// get the difference in milliseconds:
134			let ms = dateB - dateA;
135
136			if (ms > 50) { /* ignore small time spans */
137				const h = Math.floor((ms / (1000 * 60 * 60)) % 24);
138				const m = Math.floor((ms / (1000 * 60)) % 60);
139				const s = Math.floor((ms / 1000) % 60);
140
141				return ( h>0 ? h + 'h ': '') + ( m>0 ? m + 'm ': '') + ( s>0 ? s + 's': '');
142			}
143
144			return null;
145
146		}
147	}
148};
149
150/* everything specific to the "Today" tab is self-contained in the "live" object: */
151BotMon.live = {
152	init: function() {
153		//console.info('BotMon.live.init()');
154
155		// set the title:
156		const tDiff = '(<abbr title="Coordinated Universal Time">UTC</abbr>' + (BotMon._timeDiff != '' ? `, ${BotMon._timeDiff}` : '' ) + ')';
157		BotMon.live.gui.status.setTitle(`Data for <time datetime="${BotMon._datestr}">${BotMon._datestr}</time> ${tDiff}`);
158
159		// init sub-objects:
160		BotMon.t._callInit(this);
161	},
162
163	data: {
164		init: function() {
165			//console.info('BotMon.live.data.init()');
166
167			// call sub-inits:
168			BotMon.t._callInit(this);
169		},
170
171		// this will be called when the known json files are done loading:
172		_dispatch: function(file) {
173			//console.info('BotMon.live.data._dispatch(,',file,')');
174
175			// shortcut to make code more readable:
176			const data = BotMon.live.data;
177
178			// set the flags:
179			switch(file) {
180				case 'rules':
181					data._dispatchRulesLoaded = true;
182					break;
183				case 'bots':
184					data._dispatchBotsLoaded = true;
185					break;
186				case 'clients':
187					data._dispatchClientsLoaded = true;
188					break;
189				case 'platforms':
190					data._dispatchPlatformsLoaded = true;
191					break;
192				default:
193					// ignore
194			}
195
196			// are all the flags set?
197			if (data._dispatchBotsLoaded && data._dispatchClientsLoaded && data._dispatchPlatformsLoaded && data._dispatchRulesLoaded) {
198				// chain the log files loading:
199				BotMon.live.data.loadLogFile(BM_LOGTYPE.SERVER, BotMon.live.data._onServerLogLoaded);
200			}
201		},
202		// flags to track which data files have been loaded:
203		_dispatchBotsLoaded: false,
204		_dispatchClientsLoaded: false,
205		_dispatchPlatformsLoaded: false,
206		_dispatchRulesLoaded: false,
207
208		// event callback, after the server log has been loaded:
209		_onServerLogLoaded: function() {
210			//console.info('BotMon.live.data._onServerLogLoaded()');
211
212			// chain the client log file to load:
213			BotMon.live.data.loadLogFile(BM_LOGTYPE.CLIENT, BotMon.live.data._onClientLogLoaded);
214		},
215
216		// event callback, after the client log has been loaded:
217		_onClientLogLoaded: function() {
218			//console.info('BotMon.live.data._onClientLogLoaded()');
219
220			// chain the ticks file to load:
221			BotMon.live.data.loadLogFile(BM_LOGTYPE.TICKER, BotMon.live.data._onTicksLogLoaded);
222
223		},
224
225		// event callback, after the tiker log has been loaded:
226		_onTicksLogLoaded: function() {
227			//console.info('BotMon.live.data._onTicksLogLoaded()');
228
229			// analyse the data:
230			BotMon.live.data.analytics.analyseAll();
231
232			// sort the data:
233			// #TODO
234
235			// display the data:
236			BotMon.live.gui.overview.make();
237
238			//console.log(BotMon.live.data.model._visitors);
239
240		},
241
242		model: {
243			// visitors storage:
244			_visitors: [],
245
246			// find an already existing visitor record:
247			findVisitor: function(visitor, type) {
248				//console.info('BotMon.live.data.model.findVisitor()', type);
249				//console.log(visitor);
250
251				// shortcut to make code more readable:
252				const model = BotMon.live.data.model;
253
254				const timeout = 60 * 60 * 1000; // session timeout: One hour
255
256				if (visitor._type == BM_USERTYPE.KNOWN_BOT) { // known bots match by their bot ID:
257
258					for (let i=0; i<model._visitors.length; i++) {
259						const v = model._visitors[i];
260
261						// bots match when their ID matches:
262						if (v._bot && v._bot.id == visitor._bot.id) {
263							return v;
264						}
265					}
266				} else { // other types match by their DW/PHPIDs:
267
268					// loop over all visitors already registered and check for ID matches:
269					for (let i=0; i<model._visitors.length; i++) {
270						const v = model._visitors[i];
271
272						if ( v.id == visitor.id) { // match the DW/PHP IDs
273							return v;
274						}
275					}
276
277					// if not found, try to match IP address and user agent:
278					for (let i=0; i<model._visitors.length; i++) {
279						const v = model._visitors[i];
280						if (  v.ip == visitor.ip && v.agent == visitor.agent) {
281							return v;
282						}
283					}
284				}
285
286				return null; // nothing found
287			},
288
289			/* if there is already this visit registered, return the page view item */
290			_getPageView: function(visit, view) {
291
292				// shortcut to make code more readable:
293				const model = BotMon.live.data.model;
294
295				for (let i=0; i<visit._pageViews.length; i++) {
296					const pv = visit._pageViews[i];
297					if (pv.pg == view.pg) {
298						return pv;
299					}
300				}
301				return null; // not found
302			},
303
304			// register a new visitor (or update if already exists)
305			registerVisit: function(nv, type) {
306				//console.info('registerVisit', nv, type);
307
308				// shortcut to make code more readable:
309				const model = BotMon.live.data.model;
310
311				// is it a known bot?
312				const bot = BotMon.live.data.bots.match(nv.agent);
313
314				// enrich new visitor with relevant data:
315				if (!nv._bot) nv._bot = bot ?? null; // bot info
316				nv._type = ( bot ? BM_USERTYPE.KNOWN_BOT : ( nv.usr && nv.usr !== '' ? BM_USERTYPE.KNOWN_USER : BM_USERTYPE.UNKNOWN ) ); // user type
317				if (bot && bot.geo) {
318					if (!nv.geo || nv.geo == '' || nv.geo == 'ZZ') nv.geo = bot.geo;
319				} else if (!nv.geo ||nv.geo == '') {
320					nv.geo = 'ZZ';
321				}
322
323				// update first and last seen:
324				if (!nv._firstSeen) nv._firstSeen = nv.ts; // first-seen
325				nv._lastSeen = nv.ts; // last-seen
326
327				// country name:
328				try {
329					nv._country = ( nv.geo == 'local' ? "localhost" : "Unknown" );
330					if (nv.geo && nv.geo !== '' && nv.geo !== 'ZZ' && nv.geo !== 'local') {
331						const countryName = new Intl.DisplayNames(['en', BotMon._lang], {type: 'region'});
332						nv._country = countryName.of(nv.geo.substring(0,2)) ?? nv.geo;
333					}
334				} catch (err) {
335					console.error(err);
336					nv._country = 'Error';
337				}
338
339				// check if it already exists:
340				let visitor = model.findVisitor(nv, type);
341				if (!visitor) {
342					visitor = nv;
343					visitor._seenBy = [type];
344					visitor._pageViews = []; // array of page views
345					visitor._hasReferrer = false; // has at least one referrer
346					visitor._jsClient = false; // visitor has been seen logged by client js as well
347					visitor._client = BotMon.live.data.clients.match(nv.agent) ?? null; // client info
348					visitor._platform = BotMon.live.data.platforms.match(nv.agent); // platform info
349					model._visitors.push(visitor);
350				} else { // update existing
351					if (visitor._firstSeen > nv.ts) {
352						visitor._firstSeen = nv.ts;
353					}
354				}
355
356				// find browser
357
358				// is this visit already registered?
359				let prereg = model._getPageView(visitor, nv);
360				if (!prereg) {
361					// add new page view:
362					prereg = model._makePageView(nv, type);
363					visitor._pageViews.push(prereg);
364				} else {
365					// update last seen date
366					prereg._lastSeen = nv.ts;
367					// increase view count:
368					prereg._viewCount += 1;
369					prereg._tickCount += 1;
370				}
371
372				// update referrer state:
373				visitor._hasReferrer = visitor._hasReferrer ||
374					(prereg.ref !== undefined && prereg.ref !== '');
375
376				// update time stamp for last-seen:
377				if (visitor._lastSeen < nv.ts) {
378					visitor._lastSeen = nv.ts;
379				}
380
381				// if needed:
382				return visitor;
383			},
384
385			// updating visit data from the client-side log:
386			updateVisit: function(dat) {
387				//console.info('updateVisit', dat);
388
389				// shortcut to make code more readable:
390				const model = BotMon.live.data.model;
391
392				const type = BM_LOGTYPE.CLIENT;
393
394				let visitor = BotMon.live.data.model.findVisitor(dat, type);
395				if (!visitor) {
396					visitor = model.registerVisit(dat, type);
397				}
398				if (visitor) {
399
400					if (visitor._lastSeen < dat.ts) {
401						visitor._lastSeen = dat.ts;
402					}
403					if (!visitor._seenBy.includes(type)) {
404						visitor._seenBy.push(type);
405					}
406					visitor._jsClient = true; // seen by client js
407				}
408
409				// find the page view:
410				let prereg = BotMon.live.data.model._getPageView(visitor, dat);
411				if (prereg) {
412					// update the page view:
413					prereg._lastSeen = dat.ts;
414					if (!prereg._seenBy.includes(type)) prereg._seenBy.push(type);
415					prereg._jsClient = true; // seen by client js
416				} else {
417					// add the page view to the visitor:
418					prereg = model._makePageView(dat, type);
419					visitor._pageViews.push(prereg);
420				}
421				prereg._tickCount += 1;
422			},
423
424			// updating visit data from the ticker log:
425			updateTicks: function(dat) {
426				//console.info('updateTicks', dat);
427
428				// shortcut to make code more readable:
429				const model = BotMon.live.data.model;
430
431				const type = BM_LOGTYPE.TICKER;
432
433				// find the visit info:
434				let visitor = model.findVisitor(dat, type);
435				if (!visitor) {
436					console.info(`No visitor with ID “${dat.id}” found, registering as a new one.`);
437					visitor = model.registerVisit(dat, type);
438				}
439				if (visitor) {
440					// update visitor:
441					if (visitor._lastSeen < dat.ts) visitor._lastSeen = dat.ts;
442					if (!visitor._seenBy.includes(type)) visitor._seenBy.push(type);
443
444					// get the page view info:
445					let pv = model._getPageView(visitor, dat);
446					if (!pv) {
447						console.info(`No page view for visit ID “${dat.id}”, page “${dat.pg}”, registering a new one.`);
448						pv = model._makePageView(dat, type);
449						visitor._pageViews.push(pv);
450					}
451
452					// update the page view info:
453					if (!pv._seenBy.includes(type)) pv._seenBy.push(type);
454					if (pv._lastSeen.getTime() < dat.ts.getTime()) pv._lastSeen = dat.ts;
455					pv._tickCount += 1;
456
457				}
458			},
459
460			// helper function to create a new "page view" item:
461			_makePageView: function(data, type) {
462				// console.info('_makePageView', data);
463
464				// try to parse the referrer:
465				let rUrl = null;
466				try {
467					rUrl = ( data.ref && data.ref !== '' ? new URL(data.ref) : null );
468				} catch (e) {
469					console.warn(`Invalid referer: “${data.ref}”.`);
470					console.info(data);
471				}
472
473				return {
474					_by: type,
475					ip: data.ip,
476					pg: data.pg,
477					lang: data.lang || '??',
478					_ref: rUrl,
479					_firstSeen: data.ts,
480					_lastSeen: data.ts,
481					_seenBy: [type],
482					_jsClient: ( type !== BM_LOGTYPE.SERVER),
483					_viewCount: 1,
484					_tickCount: 0
485				};
486			}
487		},
488
489		analytics: {
490
491			init: function() {
492				//console.info('BotMon.live.data.analytics.init()');
493			},
494
495			// data storage:
496			data: {
497				totalVisits: 0,
498				totalPageViews: 0,
499				humanPageViews: 0,
500				bots: {
501					known: 0,
502					suspected: 0,
503					human: 0,
504					users: 0
505				}
506			},
507
508			// sort the visits by type:
509			groups: {
510				knownBots: [],
511				suspectedBots: [],
512				humans: [],
513				users: []
514			},
515
516			// all analytics
517			analyseAll: function() {
518				//console.info('BotMon.live.data.analytics.analyseAll()');
519
520				// shortcut to make code more readable:
521				const model = BotMon.live.data.model;
522				const me = BotMon.live.data.analytics;
523
524				BotMon.live.gui.status.showBusy("Analysing data …");
525
526				// loop over all visitors:
527				model._visitors.forEach( (v) => {
528
529					// count visits and page views:
530					this.data.totalVisits += 1;
531					this.data.totalPageViews += v._pageViews.length;
532
533					// check for typical bot aspects:
534					let botScore = 0;
535
536					if (v._type == BM_USERTYPE.KNOWN_BOT) { // known bots
537
538						this.data.bots.known += v._pageViews.length;
539						this.groups.knownBots.push(v);
540
541					} else if (v._type == BM_USERTYPE.KNOWN_USER) { // known users */
542
543						this.data.bots.users += v._pageViews.length;
544						this.groups.users.push(v);
545
546					} else {
547
548						// get evaluation:
549						const e = BotMon.live.data.rules.evaluate(v);
550						v._eval = e.rules;
551						v._botVal = e.val;
552
553						if (e.isBot) { // likely bots
554							v._type = BM_USERTYPE.LIKELY_BOT;
555							this.data.bots.suspected += v._pageViews.length;
556							this.groups.suspectedBots.push(v);
557						} else { // probably humans
558							v._type = BM_USERTYPE.PROBABLY_HUMAN;
559							this.data.bots.human += v._pageViews.length;
560							this.groups.humans.push(v);
561						}
562					}
563
564					// perform actions depending on the visitor type:
565					if (v._type == BM_USERTYPE.KNOWN_BOT || v._type == BM_USERTYPE.LIKELY_BOT) { /* bots only */
566
567						// add bot views to IP range information:
568						/*v._pageViews.forEach( pv => {
569							me.addToIPRanges(pv.ip);
570						});*/
571
572						// add to the country lists:
573						me.addToCountries(v.geo, v._country, v._type);
574
575					} else { /* humans only */
576
577						// add browser and platform statistics:
578						me.addBrowserPlatform(v);
579
580						// add
581						v._pageViews.forEach( pv => {
582							me.addToRefererList(pv._ref);
583						});
584					}
585
586				});
587
588				BotMon.live.gui.status.hideBusy('Done.');
589			},
590
591			// get a list of known bots:
592			getTopBots: function(max) {
593				//console.info('BotMon.live.data.analytics.getTopBots('+max+')');
594
595				//console.log(BotMon.live.data.analytics.groups.knownBots);
596
597				let botsList = BotMon.live.data.analytics.groups.knownBots.toSorted( (a, b) => {
598					return b._pageViews.length - a._pageViews.length;
599				});
600
601				const other = {
602					'id': 'other',
603					'name': "Others",
604					'count': 0
605				};
606
607				const rList = [];
608				const max2 = ( botsList.length > max ? max-1 : botsList.length );
609				let total = 0; // adding up the items
610				for (let i=0; i<botsList.length; i++) {
611					const it = botsList[i];
612					if (it && it._bot) {
613						if (i < max2) {
614							rList.push({
615								id: it._bot.id,
616								name: (it._bot.n ? it._bot.n : it._bot.id),
617								count: it._pageViews.length
618							});
619						} else {
620							other.count += it._pageViews.length;
621						};
622						total += it._pageViews.length;
623					}
624				};
625
626				// add the "other" item, if needed:
627				if (botsList.length > max2) {
628					rList.push(other);
629				};
630
631				rList.forEach( it => {
632					it.pct = (it.count * 100 / total);
633				});
634
635				return rList;
636			},
637
638			// Referer List:
639			_refererList: [],
640
641			addToRefererList: function(ref) {
642				//console.log('BotMon.live.data.analytics.addToRefererList',ref);
643
644				const me = BotMon.live.data.analytics;
645
646				// ignore internal references:
647				if (ref && ref.host == window.location.host) {
648					return;
649				}
650
651				const refInfo = me.getRefererInfo(ref);
652
653				// already exists?
654				let refObj = null;
655				for (let i = 0; i < me._refererList.length; i++) {
656					if (me._refererList[i].id == refInfo.id) {
657						refObj = me._refererList[i];
658						break;
659					}
660				}
661
662				// if not exists, create it:
663				if (!refObj) {
664					refObj = refInfo;
665					refObj.count = 1;
666					me._refererList.push(refObj);
667				} else {
668					refObj.count += 1;
669				}
670			},
671
672			getRefererInfo: function(url) {
673				//console.log('BotMon.live.data.analytics.getRefererInfo',url);
674				try {
675					url = new URL(url);
676				} catch (e) {
677					return {
678						'id': 'null',
679						'n': 'Invalid Referer'
680					};
681				}
682
683				// find the referer ID:
684				let refId = 'null';
685				let refName = 'No Referer';
686				if (url && url.host) {
687					const hArr = url.host.split('.');
688					const tld = hArr[hArr.length-1];
689					refId = ( tld == 'localhost' ? tld : hArr[hArr.length-2]);
690					refName = hArr[hArr.length-2] + '.' + tld;
691				}
692
693				return {
694					'id': refId,
695					'n': refName
696				};
697			},
698
699			getTopReferers: function(max) {
700				//console.info(('BotMon.live.data.analytics.getTopReferers(' + max + ')'));
701
702				const me = BotMon.live.data.analytics;
703
704				return me._makeTopList(me._refererList, max);
705			},
706
707			_makeTopList: function(arr, max) {
708				//console.info(('BotMon.live.data.analytics._makeTopList(arr,' + max + ')'));
709
710				const me = BotMon.live.data.analytics;
711
712				// sort the list:
713				arr.sort( (a,b) => {
714					return b.count - a.count;
715				});
716
717				const rList = []; // return array
718				const max2 = ( arr.length >= max ? max-1 : arr.length );
719				const other = {
720					'id': 'other',
721					'name': "Others",
722					'count': 0
723				};
724				let total = 0; // adding up the items
725				for (let i=0; Math.min(max, arr.length) > i; i++) {
726					const it = arr[i];
727					if (it) {
728						if (i < max2) {
729							const rIt = {
730								id: it.id,
731								name: (it.n ? it.n : it.id),
732								count: it.count
733							};
734							rList.push(rIt);
735						} else {
736							other.count += it.count;
737						}
738						total += it.count;
739					}
740				}
741
742				// add the "other" item, if needed:
743				if (arr.length > max2) {
744					rList.push(other);
745				};
746
747				rList.forEach( it => {
748					it.pct = (it.count * 100 / total);
749				});
750
751				return rList;
752			},
753
754			/* countries of visits */
755			_countries: {
756				'user': [],
757				'human': [],
758				'likelyBot': [],
759				'known_bot': []
760
761			},
762			/**
763			 * Adds a country code to the statistics.
764			 *
765			 * @param {string} iso The ISO 3166-1 alpha-2 country code.
766			 */
767			addToCountries: function(iso, name, type) {
768
769				const me = BotMon.live.data.analytics;
770
771				// find the correct array:
772				let arr = null;
773				switch (type) {
774
775					case BM_USERTYPE.KNOWN_USER:
776						arr = me._countries.user;
777						break;
778					case BM_USERTYPE.PROBABLY_HUMAN:
779						arr = me._countries.human;
780						break;
781					case BM_USERTYPE.LIKELY_BOT:
782						arr = me._countries.likelyBot;
783						break;
784					case BM_USERTYPE.KNOWN_BOT:
785						arr = me._countries.known_bot;
786						break;
787					default:
788						console.warn(`Unknown user type ${type} in function addToCountries.`);
789				}
790
791				if (arr) {
792					let cRec = arr.find( it => it.id == iso);
793					if (!cRec) {
794						cRec = {
795							'id': iso,
796							'n': name,
797							'count': 1
798						};
799						arr.push(cRec);
800					} else {
801						cRec.count += 1;
802					}
803				}
804			},
805
806			/**
807			 * Returns a list of countries with visit counts, sorted by visit count in descending order.
808			 *
809			 * @param {BM_USERTYPE} type The type of visitors to return.
810			 * @param {number} max The maximum number of entries to return.
811			 * @return {Array} A list of objects with properties 'iso' (ISO 3166-1 alpha-2 country code) and 'count' (visit count).
812			 */
813			getCountryList: function(type, max) {
814
815				const me = BotMon.live.data.analytics;
816
817				// find the correct array:
818				let arr = null;
819				switch (type) {
820
821					case BM_USERTYPE.KNOWN_USER:
822						arr = me._countries.user;
823						break;
824					case BM_USERTYPE.PROBABLY_HUMAN:
825						arr = me._countries.human;
826						break;
827					case BM_USERTYPE.LIKELY_BOT:
828						arr = me._countries.likelyBot;
829						break;
830					case BM_USERTYPE.KNOWN_BOT:
831						arr = me._countries.known_bot;
832						break;
833					default:
834						console.warn(`Unknown user type ${type} in function getCountryList.`);
835						return;
836				}
837
838				return me._makeTopList(arr, max);
839			},
840
841			/* browser and platform of human visitors */
842			_browsers: [],
843			_platforms: [],
844
845			addBrowserPlatform: function(visitor) {
846				//console.info('addBrowserPlatform', visitor);
847
848				const me = BotMon.live.data.analytics;
849
850				// add to browsers list:
851				let browserRec = ( visitor._client ? visitor._client : {'id': 'unknown'});
852				if (visitor._client) {
853					let bRec = me._browsers.find( it => it.id == browserRec.id);
854					if (!bRec) {
855						bRec = {
856							id: browserRec.id,
857							n: browserRec.n,
858							count: 1
859						};
860						me._browsers.push(bRec);
861					} else {
862						bRec.count += 1;
863					}
864				}
865
866				// add to platforms list:
867				let platformRec = ( visitor._platform ? visitor._platform : {'id': 'unknown'});
868				if (visitor._platform) {
869					let pRec = me._platforms.find( it => it.id == platformRec.id);
870					if (!pRec) {
871						pRec = {
872							id: platformRec.id,
873							n: platformRec.n,
874							count: 1
875						};
876						me._platforms.push(pRec);
877					} else {
878						pRec.count += 1;
879					}
880				}
881
882			},
883
884			getTopBrowsers: function(max) {
885
886				const me = BotMon.live.data.analytics;
887
888				return me._makeTopList(me._browsers, max);
889			},
890
891			getTopPlatforms: function(max) {
892
893				const me = BotMon.live.data.analytics;
894
895				return me._makeTopList(me._platforms, max);
896			}
897		},
898
899		bots: {
900			// loads the list of known bots from a JSON file:
901			init: async function() {
902				//console.info('BotMon.live.data.bots.init()');
903
904				// Load the list of known bots:
905				BotMon.live.gui.status.showBusy("Loading known bots …");
906				const url = BotMon._baseDir + 'config/known-bots.json';
907				try {
908					const response = await fetch(url);
909					if (!response.ok) {
910						throw new Error(`${response.status} ${response.statusText}`);
911					}
912
913					this._list = await response.json();
914					this._ready = true;
915
916				} catch (error) {
917					BotMon.live.gui.status.setError("Error while loading the known bots file:", error.message);
918				} finally {
919					BotMon.live.gui.status.hideBusy("Status: Done.");
920					BotMon.live.data._dispatch('bots')
921				}
922			},
923
924			// returns bot info if the clientId matches a known bot, null otherwise:
925			match: function(agent) {
926				//console.info('BotMon.live.data.bots.match(',agent,')');
927
928				const BotList = BotMon.live.data.bots._list;
929
930				// default is: not found!
931				let botInfo = null;
932
933				if (!agent) return null;
934
935				// check for known bots:
936				BotList.find(bot => {
937					let r = false;
938					for (let j=0; j<bot.rx.length; j++) {
939						const rxr = agent.match(new RegExp(bot.rx[j]));
940						if (rxr) {
941							botInfo = {
942								n : bot.n,
943								id: bot.id,
944								geo: (bot.geo ? bot.geo : null),
945								url: bot.url,
946								v: (rxr.length > 1 ? rxr[1] : -1)
947							};
948							r = true;
949							break;
950						};
951					};
952					return r;
953				});
954
955				// check for unknown bots:
956				if (!botInfo) {
957					const botmatch = agent.match(/([\s\d\w\-]*bot|[\s\d\w\-]*crawler|[\s\d\w\-]*spider)[\/\s;\),\\.$]/i);
958					if(botmatch) {
959						botInfo = {'id': ( botmatch[1] || "other_" ), 'n': "Other" + ( botmatch[1] ? " (" + botmatch[1] + ")" : "" ) , "bot": botmatch[1] };
960					}
961				}
962
963				//console.log("botInfo:", botInfo);
964				return botInfo;
965			},
966
967
968			// indicates if the list is loaded and ready to use:
969			_ready: false,
970
971			// the actual bot list is stored here:
972			_list: []
973		},
974
975		clients: {
976			// loads the list of known clients from a JSON file:
977			init: async function() {
978				//console.info('BotMon.live.data.clients.init()');
979
980				// Load the list of known bots:
981				BotMon.live.gui.status.showBusy("Loading known clients");
982				const url = BotMon._baseDir + 'config/known-clients.json';
983				try {
984					const response = await fetch(url);
985					if (!response.ok) {
986						throw new Error(`${response.status} ${response.statusText}`);
987					}
988
989					BotMon.live.data.clients._list = await response.json();
990					BotMon.live.data.clients._ready = true;
991
992				} catch (error) {
993					BotMon.live.gui.status.setError("Error while loading the known clients file: " + error.message);
994				} finally {
995					BotMon.live.gui.status.hideBusy("Status: Done.");
996					BotMon.live.data._dispatch('clients')
997				}
998			},
999
1000			// returns bot info if the user-agent matches a known bot, null otherwise:
1001			match: function(agent) {
1002				//console.info('BotMon.live.data.clients.match(',agent,')');
1003
1004				let match = {"n": "Unknown", "v": -1, "id": 'null'};
1005
1006				if (agent) {
1007					BotMon.live.data.clients._list.find(client => {
1008						let r = false;
1009						for (let j=0; j<client.rx.length; j++) {
1010							const rxr = agent.match(new RegExp(client.rx[j]));
1011							if (rxr) {
1012								match.n = client.n;
1013								match.v = (rxr.length > 1 ? rxr[1] : -1);
1014								match.id = client.id || null;
1015								r = true;
1016								break;
1017							}
1018						}
1019						return r;
1020					});
1021				}
1022
1023				//console.log(match)
1024				return match;
1025			},
1026
1027			// return the browser name for a browser ID:
1028			getName: function(id) {
1029				const it = BotMon.live.data.clients._list.find(client => client.id == id);
1030				return ( it && it.n ? it.n : "Unknown"); //it.n;
1031			},
1032
1033			// indicates if the list is loaded and ready to use:
1034			_ready: false,
1035
1036			// the actual bot list is stored here:
1037			_list: []
1038
1039		},
1040
1041		platforms: {
1042			// loads the list of known platforms from a JSON file:
1043			init: async function() {
1044				//console.info('BotMon.live.data.platforms.init()');
1045
1046				// Load the list of known bots:
1047				BotMon.live.gui.status.showBusy("Loading known platforms");
1048				const url = BotMon._baseDir + 'config/known-platforms.json';
1049				try {
1050					const response = await fetch(url);
1051					if (!response.ok) {
1052						throw new Error(`${response.status} ${response.statusText}`);
1053					}
1054
1055					BotMon.live.data.platforms._list = await response.json();
1056					BotMon.live.data.platforms._ready = true;
1057
1058				} catch (error) {
1059					BotMon.live.gui.status.setError("Error while loading the known platforms file: " + error.message);
1060				} finally {
1061					BotMon.live.gui.status.hideBusy("Status: Done.");
1062					BotMon.live.data._dispatch('platforms')
1063				}
1064			},
1065
1066			// returns bot info if the browser id matches a known platform:
1067			match: function(cid) {
1068				//console.info('BotMon.live.data.platforms.match(',cid,')');
1069
1070				let match = {"n": "Unknown", "id": 'null'};
1071
1072				if (cid) {
1073					BotMon.live.data.platforms._list.find(platform => {
1074						let r = false;
1075						for (let j=0; j<platform.rx.length; j++) {
1076							const rxr = cid.match(new RegExp(platform.rx[j]));
1077							if (rxr) {
1078								match.n = platform.n;
1079								match.v = (rxr.length > 1 ? rxr[1] : -1);
1080								match.id = platform.id || null;
1081								r = true;
1082								break;
1083							}
1084						}
1085						return r;
1086					});
1087				}
1088
1089				return match;
1090			},
1091
1092			// return the platform name for a given ID:
1093			getName: function(id) {
1094				const it = BotMon.live.data.platforms._list.find( pf => pf.id == id);
1095				return ( it ? it.n : 'Unknown' );
1096			},
1097
1098
1099			// indicates if the list is loaded and ready to use:
1100			_ready: false,
1101
1102			// the actual bot list is stored here:
1103			_list: []
1104
1105		},
1106
1107		rules: {
1108			// loads the list of rules and settings from a JSON file:
1109			init: async function() {
1110				//console.info('BotMon.live.data.rules.init()');
1111
1112				// Load the list of known bots:
1113				BotMon.live.gui.status.showBusy("Loading list of rules …");
1114
1115				// relative file path to the rules file:
1116				const filePath = 'config/default-config.json';
1117
1118				// load the rules file:
1119				this._loadrulesFile(BotMon._baseDir + filePath);
1120			},
1121
1122			/**
1123			 * Loads the list of rules and settings from a JSON file.
1124			 * @param {String} url - the URL from which to load the rules file.
1125			 */
1126
1127			_loadrulesFile: async function(url) {
1128				//console.info('BotMon.live.data.rules._loadrulesFile(',url,')');}
1129
1130				const me = BotMon.live.data.rules;
1131				try {
1132					const response = await fetch(url);
1133					if (!response.ok) {
1134						throw new Error(`${response.status} ${response.statusText}`);
1135					}
1136
1137					const json = await response.json();
1138
1139					if (json.rules) {
1140						me._rulesList = json.rules;
1141					}
1142
1143					// override the threshold?
1144					if (json.threshold) me._threshold = json.threshold;
1145
1146					if (json.ipRanges) {
1147						// clean up the IPs first:
1148						let list = [];
1149						json.ipRanges.forEach( it => {
1150							let item = {
1151								'from': BotMon.t._ip2Num(it.from),
1152								'to': BotMon.t._ip2Num(it.to),
1153								'label': it.label
1154							};
1155							list.push(item);
1156						});
1157
1158						me._botIPs = list;
1159					}
1160
1161					me._ready = true;
1162
1163				} catch (error) {
1164					BotMon.live.gui.status.setError("Error while loading the config file: " + error.message);
1165				} finally {
1166					BotMon.live.gui.status.hideBusy("Status: Done.");
1167					BotMon.live.data._dispatch('rules')
1168				}
1169			},
1170
1171			_rulesList: [], // list of rules to find out if a visitor is a bot
1172			_threshold: 100, // above this, it is considered a bot.
1173
1174			// returns a descriptive text for a rule id
1175			getRuleInfo: function(ruleId) {
1176				// console.info('getRuleInfo', ruleId);
1177
1178				// shortcut for neater code:
1179				const me = BotMon.live.data.rules;
1180
1181				for (let i=0; i<me._rulesList.length; i++) {
1182					const rule = me._rulesList[i];
1183					if (rule.id == ruleId) {
1184						return rule;
1185					}
1186				}
1187				return null;
1188
1189			},
1190
1191			// evaluate a visitor for lkikelihood of being a bot
1192			evaluate: function(visitor) {
1193
1194				// shortcut for neater code:
1195				const me = BotMon.live.data.rules;
1196
1197				let r =  {	// evaluation result
1198					'val': 0,
1199					'rules': [],
1200					'isBot': false
1201				};
1202
1203				for (let i=0; i<me._rulesList.length; i++) {
1204					const rule = me._rulesList[i];
1205					const params = ( rule.params ? rule.params : [] );
1206
1207					if (rule.func) { // rule is calling a function
1208						if (me.func[rule.func]) {
1209							if(me.func[rule.func](visitor, ...params)) {
1210								r.val += rule.bot;
1211								r.rules.push(rule.id)
1212							}
1213						} else {
1214							//console.warn("Unknown rule function: “${rule.func}”. Ignoring rule.")
1215						}
1216					}
1217				}
1218
1219				// is a bot?
1220				r.isBot = (r.val >= me._threshold);
1221
1222				return r;
1223			},
1224
1225			// list of functions that can be called by the rules list to evaluate a visitor:
1226			func: {
1227
1228				// check if client is on the list passed as parameter:
1229				matchesClient: function(visitor, ...clients) {
1230
1231					const clientId = ( visitor._client ? visitor._client.id : '');
1232					return clients.includes(clientId);
1233				},
1234
1235				// check if OS/Platform is one of the obsolete ones:
1236				matchesPlatform: function(visitor, ...platforms) {
1237
1238					const pId = ( visitor._platform ? visitor._platform.id : '');
1239
1240					if (visitor._platform.id == null) console.log(visitor._platform);
1241
1242					return platforms.includes(pId);
1243				},
1244
1245				// are there at lest num pages loaded?
1246				smallPageCount: function(visitor, num) {
1247					return (visitor._pageViews.length <= Number(num));
1248				},
1249
1250				// There was no entry in a specific log file for this visitor:
1251				// note that this will also trigger the "noJavaScript" rule:
1252				noRecord: function(visitor, type) {
1253					return !visitor._seenBy.includes(type);
1254				},
1255
1256				// there are no referrers in any of the page visits:
1257				noReferrer: function(visitor) {
1258
1259					let r = false; // return value
1260					for (let i = 0; i < visitor._pageViews.length; i++) {
1261						if (!visitor._pageViews[i]._ref) {
1262							r = true;
1263							break;
1264						}
1265					}
1266					return r;
1267				},
1268
1269				// test for specific client identifiers:
1270				/*matchesClients: function(visitor, ...list) {
1271
1272					for (let i=0; i<list.length; i++) {
1273						if (visitor._client.id == list[i]) {
1274								return true
1275						}
1276					};
1277					return false;
1278				},*/
1279
1280				// unusual combinations of Platform and Client:
1281				combinationTest: function(visitor, ...combinations) {
1282
1283					for (let i=0; i<combinations.length; i++) {
1284
1285						if (visitor._platform.id == combinations[i][0]
1286							&& visitor._client.id == combinations[i][1]) {
1287								return true
1288						}
1289					};
1290
1291					return false;
1292				},
1293
1294				// is the IP address from a known bot network?
1295				fromKnownBotIP: function(visitor) {
1296
1297					const ipInfo = BotMon.live.data.rules.getBotIPInfo(visitor.ip);
1298
1299					if (ipInfo) {
1300						visitor._ipInKnownBotRange = true;
1301					}
1302
1303					return (ipInfo !== null);
1304				},
1305
1306				// is the page language mentioned in the client's accepted languages?
1307				// the parameter holds an array of exceptions, i.e. page languages that should be ignored.
1308				matchLang: function(visitor, ...exceptions) {
1309
1310					if (visitor.lang && visitor.accept && exceptions.indexOf(visitor.lang) < 0) {
1311						return (visitor.accept.split(',').indexOf(visitor.lang) < 0);
1312					}
1313					return false;
1314				},
1315
1316				// the "Accept language" header contains certain entries:
1317				clientAccepts: function(visitor, ...languages) {
1318					//console.info('clientAccepts', visitor.accept, languages);
1319
1320					if (visitor.accept && languages) {;
1321						return ( visitor.accept.split(',').filter(lang => languages.includes(lang)).length > 0 );
1322					}
1323					return false;
1324				},
1325
1326				// Is there an accept-language field defined at all?
1327				noAcceptLang: function(visitor) {
1328
1329					if (!visitor.accept || visitor.accept.length <= 0) { // no accept-languages header
1330						return true;
1331					}
1332					// TODO: parametrize this!
1333					return false;
1334				},
1335				// At least x page views were recorded, but they come within less than y seconds
1336				loadSpeed: function(visitor, minItems, maxTime) {
1337
1338					if (visitor._pageViews.length >= minItems) {
1339						//console.log('loadSpeed', visitor._pageViews.length, minItems, maxTime);
1340
1341						const pvArr = visitor._pageViews.map(pv => pv._lastSeen).sort();
1342
1343						let totalTime = 0;
1344						for (let i=1; i < pvArr.length; i++) {
1345							totalTime += (pvArr[i] - pvArr[i-1]);
1346						}
1347
1348						//console.log('     ', totalTime , Math.round(totalTime / (pvArr.length * 1000)), (( totalTime / pvArr.length ) <= maxTime * 1000), visitor.ip);
1349
1350						return (( totalTime / pvArr.length ) <= maxTime * 1000);
1351					}
1352				},
1353
1354				// Country code matches one of those in the list:
1355				matchesCountry: function(visitor, ...countries) {
1356
1357					// ingore if geoloc is not set or unknown:
1358					if (visitor.geo) {
1359						return (countries.indexOf(visitor.geo) >= 0);
1360					}
1361					return false;
1362				},
1363
1364				// Country does not match one of the given codes.
1365				notFromCountry: function(visitor, ...countries) {
1366
1367					// ingore if geoloc is not set or unknown:
1368					if (visitor.geo && visitor.geo !== 'ZZ') {
1369						return (countries.indexOf(visitor.geo) < 0);
1370					}
1371					return false;
1372				}
1373			},
1374
1375			/* known bot IP ranges: */
1376			_botIPs: [],
1377
1378			// return information on a bot IP range:
1379			getBotIPInfo: function(ip) {
1380
1381				// shortcut to make code more readable:
1382				const me = BotMon.live.data.rules;
1383
1384				// convert IP address to easier comparable form:
1385				const ipNum = BotMon.t._ip2Num(ip);
1386
1387				for (let i=0; i < me._botIPs.length; i++) {
1388					const ipRange = me._botIPs[i];
1389
1390					if (ipNum >= ipRange.from && ipNum <= ipRange.to) {
1391						return ipRange;
1392					}
1393
1394				};
1395				return null;
1396
1397			}
1398
1399		},
1400
1401		/**
1402		 * Loads a log file (server, page load, or ticker) and parses it.
1403		 * @param {String} type - the type of the log file to load (srv, log, or tck)
1404		 * @param {Function} [onLoaded] - an optional callback function to call after loading is finished.
1405		 */
1406		loadLogFile: async function(type, onLoaded = undefined) {
1407			//console.info('BotMon.live.data.loadLogFile(',type,')');
1408
1409			let typeName = '';
1410			let columns = [];
1411
1412			switch (type) {
1413				case "srv":
1414					typeName = "Server";
1415					columns = ['ts','ip','pg','id','typ','usr','agent','ref','lang','accept','geo'];
1416					break;
1417				case "log":
1418					typeName = "Page load";
1419					columns = ['ts','ip','pg','id','usr','lt','ref','agent'];
1420					break;
1421				case "tck":
1422					typeName = "Ticker";
1423					columns = ['ts','ip','pg','id','agent'];
1424					break;
1425				default:
1426					console.warn(`Unknown log type ${type}.`);
1427					return;
1428			}
1429
1430			// Show the busy indicator and set the visible status:
1431			BotMon.live.gui.status.showBusy(`Loading ${typeName} log file …`);
1432
1433			// compose the URL from which to load:
1434			const url = BotMon._baseDir + `logs/${BotMon._datestr}.${type}.txt`;
1435			//console.log("Loading:",url);
1436
1437			// fetch the data:
1438			try {
1439				const response = await fetch(url);
1440				if (!response.ok) {
1441
1442					throw new Error(`${response.status} ${response.statusText}`);
1443
1444				} else {
1445
1446					// parse the data:
1447					const logtxt = await response.text();
1448					if (logtxt.length <= 0) {
1449						throw new Error(`Empty log file ${url}.`);
1450					}
1451
1452					logtxt.split('\n').forEach((line) => {
1453						if (line.trim() === '') return; // skip empty lines
1454						const cols = line.split('\t');
1455
1456						// assign the columns to an object:
1457						const data = {};
1458						cols.forEach( (colVal,i) => {
1459							colName = columns[i] || `col${i}`;
1460							const colValue = (colName == 'ts' ? new Date(colVal) : colVal.trim());
1461							data[colName] = colValue;
1462						});
1463
1464						// register the visit in the model:
1465						switch(type) {
1466							case BM_LOGTYPE.SERVER:
1467								BotMon.live.data.model.registerVisit(data, type);
1468								break;
1469							case BM_LOGTYPE.CLIENT:
1470								data.typ = 'js';
1471								BotMon.live.data.model.updateVisit(data);
1472								break;
1473							case BM_LOGTYPE.TICKER:
1474								data.typ = 'js';
1475								BotMon.live.data.model.updateTicks(data);
1476								break;
1477							default:
1478								console.warn(`Unknown log type ${type}.`);
1479								return;
1480						}
1481					});
1482				}
1483
1484			} catch (error) {
1485				BotMon.live.gui.status.setError(`Error while loading the ${typeName} log file: ${error.message} – data may be incomplete.`);
1486			} finally {
1487				BotMon.live.gui.status.hideBusy("Status: Done.");
1488				if (onLoaded) {
1489					onLoaded(); // callback after loading is finished.
1490				}
1491			}
1492		}
1493	},
1494
1495	gui: {
1496		init: function() {
1497			// init the lists view:
1498			this.lists.init();
1499		},
1500
1501		/* The Overview / web metrics section of the live tab */
1502		overview: {
1503			/**
1504			 * Populates the overview part of the today tab with the analytics data.
1505			 *
1506			 * @method make
1507			 * @memberof BotMon.live.gui.overview
1508			 */
1509			make: function() {
1510
1511				const data = BotMon.live.data.analytics.data;
1512
1513				const maxItemsPerList = 5; // how many list items to show?
1514
1515				// shortcut for neater code:
1516				const makeElement = BotMon.t._makeElement;
1517
1518				const botsVsHumans = document.getElementById('botmon__today__botsvshumans');
1519				if (botsVsHumans) {
1520					botsVsHumans.appendChild(makeElement('dt', {}, "Page views by category:"));
1521
1522					for (let i = 0; i <= 4; i++) {
1523						const dd = makeElement('dd');
1524						let title = '';
1525						let value = '';
1526						switch(i) {
1527							case 0:
1528								title = "Registered users:";
1529								value = data.bots.users;
1530								break;
1531							case 1:
1532								title = "Probably humans:";
1533								value = data.bots.human;
1534								break;
1535							case 2:
1536								title = "Suspected bots:";
1537								value = data.bots.suspected;
1538								break;
1539							case 3:
1540								title = "Known bots:";
1541								value = data.bots.known;
1542								break;
1543							case 4:
1544								title = "Total:";
1545								value = data.totalPageViews;
1546								break;
1547							default:
1548								console.warn(`Unknown list type ${i}.`);
1549						}
1550						dd.appendChild(makeElement('span', {}, title));
1551						dd.appendChild(makeElement('strong', {}, value));
1552						botsVsHumans.appendChild(dd);
1553					}
1554				}
1555
1556				// update known bots list:
1557				const botElement = document.getElementById('botmon__botslist'); /* Known bots */
1558				if (botElement) {
1559					botElement.innerHTML = `<dt>Top visiting bots:</dt>`;
1560
1561					let botList = BotMon.live.data.analytics.getTopBots(maxItemsPerList);
1562					botList.forEach( (botInfo) => {
1563						const bli = makeElement('dd');
1564						bli.appendChild(makeElement('span', {'class': 'has_icon bot bot_' + botInfo.id }, botInfo.name));
1565						bli.appendChild(makeElement('span', {'class': 'count' }, botInfo.count));
1566						botElement.append(bli)
1567					});
1568				}
1569
1570				// update the suspected bot IP ranges list:
1571				/*const botIps = document.getElementById('botmon__today__botips');
1572				if (botIps) {
1573					botIps.appendChild(makeElement('dt', {}, "Bot IP ranges (top 5)"));
1574
1575					const ipList = BotMon.live.data.analytics.getTopBotIPRanges(5);
1576					ipList.forEach( (ipInfo) => {
1577						const li = makeElement('dd');
1578						li.appendChild(makeElement('span', {'class': 'has_icon ipaddr ip' + ipInfo.typ }, ipInfo.ip));
1579						li.appendChild(makeElement('span', {'class': 'count' }, ipInfo.num));
1580						botIps.append(li)
1581					});
1582				}*/
1583
1584				// update the top bot countries list:
1585				const botCountries = document.getElementById('botmon__today__countries');
1586				if (botCountries) {
1587					botCountries.appendChild(makeElement('dt', {}, `Top bot Countries:`));
1588					const countryList = BotMon.live.data.analytics.getCountryList('likely_bot', 5);
1589					countryList.forEach( (cInfo) => {
1590						const cLi = makeElement('dd');
1591						cLi.appendChild(makeElement('span', {'class': 'has_icon country ctry_' + cInfo.id.toLowerCase() }, cInfo.name));
1592						cLi.appendChild(makeElement('span', {'class': 'count' }, cInfo.count));
1593						botCountries.appendChild(cLi);
1594					});
1595				}
1596
1597				// update the webmetrics overview:
1598				const wmoverview = document.getElementById('botmon__today__wm_overview');
1599				if (wmoverview) {
1600					const bounceRate = Math.round(data.totalVisits / data.totalPageViews * 100);
1601
1602					wmoverview.appendChild(makeElement('dt', {}, "Visitor overview"));
1603					for (let i = 0; i < 3; i++) {
1604						const dd = makeElement('dd');
1605						let title = '';
1606						let value = '';
1607						switch(i) {
1608							case 0:
1609								title = "Total page views:";
1610								value = data.totalPageViews;
1611								break;
1612							case 1:
1613								title = "Total visitors (est.):";
1614								value = data.totalVisits;
1615								break;
1616							case 2:
1617								title = "Bounce rate (est.):";
1618								value = bounceRate + '%';
1619								break;
1620							default:
1621								console.warn(`Unknown list type ${i}.`);
1622						}
1623						dd.appendChild(makeElement('span', {}, title));
1624						dd.appendChild(makeElement('strong', {}, value));
1625						wmoverview.appendChild(dd);
1626					}
1627				}
1628
1629				// update the webmetrics clients list:
1630				const wmclients = document.getElementById('botmon__today__wm_clients');
1631				if (wmclients) {
1632
1633					wmclients.appendChild(makeElement('dt', {}, "Browsers"));
1634
1635					const clientList = BotMon.live.data.analytics.getTopBrowsers(maxItemsPerList);
1636					if (clientList) {
1637						clientList.forEach( (cInfo) => {
1638							const cDd = makeElement('dd');
1639							cDd.appendChild(makeElement('span', {'class': 'has_icon client cl_' + cInfo.id }, ( cInfo.name ? cInfo.name : cInfo.id)));
1640							cDd.appendChild(makeElement('span', {
1641								'class': 'count',
1642								'title': cInfo.count + " page views"
1643							}, cInfo.pct.toFixed(1) + '%'));
1644							wmclients.appendChild(cDd);
1645						});
1646					}
1647				}
1648
1649				// update the webmetrics platforms list:
1650				const wmplatforms = document.getElementById('botmon__today__wm_platforms');
1651				if (wmplatforms) {
1652
1653					wmplatforms.appendChild(makeElement('dt', {}, "Platforms"));
1654
1655					const pfList = BotMon.live.data.analytics.getTopPlatforms(maxItemsPerList);
1656					if (pfList) {
1657						pfList.forEach( (pInfo) => {
1658							const pDd = makeElement('dd');
1659							pDd.appendChild(makeElement('span', {'class': 'has_icon platform pf_' + pInfo.id }, ( pInfo.name ? pInfo.name : pInfo.id)));
1660							pDd.appendChild(makeElement('span', {
1661								'class': 'count',
1662								'title': pInfo.count + " page views"
1663							}, pInfo.pct.toFixed(1) + '%'));
1664							wmplatforms.appendChild(pDd);
1665						});
1666					}
1667				}
1668
1669				// update the top referrers;
1670				const wmreferers = document.getElementById('botmon__today__wm_referers');
1671				if (wmreferers) {
1672
1673					wmreferers.appendChild(makeElement('dt', {}, "Referers"));
1674
1675					const refList = BotMon.live.data.analytics.getTopReferers(maxItemsPerList);
1676					if (refList) {
1677						refList.forEach( (rInfo) => {
1678							const rDd = makeElement('dd');
1679							rDd.appendChild(makeElement('span', {'class': 'has_icon referer ref_' + rInfo.id }, rInfo.name));
1680							rDd.appendChild(makeElement('span', {
1681								'class': 'count',
1682								'title': rInfo.count + " references"
1683							}, rInfo.pct.toFixed(1) + '%'));
1684							wmreferers.appendChild(rDd);
1685						});
1686					}
1687				}
1688			}
1689		},
1690
1691		status: {
1692			setText: function(txt) {
1693				const el = document.getElementById('botmon__today__status');
1694				if (el && BotMon.live.gui.status._errorCount <= 0) {
1695					el.innerText = txt;
1696				}
1697			},
1698
1699			setTitle: function(html) {
1700				const el = document.getElementById('botmon__today__title');
1701				if (el) {
1702					el.innerHTML = html;
1703				}
1704			},
1705
1706			setError: function(txt) {
1707				console.error(txt);
1708				BotMon.live.gui.status._errorCount += 1;
1709				const el = document.getElementById('botmon__today__status');
1710				if (el) {
1711					el.innerText = "Data may be incomplete.";
1712					el.classList.add('error');
1713				}
1714			},
1715			_errorCount: 0,
1716
1717			showBusy: function(txt = null) {
1718				BotMon.live.gui.status._busyCount += 1;
1719				const el = document.getElementById('botmon__today__busy');
1720				if (el) {
1721					el.style.display = 'inline-block';
1722				}
1723				if (txt) BotMon.live.gui.status.setText(txt);
1724			},
1725			_busyCount: 0,
1726
1727			hideBusy: function(txt = null) {
1728				const el = document.getElementById('botmon__today__busy');
1729				BotMon.live.gui.status._busyCount -= 1;
1730				if (BotMon.live.gui.status._busyCount <= 0) {
1731					if (el) el.style.display = 'none';
1732					if (txt) BotMon.live.gui.status.setText(txt);
1733				}
1734			}
1735		},
1736
1737		lists: {
1738			init: function() {
1739
1740				// function shortcut:
1741				const makeElement = BotMon.t._makeElement;
1742
1743				const parent = document.getElementById('botmon__today__visitorlists');
1744				if (parent) {
1745
1746					for (let i=0; i < 4; i++) {
1747
1748						// change the id and title by number:
1749						let listTitle = '';
1750						let listId = '';
1751						let infolink = null;
1752						switch (i) {
1753							case 0:
1754								listTitle = "Registered users";
1755								listId = 'users';
1756								break;
1757							case 1:
1758								listTitle = "Probably humans";
1759								listId = 'humans';
1760								break;
1761							case 2:
1762								listTitle = "Suspected bots";
1763								listId = 'suspectedBots';
1764								infolink = 'https://leib.be/sascha/projects/dokuwiki/botmon/info/suspected_bots';
1765								break;
1766							case 3:
1767								listTitle = "Known bots";
1768								listId = 'knownBots';
1769								infolink = 'https://leib.be/sascha/projects/dokuwiki/botmon/info/known_bots';
1770								break;
1771							default:
1772								console.warn('Unknown list number.');
1773						}
1774
1775						const details = makeElement('details', {
1776							'data-group': listId,
1777							'data-loaded': false
1778						});
1779						const title = details.appendChild(makeElement('summary'));
1780						title.appendChild(makeElement('span', {'class': 'title'}, listTitle));
1781						if (infolink) {
1782							title.appendChild(makeElement('a', {
1783								'class': 'info',
1784								'target': '_blank',
1785								'href': infolink,
1786								'title': "More information"
1787							}, "Info"));
1788						}
1789						details.addEventListener("toggle", this._onDetailsToggle);
1790
1791						parent.appendChild(details);
1792
1793					}
1794				}
1795			},
1796
1797			_onDetailsToggle: function(e) {
1798				//console.info('BotMon.live.gui.lists._onDetailsToggle()');
1799
1800				const target = e.target;
1801
1802				if (target.getAttribute('data-loaded') == 'false') { // only if not loaded yet
1803					target.setAttribute('data-loaded', 'loading');
1804
1805					const fillType = target.getAttribute('data-group');
1806					const fillList = BotMon.live.data.analytics.groups[fillType];
1807					if (fillList && fillList.length > 0) {
1808
1809						const ul = BotMon.t._makeElement('ul');
1810
1811						fillList.forEach( (it) => {
1812							ul.appendChild(BotMon.live.gui.lists._makeVisitorItem(it, fillType));
1813						});
1814
1815						target.appendChild(ul);
1816						target.setAttribute('data-loaded', 'true');
1817					} else {
1818						target.setAttribute('data-loaded', 'false');
1819					}
1820
1821				}
1822			},
1823
1824			_makeVisitorItem: function(data, type) {
1825
1826				// shortcut for neater code:
1827				const make = BotMon.t._makeElement;
1828
1829				let ipType = ( data.ip.indexOf(':') >= 0 ? '6' : '4' );
1830				if (data.ip == '127.0.0.1' || data.ip == '::1' ) ipType = '0';
1831
1832				const platformName = (data._platform ? data._platform.n : 'Unknown');
1833				const clientName = (data._client ? data._client.n: 'Unknown');
1834
1835				const sumClass = ( !data._seenBy || data._seenBy.indexOf(BM_LOGTYPE.SERVER) < 0 ? 'noServer' : 'hasServer');
1836
1837				const li = make('li'); // root list item
1838				const details = make('details');
1839				const summary = make('summary', {
1840					'class': sumClass
1841				});
1842				details.appendChild(summary);
1843
1844				const span1 = make('span'); /* left-hand group */
1845
1846				if (data._type !== BM_USERTYPE.KNOWN_BOT) { /* No platform/client for bots */
1847					span1.appendChild(make('span', { /* Platform */
1848						'class': 'icon_only platform pf_' + (data._platform ? data._platform.id : 'unknown'),
1849						'title': "Platform: " + platformName
1850					}, platformName));
1851
1852					span1.appendChild(make('span', { /* Client */
1853						'class': 'icon_only client client cl_' + (data._client ? data._client.id : 'unknown'),
1854						'title': "Client: " + clientName
1855					}, clientName));
1856				}
1857
1858				// identifier:
1859				if (data._type == BM_USERTYPE.KNOWN_BOT) { /* Bot only */
1860
1861					const botName = ( data._bot && data._bot.n ? data._bot.n : "Unknown");
1862					span1.appendChild(make('span', { /* Bot */
1863						'class': 'has_icon bot bot_' + (data._bot ? data._bot.id : 'unknown'),
1864						'title': "Bot: " + botName
1865					}, botName));
1866
1867				} else if (data._type == BM_USERTYPE.KNOWN_USER) { /* User only */
1868
1869					span1.appendChild(make('span', { /* User */
1870						'class': 'has_icon user_known',
1871						'title': "User: " + data.usr
1872					}, data.usr));
1873
1874				} else { /* others */
1875
1876
1877					/*span1.appendChild(make('span', { // IP-Address
1878						'class': 'has_icon ipaddr ip' + ipType,
1879						'title': "IP-Address: " + data.ip
1880					}, data.ip));*/
1881
1882					span1.appendChild(make('span', { /* Internal ID */
1883						'class': 'has_icon session typ_' + data.typ,
1884						'title': "ID: " + data.id
1885					}, data.id));
1886				}
1887
1888				// country flag:
1889				if (data.geo && data.geo !== 'ZZ') {
1890					span1.appendChild(make('span', {
1891						'class': 'icon_only country ctry_' + data.geo.toLowerCase(),
1892						'data-ctry': data.geo,
1893						'title': "Country: " + ( data._country || "Unknown")
1894					}, ( data._country || "Unknown") ));
1895				}
1896
1897				// referer icons:
1898				if ((data._type == BM_USERTYPE.PROBABLY_HUMAN || data._type == BM_USERTYPE.LIKELY_BOT) && data.ref) {
1899					const refInfo = BotMon.live.data.analytics.getRefererInfo(data.ref);
1900					span1.appendChild(make('span', {
1901						'class': 'icon_only referer ref_' + refInfo.id,
1902						'title': "Referer: " + data.ref
1903					}, refInfo.n));
1904				}
1905
1906				summary.appendChild(span1);
1907				const span2 = make('span'); /* right-hand group */
1908
1909					span2.appendChild(make('span', { /* first-seen */
1910						'class': 'has_iconfirst-seen',
1911						'title': "First seen: " + data._firstSeen.toLocaleString() + " UTC"
1912					}, BotMon.t._formatTime(data._firstSeen)));
1913
1914					span2.appendChild(make('span', { /* page views */
1915						'class': 'has_icon pageviews',
1916						'title': data._pageViews.length + " page view(s)"
1917					}, data._pageViews.length));
1918
1919				summary.appendChild(span2);
1920
1921				// add details expandable section:
1922				details.appendChild(BotMon.live.gui.lists._makeVisitorDetails(data, type));
1923
1924				li.appendChild(details);
1925				return li;
1926			},
1927
1928			_makeVisitorDetails: function(data, type) {
1929
1930				// shortcut for neater code:
1931				const make = BotMon.t._makeElement;
1932
1933				let ipType = ( data.ip.indexOf(':') >= 0 ? '6' : '4' );
1934				if (data.ip == '127.0.0.1' || data.ip == '::1' ) ipType = '0';
1935				const platformName = (data._platform ? data._platform.n : 'Unknown');
1936				const clientName = (data._client ? data._client.n: 'Unknown');
1937
1938				const dl = make('dl', {'class': 'visitor_details'});
1939
1940				if (data._type == BM_USERTYPE.KNOWN_BOT) {
1941
1942					dl.appendChild(make('dt', {}, "Bot name:")); /* bot info */
1943					dl.appendChild(make('dd', {'class': 'icon_only bot bot_' + (data._bot ? data._bot.id : 'unknown')},
1944						(data._bot ? data._bot.n : 'Unknown')));
1945
1946					if (data._bot && data._bot.url) {
1947						dl.appendChild(make('dt', {}, "Bot info:")); /* bot info */
1948						const botInfoDd = dl.appendChild(make('dd'));
1949						botInfoDd.appendChild(make('a', {
1950							'href': data._bot.url,
1951							'target': '_blank'
1952						}, data._bot.url)); /* bot info link*/
1953
1954					}
1955
1956				} else { /* not for bots */
1957
1958					dl.appendChild(make('dt', {}, "Client:")); /* client */
1959					dl.appendChild(make('dd', {'class': 'has_icon client cl_' + (data._client ? data._client.id : 'unknown')},
1960						clientName + ( data._client.v > 0 ? ' (' + data._client.v + ')' : '' ) ));
1961
1962					dl.appendChild(make('dt', {}, "Platform:")); /* platform */
1963					dl.appendChild(make('dd', {'class': 'has_icon platform pf_' + (data._platform ? data._platform.id : 'unknown')},
1964						platformName + ( data._platform.v > 0 ? ' (' + data._platform.v + ')' : '' ) ));
1965
1966					dl.appendChild(make('dt', {}, "IP-Address:"));
1967					const ipItem = make('dd', {'class': 'has_icon ipaddr ip' + ipType});
1968						ipItem.appendChild(make('span', {'class': 'address'} , data.ip));
1969						ipItem.appendChild(make('a', {
1970							'class': 'icon_only extlink dnscheck',
1971							'href': `https://dnschecker.org/ip-location.php?ip=${encodeURIComponent(data.ip)}`,
1972							'target': 'dnscheck',
1973							'title': "View this address on DNSChecker.org"
1974						} , "Check Address"));
1975						ipItem.appendChild(make('a', {
1976							'class': 'icon_only extlink ipinfo',
1977							'href': `https://ipinfo.io/${encodeURIComponent(data.ip)}`,
1978							'target': 'ipinfo',
1979							'title': "View this address on IPInfo.io"
1980						} , "DNS Info"));
1981					dl.appendChild(ipItem);
1982
1983					/*dl.appendChild(make('dt', {}, "ID:"));
1984					dl.appendChild(make('dd', {'class': 'has_icon ip' + data.typ}, data.id));*/
1985				}
1986
1987				if (Math.abs(data._lastSeen - data._firstSeen) < 100) {
1988					dl.appendChild(make('dt', {}, "Seen:"));
1989					dl.appendChild(make('dd', {'class': 'seen'}, data._firstSeen.toLocaleString()));
1990				} else {
1991					dl.appendChild(make('dt', {}, "First seen:"));
1992					dl.appendChild(make('dd', {'class': 'firstSeen'}, data._firstSeen.toLocaleString()));
1993					dl.appendChild(make('dt', {}, "Last seen:"));
1994					dl.appendChild(make('dd', {'class': 'lastSeen'}, data._lastSeen.toLocaleString()));
1995				}
1996
1997				dl.appendChild(make('dt', {}, "User-Agent:"));
1998				dl.appendChild(make('dd', {'class': 'agent'}, data.agent));
1999
2000				dl.appendChild(make('dt', {}, "Languages:"));
2001				dl.appendChild(make('dd', {'class': 'langs'}, ` [${data.accept}]`));
2002
2003				if (data.geo && data.geo !=='') {
2004					dl.appendChild(make('dt', {}, "Location:"));
2005					dl.appendChild(make('dd', {
2006						'class': 'has_icon country ctry_' + data.geo.toLowerCase(),
2007						'data-ctry': data.geo,
2008						'title': "Country: " + data._country
2009					}, data._country + ' (' + data.geo + ')'));
2010				}
2011
2012				dl.appendChild(make('dt', {}, "Session ID:"));
2013				dl.appendChild(make('dd', {'class': 'has_icon session typ_' + data.typ}, data.id));
2014
2015				dl.appendChild(make('dt', {}, "Seen by:"));
2016				dl.appendChild(make('dd', undefined, data._seenBy.join(', ') ));
2017
2018				dl.appendChild(make('dt', {}, "Visited pages:"));
2019				const pagesDd = make('dd', {'class': 'pages'});
2020				const pageList = make('ul');
2021
2022				/* list all page views */
2023				data._pageViews.sort( (a, b) => a._firstSeen - b._firstSeen );
2024				data._pageViews.forEach( (page) => {
2025					pageList.appendChild(BotMon.live.gui.lists._makePageViewItem(page));
2026				});
2027				pagesDd.appendChild(pageList);
2028				dl.appendChild(pagesDd);
2029
2030				/* bot evaluation rating */
2031				if (data._type !== BM_USERTYPE.KNOWN_BOT && data._type !== BM_USERTYPE.KNOWN_USER) {
2032					dl.appendChild(make('dt', undefined, "Bot rating:"));
2033					dl.appendChild(make('dd', {'class': 'bot-rating'}, ( data._botVal ? data._botVal : '–' ) + ' (of ' + BotMon.live.data.rules._threshold + ')'));
2034
2035					/* add bot evaluation details: */
2036					if (data._eval) {
2037						dl.appendChild(make('dt', {}, "Bot evaluation:"));
2038						const evalDd = make('dd');
2039						const testList = make('ul',{
2040							'class': 'eval'
2041						});
2042						data._eval.forEach( test => {
2043
2044							const tObj = BotMon.live.data.rules.getRuleInfo(test);
2045							let tDesc = tObj ? tObj.desc : test;
2046
2047							// special case for Bot IP range test:
2048							if (tObj.func == 'fromKnownBotIP') {
2049								const rangeInfo = BotMon.live.data.rules.getBotIPInfo(data.ip);
2050								if (rangeInfo) {
2051									tDesc += ' (' + (rangeInfo.label ? rangeInfo.label : 'Unknown') + ')';
2052								}
2053							}
2054
2055							// create the entry field
2056							const tstLi = make('li');
2057							tstLi.appendChild(make('span', {
2058								'data-testid': test
2059							}, tDesc));
2060							tstLi.appendChild(make('span', {}, ( tObj ? tObj.bot : '—') ));
2061							testList.appendChild(tstLi);
2062						});
2063
2064						// add total row
2065						const tst2Li = make('li', {
2066							'class': 'total'
2067						});
2068						/*tst2Li.appendChild(make('span', {}, "Total:"));
2069						tst2Li.appendChild(make('span', {}, data._botVal));
2070						testList.appendChild(tst2Li);*/
2071
2072						evalDd.appendChild(testList);
2073						dl.appendChild(evalDd);
2074					}
2075				}
2076				// return the element to add to the UI:
2077				return dl;
2078			},
2079
2080			// make a page view item:
2081			_makePageViewItem: function(page) {
2082				//console.log("makePageViewItem:",page);
2083
2084				// shortcut for neater code:
2085				const make = BotMon.t._makeElement;
2086
2087				// the actual list item:
2088				const pgLi = make('li');
2089
2090				const row1 = make('div', {'class': 'row'});
2091
2092					row1.appendChild(make('span', { // page id is the left group
2093						'data-lang': page.lang,
2094						'title': "PageID: " + page.pg
2095					}, page.pg)); /* DW Page ID */
2096
2097					// get the time difference:
2098					row1.appendChild(make('span', {
2099						'class': 'first-seen',
2100						'title': "First visited: " + page._firstSeen.toLocaleString() + " UTC"
2101					}, BotMon.t._formatTime(page._firstSeen)));
2102
2103				pgLi.appendChild(row1);
2104
2105				/* LINE 2 */
2106
2107				const row2 = make('div', {'class': 'row'});
2108
2109					// page referrer:
2110					if (page._ref) {
2111						row2.appendChild(make('span', {
2112							'class': 'referer',
2113							'title': "Referrer: " + page._ref.href
2114						}, page._ref.hostname));
2115					} else {
2116						row2.appendChild(make('span', {
2117							'class': 'referer'
2118						}, "No referer"));
2119					}
2120
2121					// visit duration:
2122					let visitTimeStr = "Bounce";
2123					const visitDuration = page._lastSeen.getTime() - page._firstSeen.getTime();
2124					if (visitDuration > 0) {
2125						visitTimeStr = Math.floor(visitDuration / 1000) + "s";
2126					}
2127					const tDiff = BotMon.t._formatTimeDiff(page._firstSeen, page._lastSeen);
2128					if (tDiff) {
2129						row2.appendChild(make('span', {'class': 'visit-length', 'title': 'Last seen: ' + page._lastSeen.toLocaleString()}, tDiff));
2130					} else {
2131						row2.appendChild(make('span', {
2132							'class': 'bounce',
2133							'title': "Visitor bounced"}, "Bounce"));
2134					}
2135
2136				pgLi.appendChild(row2);
2137
2138				return pgLi;
2139			}
2140		}
2141	}
2142};
2143
2144/* launch only if the BotMon admin panel is open: */
2145if (document.getElementById('botmon__admin')) {
2146	BotMon.init();
2147}