summaryrefslogtreecommitdiffstats
path: root/modules-available/statistics/hooks/locations-column.inc.php
blob: 51f280be0e0d4d5822caca287e3f97eb3705577c (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
149
150
<?php

if (!User::hasPermission('.statistics.view.list')) {
	return null;
}

class ClientCountLocationColumn extends AbstractLocationColumn
{

	private $lookup;

	public function __construct()
	{
		$this->lookup = StatisticsColumnGetData([]);
	}

	public function getColumnHtml(int $locationId): string
	{
		if (!isset($this->lookup[$locationId]))
			return '';
		if ($this->lookup[$locationId]['hasChild'] ?? false) {
			$child = <<<EOF
			(<a href="?do=Statistics&amp;show=list&amp;filters=location~{$locationId}">&downarrow;{$this->lookup[$locationId]['clientCountSum']}</a>)
EOF;
		} else {
			$child = '';
		}

		return <<<EOF
			<div class="pull-right">
			<a href="?do=Statistics&amp;show=list&amp;filters=location={$locationId}">&nbsp;{$this->lookup[$locationId]['clientCount']}&nbsp;</a>
			<span class="text-right" style="display:inline-block;width:6ex">$child</span>
			</div>
EOF;
	}

	public function getEditUrl(int $locationId): string
	{
		return '';
	}

	public function header(): string
	{
		return Dictionary::translateFileModule('statistics', 'module', 'location-column-header-count');
	}

	public function priority(): int
	{
		return 800;
	}

}

class ClientLoadLocationColumn extends AbstractLocationColumn
{

	private $lookup;

	public function __construct()
	{
		$this->lookup = StatisticsColumnGetData([]);
	}

	public function getColumnHtml(int $locationId): string
	{
		if (!isset($this->lookup[$locationId]) || $this->lookup[$locationId]['clientCount'] === 0)
			return '';
		$c =& $this->lookup[$locationId];
		return <<<EOF
			<div class="load-col text-right" style="background:linear-gradient(to right, #f97, #f97 {$c['clientLoad']}%,
				#6fa {$c['clientLoad']}%, #6fa {$c['clientIdle']}%, #eee {$c['clientIdle']}%)">
				{$c['clientLoad']}&thinsp;%
			</div>
EOF;

	}

	public function getEditUrl(int $locationId): string
	{
		return '';
	}

	public function header(): string
	{
		return Dictionary::translateFileModule('statistics', 'module', 'location-column-header-load');
	}

	public function priority(): int
	{
		return 900;
	}

}

function StatisticsColumnGetData(array $allowedLocationIds): array
{
	static $data = [];
	if (!empty($data))
		return $data;
	$extra = '';
	if (in_array(0, $allowedLocationIds)) {
		$extra = ' OR locationid IS NULL';
	}
	$locs = Location::getLocationsAssoc();
	$res = Database::simpleQuery("SELECT m.locationid, Count(*) AS cnt,
       		Sum(If(m.state = 'OCCUPIED', 1, 0)) AS used, Sum(If(m.state = 'IDLE', 1, 0)) AS idle
 				 FROM machine m WHERE (locationid IN (:allowedLocationIds) $extra) GROUP BY locationid", compact('allowedLocationIds'));
	foreach ($res as $row) {
		$locId = (int)$row['locationid'];
		$data[$locId] = [
			'clientCount' => $row['cnt'],
			'clientLoad' => round(100 * $row['used'] / $row['cnt']),
			'clientIdle' => round(100 * ($row['used'] + $row['idle']) / $row['cnt']),
		];
	}
	foreach ($allowedLocationIds as $locId) {
		if (isset($data[$locId]))
			continue;
		$data[$locId] = [
			'clientCount' => 0,
			'clientLoad' => 0,
			'clientIdle' => 0,
		];
	}
	foreach ($data as $locId => &$loc) {
		if (!in_array($locId, $allowedLocationIds))
			continue;
		if (!isset($loc['clientCountSum'])) {
			$loc['clientCountSum'] = 0;
		}
		$loc['clientCountSum'] += $loc['clientCount'];
		if ($locId !== 0) {
			foreach ($locs[$locId]['parents'] as $pid) {
				if (!in_array($pid, $allowedLocationIds))
					continue;
				$data[$pid]['hasChild'] = true;
				if (!isset($data[$pid]['clientCountSum'])) {
					$data[$pid]['clientCountSum'] = 0;
				}
				$data[$pid]['clientCountSum'] += $loc['clientCount'];
			}
		}
	}
	unset($loc);
	return $data;
}

StatisticsColumnGetData($allowedLocationIds);

return [new ClientCountLocationColumn(), new ClientLoadLocationColumn()];