source: trunk/Mars/datacenter/db/fact_runinfo.php@ 13408

Last change on this file since 13408 was 13363, checked in by Daniela Dorner, 13 years ago
fixed column names
  • Property svn:executable set to *
File size: 14.4 KB
Line 
1<?php
2{
3 function CreateQuery($_GET, $alias, $checkwhere, $checkgroup, $checkstatusgroup)
4 {
5 $fromtable="RunInfo";
6
7 $groups = 0;
8 foreach ($checkgroup as $element)
9 if ($element==-1)
10 $groups++;
11
12 $query0 = "SELECT SQL_CALC_FOUND_ROWS ";
13
14 if ($groups>0)
15 {
16 foreach ($checkgroup as $key => $element)
17 if ($element==-1)
18 $query0 .= $key . " as '" . $alias[$key] . "' " . ", ";
19 //don't put a '#' at the beginning of the alias, else SortBy won't work anymore
20 $query0 .= " COUNT(*) as 'NumRuns', ";
21 //$query0 .= " Round(Sum(TimeDiff(fRunStop,fRunStart))/60./60.,2) as 'Time [h]' ";
22 $query0 .= " Sec_to_time(Sum(Time_to_sec(TimeDiff(fRunStop,fRunStart)))) as 'Time<br>[hh:mm:ss]' ";
23 }
24 else
25 {
26 $query0 .= " Concat(fNight,'_', LPAD(fRunID, 3, 0)) as 'Run' ";
27
28 foreach ($_GET as $key => $element)
29 if ($_GET[$key]=="On")
30 if ($key!="fComments")
31 if (empty($checkwhere[$key]) || $checkwhere[$key]==0)
32 $query0 .= ", " . $key . " as '" . $alias[$key] . "' ";
33
34 if (!empty($_GET["fComments"]))
35 {
36 //$query0 .= ", if (Count(fComment)>0, Concat('<a&ws;id=\'commentcell', fNight, fRunID, '\'&ws;href=\'run_comment.php?fNight=',fNight,'&fRunID=', fRunID, '\'&ws;onmouseover=\'get_comments(', fNight, ',', fRunID, ')\'>', Count(fComment), '</a>'), ' ') ";
37 $query0 .= ", CONCAT(if (Count(fComment)>0, Concat('<a&ws;id=\'commentcell', fNight, fRunID, '\'&ws;href=\'run_comment.php?fNight=',fNight,'&fRunID=', fRunID, '\'&ws;onmouseover=\'get_comments(', fNight, ',', fRunID, ')\'>', Count(fComment), '</a>')";
38 $query0 .= ", ' '), ' ', ";
39 $query0 .= "CONCAT('<a&ws;id=\'commentcell', fNight, fRunID, '\'&ws;href=\'../../logbook/calendar.php?calendar=1&year=', left(fNight,4), '&month=', left(right(fNight,4),2), '&day=', right(fNight,2), '&action=dayview\'>lbk</a>', '')) ";
40 //$query0 .= ", if (Count(fComment)>0, Concat('<a&ws;href=\'run_comment.php?fNight=',fNight,'&fRunID=', fRunID, '\'&ws;onmouseover=\'bla\'>', Count(fComment), '</a>'), ' ') ";
41 $query0 .= " as 'Comments'";
42 }
43
44 }
45
46 $query0 .= " FROM " . $fromtable;
47
48 foreach ($_GET as $key => $element)
49 if (($_GET[$key]=="On" || $groups>0))// && !empty($joins[$key]))
50 $query0 .= GetJoin($fromtable, $key);
51 $query0 .= " LEFT JOIN RunComments USING (fNight, fRunID) ";
52
53 foreach ($checkwhere as $key => $element)
54 {
55 if (empty($element) || $element<=0)
56 continue;
57
58 if (strpos($query0, " WHERE ")==FALSE)
59 $query0 .= " WHERE ";
60 else
61 if ($element!=-1)
62 if (strrpos($query0, " AND ")!=strlen($query0)-5)// this if clause doesn't work
63 $query0 .= " AND ";
64
65 if ($element!=-1)
66 $query0 .= GetCheck($fromtable, $key) . "=" . $element;
67 }
68
69 if (strpos($query0, " WHERE ")==FALSE)
70 $query0 .= " WHERE ";
71 else
72 $query0 .= " AND ";
73
74 if (!empty($_GET["fStartDate"]))
75 {
76 $startdate=substr($_GET["fStartDate"], 0, 10);
77// if ($startdate=="0000-00-00")
78 $query0 .=" fNight >= " . $startdate . " ";
79// else
80// $query0 .= " fNight >= " . $startdate . " - 1 ";
81 }
82
83 if (!empty($_GET["fStopDate"]))
84 {
85// if (strpos(strrev($query0), " DNA ")!=0)
86// $query0 .= " AND ";
87
88 $stopdate=substr($_GET["fStopDate"], 0, 10);
89 $query0 .= " AND fNight <= " . $stopdate . " ";
90 }
91
92 if ($groups>0)
93 {
94 $query0 .= " GROUP BY ";
95 $num = $groups;
96 foreach ($checkgroup as $key => $element)
97 if ($element==-1)
98 {
99 $query0 .= GetCheck($fromtable, $key);
100 if ($num-->1)
101 $query0 .= ", ";
102 }
103 }
104 else
105 $query0 .= " GROUP BY fNight, fRunID ";
106
107
108
109 if (!empty($_GET["fSortBy"]))
110 {
111 $query0 .= " ORDER BY " . substr($_GET["fSortBy"], 0, -1) . " ";
112 if (substr($_GET["fSortBy"], -1)=="-")
113 $query0 .= "DESC";
114 }
115 else
116 if ($groups==0)
117 $query0 .= " ORDER BY Run DESC";
118
119 if (empty($_GET["fNumStart"]))
120 $_GET["fNumStart"]=0;
121
122 if (empty($_GET["fSendTxt"]))
123 $query0 .= " LIMIT " . $_GET["fNumStart"] . ", " . $_GET["fNumResults"];
124
125 return $query0;
126 }
127
128 function InitGet($_GET)
129 {
130 // Find out whether it is the first call to the php script
131 $first = empty($_GET["fStartDate"]) && empty($_GET["fStopDate"]);
132
133 if (empty($_GET["fNumResults"]))
134 $_GET["fNumResults"]="200";
135
136 if (empty($_GET["fNumEvents"]))
137 $_GET["fNumEvents"]=$first?"Off":"";
138
139 if (empty($_GET["fComments"]))
140 $_GET["fComments"]=$first?"On":"";
141
142 if (empty($_GET["fNumPhysicsTrigger"]))
143 $_GET["fNumPhysicsTrigger"]=$first?"On":"";
144
145 if (empty($_GET["Round(fNumPhysicsTrigger/Time_to_sec(TimeDiff(fRunStop,fRunStart)),1)"]))
146 $_GET["Round(fNumPhysicsTrigger/Time_to_sec(TimeDiff(fRunStop,fRunStart)),1)"]=$first?"On":"";
147
148 if (empty($_GET["fNumPedestalTrigger"]))
149 $_GET["fNumPedestalTrigger"]=$first?"On":"";
150
151 if (empty($_GET["fNumELPTrigger"]))
152 $_GET["fNumELPTrigger"]=$first?"On":"";
153
154 if (empty($_GET["fNumTimeTrigger"]))
155 $_GET["fNumTimeTrigger"]=$first?"Off":"";
156
157 if (empty($_GET["fNumExt1Trigger"]))
158 $_GET["fNumExt1Trigger"]=$first?"Off":"";
159
160 if (empty($_GET["fNumExt2Trigger"]))
161 $_GET["fNumExt2Trigger"]=$first?"Off":"";
162
163 if (empty($_GET["fNumILPTrigger"]))
164 $_GET["fNumILPTrigger"]=$first?"Off":"";
165
166 if (empty($_GET["fNumOtherTrigger"]))
167 $_GET["fNumOtherTrigger"]=$first?"Off":"";
168
169 if (empty($_GET["TimeDiff(fRunStop,fRunStart)"]))
170 $_GET["TimeDiff(fRunStop,fRunStart)"]=$first?"On":"";
171
172 if (empty($_GET["fRunStart"]))
173 $_GET["fRunStart"]=$first?"On":"";
174
175 if (empty($_GET["fRunStop"]))
176 $_GET["fRunStop"]=$first?"Off":"";
177
178 if (empty($_GET["fROI"]))
179 $_GET["fROI"]=$first?"On":"";
180
181 if (empty($_GET["fROITimeMarker"]))
182 $_GET["fROITimeMarker"]=$first?"Off":"";
183
184 if (empty($_GET["fFitsFileErrors"]))
185 $_GET["fFitsFileErrors"]=$first?"Off":"";
186
187 if (empty($_GET["fCompileTime"]))
188 $_GET["fCompileTime"]=$first?"Off":"";
189
190 if (empty($_GET["fRevisionNumber"]))
191 $_GET["fRevisionNumber"]=$first?"Off":"";
192
193 if (empty($_GET["if(fHasDrsFile=0,\"N\",fDrsStep)"]))
194 $_GET["if(fHasDrsFile=0,\"N\",fDrsStep)"]=$first?"Off":"";
195
196 if (empty($_GET["fMd5sumRaw"]))
197 $_GET["fMd5sumRaw"]=$first?"Off":"";
198
199 if (empty($_GET["fMd5sumRawZip"]))
200 $_GET["fMd5sumRawZip"]=$first?"Off":"";
201
202 if (empty($_GET["fRunTypeName"]))
203 $_GET["fRunTypeName"]=$first?"On":"";
204
205 if (empty($_GET["fTriggerRateMedian"]))
206 $_GET["fTriggerRateMedian"]=$first?"Off":"";
207
208 if (empty($_GET["fThresholdMedian"]))
209 $_GET["fThresholdMedian"]=$first?"On":"";
210
211 if (empty($_GET["fBiasVoltageMedian"]))
212 $_GET["fBiasVoltageMedian"]=$first?"Off":"";
213
214 if (empty($_GET["fRightAscension"]))
215 $_GET["fRightAscension"]=$first?"Off":"";
216
217 if (empty($_GET["fDeclination"]))
218 $_GET["fDeclination"]=$first?"Off":"";
219
220 if (empty($_GET["fZenithDistanceMean"]))
221 $_GET["fZenithDistanceMean"]=$first?"On":"";
222
223 if (empty($_GET["fAzimuthMean"]))
224 $_GET["fAzimuthMean"]=$first?"On":"";
225
226 }
227
228 function PrintForm($_GET, $host, $user, $pw, $db)
229 {
230 printf("<center>\n");
231 printf("<form action=\"fact_runinfo.php\" METHOD=\"GET\">\n");
232
233 // pull down boxes
234
235 printf(" <table>\n");
236 printf(" <tr><td>\n");
237 PrintPullDown($host, $user, $pw, $db, "RunType", "fRunTypeName", "fRunTypeKEY", "RunType");
238 printf(" </td></tr></table>\n");
239 printf(" <p>\n");
240
241 printf(" <table>\n");
242 printf(" <tr>\n");
243
244 CheckBox("fComments", "comments");
245 CheckBox("fRunStart", "start time");
246 CheckBox("fRunStop", "stop time");
247 CheckBox("TimeDiff(fRunStop,fRunStart)", "duration");
248 CheckBox("fROI", "roi");
249 CheckBox("fNumEvents", "#Evts");
250
251 printf(" </tr>\n");
252 printf(" <tr>\n");
253
254 CheckBox("Round(fNumPhysicsTrigger/Time_to_sec(TimeDiff(fRunStop,fRunStart)),1)", "phys rate");
255 CheckBox("fNumPhysicsTrigger", "#Phys");
256 CheckBox("fNumPedestalTrigger", "#Ped");
257 CheckBox("fNumELPTrigger", "#ELP");
258 CheckBox("fNumTimeTrigger", "#Time");
259
260 printf(" </tr>\n");
261 printf(" <tr>\n");
262
263 CheckBox("fNumExt1Trigger", "#Ext1");
264 CheckBox("fNumExt2Trigger", "#Ext2");
265 CheckBox("fNumILPTrigger", "#ILP");
266 CheckBox("fNumOtherTrigger", "#Other");
267
268 printf(" </tr>\n");
269 printf(" <tr>\n");
270
271 CheckBox("fROITimeMarker", "roi-tm");
272 CheckBox("fFitsFileErrors", "file err");
273 CheckBox("fCompileTime", "compiled");
274 CheckBox("fRevisionNumber", "rev#");
275 CheckBox("if(fHasDrsFile=0,\"N\",fDrsStep)", "drs");
276 CheckBox("fMd5sumRaw", "md5");
277 CheckBox("fMd5sumRawZip", "md5(gz)");
278
279 printf(" </tr>\n");
280 printf(" <tr>\n");
281
282 CheckBox("fTriggerRateMedian", "rate");
283 CheckBox("fThresholdMedian", "threshold");
284 CheckBox("fBiasVoltageMedian", "voltage");
285 CheckBox("fRightAscension", "RA");
286 CheckBox("fDeclination", "Dec");
287 CheckBox("fZenithDistanceMean", "ZdMean");
288 CheckBox("fAzimuthMean", "AzMean");
289
290 printf(" </tr>\n");
291 printf(" </table>\n");
292 printf(" <p>\n");
293
294 if (empty($_GET["fStartDate"]))
295 $timemin = GetMin("fNight", "RunInfo", $host, $user, $pw, $db);
296 else
297 $timemin = $_GET["fStartDate"];
298
299 if (empty($_GET["fStopDate"]))
300 $timemax = GetMax("fNight", "RunInfo", $host, $user, $pw, $db);
301 else
302 $timemax = $_GET["fStopDate"];
303
304 printf("Run&nbsp;(yyyymmdd)&nbsp;from&nbsp;<input name=\"fStartDate\" type=\"text\" size=\"10\" maxlength=\"10\" value=\"%s\">\n", $timemin);
305 printf("to&nbsp;<input name=\"fStopDate\" type=\"text\" size=\"10\" maxlength=\"10\" value=\"%s\">&nbsp;&nbsp;&nbsp;&nbsp;\n", $timemax);
306
307 printf(" Results:\n");
308 printf(" <select name=\"fNumResults\">\n");
309
310 $numres = array("10", "20", "50", "100", "200", "500", "1000", "2000");
311 foreach ($numres as $element)
312 {
313 if ($element==$_GET["fNumResults"])
314 printf("<option value=\"%s\" selected>%3s</option>\n", $element, $element);
315 else
316 printf("<option value=\"%s\">%3s</option>\n", $element, $element);
317 }
318 printf(" </select>\n");
319 printf(" &nbsp;&nbsp;&nbsp;\n");
320
321 ini_set("mysql.trace_mode", "Off");
322 ini_set("display_errors", "Off");
323
324 printf("<input class='Width' type='submit' value='Query Table'>&nbsp;&nbsp;&nbsp;\n");
325 printf("<input class='Width' type='button' value='Reset' onClick='self.location.href=\"fact_runinfo.php\"'>&nbsp;&nbsp;&nbsp;\n");
326 if (strchr($_SERVER["REQUEST_URI"], '?')!=FALSE)
327 printf("<input class='Width' type='button' value='Get .txt' onClick='self.location.href=\"%s&fSendTxt=1\"'>&nbsp;&nbsp;&nbsp;\n", $_SERVER["REQUEST_URI"]);
328 printf("</form>\n");
329 printf("</center>\n");
330 printf("</td>\n");
331 printf("</tr>\n");
332 printf("<tr class='Block'>\n");
333 printf("<td>\n");
334 }
335
336 function PrintPage($html, $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup)
337 {
338 $db_id = mysql_connect($host, $user, $pw);
339 if ($db_id==FALSE)
340 {
341 printf("mysql_connect returned the following error: %s\n", mysql_error());
342 die("");
343 }
344 mysql_select_db($db);
345 mysql_query("SET BIG_TABLES=1"); // necessary for mySQL <= 4
346
347
348 $query0 = CreateQuery($_GET, $alias, $checkwhere, $checkgroup, $checkstatusgroup);
349
350 $result0 = mysql_query($query0, $db_id);
351 $result1 = mysql_query("SELECT FOUND_ROWS()", $db_id);
352
353 if ($result0)
354 {
355 if ($html=="1")
356 PrintMagicTable($result0, $alias, $rightalign, "", "", "", $result1);
357 else
358 PrintText($result0);
359
360 mysql_free_result($result0);
361 mysql_free_result($result1);
362 }
363 mysql_close($db_id);
364
365 if ($html=="1")
366 PrintSubmittedQuery($query0, $db, "old");
367 }
368
369 include ("include.php");
370 include ("db.php");
371 include ("factdefs.php");
372
373 ini_set("display_errors", "On");
374 ini_set("mysql.trace_mode", "On");
375
376 if (!empty($_GET["fSendTxt"]))
377 {
378 header("Content-type: application/octet");
379 header("Content-Disposition: attachment; filename=query-result.txt");
380
381 PrintPage("0", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup);
382 }
383 else
384 {
385 echo (file_get_contents("index-header.html"));
386
387 $environment = sizeof($_GET);
388
389 InitGet($_GET);
390 PrintForm($_GET, $host, $user, $pw, $db);
391
392 if ($environment==0)
393 printf("No query submitted yet.<BR>");
394 else
395 PrintPage("1", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup);
396
397 echo (file_get_contents("index-footer.html"));
398 }
399
400 ini_set("display_errors", "Off");
401 ini_set("mysql.trace_mode", "Off");
402}
403?>
Note: See TracBrowser for help on using the repository browser.