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

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