source: trunk/MagicSoft/Mars/datacenter/db/queryseq.php@ 8015

Last change on this file since 8015 was 7974, checked in by Daniela Dorner, 18 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 13.2 KB
Line 
1<?php
2{
3 function CreateQuery($_GET, $alias, $checkwhere, $checkgroup, $checkstatusgroup)
4 {
5 $fromtable="Sequences";
6
7 $groups = 0;
8 foreach ($checkgroup as $element)
9 if ($element==-1)
10 $groups++;
11
12 $query0 = "SELECT ";
13
14 if ($groups>0)
15 {
16 foreach ($checkgroup as $key => $element)
17 if ($element==-1)
18 $query0 .= $key . " as '" . $alias[$key] . "' " . ", ";
19 //--------------------------------------------------
20 $query0 .= "SUM(fRunTime)/3600 as '" . $alias["SUM(fRunTime)/3600"] . "', ";
21 //--------------------------------------------------
22 $query0 .= " SUM(fNumEvents) as '" . $alias["SUM(fNumEvents)"] . "', ";
23 $query0 .= " Min(fZenithDistanceMin) as '" . $alias["Min(fZenithDistanceMin)"] . "', ";
24 $query0 .= " Max(fZenithDistanceMax) as '" . $alias["Max(fZenithDistanceMax)"] . "', ";
25 $query0 .= " COUNT(*) as 'Sequence#' ";
26 }
27 else
28 {
29 if (!empty($_GET["fSendTxt"]))
30 $query0 .= " SELECT fSequenceFirst ";
31 else
32 $query0 .= " CONCAT('<A&ws;HREF=\"index.php?', 'fRunStart=On', '&fZenithDistance=On', '&fRunMin=', fSequenceFirst, '&fMeanTriggerRate=On', '&fRunTypeName=On', '&fRunMax=', fSequenceLast, '&fNumEvents=On', '&fSourceName=On&', 'fExcludedFDAKEY=1', '&fSequenceFirst=On', '&fNumResults=500\">', fSequenceFirst, '</A>') ";
33 $query0 .= " as 'Sequence#' ";
34
35 foreach ($_GET as $key => $element)
36 if ($_GET[$key]=="On")
37 if (empty($checkwhere[$key]) || $checkwhere[$key]==0)
38 $query0 .= ", " . $key . " as '" . $alias[$key] . "' ";
39 }
40
41 $query0 .= " FROM Sequences ";
42
43 foreach ($_GET as $key => $element)
44 if (($_GET[$key]=="On" || $groups>0))// && !empty($joins[$key]))
45 $query0 .= GetJoin($fromtable, $key);
46
47 foreach ($checkwhere as $key => $element)
48 {
49 if (empty($element) || $element<=0)
50 continue;
51
52 if (strpos($query0, " WHERE ")==FALSE)
53 $query0 .= " WHERE ";
54 else
55 if ($element!=-1)
56 if (strrpos($query0, " AND ")!=strlen($query0)-5)
57 $query0 .= " AND ";
58
59 if ($element!=-1)
60 $query0 .= GetCheck($fromtable, $key) . "=" . $element;
61 }
62
63 if (strpos($query0, " WHERE ")==FALSE)
64 $query0 .= " WHERE ";
65 else
66 $query0 .= " AND ";
67
68 if (!empty($_GET["fRunMin"]) && !empty($_GET["fRunMax"]))
69 $query0 .= "fSequenceFirst BETWEEN " . $_GET["fRunMin"] . " AND " . $_GET["fRunMax"] . " ";
70
71/*
72 if (!empty($_GET["fDate"]))
73 $query0 .= " AND fRunStart REGEXP \"^" . $_GET["fDate"] . "\" ";
74*/
75
76 if (!empty($_GET["fStartDate"]))
77 {
78 if (strpos(strrev($query0), " DNA ")!=0)
79 $query0 .= " AND ";
80
81 $startdate=substr($_GET["fStartDate"], 0, 10);
82 if ($startdate=="0000-00-00")
83 $query0 .=" fRunStart >= '" . $startdate . " 00:00:00' ";
84 else
85 $query0 .= " fRunStart >= ADDDATE('" . $startdate . " 13:00:00', INTERVAL -1 DAY) ";
86 }
87
88 if (!empty($_GET["fStopDate"]))
89 {
90 if (strpos(strrev($query0), " DNA ")!=0 || !strpos(strrev($query0), " DNA "))
91 $query0 .= " AND ";
92
93 $stopdate=substr($_GET["fStopDate"], 0, 10);
94 $query0 .= " fRunStart < '" . $stopdate . " 13:00:00' ";
95 }
96
97 if ($groups>0)
98 {
99 $query0 .= " GROUP BY ";
100 $num = $groups;
101 foreach ($checkgroup as $key => $element)
102 if ($element==-1)
103 {
104 $query0 .= GetCheck($fromtable,$key);
105 if ($num-->1)
106 $query0 .= ", ";
107 }
108 }
109
110 if (!empty($_GET["fSortBy"]))
111 {
112 $query0 .= " ORDER BY " . substr($_GET["fSortBy"], 0, -1) . " ";
113 if (substr($_GET["fSortBy"], -1)=="-")
114 $query0 .= "DESC";
115 }
116
117 if (empty($_GET["fSortBy"]) && $groups==0)
118 $query0 .= "ORDER BY fSequenceFirst ASC ";
119
120 if (empty($_GET["fNumStart"]))
121 $_GET["fNumStart"]=0;
122
123 if (empty($_GET["fSendTxt"]))
124 $query0 .= " LIMIT " . $_GET["fNumStart"] . ", " . $_GET["fNumResults"];
125
126 return $query0;
127 }
128
129 function InitGet($_GET)
130 {
131 // Find out whether it is the first call to the php script
132 $first = empty($_GET["fRunMin"]) && empty($_GET["fRunMax"]);
133
134 if (empty($_GET["fNumResults"]))
135 $_GET["fNumResults"]="20";
136
137 if (empty($_GET["fLastUpdate"]))
138 $_GET["fLastUpdate"]="Off";
139
140 if (empty($_GET["fNumEvents"]))
141 $_GET["fNumEvents"]=$first?"On":"";
142
143 if (empty($_GET["fRunStart"]))
144 $_GET["fRunStart"]=$first?"On":"";
145
146 if (empty($_GET["fRunTime/60"]))
147 $_GET["fRunTime/60"]=$first?"On":"";
148
149 if (empty($_GET["fSequenceLast"]))
150 $_GET["fSequenceLast"]=$first?"On":"";
151
152 if (empty($_GET["fAzimuthMin"]))
153 $_GET["fAzimuthMin"]="Off";
154
155 if (empty($_GET["fAzimuthMax"]))
156 $_GET["fAzimuthMax"]="Off";
157
158 if (empty($_GET["fZenithDistanceMin"]))
159 $_GET["fZenithDistanceMin"]=$first?"On":"";
160
161 if (empty($_GET["fZenithDistanceMax"]))
162 $_GET["fZenithDistanceMax"]="Off";
163
164 if (empty($_GET["fManuallyChangedName"]))
165 $_GET["fManuallyChangedName"]="Off";
166
167 if (empty($_GET["fSourceName"]))
168 $_GET["fSourceName"]=$first?"On":"";
169
170 if (empty($_GET["fProjectName"]))
171 $_GET["fProjectName"]="Off";
172
173 if (empty($_GET["fTriggerTableName"]))
174 $_GET["fTriggerTableName"]="Off";
175
176 if (empty($_GET["fHvSettingsName"]))
177 $_GET["fHvSettingsName"]="Off";
178
179 if (empty($_GET["fDiscriminatorThresholdTableName"]))
180 $_GET["fDiscriminatorThresholdTableName"]="Off";
181
182 if (empty($_GET["fTriggerDelayTableName"]))
183 $_GET["fTriggerDelayTableName"]="Off";
184
185 if (empty($_GET["fLightConditionsName"]))
186 $_GET["fLightConditionsName"]="Off";
187
188 if (empty($_GET["fTestFlagName"]))
189 $_GET["fTestFlagName"]="Off";
190 }
191
192 function PrintForm($_GET, $host, $user, $pw, $db)
193 {
194 printf("<center>\n");
195 printf("<form action=\"queryseq.php\" METHOD=\"GET\">\n");
196 printf(" <table>\n");
197 printf(" <tr>\n");
198
199 CheckBox("fSequenceLast", "Last run");
200 CheckBox("fRunStart", "Start time");
201 CheckBox("fAzimuthMin", "Azimuth min");
202 CheckBox("fAzimuthMax", "Azimuth max");
203
204 printf(" </tr><tr>\n");
205
206 CheckBox("fNumEvents", "Num of events");
207 CheckBox("fRunTime/60", "Duration");
208 CheckBox("fZenithDistanceMin", "Zenith distance min");
209 CheckBox("fZenithDistanceMax", "Zenith distance max");
210
211 printf(" </table>\n");
212 printf(" <p>\n");
213
214 // pull down boxes
215
216 printf(" <table>\n");
217 printf(" <tr><td>\n");
218 PrintPullDown($host, $user, $pw, $db, "Source", "fSourceName", "fSourceKEY", "Source Name");
219 printf(" </td><td>\n");
220 PrintPullDown($host, $user, $pw, $db, "TriggerTable", "fTriggerTableName", "fTriggerTableKEY", "Trigger Table");
221 printf(" </td><td>\n");
222 PrintPullDown($host, $user, $pw, $db, "TriggerDelayTable", "fTriggerDelayTableName", "fTriggerDelayTableKEY", "Trigger Delay Table");
223 printf(" </td></tr><tr><td>\n");
224 PrintPullDown($host, $user, $pw, $db, "Project", "fProjectName", "fProjectKEY", "Project Name");
225 printf(" </td><td>\n");
226 PrintPullDown($host, $user, $pw, $db, "HvSettings", "fHvSettingsName", "fHvSettingsKEY", "HV Settings");
227 printf(" </td><td>\n");
228 PrintPullDown($host, $user, $pw, $db, "DiscriminatorThresholdTable", "fDiscriminatorThresholdTableName", "fDiscriminatorThresholdTableKEY", "DT Table");
229 printf(" </td></tr><tr><td>\n");
230 PrintPullDown($host, $user, $pw, $db, "TestFlag", "fTestFlagName", "fTestFlagKEY", "Test Flag");
231 printf(" </td><td>\n");
232 PrintPullDown($host, $user, $pw, $db, "LightConditions", "fLightConditionsName", "fLightConditionsKEY", "Light Conditions");
233 printf(" </td><td>\n");
234 PrintPullDown($host, $user, $pw, $db, "ManuallyChanged", "fManuallyChangedName", "fManuallyChangedKEY", "Manually changed");
235 printf(" </td></tr>\n");
236 printf(" </table>\n");
237 printf(" <p>\n");
238
239 if (empty($_GET["fStartDate"]))
240 $timemin = GetMin("fRunStart", "RunData", $host, $user, $pw, $db);
241 else
242 $timemin = $_GET["fStartDate"];
243
244 if (empty($_GET["fStopDate"]))
245 $timemax = GetMax("fRunStart", "RunData", $host, $user, $pw, $db);
246 else
247 $timemax = $_GET["fStopDate"];
248
249 printf("Night&nbsp;(yyyy-mm-dd)&nbsp;from&nbsp;<input name=\"fStartDate\" type=\"text\" size=\"10\" maxlength=\"10\" value=\"%s\">\n", $timemin);
250 printf("to&nbsp;<input name=\"fStopDate\" type=\"text\" size=\"10\" maxlength=\"10\" value=\"%s\">&nbsp;&nbsp;&nbsp;&nbsp;\n", $timemax);
251
252 if (empty($_GET["fRunMin"]))
253 $min = GetMin("fSequenceFirst", "Sequences", $host, $user, $pw, $db);
254 else
255 $min = $_GET["fRunMin"];
256
257 if (empty($_GET["fRunMax"]))
258 $max = GetMax("fSequenceFirst", "Sequences", $host, $user, $pw, $db);
259 else
260 $max = $_GET["fRunMax"];
261
262/*
263 printf("Date&nbsp;(yyyy-mm-dd)&nbsp;<input name=\"fDate\" type=\"text\" size=\"10\" maxlength=\"10\" value=\"");
264 if (!empty($_GET["fDate"]))
265 printf("%s", $_GET["fDate"]);
266 printf("\">&nbsp;&nbsp;&nbsp;\n");
267*/
268
269 printf("Sequences&nbsp;from&nbsp;<input name=\"fRunMin\" type=\"text\" size=\"6\" maxlength=\"6\" value=\"%s\">\n", $min);
270 printf("to&nbsp;<input name=\"fRunMax\" type=\"text\" size=\"6\" maxlength=\"6\" value=\"%s\">&nbsp;&nbsp;&nbsp;\n", $max);
271
272 printf(" <P>\n");
273
274 printf(" Results:\n");
275 printf(" <select name=\"fNumResults\">\n");
276
277 $numres = array("10", "20", "50", "100", "200", "500");
278 foreach ($numres as $element)
279 {
280 if ($element==$_GET["fNumResults"])
281 printf("<option value=\"%s\" selected>%3s</option>\n", $element, $element);
282 else
283 printf("<option value=\"%s\">%3s</option>\n", $element, $element);
284 }
285 printf(" </select>\n");
286 printf(" &nbsp;&nbsp;&nbsp;\n");
287
288 ini_set("mysql.trace_mode", "Off");
289 ini_set("display_errors", "Off");
290
291 printf("<input class='Width' type='submit' value='Query Table'>&nbsp;&nbsp;&nbsp;\n");
292 printf("<input class='Width' type='button' value='Reset' onClick='self.location.href=\"queryseq.php\"'>&nbsp;&nbsp;&nbsp;\n");
293 if (strchr($_SERVER["REQUEST_URI"], '?')!=FALSE)
294 printf("<input class='Width' type='button' value='Get .txt' onClick='self.location.href=\"%s&fSendTxt=1\"'>&nbsp;&nbsp;&nbsp;\n", $_SERVER["REQUEST_URI"]);
295 printf("</form>\n");
296 printf("</center>\n");
297 printf("</td>\n");
298 printf("</tr>\n");
299 printf("<tr class='Block'>\n");
300 printf("<td>\n");
301 }
302
303 function PrintPage($html, $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup)
304 {
305 $db_id = mysql_connect($host, $user, $pw);
306 if ($db_id==FALSE)
307 {
308 printf("mysql_connect returned the following error: %s\n", mysql_error());
309 die("");
310 }
311 mysql_select_db($db);
312
313 $query0 = CreateQuery($_GET, $alias, $checkwhere, $checkgroup, $checkstatusgroup);
314
315 $result0 = mysql_query($query0, $db_id);
316
317 if ($result0)
318 {
319 if ($html=="1")
320 PrintMagicTable($result0, $alias, $rightalign, "", "", "", $_GET);
321 else
322 PrintText($result0);
323
324 mysql_free_result($result0);
325 }
326 mysql_close($db_id);
327
328 PrintSubmittedQuery($query0, $html, $db, "old");
329 }
330
331 include ("include.php");
332 include ("db.php");
333 include ("magicdefs.php");
334
335 ini_set("display_errors", "On");
336 ini_set("mysql.trace_mode", "On");
337
338 if (!empty($_GET["fSendTxt"]))
339 {
340 header("Content-type: application/octet");
341 header("Content-Disposition: attachment; filename=query-result.txt");
342
343 PrintPage("0", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup);
344 }
345 else
346 {
347 echo (file_get_contents("index-header.html"));
348
349 $environment = sizeof($_GET);
350
351 InitGet($_GET);
352 PrintForm($_GET, $host, $user, $pw, $db);
353
354 if ($environment==0)
355 printf("No query submitted yet.<BR>");
356 else
357 PrintPage("1", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup);
358
359 echo (file_get_contents("index-footer.html"));
360 }
361
362 ini_set("display_errors", "Off");
363 ini_set("mysql.trace_mode", "Off");
364}
365?>
Note: See TracBrowser for help on using the repository browser.