source: trunk/Modules/TimeMeasure/Main.php

Last change on this file was 935, checked in by chronos, 3 years ago
  • Modified: Code cleanup.
File size: 10.0 KB
Line 
1<?php
2
3class PageMeasure extends Page
4{
5 public array $GraphTimeRanges;
6 public array $DefaultVariables;
7 public int $ImageHeight;
8 public int $ImageWidth;
9
10 function __construct(System $System)
11 {
12 parent::__construct($System);
13 $this->Title = 'Grafy';
14 $this->Description = 'Časové grafy veličin';
15 $this->ImageWidth = 800;
16 $this->ImageHeight = 200;
17 $this->ParentClass = 'PagePortal';
18 $this->DefaultVariables = array(
19 'TimeSpecify' => 0,
20 'Period' => 'day',
21 'Measure' => 1,
22 'Differential' => 0,
23 );
24 $this->GraphTimeRanges = array
25 (
26 'hour' => array(
27 'caption' => 'Hodina',
28 'period' => 3600,
29 ),
30 'day' => array(
31 'caption' => 'Den',
32 'period' => 86400, // 3600 * 24,
33 ),
34 'week' => array(
35 'caption' => 'Týden',
36 'period' => 604800, // 3600 * 24 * 7,
37 ),
38 'month' => array(
39 'caption' => 'Měsíc',
40 'period' => 2592000, // 3600 * 24 * 30,
41 ),
42 'year' => array(
43 'caption' => 'Rok',
44 'period' => 31536000, // 3600 * 24 * 365,
45 ),
46 'years' => array(
47 'caption' => 'Desetiletí',
48 'period' => 315360000, // 3600 * 24 * 365 * 10,
49 ),
50 );
51 }
52
53 function EditTime($Time)
54 {
55 global $MonthNames;
56
57 $Output = '<form style="display: inline;" action="?Operation=SetTime&amp;Time='.$Time.'" method="post">';
58
59 $TimeParts = getdate($_SESSION[$Time]);
60
61 // Day selection
62 $Output .= '<select name="Day">';
63 for ($I = 1; $I < 32; $I++)
64 {
65 if ($I == $TimeParts['mday']) $Selected = ' selected="1"'; else $Selected = '';
66 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
67 }
68 $Output .= '</select>. ';
69
70 // Month selection
71 $Output .= '<select name="Month">';
72 foreach ($MonthNames as $Index => $Month)
73 {
74 if ($Index == $TimeParts['mon']) $Selected = ' selected="1"'; else $Selected = '';
75 if ($Index > 0) $Output .= '<option value="'.$Index.'"'.$Selected.'>'.$Month.'</option>';
76 }
77 $Output .= '</select>. ';
78
79 // Day selection
80 $Output .= '<select name="Year">';
81 for ($I = 2000; $I < 2010; $I++)
82 {
83 if ($I == $TimeParts['year']) $Selected = ' selected="1"'; else $Selected = '';
84 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
85 }
86 $Output .= '</select> &nbsp;&nbsp; ';
87
88 // Hour selection
89 $Output .= '<select name="Hour">';
90 for ($I = 0; $I < 24; $I++)
91 {
92 if ($I == $TimeParts['hours']) $Selected = ' selected="1"'; else $Selected = '';
93 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
94 }
95 $Output .= '</select> : ';
96
97 // Minute selection
98 $Output .= '<select name="Minute">';
99 for ($I = 0; $I < 60; $I++)
100 {
101 if ($I == $TimeParts['minutes']) $Selected = ' selected="1"'; else $Selected = '';
102 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
103 }
104 $Output .= '</select> ';
105 $Output .= '<input type="submit" value="Nastavit">';
106 $Output .= '</form>';
107
108 $Output .= ' <form style="display: inline;" action="?Operation=SetTimeNow&amp;Time='.$Time.'" method="post">';
109 $Output .= '<input type="submit" value="Aktuální čas">';
110 $Output .= '</form>';
111
112 return $Output;
113 }
114
115 function Show(): string
116 {
117 $Debug = 0;
118
119 foreach ($this->DefaultVariables as $Index => $Variable)
120 {
121 if (!array_key_exists($Index, $_SESSION)) $_SESSION[$Index] = $Variable;
122 if (array_key_exists($Index, $_GET)) $_SESSION[$Index] = $_GET[$Index];
123 if (array_key_exists($Index, $_POST)) $_SESSION[$Index] = $_POST[$Index];
124 //$$Index = $_SESSION[$Index];
125 }
126
127 if ($_SESSION['TimeSpecify'] == 0)
128 {
129 $_SESSION['TimeEnd'] = time() - 60;
130 $_SESSION['TimeStart'] = $_SESSION['TimeEnd'] - $this->GraphTimeRanges[$_SESSION['Period']]['period'];
131 }
132
133 $Output = '<div style="text-align: center;">';
134
135 if (!array_key_exists('Operation', $_GET)) $_GET['Operation'] = '';
136 switch ($_GET['Operation'])
137 {
138 case 'SetTime':
139 if (array_key_exists('Time', $_GET) and array_key_exists('Month', $_POST) and array_key_exists('Day', $_POST) and
140 array_key_exists('Year', $_POST) and array_key_exists('Hour', $_POST) and array_key_exists('Minute', $_POST))
141 {
142 if (($_GET['Time'] == 'TimeStart') or ($_GET['Time'] == 'TimeEnd'))
143 {
144 $_SESSION[$_GET['Time']] = mktime($_POST['Hour'], $_POST['Minute'], 0, $_POST['Month'],
145 $_POST['Day'], $_POST['Year']);
146 $$_GET['Time'] = $_SESSION[$_GET['Time']];
147 }
148 }
149 break;
150 case 'SetTimeNow':
151 if (array_key_exists('Time', $_GET))
152 {
153 if (($_GET['Time'] == 'TimeStart') or ($_GET['Time'] == 'TimeEnd'))
154 {
155 $_SESSION[$_GET['Time']] = time();
156 $$_GET['Time'] = $_SESSION[$_GET['Time']];
157 }
158 }
159 break;
160 }
161 $Output .= '<strong>Časový úsek:</strong><br>';
162 // Show graf time range menu
163 if ($_SESSION['TimeSpecify'] == 0)
164 {
165 $Output .= 'Délka úseku: ';
166 foreach ($this->GraphTimeRanges as $Index => $Item)
167 $Output .= '<a href="?Period='.$Index.'">'.$Item['caption'].'</a>&nbsp;';
168 $Output .= '<br/>';
169 $Output .= '<a href="?TimeSpecify=1">Přesnější nastavení...</a><br>';
170 } else {
171 $Output .= '<table style="margin: 10px auto; " cellspacing="0" cellpadding="2" border="0">';
172 $Output .= '<tr><td>Počátek:</td><td>'.$this->EditTime('TimeStart').'</td></tr>';
173 $Output .= '<tr><td>Konec:</td><td>'.$this->EditTime('TimeEnd').'</td></tr>';
174 $Output .= '</table>';
175 $Output .= '<a href="?TimeSpecify=0">Jednoduché nastavení...</a><br>';
176 }
177 $Output .= '<br/>'.$this->Graph();
178
179 $Output .= '<br/>'.$this->MeasureTable();
180 return $Output;
181 }
182
183 function Graph(): string
184 {
185 $Output = '<strong>Graf:</strong><br/>';
186 $Output .= '<img alt="Graf" src="'.$this->System->Link('/grafy/graf.png?Measure='.$_SESSION['Measure'].
187 '&amp;From='.$_SESSION['TimeStart'].'&amp;To='.$_SESSION['TimeEnd'].'&amp;Width='.
188 $this->ImageWidth.'&amp;Height='.$this->ImageHeight.'&amp;Differential='.
189 $_SESSION['Differential']).'" width="'.$this->ImageWidth.'" height="'.$this->ImageHeight.'"><br>';
190 $Output .= '<a href="?Measure='.$_SESSION['Measure'].'&amp;TimeStart='.
191 $_SESSION['TimeStart'].'&amp;TimeEnd='.$_SESSION['TimeEnd'].'&amp;TimeSpecify=1&amp;Differential='.$_SESSION['Differential'].'">Odkaz na vybraný graf</a><br>';
192 return $Output;
193 }
194
195 function MeasureTable(): string
196 {
197 $PrefixMultiplier = new PrefixMultiplier();
198
199 $DbResult = $this->Database->query('SELECT COUNT(*) FROM `Measure` WHERE `Enabled`=1');
200 $DbRow = $DbResult->fetch_row();
201 $PageList = GetPageList('Measures', $DbRow[0]);
202
203 $Output = $PageList['Output'];
204 $Output .= '<table class="WideTable" style="font-size: small;">';
205
206 $TableColumns = array(
207 array('Name' => 'Name', 'Title' => 'Měřená veličina'),
208 array('Name' => 'LastValue', 'Title' => 'Poslední hodnota'),
209 array('Name' => 'LastValueTime', 'Title' => 'Čas posledního měření'),
210 array('Name' => 'Continuous', 'Title' => 'Interpolace'),
211 array('Name' => 'Description', 'Title' => 'Popis'),
212 );
213 if (array_key_exists('Debug', $_GET))
214 {
215 $TableColumns[] = array('Name' => 'ItemCount', 'Title' => 'Počet položek');
216 $TableColumns[] = array('Name' => 'MeasureDuration', 'Title' => 'Čas vykonání');
217 }
218 $Order = GetOrderTableHeader('Measures', $TableColumns, 'Name', 0);
219 $Output .= $Order['Output'];
220
221 $Result = $this->Database->select('Measure', '*', '`Enabled`=1 '.$Order['SQL'].$PageList['SQLLimit']);
222 while ($Measure = $Result->fetch_array())
223 {
224 $DbResult2 = $this->Database->select('MeasureMethod', '*', '`Id`='.$Measure['Method']);
225 $MeasureMethod = $DbResult2->fetch_assoc();
226 $StopWatchStart = GetMicrotime();
227 if (array_key_exists('Debug', $_GET))
228 {
229 $DbResult = $this->Database->select($Measure['DataTable'], 'COUNT(*)', 'Measure='.$Measure['Id']);
230 $RowCount = $DbResult->fetch_array();
231 $RowCount = $RowCount[0];
232 }
233 $Result2 = $this->Database->select($Measure['DataTable'], 'Time, Avg', 'Measure='.$Measure['Id'].' AND Level=0 ORDER BY Time DESC LIMIT 1');
234 if ($Result2->num_rows > 0)
235 {
236 $Row = $Result2->fetch_array();
237 $LastMeasureTime = date('j.n.Y G:i:s', MysqlDateTimeToTime($Row['Time']));
238 $LastMeasureValue = $PrefixMultiplier->Add($Row['Avg'], $MeasureMethod['Unit']);
239 } else
240 {
241 $LastMeasureTime = '&nbsp;';
242 $LastMeasureValue = '&nbsp;';
243 }
244 if ($Measure['Continuity'] == 1) $Interpolate = 'Ano';
245 else $Interpolate = 'Ne';
246 //if ($Measure['Description'] == '') $Measure['Description'] = '&nbsp;';
247 $GenerationTime = floor((GetMicrotime() - $StopWatchStart) * 1000 ) / 1000;
248 $Output .= '<tr><td><a href="?Measure='.$Measure['Id'].'&amp;Differential=0">'.$Measure['Name'].'</a></td><td align="center">'.$LastMeasureValue.'</td><td align="center">'.$LastMeasureTime.'</td><td align="center">'.$Interpolate.'</td><td>'.$Measure['Description'].'</td>';
249 if (array_key_exists('Debug', $_GET)) $Output .= '<td>'.$RowCount.'</td><td>'.$GenerationTime.'</td>';
250 $Output .= '</tr>';
251 }
252 $Output .= '</table>';
253 $Output .= $PageList['Output'];
254
255 //ShowPage($Output);
256 $Output .= '</div>';
257 return $Output;
258 }
259}
260
261class PageMeasureAddValue extends Page
262{
263 function Show(): string
264 {
265 $this->RawPage = true;
266
267 $Output = '';
268 if (!array_key_exists('MeasureId', $_GET)) return 'Nebylo zadáno Id měření.';
269 if (!array_key_exists('Value', $_GET)) return 'Nebyla zadána hodnota.';
270 $Measure = new Measure($this->System);
271 $Measure->Load($_GET['MeasureId']);
272 if (!isset($Measure->Data['Id'])) return 'Měření s Id '.$_GET['MeasureId'].' nenalezeno.';
273 $Measure->AddValue(array('Min' => $_GET['Value'], 'Avg' => $_GET['Value'], 'Max' => $_GET['Value']));
274 return $Output;
275 }
276}
Note: See TracBrowser for help on using the repository browser.