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

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