source: trunk/www/dch/obs_summary.php@ 19450

Last change on this file since 19450 was 19450, checked in by Daniela Dorner, 6 years ago
fixed bug for two resumes scheduled without suspend inbetween
File size: 14.2 KB
Line 
1<?php
2
3 include("db.php");
4 require_once("class.Diff.php");
5
6 // ini_set("display_errors", "On");
7 // ini_set("mysql.trace_mode", "On");
8
9
10$db_id = mysql_connect($host, $user, $pw);
11if ($db_id==FALSE)
12{
13 printf("mysql_connect returned the following error: %s\n", mysql_error());
14 die("");
15}
16mysql_select_db($db);
17
18date_default_timezone_set('UTC');
19
20$date = isset($_GET["d"]) ? $_GET["d"] : date("Y-m-d");//'2016-02-16';
21$night = str_replace("-", "", $date);
22
23// ====================================================================
24
25function fmt($date)
26{
27 $d = date_create_from_format("Y-m-d G:i:s", $date);
28 return "new Date(".$d->format("Y,n-1,j,G,i,s,0").")";
29 //return "new Date(".($d->getTimestamp()*1000).")";//format("Y,n-1,j,G,i,s,0").")";
30}
31
32// ====================================================================
33
34function QuerySchedule(&$data, &$html1, &$html2, &$query, $db_id, $table, $date)
35{
36$query = <<<EOT
37
38SELECT
39 fStart, fSourceName, fUser, fMeasurementTypeName
40FROM $table
41LEFT JOIN Source USING (fSourceKey)
42LEFT JOIN MeasurementType USING (fMeasurementTypeKey)
43WHERE fStart
44 BETWEEN ADDDATE('$date', INTERVAL +12 HOUR)
45 AND ADDDATE('$date', INTERVAL +36 HOUR)
46ORDER BY fStart
47
48EOT;
49
50$html1 = "";
51$array = array();
52
53$result = mysql_query($query, $db_id);
54if ($result)
55{
56 $html1 .= "<table>\n";
57
58 $n = 0;
59 while ($row = mysql_fetch_assoc($result))
60 {
61 $html1 .= " <tr>\n";
62 $html1 .= " <td>".$row["fStart"]."|</td>\n";
63 $html1 .= " <td>".$row["fMeasurementTypeName"]."</td>\n";
64 $html1 .= " <td>|".$row["fSourceName"]."</td>\n";
65 $html1 .= " <td>[".$row["fUser"]."]</td>\n";
66 $html1 .= " </tr>\n";
67
68 $array[$n++] = $row;
69 }
70 $html1 .= "</table>\n";
71
72 mysql_free_result($result);
73}
74else
75 $html1 = "[empty:".mysql_error()."]\n";
76
77// --------------------------------------------------------------------
78
79$html2 = "<table>\n";
80
81$suspend = false;
82$doubleresume=false;
83$prev_data = array();
84
85for ($i=0; $i<$n-1; $i++)
86{
87 // Switch to "suspend-mode"
88 if ($array[$i]['fMeasurementTypeName']=='Suspend')
89 $suspend = true;
90
91 // If a resume is found, the data is replaced with the
92 // last data run with the resume-time as start time.
93 if ($array[$i]['fMeasurementTypeName']=='Resume')
94 {
95 // Sanity check: There is not much we can do
96 if (empty($prev_data) || !$suspend)
97 {
98 $doubleresume=true;
99 continue;
100 }
101
102 $prev_data['fStart'] = $array[$i]['fStart'];
103 $array[$i] = $prev_data;
104 $suspend = false;
105 }
106
107 // None data runs are not displayed
108 if ($array[$i]['fMeasurementTypeName']!='Data')
109 continue;
110
111 // If this is a data run keep that for use after a suspend/resume
112 $prev_data = $array[$i];
113
114 // If the system is suspended, don't display the runs
115 if ($suspend)
116 continue;
117
118 $data .= "[";
119 $data .= "'".$table."',";
120 $data .= "'".$array[$i]['fSourceName']."',";
121 $data .= fmt($array[$i]['fStart']).",";
122 // in case two resumes are scheduled without suspend inbetween, the stop of a later observations needs to be used
123 if ($array[$i+1]['fMeasurementTypeName']=='Resume' && !$suspend)
124 $data .= fmt($array[$i+2]['fStart']).",";
125 else
126 $data .= fmt($array[$i+1]['fStart']).",";
127 $data .= "],\n";
128
129 $html2 .= " <tr>";
130 $html2 .= " <td>".$array[$i]['fStart']."|</td>\n";
131 $html2 .= " <td>".$array[$i]["fSourceName"]."</td>\n";
132 // in case two resumes are scheduled without suspend inbetween, the stop of a later observations needs to be used
133 if ($array[$i+1]['fMeasurementTypeName']=='Resume' && !$suspend)
134 $html2 .= " <td>|".$array[$i+2]["fStart"]."|</td>\n";
135 else
136 $html2 .= " <td>|".$array[$i+1]["fStart"]."|</td>\n";
137 $html2 .= " </tr>\n";
138 $doubleresume=false;
139}
140
141$html2 .= "</table>\n";
142}
143
144// --------------------------------------------------------------------
145
146function QueryRunInfo(&$data, &$diff, &$html2, &$query2, $night, $db_id, $where)
147{
148$query2 = <<< EOT
149
150SELECT
151 fRunID,
152 fSourceName,
153 fRunStart,
154 fRunStop
155FROM RunInfo
156LEFT JOIN Source USING(fSourceKey)
157LEFT JOIN AnalysisResultsRunLP USING (fNight, fRunID)
158WHERE fNight=$night
159 AND NOT fRunStart='0000-00-00 00:00:00'
160 AND NOT fRunStop='0000-00-00 00:00:00'
161 AND fRunTypeKEY=1
162 $where
163ORDER BY fRunID
164
165EOT;
166
167// --------------------------------------------------------------------
168
169$array2 = array();
170
171$result2 = mysql_query($query2, $db_id);
172if ($result2)
173{
174 $html2 .= "<table>\n";
175
176 $n = 0;
177 while ($row = mysql_fetch_assoc($result2))
178 {
179 if (empty($row['fSourceName']))
180 continue;
181
182 $data .= "[";
183 $data .= "'RunInfo',";
184 $data .= "'".$row['fSourceName']."',";
185 $data .= fmt($row['fRunStart']).",";
186 $data .= fmt($row['fRunStop']).",";
187 $data .= "],\n";
188
189
190 $html2 .= " <tr>\n";
191 $html2 .= " <td>".$row["fRunStart"]." | </td>\n";
192 $html2 .= " <td>".$row["fSourceName"]."</td>\n";
193 $html2 .= " <td> | ".$row["fRunStop"]."</td>\n";
194 $html2 .= " </tr>\n";
195
196 $diff .= $row["fRunStart"]." | ";
197 $diff .= $row["fSourceName"]." | ";
198 $diff .= $row["fRunStop"]."\n";
199
200 $n++;
201 }
202 $html2 .= "</table>\n";
203
204 mysql_free_result($result2);
205}
206else
207 $html2 = "[empty:".mysql_error()."]\n";
208}
209
210// --------------------------------------------------------------------
211
212QuerySchedule($data, $html0a, $html0b, $query0, $db_id, "AutoSchedule", $date);
213QuerySchedule($data, $html1a, $html1b, $query1, $db_id, "Schedule", $date);
214
215// --------------------------------------------------------------------
216
217QueryRunInfo($dataA, $diffA, $html2, $query2, $night, $db_id, "");
218QueryRunInfo($dataB, $diffB, $html3, $query3, $night, $db_id, "AND NOT ISNULL(fNumExcEvts)");
219
220$data .= $dataA;
221
222$diff = "";
223if ($dataA!=$dataB)
224{
225 $diff = Diff::toTable(Diff::compare($diffA, $diffB));
226 $data .= str_replace("RunInfo", "QLA", $dataB);
227}
228
229// --------------------------------------------------------------------
230
231mysql_close($db_id);
232
233// --------------------------------------------------------------------
234
235$color = "false";
236if (empty($data))
237{
238 $data = "['No Schedule','',new Date(1970,1,1,19,0,0),new Date(1970,1,2,7,0,0)]";
239 $color = "'#eee'";
240}
241
242$querycol0 = colorize($query0);
243$querycol1 = colorize($query1);
244$querycol2 = colorize($query2);
245$querycol3 = colorize($query3);
246
247// --------------------------------------------------------------------
248
249echo <<<EOT
250
251<!DOCTYPE HTML>
252<html>
253<head>
254<script src="//code.jquery.com/jquery-2.2.1.min.js" integrity="sha256-gvQgAFzTH6trSrAWoH1iPo9Xc96QxSZ3feW6kem+O00=" crossorigin="anonymous"></script>
255<script src="//code.jquery.com/ui/1.11.4/jquery-ui.min.js" integrity="sha256-xNjb53/rY+WmG+4L6tTl9m6PpqknWZvRt0rO1SRnJzw=" crossorigin="anonymous"></script>
256
257<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
258
259<script type="text/javascript" src="//www.gstatic.com/charts/loader.js"></script>
260<script type="text/javascript">
261
262 google.charts.load('current', {packages: ['timeline']});
263 google.charts.setOnLoadCallback(draw);
264
265 function draw()
266 {
267 $(document).keydown(function(e)
268 {
269 var offset = 0;
270 if (e.which == 38) offset = 7*24*3600*1000;
271 if (e.which == 40) offset = -7*24*3600*1000;
272 if (e.which == 39) offset = 1*24*3600*1000;
273 if (e.which == 37) offset = -1*24*3600*1000;
274 if (!offset)
275 return;
276
277 var d = $("#datepicker").datepicker("getDate");//.getTime();
278
279 var c = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())+offset);
280
281 window.location = "?d="+c.toISOString().split("T")[0];
282
283 //$("#datepicker").datepicker("setDate", date);
284 //$("#datepicker").trigger("select");
285
286 return false;
287 });
288
289 var dp = $("#datepicker").datepicker({
290 autoSize: true,
291 dateFormat: "yy-mm-dd",
292 selectOtherMonths: true,
293 /*showButtonPanel: true,*/
294 showWeek: true,
295 defaultDate: "$date",
296 inline: true,
297 changeMonth: true,
298 changeYear: true,
299 minDate: new Date(2011, 10, 15),
300 onSelect: function(date) { window.location = "?d="+date; }
301
302});
303
304 var data = new google.visualization.DataTable();
305 data.addColumn({ type: 'string', id: 'Role' });
306 data.addColumn({ type: 'string', id: 'Name' });
307 data.addColumn({ type: 'date', id: 'Start' });
308 data.addColumn({ type: 'date', id: 'End' });
309 data.addRows([$data]);
310
311 //var dateFormatter = new google.visualization.DateFormat({pattern: 'dd/MM/yyyy HH:mm'});
312 //dateFormatter.format(data, 2);
313 //dateFormatter.format(data, 3);
314
315 var options = {
316 hAxis: {format:'HH:mm', minorGridlines: {count:1}},
317 avoidOverlappingGridLines: false,
318 timeline: { singleColor: $color },
319 };
320
321 var chart = new google.visualization.Timeline(document.getElementById('chart_div'));
322 chart.draw(data, options);
323 //document.getElementById('print_chart').innerHTML = '<a href="' + chart.getImageURI() + '">Print</a>';
324 console.log("Google.chart.ready");
325 }
326</script>
327<style>
328dl {
329 //border: 3px double #ccc;
330 padding: 0.5em;
331 }
332 dt {
333 float: left;
334 clear: left;
335 width: 130px;
336 text-align: right;
337 font-weight: bold;
338 color: green;
339 }
340 dt:after {
341 content: ":";
342 }
343 dd {
344 margin: 0 0 0 140px;
345 padding: 0 0 1em 0;
346 }
347 code {
348 padding: 0 3px;
349 background-color: #eee; /* support: IE8 */;
350 background-color: rgba( 0, 0, 0, .1 );
351 border-radius: 3px;
352}
353.diff td{
354 vertical-align : top;
355 /*white-space : pre;
356 white-space : pre-wrap;*/
357 font-family : monospace;
358}
359.diff span{
360 margin-right: 2em;
361}
362
363/* .diff span:first-child{
364 margin-top:0;
365 }*/
366
367 .diffDeleted span{
368 border:1px solid rgb(255,192,192);
369 background:rgb(255,224,224);
370 } <
371
372 .diffInserted span{
373 border:1px solid rgb(192,255,192);
374 background:rgb(224,255,224);
375 }
376
377
378</style>
379
380<body>
381
382<p style="font-weight: bold;font-size:130%;">Date: <input type="text" id="datepicker" style="font-weight:normal;font-size:100%;" value="$date"></p>
383
384<div style='position:relative'>
385<!--<H3>$date</H3>-->
386<div id='chart_div' style="height:220px;width:99%;margin-left:0.5%;">Preparing chart... please stand by.</div>
387<div id='print_chart' style='position:absolute;top:10px;right:40px;'></div>
388</div>
389
390<hr>
391
392<input type="button" onclick="$('#spoiler0').toggle(300);" value="Legend"/>
393<div id="spoiler0" style="display:none">
394 <dl>
395 <dt>AutoSchedule</dt>
396 <dd>(if available) The schedule from the AutoSchedule table as planned by the autoscheduler (<i>makeschedule</I>).</dd>
397 <dt>Schedule</dt>
398 <dd>The schedule as available in the Schedule table after the night.</dd>
399 <dt>RunInfo</dt>
400 <dd>The schedule as executed, taken from the RunInfo table. Only data runs are counted.</dd>
401 <dt>QLA</dt>
402 <dd>This row is only displayed if different from RunInfo. It shows all runs
403 which have been processed by the QLA already.
404 </dl>
405Note that the two schedules will never perfectly coincide with the runs taken
406because finite Measurements scheduled together with a source in one observation
407are counted to be part of the source. Also automatic calibration
408runs are obviously not accounted for in the schedule.
409
410<hr>
411
412<h3>Keyboard interaction</h3>
413
414<li><code>LEFT</code>: Move to the previous day.</li>
415<li><code>RIGHT</code>: Move to the next day.</li>
416<li><code>UP</code>: Move to the previous week.</li>
417<li><code>DOWN</code>: Move the next week.</li>
418
419<p>While the datepicker is open, the following key commands are available:</p>
420<ul>
421<li><code>PAGE UP</code>: Move to the previous month.</li>
422<li><code>PAGE DOWN</code>: Move to the next month.</li>
423<li><code>CTRL</code> + <code>PAGE UP</code>: Move to the previous year.</li>
424<li><code>CTRL</code> + <code>PAGE DOWN</code>: Move to the next year.</li>
425<li><code>CTRL</code> + <code>HOME</code>: Open the datepicker if closed.</li>
426<li><code>CTRL</code>/<code>COMMAND</code> + <code>HOME</code>: Move to the current month.</li>
427<li><code>CTRL</code>/<code>COMMAND</code> + <code>LEFT</code>: Move to the previous day.</li>
428<li><code>CTRL</code>/<code>COMMAND</code> + <code>RIGHT</code>: Move to the next day.</li>
429<li><code>CTRL</code>/<code>COMMAND</code> + <code>UP</code>: Move to the previous week.</li>
430<li><code>CTRL</code>/<code>COMMAND</code> + <code>DOWN</code>: Move the next week.</li>
431<li><code>ENTER</code>: Select the focused date.</li>
432<li><code>CTRL</code>/<code>COMMAND</code> + <code>END</code>: Close the datepicker and erase the date.</li>
433<li><code>ESCAPE</code>: Close the datepicker without selection.</li>
434</ul>
435
436</div>
437
438<hr>
439
440<input type="button" onclick="$('#spoiler1').show(300);$('#spoiler2').show(300);$('#spoiler3').show(300);$('#spoiler4').show(300);" value="Show all tables"/>
441<input type="button" onclick="$('#spoiler1').hide(300);$('#spoiler2').hide(300);$('#spoiler3').hide(300);$('#spoiler4').hide(300);" value="Hide all tables"/>
442
443<hr>
444
445<input type="button" onclick="$('#spoiler1').toggle(300);" value="Planned Schedule"/>
446<div id="spoiler1" style="display:none">
447$html0a
448<hr style='width:95%'>
449$html0b
450</div>
451
452<hr>
453
454<input type="button" onclick="$('#spoiler2').toggle(300);" value="Scheduled observations"/>
455<div id="spoiler2" style="display:none">
456$html1a
457<hr style='width:95%'>
458$html1b
459</div>
460
461<hr>
462
463<input type="button" onclick="$('#spoiler3').toggle(300);" value="Runs taken"/>
464<div id="spoiler3" style="display:none">
465$html2
466</div>
467
468<hr>
469
470<input type="button" onclick="$('#spoiler4').toggle(300);" value="Runs analysed"/>
471<div id="spoiler4" style="display:none">
472$html2
473</div>
474
475<hr>
476
477<input type="button" onclick="$('#spoiler5').toggle(300);" value="Diff taken/analysed"/>
478<div id="spoiler5" style="display:none">
479$diff
480</div>
481
482<hr>
483
484<input type="button" onclick="$('#spoiler6').toggle(300);" value="SQL query (Auto)Schedule"/>
485<div id="spoiler6" style="display:none">
486<pre>
487$querycol0
488<hr style='width:95%'>
489$querycol1
490</pre>
491</div>
492
493<hr>
494
495<input type="button" onclick="$('#spoiler7').toggle(300);" value="SQL query RunInfo"/>
496<div id="spoiler7" style="display:none">
497<pre>
498$querycol2
499<hr style='width:95%'>
500$querycol3
501</pre>
502</div>
503
504<hr>
505
506</body>
507</html>
508EOT;
509
510?>
Note: See TracBrowser for help on using the repository browser.