summaryrefslogblamecommitdiffstats
path: root/modules-available/statistics_reporting/inc/getdata.inc.php
blob: 75db119d6a15f98bf8a0c36216a51533989f40c6 (plain) (tree)
1
2
3
4
5
6
7
8
9

     


                               

             

                            

                                           
                            













                                                                                                                                        
                                                                                      




























                                                                                                                                                         

                

                                                           
                                                                
                                                                                  
                                                                                                                            
 
                                 
                                                   
                 
                                                                                           




                             

                                                                 

                                                                



                                                                                                                             
                                         
                                                            
                         




                             

                                                               

                                                                




                                                                                                                           
                                         
                                                            
                         




                             

                                                             
                                                                
                                                                                                                        
                                
                                        



                                                                                             





                             

                                                           
                                                                
                                                                                                                      
                                
                                        
                                               



                                                                                           



                             
                                                             






                                              

                                                                                
                                                                   
         
                                                                                                                                              

         
 
<?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');
		}
		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(int $flags = 0): array
	{
		$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(int $flags = 0): array
	{
		$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(int $flags = 0): array
	{
		$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(int $flags = 0): array
	{
		$anonymize = 0 !== ($flags & GETDATA_ANONYMOUS);
		$res = Queries::getUserStatistics(self::$from, self::$to, self::$lowerTimeBound, self::$upperTimeBound);
		$data = array();
		foreach ($res as $row) {
			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(int $flags = 0): array
	{
		$anonymize = 0 !== ($flags & GETDATA_ANONYMOUS);
		$res = Queries::getVMStatistics(self::$from, self::$to, self::$lowerTimeBound, self::$upperTimeBound);
		$data = array();
		foreach ($res as $row) {
			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(array &$row): void
	{
		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(int $seconds): string
	{
		return sprintf('%dd, %02d:%02d:%02d', $seconds / (3600*24), ($seconds % (3600*24)) / 3600, ($seconds%3600) / 60, $seconds%60);
	}

}