source: trunk/MagicSoft/Mars/datacenter/db/mcinfo.php@ 9549

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