source: trunk/MagicSoft/Mars/datacenter/db/dbstatus.php@ 9442

Last change on this file since 9442 was 9106, checked in by Daniela Dorner, 16 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 15.2 KB
Line 
1<?php
2{
3 function CreateQuery($_GET, $alias, $needs)
4 {
5 $joins = array
6 (
7 "fSourceName" => " LEFT JOIN Source USING(fSourceKEY) ",
8 );
9
10 $checkwhere = array
11 (
12 "fSourceName" => CheckWhere("fSourceKEY", $_GET),
13 );
14 $checkgroup = array
15 (
16 "fSourceName" => CheckGroup("fSourceKEY", $_GET),
17 );
18
19 $checkstatusgroup = array
20 (
21 "fAllFilesAvail" => CheckStatusGroup("fAllFilesAvailStatus", $_GET),
22 "fCallisto" => CheckStatusGroup("fCallistoStatus", $_GET),
23 "fFillCallisto" => CheckStatusGroup("fFillCallistoStatus", $_GET),
24 "fStar" => CheckStatusGroup("fStarStatus", $_GET),
25 "fFillStar" => CheckStatusGroup("fFillStarStatus", $_GET),
26 );
27
28 $check = array
29 (
30 "fSourceName" => " Sequences.fSourceKEY",
31 );
32
33 $table = array
34 (
35 "fSourceName" => "Source.fSourceName",
36 "fAllFilesAvail" => "SequenceProcessStatus.fAllFilesAvail",
37 "fCallisto" => "SequenceProcessStatus.fCallisto",
38 "fFillCallisto" => "SequenceProcessStatus.fFillCallisto",
39 "fStar" => "SequenceProcessStatus.fStar",
40 "fFillStar" => "SequenceProcessStatus.fFillStar",
41 );
42
43 $groups = 0;
44 foreach ($checkgroup as $element)
45 if ($element==-1)
46 $groups++;
47
48 $statusgroups = 0;
49 foreach ($checkstatusgroup as $element)
50 if ($element==-1)
51 $statusgroups++;
52
53 $query0 = "SELECT ";
54
55 if ($groups>0)
56 {
57 foreach ($checkgroup as $key => $element)
58 if ($element==-1)
59 $query0 .= $key . " as '" . $alias[$key] . "' " . ", ";
60 //--------------------------------------------------
61 $query0 .= "SUM(fRunTime)/3600 as '" . $alias["SUM(fRunTime)/3600"] . "', ";
62 //--------------------------------------------------
63 $query0 .= " SUM(fNumEvents) as '" . $alias["SUM(fNumEvents)"] . "', ";
64 $query0 .= " Min(fZenithDistanceMin) as '" . $alias["Min(fZenithDistanceMin)"] . "', ";
65 $query0 .= " Max(fZenithDistanceMax) as '" . $alias["Max(fZenithDistanceMax)"] . "', ";
66 $query0 .= " COUNT(*) as 'Sequence#' ";
67 }
68 else
69 {
70 if ($statusgroups>0)
71 {
72 foreach ($checkstatusgroup as $key => $element)
73 if ($element==-1)
74 $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] . "', ";
75// $query0 .= " (if(IsNull(" . $key . "), 'not done' ,if(" . $key . "='1970-01-01 00:00:00','dont do','done'))) as '" . $alias[$key] . "', ";
76
77 $query0 .= " count(*) as 'Sequence#'";
78
79 }
80 else
81 {
82 if (!empty($_GET["fSendTxt"]))
83 $query0 .= " Sequences.fSequenceFirst ";
84 else
85 $query0 .= " CONCAT('<A&ws;HREF=\"runinfo.php?', 'fRunStart=On', '&fZenithDistance=On', '&fRunMin=', Sequences.fSequenceFirst, '&fMeanTriggerRate=On', '&fRunTypeName=On', '&fRunMax=', fSequenceLast, '&fNumEvents=On', '&fSourceName=On&', 'fExcludedFDAKEY=1', '&fSequenceFirst=On', '&fRawFileAvail=On', '&fCCFileAvail=On', '&fCaCoFileAvail=On\">', Sequences.fSequenceFirst, '</A>') ";
86 $query0 .= " as 'Sequence#' ";
87
88 foreach ($_GET as $key => $element)
89 if ($_GET[$key]=="On")
90 if (empty($checkwhere[$key]) || $checkwhere[$key]==0)
91 $query0 .= ", " . $key . " as '" . $alias[$key] . "' ";
92 }
93 }
94
95 $query0 .= " FROM Sequences ";
96
97 $query0 .= " LEFT JOIN SequenceProcessStatus USING(fSequenceFirst,fTelescopeNumber) ";
98 $query0 .= " LEFT JOIN Calibration USING(fSequenceFirst,fTelescopeNumber) ";
99 $query0 .= " LEFT JOIN Star USING(fSequenceFirst,fTelescopeNumber) ";
100
101 foreach ($_GET as $key => $element)
102 if (($_GET[$key]=="On" || $groups>0) && !empty($joins[$key]))
103 $query0 .= $joins[$key];
104
105 foreach ($checkwhere as $key => $element)
106 {
107 if (empty($element) || $element<=0)
108 continue;
109
110 if (strpos($query0, " WHERE ")==FALSE)
111 $query0 .= " WHERE ";
112 else
113 if ($element!=-1)
114 if (strrpos($query0, " AND ")!=strlen($query0)-5)
115 $query0 .= " AND ";
116
117 if ($element!=-1)
118 $query0 .= $check[$key] . "=" . $element;
119 }
120
121 if (strpos($query0, " WHERE ")==FALSE)
122 $query0 .= " WHERE ";
123 else
124 $query0 .= " AND ";
125
126 $query0 .= StatusQuery("fSequenceFileWritten", $needs);
127 $query0 .= StatusQuery("fAllFilesAvail", $needs);
128 $query0 .= StatusQuery("fCallisto", $needs);
129 $query0 .= StatusQuery("fFillCallisto", $needs);
130 $query0 .= StatusQuery("fStar", $needs);
131 $query0 .= StatusQuery("fFillStar", $needs);
132
133 if (!empty($_GET["fRunMin"]) && !empty($_GET["fRunMax"]))
134 $query0 .= "Sequences.fSequenceFirst BETWEEN " . $_GET["fRunMin"] . " AND " . $_GET["fRunMax"] . " ";
135 else
136 $query0 = substr($query0, 0, -6);
137
138/*
139 if (!empty($_GET["fDate"]))
140 $query0 .= " AND fRunStart REGEXP \"^" . $_GET["fDate"] . "\" ";
141*/
142
143 if (!empty($_GET["fSourceN"]))
144 $query0 .= " AND fSourceName REGEXP \"^" . $_GET["fSourceN"] . "\" ";
145
146 if (!empty($_GET["fStartDate"]))
147 {
148 if (strpos(strrev($query0), " DNA ")!=0 || !strpos(strrev($query0), " DNA "))
149 $query0 .= " AND ";
150
151 $startdate=substr($_GET["fStartDate"], 0, 10);
152 if ($startdate=="0000-00-00")
153 $query0 .=" fRunStart >= '" . $startdate . " 00:00:00' ";
154 else
155 $query0 .= " fRunStart >= ADDDATE('" . $startdate . " 13:00:00', INTERVAL -1 DAY) ";
156 }
157
158 if (!empty($_GET["fStopDate"]))
159 {
160 if (strpos(strrev($query0), " DNA ")!=0 || !strpos(strrev($query0), " DNA "))
161 $query0 .= " AND ";
162
163 $stopdate=substr($_GET["fStopDate"], 0, 10);
164 $query0 .= " fRunStart < '" . $stopdate . " 13:00:00' ";
165 }
166
167 if ($groups>0)
168 {
169 $query0 .= " GROUP BY ";
170 $num = $groups;
171 foreach ($checkgroup as $key => $element)
172 if ($element==-1)
173 {
174 $query0 .= $check[$key];
175 if ($num-->1)
176 $query0 .= ", ";
177 }
178 }
179
180 if ($statusgroups>0)
181 {
182 $query0 .= " GROUP BY ";
183 $num = $statusgroups;
184 foreach ($checkstatusgroup as $key => $element)
185 if ($element==-1)
186 {
187 $query0 .= $alias[$key];
188 if ($num-->1)
189 $query0 .= ", ";
190 }
191 }
192
193 if (!empty($_GET["fSortBy"]))
194 {
195 $val=substr($_GET["fSortBy"], 0, -1);
196 $query0 .= " ORDER BY " . GetTable($fromtable, $val) . " ";
197 if (substr($_GET["fSortBy"], -1)=="-")
198 $query0 .= "DESC";
199 }
200
201 if (empty($_GET["fSortBy"]) && $groups==0 && $statusgroups==0)
202 $query0 .= "ORDER BY Sequences.fSequenceFirst ASC ";
203
204 if (empty($_GET["fNumStart"]))
205 $_GET["fNumStart"]=0;
206
207 return $query0;
208 }
209
210 function InitGet($_GET)
211 {
212 // Find out whether it is the first call to the php script
213 $first = empty($_GET["fRunMin"]) && empty($_GET["fRunMax"]);
214
215 if (empty($_GET["fSourceName"]))
216 $_GET["fSourceName"]=$first?"On":"";
217
218 if (empty($_GET["fAllFilesAvail"]))
219 $_GET["fAllFilesAvail"]="On";
220
221 if (empty($_GET["fAllFilesAvailStatus"]))
222 $_GET["fAllFilesAvailStatus"]=$first?"7":"";
223
224 if (empty($_GET["fCallisto"]))
225 $_GET["fCallisto"]="On";
226
227 if (empty($_GET["fCallistoStatus"]))
228 $_GET["fCallistoStatus"]=$first?"7":"";
229
230 if (empty($_GET["fFillCallisto"]))
231 $_GET["fFillCallisto"]="Off";
232
233 if (empty($_GET["fFillCallistoStatus"]))
234 $_GET["fFillCallistoStatus"]="0";
235
236 if (empty($_GET["fStar"]))
237 $_GET["fStar"]=$first?"On":"";
238
239 if (empty($_GET["fStarStatus"]))
240 $_GET["fStarStatus"]=$first?"7":"";
241
242 if (empty($_GET["fFillStar"]))
243 $_GET["fFillStar"]="Off";
244
245 if (empty($_GET["fFillStarStatus"]))
246 $_GET["fFillStarStatus"]="0";
247 }
248
249 function PrintForm($_GET, $host, $user, $pw, $db)
250 {
251 printf("<center>\n");
252 printf("<form action=\"dbstatus.php\" METHOD=\"GET\">\n");
253 printf(" <table><tr><td>\n");
254
255 PrintStatusMenu("fAllFilesAvail", "Files avail");
256 printf(" </td><td>\n");
257 PrintStatusMenu("fCallisto", "Callisto");
258 printf(" </td><td>\n");
259 PrintStatusMenu("fStar", "Star");
260 printf(" </td><td>\n");
261 PrintStatusMenu("fFillCallisto", "Fillcallisto");
262 printf(" </td><td>\n");
263 PrintStatusMenu("fFillStar", "Fillstar");
264
265 printf("</td></tr> </table>\n");
266 printf(" <p>\n");
267
268 // pull down boxes
269
270 printf(" <table>\n");
271 printf(" <tr><td>\n");
272 PrintPullDown($host, $user, $pw, $db, "Source", "fSourceName", "fSourceKEY", "Source Name");
273 printf(" </td><td>\n");
274
275 printf("&nbsp;&nbsp;&nbsp;Source&nbsp;(<A HREF=\"regexp.html\">regexp</A>)&nbsp;<input name=\"fSourceN\" type=\"text\" size=\"15\" maxlength=\"15\" value=\"");
276 if (!empty($_GET["fSourceN"]))
277 printf("%s", $_GET["fSourceN"]);
278 printf("\">&nbsp;&nbsp;&nbsp;\n");
279
280 printf(" </td></tr>\n");
281 printf(" </table>\n");
282 printf(" <p>\n");
283
284 if (empty($_GET["fStartDate"]))
285 $timemin = GetMin("fRunStart", "Sequences", $host, $user, $pw, $db);
286 else
287 $timemin = $_GET["fStartDate"];
288
289 if (empty($_GET["fStopDate"]))
290 $timemax = GetMax("fRunStart", "Sequences", $host, $user, $pw, $db);
291 else
292 $timemax = $_GET["fStopDate"];
293
294 printf("Night&nbsp;(yyyy-mm-dd)&nbsp;from&nbsp;<input name=\"fStartDate\" type=\"text\" size=\"10\" maxlength=\"10\" value=\"%s\">\n", $timemin);
295 printf("to&nbsp;<input name=\"fStopDate\" type=\"text\" size=\"10\" maxlength=\"10\" value=\"%s\">&nbsp;&nbsp;&nbsp;&nbsp;\n", $timemax);
296
297 if (empty($_GET["fRunMin"]))
298 $min = GetMin("fSequenceFirst", "Sequences", $host, $user, $pw, $db);
299 else
300 $min = $_GET["fRunMin"];
301
302 if (empty($_GET["fRunMax"]))
303 $max = GetMax("fSequenceFirst", "Sequences", $host, $user, $pw, $db);
304 else
305 $max = $_GET["fRunMax"];
306
307 printf("Sequences&nbsp;from&nbsp;<input name=\"fRunMin\" type=\"text\" size=\"6\" maxlength=\"6\" value=\"%s\">\n", $min);
308 printf("to&nbsp;<input name=\"fRunMax\" type=\"text\" size=\"6\" maxlength=\"6\" value=\"%s\">&nbsp;&nbsp;&nbsp;\n", $max);
309
310 printf(" <P>\n");
311
312 printf(" &nbsp;&nbsp;&nbsp;\n");
313
314 ini_set("mysql.trace_mode", "Off");
315 ini_set("display_errors", "Off");
316
317 printf("<input class='Width' type='submit' value='Query Table'>&nbsp;&nbsp;&nbsp;\n");
318 printf("<input class='Width' type='button' value='Reset' onClick='self.location.href=\"dbstatus.php\"'>&nbsp;&nbsp;&nbsp;\n");
319 if (strchr($_SERVER["REQUEST_URI"], '?')!=FALSE)
320 printf("<input class='Width' type='button' value='Get .txt' onClick='self.location.href=\"%s&fSendTxt=1\"'>&nbsp;&nbsp;&nbsp;\n", $_SERVER["REQUEST_URI"]);
321 if (strchr($_SERVER["REQUEST_URI"], '?')!=FALSE)
322 printf("<input class='Width' type='button' value='Print' onClick='self.location.href=\"%s&fPrintTable=1\"'>&nbsp;&nbsp;&nbsp;\n", $_SERVER["REQUEST_URI"]);
323 printf("</form>\n");
324 printf("</center>\n");
325 printf("</td>\n");
326 printf("</tr>\n");
327 printf("<tr class='Block'>\n");
328 printf("<td>\n");
329 }
330
331 function PrintPage($html, $host, $user, $pw, $db, $needs)
332 {
333 $db_id = mysql_connect($host, $user, $pw);
334 if ($db_id==FALSE)
335 {
336 printf("mysql_connect returned the following error: %s\n", mysql_error());
337 die("");
338 }
339 mysql_select_db($db);
340
341 $alias = array
342 (
343 "fSourceName" => "Source",
344 "fAllFilesAvail" => "FilesAvail",
345 "fCallisto" => "Callisto",
346 "fStar" => "Star",
347 "fFillCallisto" => "FillCal",
348 "fFillStar" => "FillStar",
349 );
350 $rightalign = array
351 (
352 );
353
354 $query0 = CreateQuery($_GET, $alias, $needs);
355
356 $result0 = mysql_query($query0, $db_id);
357
358 if ($result0)
359 {
360 if ($html=="1" || $html=="2")
361 PrintMagicTable($result0, $alias, $rightalign, "", "", "", "", $_GET);
362 else
363 PrintText($result0);
364
365 mysql_free_result($result0);
366 }
367 mysql_close($db_id);
368
369 PrintSubmittedQuery($query0, $html, $db, "old");
370 }
371
372 include ("include.php");
373 include ("db.php");
374 include ("magicdefs.php");
375
376 ini_set("display_errors", "On");
377 ini_set("mysql.trace_mode", "On");
378
379 if (!empty($_GET["fSendTxt"]))
380 {
381 header("Content-type: application/octet");
382 header("Content-Disposition: attachment; filename=query-result.txt");
383
384 PrintPage("0", $host, $user, $pw, $db);
385 }
386 else
387 {
388 echo (file_get_contents("index-header.html"));
389
390 $environment = sizeof($_GET);
391
392 InitGet($_GET);
393 if (empty($_GET["fPrintTable"]))
394 PrintForm($_GET, $host, $user, $pw, $db);
395
396 if ($environment==0)
397 printf("No query submitted yet.<BR>");
398 else
399 {
400 if (empty($_GET["fPrintTable"]))
401 PrintPage("1", $host, $user, $pw, $db, $needs);
402 else
403 PrintPage("2", $host, $user, $pw, $db, $needs);
404 }
405
406 echo (file_get_contents("index-footer.html"));
407 }
408
409 ini_set("display_errors", "Off");
410 ini_set("mysql.trace_mode", "Off");
411}
412?>
Note: See TracBrowser for help on using the repository browser.