source: trunk/Mars/datacenter/db/mcinfo.php@ 9872

Last change on this file since 9872 was 9617, checked in by Daniela Dorner, 14 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 12.5 KB
Line 
1<?php
2{
3 function CreateQuery($_GET, $alias, $checkwhere, $checkgroup, $checkstatusgroup, $timelimits)
4 {
5 $fromtable="CeresInfo";
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 SQL_CALC_FOUND_ROWS ";
18
19 if ($groups>0 || $statusgroups>0)
20 {
21 if ($groups>0)
22 {
23 foreach ($checkgroup as $key => $element)
24 if ($element==-1)
25 $query0 .= $key . " as '" . $alias[$key] . "' " . ", ";
26 /*
27 //--------------------------------------------------
28 //$query0 .= " TIMEDIFF(fRunStop, fRunStart), ";
29 // Only available in MySQL>4.1.1
30 $query0 .= "SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0, " .
31 "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60, " .
32 "TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)))/3600 as '" . $alias["SUM(fRunTime)/3600"] . "', ";
33 //--------------------------------------------------
34 $query0 .= " SUM(fNumEvents) as '" . $alias["SUM(fNumEvents)"] . "', ";
35 $query0 .= " Min(fZenithDistance) as '" . $alias["Min(fZenithDistance)"] . "', ";
36 $query0 .= " Max(fZenithDistance) as '" . $alias["Max(fZenithDistance)"] . "', ";
37 */
38 }
39
40 if ($statusgroups>0)
41 {
42 foreach ($checkstatusgroup as $key => $element)
43 if ($element==-1)
44 $query0 .= " IF( ISNULL(" . $key . "Status.fStartTime) "
45 ." AND ISNULL(" . $key . "Status.fStopTime) "
46 ." AND ISNULL(" . $key . "Status.fReturnCode), 'not done', "
47 ." IF (NOT ISNULL(" . $key . "Status.fStartTime) "
48 ." AND DATE_SUB(Now(),INTERVAL " . $timelimits[$key] . " HOUR) > " . $key . "Status.fStartTime "
49 ." AND ISNULL(" . $key . "Status.fStopTime) "
50 ." AND ISNULL(" . $key . "Status.fReturnCode), 'crashed', "
51 ." IF (NOT ISNULL(" . $key . "Status.fStartTime) "
52 ." AND DATE_SUB(Now(),INTERVAL " . $timelimits[$key] . " HOUR) < " . $key . "Status.fStartTime "
53 ." AND ISNULL(" . $key . "Status.fStopTime) "
54 ." AND ISNULL(" . $key . "Status.fReturnCode), 'running', "
55 ." IF (NOT ISNULL(" . $key . "Status.fStartTime) "
56 ." AND NOT ISNULL(" . $key . "Status.fStopTime) "
57 ." AND ISNULL(" . $key . "Status.fReturnCode), 'done', 'failed' "
58 ." )))) AS '". $key . "', ";
59 }
60 $query0 .= " COUNT(*) as '# Files' ";
61 $query0 .= ", SUM(fNumEvents) as '" . $alias["SUM(fNumEvents)"] . "' ";
62 }
63 else
64 {
65 $query0 .= " CONCAT(LPAD(" . $fromtable . ".fRunNumber,8, ' '),'.', ";
66 $query0 .= " RIGHT(LPAD(" . $fromtable . ".fFileNumber, 6, 0), 3)) as 'Run.File' ";
67
68 if (empty($_GET["fSendTxt"]) && !empty($_GET["fLinks"]))
69 {
70 $query0 .= ", CONCAT('<A&ws;HREF=\"queryrbk.php?fNight=', date_format(adddate(fRunStart, interval +13 HOUR), '%Y-%m-%d') , '&amp;fDate=2\">rbk</A>') ";
71 $query0 .= " as 'Links'";
72 }
73
74 foreach ($_GET as $key => $element)
75 if ($_GET[$key]=="On" && $key!="fTest" && $key!="fLinks")
76 if (!ereg("^f", $key))
77 {
78
79 $query0 .= ", IF (ISNULL(" . $key . "Status.fStartTime) AND ";
80 $query0 .= " ISNULL(" . $key . "Status.fStopTime) AND ";
81 $query0 .= " ISNULL(" . $key . "Status.fReturnCode), 'not done', ";
82 $query0 .= " IF (NOT ISNULL(" . $key . "Status.fStartTime) ";
83 $query0 .= " AND ISNULL(" . $key . "Status.fStopTime) ";
84 $query0 .= " AND ISNULL(" . $key . "Status.fReturnCode), ";
85 $query0 .= " CONCAT('running (since ', " . $key . "Status.fStartTime, ')'), ";
86 $query0 .= " IF (NOT ISNULL(" . $key . "Status.fReturnCode), ";
87 $query0 .= " CONCAT('failed (', " . $key . "Status.fReturnCode, ', ', " . $key . "Status.fStopTime, ')'), ";
88 $query0 .= " CONCAT('done (', " . $key . "Status.fStopTime, ";
89 $query0 .= " ', ', TIMEDIFF(" . $key . "Status.fStopTime, " . $key . "Status.fStartTime), ')')))) ";
90
91 //$query0 .= ", CONCAT( 'halo', " . $key . "Status.fStartTime)";
92 $query0 .= " AS '" . $alias[$key] . "' ";
93 //echo "ereg: " . $key . "<br>";
94 }
95 else
96 if (empty($checkwhere[$key]) || $checkwhere[$key]==0)
97 $query0 .= ", " . $key . " as '" . $alias[$key] . "' ";
98 }
99
100 $query0 .= " FROM " . $fromtable;
101
102 $query0 .= " LEFT JOIN CorsikaInfo USING(fRunNumber,fFileNumber) ";
103
104 foreach ($_GET as $key => $element)
105 if ($_GET[$key]=="On" || $_GET[$key]!=0)
106 if (strpos($query0, GetJoin($fromtable,$key))==FALSE)
107 $query0 .= GetJoin($fromtable,$key);
108
109 foreach ($checkwhere as $key => $element)
110 {
111 if (empty($element) || $element<=0)
112 continue;
113
114 if (strpos($query0, " WHERE ")==FALSE)
115 $query0 .= " WHERE ";
116 else
117 if ($element!=-1)
118 if (strrpos($query0, " AND ")!=strlen($query0)-5)// this if clause doesn't work
119 $query0 .= " AND ";
120
121 if ($element!=-1)
122 $query0 .= GetCheck($fromtable, $key) . "=" . $element;
123 }
124
125 if (strpos($query0, " WHERE ")==FALSE)
126 $query0 .= " WHERE";
127 else
128 $query0 .= " AND ";
129
130 $query0 .= StatusQuery("Corsika", $timelimits);
131 $query0 .= StatusQuery("Ceres", $timelimits);
132 $query0 .= StatusQuery("SequenceFile", $timelimits);
133 $query0 .= StatusQuery("Callisto", $timelimits);
134 $query0 .= StatusQuery("Star", $timelimits);
135
136 if ((!empty($_GET["fRunMin"]) || $_GET["fRunMin"]=="0") && !empty($_GET["fRunMax"]))
137 $query0 .= " " . $fromtable . ".fRunNumber BETWEEN " . $_GET["fRunMin"] . " AND " . $_GET["fRunMax"] . " ";
138
139
140 //remove WHERE or AND in the end of the query
141 $query0=preg_replace("/ WHERE\s+$/", " ", $query0);
142 $query0=preg_replace("/ AND\s+$/", " ", $query0);
143
144 if ($groups>0)
145 {
146 $query0 .= " GROUP BY ";
147 $num = $groups;
148 foreach ($checkgroup as $key => $element)
149 if ($element==-1)
150 {
151 $query0 .= GetCheck($fromtable,$key);
152 if ($num-->1)
153 $query0 .= ", ";
154 }
155 }
156
157 if ($statusgroups>0)
158 {
159 if (strpos($query0, " GROUP BY ")==FALSE)
160 $query0 .= " GROUP BY ";
161 else
162 $query0 .= ", ";
163 $num = $statusgroups;
164 foreach ($checkstatusgroup as $key => $element)
165 if ($element==-1)
166 {
167 $query0 .= $alias[$key];
168 if ($num-->1)
169 $query0 .= ", ";
170 }
171 }
172
173 if (!empty($_GET["fSortBy"]))
174 {
175 $val=substr($_GET["fSortBy"], 0, -1);
176 $query0 .= " ORDER BY " . GetTable($fromtable, $val) . " ";
177 if (substr($_GET["fSortBy"], -1)=="-")
178 $query0 .= "DESC";
179 }
180 else
181 $query0 .=" ORDER BY " . $fromtable . ".fRunNumber ASC";
182
183 if (empty($_GET["fNumStart"]))
184 $_GET["fNumStart"]=0;
185
186 if (empty($_GET["fSendTxt"]))
187 $query0 .= " LIMIT " . $_GET["fNumStart"] . ", " . $_GET["fNumResults"];
188
189 return $query0;
190 }
191
192 function InitGet($_GET)
193 {
194 // Find out whether it is the first call to the php script
195 $first = empty($_GET["fRunMin"]) && empty($_GET["fRunMax"]);
196
197 InitMCRunInfo($_GET, $first);
198 }
199
200 function PrintForm($_GET, $host, $user, $pw, $db)
201 {
202 printf("<center>\n");
203 printf("<form action=\"mcinfo.php\" METHOD=\"GET\">\n");
204
205 printf("<input id='sh' type='hidden' name='fShowHide' value='");
206 if (!empty($_GET["fShowHide"]))
207 printf("%s", $_GET["fShowHide"]);
208 else
209 printf("1000000010");
210 printf("'>\n");
211 printf("<img id='allbutton' src='minus.png' alt='-' onclick=\"showhide('all')\"> <b>Menu</b>&nbsp;&nbsp;&nbsp;&nbsp;\n");
212 printf("&nbsp;&nbsp;<img id='infobutton' src='plus.png' alt='+' onClick=\"showhide('info');showhide('info2')\"> MCRunInfo \n");
213 printf("&nbsp;&nbsp;<img id='statbutton' src='plus.png' alt='+' onClick=\"showhide('stat')\"> StatusInfo \n");
214 printf("&nbsp;&nbsp;<img id='rangesbutton' src='plus.png' alt='+' onClick=\"showhide('ranges')\"> Ranges \n");
215
216 printf(" <div id='all' style='display:block'>");
217 PrintMCRunInfoMenu($host,$user,$pw,$db);
218 PrintMCRunStatMenu();
219 printf(" <div id='ranges' style='display:none'>");
220 printf("<p>");
221 PrintMCRunRangeMenu($host,$user,$pw,$db);
222 printf("<p>");
223 printf("</div>");
224 printf("</div>");
225 printf(" <P>\n");
226
227 ini_set("mysql.trace_mode", "Off");
228 ini_set("display_errors", "Off");
229
230 PrintNumResPullDown();
231 PrintButtons("mcinfo.php");
232
233 printf("</form>\n");
234 printf("</center>\n");
235 printf("</td>\n");
236 printf("</tr>\n");
237 printf("<tr class='Block'>\n");
238 printf("<td>\n");
239 }
240
241 function PrintPage($html, $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup, $timelimits)
242 {
243 $db_id = mysql_connect($host, $user, $pw);
244 if ($db_id==FALSE)
245 {
246 printf("mysql_connect returned the following error: %s\n", mysql_error());
247 die("");
248 }
249 mysql_select_db($db);
250 mysql_query("SET BIG_TABLES=1"); // necessary for mySQL <= 4
251
252 $query0 = CreateQuery($_GET, $alias, $checkwhere, $checkgroup, $checkstatusgroup, $timelimits);
253
254 $result0 = mysql_query($query0, $db_id);
255 $result1 = mysql_query("SELECT FOUND_ROWS()", $db_id);
256
257 if ($result0)
258 {
259 if ($html=="1" || $html=="2")
260 PrintMagicTable($result0, $alias, $rightalign, "", "", "", $result1);
261 else
262 PrintText($result0);
263
264 mysql_free_result($result0);
265 mysql_free_result($result1);
266 }
267 mysql_close($db_id);
268
269 if ($html=="1")
270 PrintSubmittedQuery($query0, $db, "");
271 }
272
273 include ("include.php");
274 include ("db2.php");
275 include ("menu.php");
276 include ("mcdefs.php");
277
278 ini_set("display_errors", "On");
279 ini_set("mysql.trace_mode", "On");
280
281 if (!empty($_GET["fSendTxt"]))
282 {
283 header("Content-type: application/octet");
284 header("Content-Disposition: attachment; filename=query-result.txt");
285
286 PrintPage("0", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup, $timelimits);
287 }
288 else
289 {
290 if (empty($_GET["fPrintTable"]))
291 echo (file_get_contents("index-header-fact.html"));
292
293 $environment = sizeof($_GET);
294
295 InitGet($_GET);
296 if (empty($_GET["fPrintTable"]))
297 PrintForm($_GET, $host, $user, $pw, $db);
298
299 if ($environment==0)
300 printf("No query submitted yet.<BR>");
301 else
302 {
303 if (empty($_GET["fPrintTable"]))
304 PrintPage("1", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup, $timelimits);
305 else
306 PrintPage("2", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup, $timelimits);
307 }
308
309 if (empty($_GET["fPrintTable"]))
310 echo (file_get_contents("index-footer.html"));
311 }
312
313 ini_set("display_errors", "Off");
314 ini_set("mysql.trace_mode", "Off");
315}
316?>
Note: See TracBrowser for help on using the repository browser.