source: trunk/MagicSoft/Mars/datacenter/db/runinfo-aio.php@ 9437

Last change on this file since 9437 was 9249, checked in by Daniela Dorner, 16 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 13.8 KB
Line 
1<?php
2{
3 function CreateQuery($_GET, $alias, $checkwhere, $checkgroup, $checkstatusgroup, $needs)
4 {
5 $fromtable="RunData";
6
7 $groups = 0;
8 foreach ($checkgroup as $element)
9 if ($element==-1)
10 $groups++;
11
12 $statusgroups = 0;
13 foreach ($checkstatusgroup as $element)
14 if ($element==-1)
15 $statusgroups++;
16
17 $query0 = "SELECT ";
18
19 if ($groups>0 || ((!empty($_GET["fGroupByDate"])) && !$_GET["fGroupByDate"]==0) || $statusgroups>0)
20 {
21 foreach ($checkgroup as $key => $element)
22 if ($element==-1)
23 $query0 .= $key . " as '" . $alias[$key] . "' " . ", ";
24
25 if ((!empty($_GET["fGroupByDate"])) && !$_GET["fGroupByDate"]==0)
26 {
27 $query0 .= " DATE_FORMAT(ADDDATE(fRunStart,Interval 12 hour), '";
28 switch($_GET["fGroupByDate"])
29 {
30 case "Year":
31 $query0 .= "%Y ";
32 break;
33 case "Month":
34 $query0 .= "%Y-%m ";
35 break;
36 case "Night":
37 $query0 .= "%Y-%m-%d ";
38 break;
39 }
40 $query0 .= "') AS GroupByDate, ";
41 }
42
43 //--------------------------------------------------
44 //$query0 .= " TIMEDIFF(fRunStop, fRunStart), ";
45 // Only available in MySQL>4.1.1
46 $query0 .= "SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0, " .
47 "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60, " .
48 "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)))/3600 as '" . $alias["SUM(fRunTime)/3600"] . "', ";
49 //--------------------------------------------------
50 $query0 .= " SUM(fNumEvents) as '" . $alias["SUM(fNumEvents)"] . "', ";
51 $query0 .= " Min(fZenithDistance) as '" . $alias["Min(fZenithDistance)"] . "', ";
52 $query0 .= " Max(fZenithDistance) as '" . $alias["Max(fZenithDistance)"] . "', ";
53 $query0 .= " COUNT(*) as '# Runs' ";
54
55 if ($statusgroups>0)
56 {
57 foreach ($checkstatusgroup as $key => $element)
58 if ($element==-1)
59 $query0 .= ", (if(IsNull(" . $key . "), if(isnull(fStartTime), 'not done', if(isnull(fFailedTime),if(isnull(" . $needs[$key] . "),'not done',if(date_sub(Now(),interval 12 hour) < fStartTime,'running','crashed')),if(isnull(" . $needs[$key] . "),'not done','failed'))) ,if(" . $key . "='1970-01-01 00:00:00','dont do','done'))) as '" . $alias[$key] . "' ";
60// $query0 .= " (if(IsNull(" . $key . "), 'not done' ,if(" . $key . "='1970-01-01 00:00:00','dont do','done'))) as '" . $alias[$key] . "', ";
61
62 $query0 .= ", COUNT(*) AS '# Runs'";
63
64 }
65 }
66 else
67 {
68 //$query0 .= " RunData.fRunNumber as 'Run#' ";
69 //$query0 .= " Concat(RunData.fTelescopeNumber,':', RunData.fRunNumber,':', RunData.fFileNumber) as 'Run' ";
70 $query0 .= " Concat('M', RunData.fTelescopeNumber,':', LPAD(RunData.fRunNumber,8, ' '),'/', LPAD(RunData.fFileNumber, 3, ' ')) as 'Tel:Run/File' ";
71
72 if (empty($_GET["fSendTxt"]) && !empty($_GET["fLinks"]))
73 {
74 $query0 .= ", CONCAT('<A&ws;HREF=\"queryrbk.php?fNight=', date_format(adddate(fRunStart, interval +13 HOUR), '%Y-%m-%d') , '&amp;fDate=2\">rbk</A>') ";
75 $query0 .= " as 'Links'";
76 }
77
78 foreach ($_GET as $key => $element)
79 if ($key!="fLinks" && $_GET[$key]=="On" && $key!="fTest")
80 if (empty($checkwhere[$key]) || $checkwhere[$key]==0)
81 $query0 .= ", " . $key . " as '" . $alias[$key] . "' ";
82 }
83
84 $query0 .= " FROM RunData ";
85
86 $query0 .= " LEFT JOIN RunProcessStatus USING(fRunNumber,fTelescopeNumber,fFileNumber) ";
87
88 foreach ($_GET as $key => $element)
89 if (($_GET[$key]=="On" || $groups>0))// && !empty($joins[$key]))
90 $query0 .= GetJoin($fromtable,$key);
91
92 if ($_GET["fTest"]!="On")
93 {
94 if ($_GET["fSourceName"]!="On")
95 $query0 .= " LEFT JOIN Source USING(fSourceKEY) ";
96 $query0 .= " WHERE fTest='no'";
97 }
98
99 foreach ($checkwhere as $key => $element)
100 {
101 if (empty($element) || $element<=0)
102 continue;
103
104 if (strpos($query0, " WHERE ")==FALSE)
105 $query0 .= " WHERE ";
106 else
107 if ($element!=-1)
108 if (strrpos($query0, " AND ")!=strlen($query0)-5)// this if clause doesn't work
109 $query0 .= " AND ";
110
111 if ($element!=-1)
112 $query0 .= GetCheck($fromtable, $key) . "=" . $element;
113 }
114
115 if (strpos($query0, " WHERE ")==FALSE)
116 $query0 .= " WHERE ";
117 else
118 $query0 .= " AND ";
119
120 $query0 .= StatusQuery("fCCFileAvail", $needs);
121 $query0 .= StatusQuery("fCaCoFileAvail", $needs);
122 $query0 .= StatusQuery("fCaCoFileFound", $needs);
123 $query0 .= StatusQuery("fRawFileAvail", $needs);
124 $query0 .= StatusQuery("fDataCheckDone", $needs);
125 $query0 .= StatusQuery("fTimingCorrection", $needs);
126
127 if ((!empty($_GET["fRunMin"]) || $_GET["fRunMin"]=="0") && !empty($_GET["fRunMax"]))
128 $query0 .= "RunData.fRunNumber BETWEEN " . $_GET["fRunMin"] . " AND " . $_GET["fRunMax"] . " ";
129
130 if ((!empty($_GET["fZDMin"]) || $_GET["fZDMin"]==0) && !empty($_GET["fZDMax"]))
131 $query0 .= "AND fZenithDistance BETWEEN " . $_GET["fZDMin"] . " AND " . $_GET["fZDMax"] . " ";
132
133/*
134 if (!empty($_GET["fDate"]))
135 $query0 .= " AND fRunStart REGEXP \"^" . $_GET["fDate"] . "\" ";
136*/
137
138 if (!empty($_GET["fSourceN"]))
139 $query0 .= " AND fSourceName REGEXP \"^" . $_GET["fSourceN"] . "\" ";
140
141 if (!empty($_GET["fStartDate"]))
142 {
143 if (strpos(strrev($query0), " DNA ")!=0)
144 $query0 .= " AND ";
145
146 $startdate=substr($_GET["fStartDate"], 0, 10);
147 if ($startdate=="0000-00-00")
148 $query0 .=" fRunStart >= '" . $startdate . " 00:00:00' ";
149 else
150 $query0 .= " fRunStart >= ADDDATE('" . $startdate . " 13:00:00', INTERVAL -1 DAY) ";
151 }
152
153 if (!empty($_GET["fStopDate"]))
154 {
155 if (strpos(strrev($query0), " DNA ")!=0)
156 $query0 .= " AND ";
157
158 $stopdate=substr($_GET["fStopDate"], 0, 10);
159 $query0 .= " fRunStart < '" . $stopdate . " 13:00:00' ";
160 }
161
162 if (!empty($_GET["fSequenceNo"]) || $_GET["fSequenceNo"]=="0")
163 {
164 if (strpos(strrev($query0), " DNA ")!=0)
165 $query0 .= " AND ";
166
167 $query0 .= " fSequenceFirst = '" . $_GET["fSequenceNo"] . "' ";
168 }
169
170
171 if ($groups>0)
172 {
173 $query0 .= " GROUP BY ";
174 $num = $groups;
175 foreach ($checkgroup as $key => $element)
176 if ($element==-1)
177 {
178 $query0 .= GetCheck($fromtable,$key);
179 if ($num-->1)
180 $query0 .= ", ";
181 }
182 }
183
184 if ((!empty($_GET["fGroupByDate"])) && !$_GET["fGroupByDate"]==0)
185 if (strpos($query0, " GROUP BY ")==FALSE)
186 $query0 .= " GROUP BY GroupByDate ";
187 else
188 $query0 .= ", GroupByDate ";
189
190 if ($statusgroups>0)
191 {
192 if (strpos($query0, " GROUP BY ")==FALSE)
193 $query0 .= " GROUP BY ";
194 else
195 $query0 .= ", ";
196 $num = $statusgroups;
197 foreach ($checkstatusgroup as $key => $element)
198 if ($element==-1)
199 {
200 $query0 .= $alias[$key];
201 if ($num-->1)
202 $query0 .= ", ";
203 }
204 }
205
206 if (!empty($_GET["fSortBy"]))
207 {
208 $val=substr($_GET["fSortBy"], 0, -1);
209 $query0 .= " ORDER BY " . GetTable($fromtable, $val) . " ";
210 if (substr($_GET["fSortBy"], -1)=="-")
211 $query0 .= "DESC";
212 }
213 else
214 $query0 .=" ORDER BY RunData.fRunNumber ASC";
215
216 if (empty($_GET["fNumStart"]))
217 $_GET["fNumStart"]=0;
218
219 if (empty($_GET["fSendTxt"]))
220 $query0 .= " LIMIT " . $_GET["fNumStart"] . ", " . $_GET["fNumResults"];
221
222 return $query0;
223 }
224
225 function InitGet($_GET)
226 {
227 // Find out whether it is the first call to the php script
228 $first = empty($_GET["fRunMin"]) && empty($_GET["fRunMax"]);
229
230 if (empty($_GET["fNumResults"]))
231 $_GET["fNumResults"]="20";
232
233 if (empty($_GET["fTest"]))
234 $_GET["fTest"]=$first?"On":"";
235
236 if (empty($_GET["fNumEvents"]))
237 $_GET["fNumEvents"]=$first?"On":"";
238
239 if (empty($_GET["fRunStart"]))
240 $_GET["fRunStart"]=$first?"On":"";
241
242 if (empty($_GET["fZenithDistance"]))
243 $_GET["fZenithDistance"]=$first?"On":"";
244
245 if (empty($_GET["fRunTypeName"]))
246 $_GET["fRunTypeName"]=$first?"On":"";
247
248 if (empty($_GET["fSourceName"]))
249 $_GET["fSourceName"]=$first?"On":"";
250
251 if (empty($_GET["fMeanTriggerRate"]))
252 $_GET["fMeanTriggerRate"]=$first?"On":"";
253
254 if (empty($_GET["fExcludedFDAName"]))
255 $_GET["fExcludedFDAName"]=$first?"On":"";
256
257 if (empty($_GET["fSequenceNo"]))
258 $_GET["fSequenceNo"]="";
259
260 InitRunInfo($_GET, $first);
261 }
262
263 function PrintForm($_GET, $host, $user, $pw, $db)
264 {
265 printf("<center>\n");
266 printf("<form action=\"runinfo-aio.php\" METHOD=\"GET\">\n");
267
268 printf("<input id='sh' type='hidden' name='fShowHide' value='");
269 if (!empty($_GET["fShowHide"]))
270 printf("%s", $_GET["fShowHide"]);
271 else
272 printf("1000000010");
273 printf("'>\n");
274 printf("<img id='allbutton' src='minus.png' alt='-' onclick=\"showhide('all')\"> <b>Menu</b>&nbsp;&nbsp;&nbsp;&nbsp;\n");
275 printf("&nbsp;&nbsp;<img id='infobutton' src='plus.png' alt='+' onClick=\"showhide('info');showhide('info2')\"> RunInfo \n");
276 printf("&nbsp;&nbsp;<img id='statbutton' src='plus.png' alt='+' onClick=\"showhide('stat');showhide('fail')\"> StatusInfo \n");
277 printf("&nbsp;&nbsp;<img id='rangesbutton' src='plus.png' alt='+' onClick=\"showhide('ranges')\"> Ranges \n");
278
279 printf(" <div id='all' style='display:block'>");
280 PrintRunInfoMenu();
281 PrintRunInfo2Menu($host,$user,$pw,$db);
282 PrintRunStatMenu();
283 PrintFailMenu();
284 printf(" <div id='ranges' style='display:none'>");
285 PrintSequMenu($host,$user,$pw,$db);
286 PrintNightRangeMenu($host,$user,$pw,$db, "RunData");
287 printf("<p>");
288 PrintZdRangeMenu($host,$user,$pw,$db);
289 PrintRunRangeMenu($host,$user,$pw,$db);
290 printf(" <P>\n");
291 PrintSourceMenu($host,$user,$pw,$db);
292 PrintGroupByDateMenu();
293 printf("<p>");
294 printf("</div>");
295 printf("</div>");
296 printf(" <P>\n");
297
298 ini_set("mysql.trace_mode", "Off");
299 ini_set("display_errors", "Off");
300
301 PrintNumResPullDown();
302 PrintButtons("runinfo-aio.php");
303
304 printf("</form>\n");
305 printf("</center>\n");
306 printf("</td>\n");
307 printf("</tr>\n");
308 printf("<tr class='Block'>\n");
309 printf("<td>\n");
310 }
311
312 function PrintPage($html, $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup, $needs)
313 {
314 $db_id = mysql_connect($host, $user, $pw);
315 if ($db_id==FALSE)
316 {
317 printf("mysql_connect returned the following error: %s\n", mysql_error());
318 die("");
319 }
320 mysql_select_db($db);
321 mysql_query("SET BIG_TABLES=1"); // necessary for mySQL <= 4
322
323 $query0 = CreateQuery($_GET, $alias, $checkwhere, $checkgroup, $checkstatusgroup, $needs);
324
325 $result0 = mysql_query($query0, $db_id);
326
327 if ($result0)
328 {
329 if ($html=="1" || $html=="2")
330 PrintMagicTable($result0, $alias, $rightalign, "", "", "", "", $_GET);
331 else
332 PrintText($result0);
333
334 mysql_free_result($result0);
335 }
336 mysql_close($db_id);
337
338 PrintSubmittedQuery($query0, $html, $db, "");
339 }
340
341 include ("include.php");
342 include ("db.php");
343 include ("menu.php");
344 include ("magicdefs.php");
345
346 ini_set("display_errors", "On");
347 ini_set("mysql.trace_mode", "On");
348
349 if (!empty($_GET["fSendTxt"]))
350 {
351 header("Content-type: application/octet");
352 header("Content-Disposition: attachment; filename=query-result.txt");
353
354 PrintPage("0", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup, $needs);
355 }
356 else
357 {
358 if (empty($_GET["fPrintTable"]))
359 echo (file_get_contents("index-header.html"));
360
361 $environment = sizeof($_GET);
362
363 InitGet($_GET);
364 if (empty($_GET["fPrintTable"]))
365 PrintForm($_GET, $host, $user, $pw, $db);
366
367 if ($environment==0)
368 printf("No query submitted yet.<BR>");
369 else
370 {
371 if (empty($_GET["fPrintTable"]))
372 PrintPage("1", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup, $needs);
373 else
374 PrintPage("2", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup, $needs);
375 }
376
377 if (empty($_GET["fPrintTable"]))
378 echo (file_get_contents("index-footer.html"));
379 }
380
381 ini_set("display_errors", "Off");
382 ini_set("mysql.trace_mode", "Off");
383}
384?>
Note: See TracBrowser for help on using the repository browser.