summaryrefslogtreecommitdiffstats
path: root/modules-available/statistics_reporting/inc/getdata.inc.php
blob: 13d395029a154c27e154419ff9a8009a38728c97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

define('GETDATA_ANONYMOUS', 1);
define('GETDATA_PRINTABLE', 2);

class GetData
{
	public static $from;
	public static $to;
	public static $lowerTimeBound = 0;
	public static $upperTimeBound = 24;
	public static $salt;
	private static $TS_LIST = false;
	private static $SECS_LIST = false;

	private static function fillLocation(&$entry, $anonymize)
	{
		$locations = Location::getLocationsAssoc();
		if ($anonymize) {
			$entry['locationname'] = md5($entry['locationid'] . self::$salt);
		} elseif (isset($locations[$entry['locationid']])) {
			$entry['locationname'] = $locations[$entry['locationid']]['locationname'];
			$entry['parentlocations'] = array_reduce($locations[$entry['locationid']]['parents'], function ($carry, $item) {
				return $carry . sprintf("%04d", $item);
			}) . sprintf("%04d", $entry['locationid']);
		} else {
			$entry['locationname'] = Dictionary::translate('notAssigned', true);
		}
		if ($anonymize) {
			unset($entry['locationid']);
		}
	}

	private static function addPrintables(&$entry)
	{
		if (self::$SECS_LIST === false) {
			self::$SECS_LIST = ['totalTime', 'totalOffTime', 'totalIdleTime', 'totalSessionTime', 'totalStandbyTime', 'medianSessionLength'];
		}
		if (self::$TS_LIST === false) {
			self::$TS_LIST = ['lastStart', 'lastLogout'];
		}
		$perc = isset($entry['totalTime']) && $entry['totalTime'] > 0;
		foreach (self::$SECS_LIST as $k) {
			if (isset($entry[$k])) {
				$entry[$k . '_s'] = self::formatSeconds($entry[$k]);
				if ($perc && $k !== 'totalTime') {
					$entry[$k . '_p'] = round($entry[$k] / $entry['totalTime'] * 100);
				}
			}
		}
		foreach (self::$TS_LIST as $k) {
			if (isset($entry[$k])) {
				$entry[$k . '_s'] = Util::prettyTime($entry[$k]);
			}
		}
	}

	// total
	public static function total($flags = 0) {
		$printable = 0 !== ($flags & GETDATA_PRINTABLE);
		// total time online, average time online, total  number of logins
		$data = Queries::getOverallStatistics(self::$from, self::$to, self::$lowerTimeBound, self::$upperTimeBound);

		if ($printable) {
			self::addPrintables($data);
		}
		$data['uniqueUsers'] = Queries::getUniqueUserCount(self::$from, self::$to);

		return $data;
	}

	// per location
	public static function perLocation($flags = 0) {
		$anonymize = 0 !== ($flags & GETDATA_ANONYMOUS);
		$printable = 0 !== ($flags & GETDATA_PRINTABLE);
		$data = Queries::getLocationStatistics(self::$from, self::$to, self::$lowerTimeBound, self::$upperTimeBound);
		foreach ($data as &$entry) {
			//self::nullToZero($entry);
			self::fillLocation($entry, $anonymize);
			if ($printable) {
				self::addPrintables($entry);
			}
		}
		return $data;
	}

	// per client
	public static function perClient($flags = 0, $new = false) {
		$anonymize = 0 !== ($flags & GETDATA_ANONYMOUS);
		$printable = 0 !== ($flags & GETDATA_PRINTABLE);
		$data = Queries::getClientStatistics(self::$from, self::$to, self::$lowerTimeBound, self::$upperTimeBound);
		foreach ($data as &$entry) {
			//self::nullToZero($entry);
			$entry['hostname'] = ($anonymize ? md5($entry['clientName'] . self::$salt) : $entry['clientName']);
			self::fillLocation($entry, $anonymize);
			if ($printable) {
				self::addPrintables($entry);
			}
		}
		return $data;
	}

	// per user
	public static function perUser($flags = 0) {
		$anonymize = 0 !== ($flags & GETDATA_ANONYMOUS);
		$res = Queries::getUserStatistics(self::$from, self::$to, self::$lowerTimeBound, self::$upperTimeBound);
		$data = array();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			if ($anonymize && $row['name'] !== 'anonymous') {
				$row['name'] = md5($row['name'] . self::$salt);
			}
			$data[] = array('user' => $row['name'], 'sessions' => $row['count']);
		}
		return $data;
	}


	// per vm
	public static function perVM($flags = 0) {
		$anonymize = 0 !== ($flags & GETDATA_ANONYMOUS);
		$res = Queries::getVMStatistics(self::$from, self::$to, self::$lowerTimeBound, self::$upperTimeBound);
		$data = array();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			self::nullToZero($row);
			if ($anonymize) {
				$row['name'] = md5($row['name'] . self::$salt);
			}
			$data[] = array('vm' => $row['name'], 'sessions' => $row['count']);
		}
		return $data;
	}

	private static function nullToZero(&$row)
	{
		foreach ($row as &$field) {
			if (is_null($field)) {
				$field = 0;
			}
		}
	}

	// Format $seconds into ".d .h .m .s" format (day, hour, minute, second)
	private static function formatSeconds($seconds)
	{
		return sprintf('%dd, %02d:%02d:%02d', $seconds / (3600*24), ($seconds % (3600*24)) / 3600, ($seconds%3600) / 60, $seconds%60);
	}

}