Added rantest and cqtool to repo
[clearscm.git] / rantest / web / Rantest / FailureAnalysis.php
1 <?php
2 ////////////////////////////////////////////////////////////////////////////////
3 //
4 // File:        FailureAnalysis.php
5 // Revision:    1.2
6 // Description: Produce a report showing an analysis of failures for a day
7 // Author:      Andrew@ClearSCM.com
8 // Created:     Mon Apr 28 15:20:06 MST 2008
9 // Modified:    
10 // Language:    PHP
11 //
12 // (c) Copyright 2008, General Dynamics, all rights reserved.
13 //
14 // All rights reserved except as subject to DFARS 252.227-7014 of contract
15 // number CP02H8901N issued under prime contract N00039-04-C-2009.
16 //
17 // Warning: This document contains technical data whose export is restricted
18 // by the Arms Export Control Act (Title 22, U.S.C., Sec 2751, et seq.) or the
19 // Export Administration Act of 1979, as amended, Title, 50, U.S.C., App. 2401
20 // et seq. Violations of these export laws are subject to severe criminal
21 // penalties. Disseminate in accordance with provisions of DoD Directive
22 // 5230.25.
23 //
24 ////////////////////////////////////////////////////////////////////////////////
25 $script = basename ($_SERVER["PHP_SELF"]);
26
27 include_once "$_SERVER[DOCUMENT_ROOT]/php/Utils.php";
28 include_once "$_SERVER[DOCUMENT_ROOT]/php/RantestDB.php";
29
30 $day            = $_REQUEST["day"];
31 $user           = $_REQUEST["user"];
32
33 $action         = (empty ($_REQUEST["action"]))    ? "Report"     : $_REQUEST["action"];
34 $sortBy         = (empty ($_REQUEST["sortBy"]))    ? "Date"       : $_REQUEST["sortBy"];
35 $direction      = (empty ($_REQUEST["direction"])) ? "ascending"  : $_REQUEST["direction"];
36
37 // Sorting functions
38 function sortTime ($a, $b) {
39   global $direction;
40
41   return cmpStr ($a, $b, "Time", $direction);
42 } // sortTime
43
44 function getData ($day) {
45   global $sortBy;
46
47   $data = getFailures ($day);
48
49   // Sort data
50   if ($sortBy == "Testcase") {
51     uasort ($data, "sortTestcase");
52   } else if ($sortBy == "Unit") {
53     uasort ($data, "sortUnit");
54   } else {
55     uasort ($data, "sortTime");
56   } // if
57
58   return $data;
59 } // getData
60
61 function createCSV ($day) {
62   $data = getData ($day);
63
64   // Title line
65   $csv  = "Failure Analysis for $day\n";
66
67   // Create header line
68   $firstTime = true;
69
70   foreach ($data[0] as $key => $value) {
71     // Skip "hidden" fields - fields beginning with "_"
72     if (preg_match ("/^_/", $key) == 1) {
73       continue;
74     } // if
75
76     if ($key == "Failures") {
77       $csv .= "\n";
78       $csv .= ",Teststep,Failure Reason";
79       
80       continue;
81     } // if
82
83     if (!$firstTime) {
84       $csv .= ",";
85     } else {
86       $firstTime = false;
87     } // if
88
89     $csv .= "\"$key\"";
90   } // foreach
91
92   $csv .= "\n";
93
94   // Data lines
95   foreach ($data as $entry) {
96     $firstTime = true;
97
98     foreach ($entry as $key => $value) {
99       // Skip "hidden" fields - fields beginning with "_"
100       if (preg_match ("/^_/", $key) == 1) {
101         continue;
102       } // if
103
104       if ($key == "Failures") {
105         $csv .= "\n";
106
107         foreach ($value as $key => $failure) {
108           $csv .= ",$failure[Step],$failure[Reason]\n";
109         } // foreach
110
111         continue;
112       } // if
113
114       if (!$firstTime) {
115         $csv .= ",";
116       } else {
117         $firstTime = false;
118       } // if
119
120       $csv .= "\"$value\"";
121     } // foreach
122
123     $csv .= "\n";
124   } // foreach
125
126   return $csv;
127 } // createCSV
128
129 function exportStats ($day) {
130   $filename = "Test Statistics." . $day . ".csv";
131
132   header ("Content-Type: application/octect-stream");
133   header ("Content-Disposition: attachment; filename=\"$filename\"");
134
135   print createCSV ($day);
136
137   exit;
138 } // exportStats
139
140 function createHeader ($day) {
141   $header = <<<END
142 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
143    "http://www.w3.org/TR/html4/strict.dtd">
144 <html>
145 <head>
146   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
147   <link rel="stylesheet" type="text/css" media="screen" href="/css/Testing.css">
148   <link rel="stylesheet" type="text/css" media="screen" href="/css/Tables.css">
149   <title>Failure Analysis for $day</title>
150 <script language="javascript">
151 function ChangeDay (day, script) {
152   window.location = script + "?day=" + day;
153 } // ChangeDay
154 </script>
155 <body>
156 END;
157
158   $header .= banner ();
159
160   return $header;
161 } // createHeader
162
163 function createPage ($day, $forEmail = false, $message = "") {
164   global $direction, $sortBy, $direction, $script;
165
166   $data         = getData ($day);
167   $dateDropdown = reportDateDropdown ();
168   $row_nbr      = 0;
169
170   if (!$forEmail) {
171     $page .= "<h1 align=\"center\">Failure Analysis for $dateDropdown</h1>";
172
173     // Flip direction
174     $direction = ($direction == "ascending") ? "descending" : "ascending";
175     $urlParms  = "$script?day=$day&action=$action&direction=$direction&sortBy";
176
177     if ($sortBy == "Testcase") {
178       $testcaseDirection = ($direction == "ascending") 
179         ? "<img src=/images/down.png border=0>"
180         : "<img src=/images/up.png border=0>";
181     } else if ($sortBy == "Unit") {
182       $unitDirection = ($direction == "ascending") 
183         ? "<img src=/images/down.png border=0>"
184         : "<img src=/images/up.png border=0>";
185     } else {
186       $timeDirection = ($direction == "ascending") 
187         ? "<img src=/images/down.png border=0>"
188         : "<img src=/images/up.png border=0>";
189     } // if
190
191     if (isset ($message)) {
192       $page .= "<div align=center>$message</div>";
193     } // if
194   } else {
195     $page .= "<h1 align=\"center\">Failure Analysis for $day</h1>";
196   } // if
197
198   $page .= <<<END
199 <table align=center width=90%>
200   <thead>
201 END;
202
203   if (!$forEmail) {
204     $page .= <<<END
205     <tr>
206       <th class="clear" align="left"><a href="$script?action=Export&day=$day"><input type="submit" value="Export to CSV"></a></th>
207       <th class="clear" colspan="2"
208  align="right"><form action="$script?action=Mail&day=$day" method="post">
209 END;
210
211     $page .= emailUsersDropdown ();
212     $page .= <<<END
213         </select>
214         <input type="submit" value="Send"></form>
215       </th>
216     </tr>
217 END;
218   } // if
219
220   $page .= <<<END
221     <tr>
222       <th class=left><a href="$urlParms=Testcase">Testcase&nbsp;$testcaseDirection</a></th>
223       <th><a href="$urlParms=Unit">Unit&nbsp;$unitDirection</a></th>
224       <th class=right><a href="$urlParms=Time">Time&nbsp;$timeDirection</a></th>
225     </tr>
226     <tr>
227       <th width="100px">&nbsp;</th>
228       <th>Teststep</th>
229       <th>Failure Reason</th>
230     </tr>
231   </thead>
232   <tbody>
233 END;
234
235   foreach ($data as $result) {
236     $row_color = " class=other";
237
238     $unit = ($result["Unit"]) ? $result["Unit"] : "";
239     $time = ($result["Time"]) ? $result["Time"] : "";
240
241     $page .= <<<END
242     <tr $row_color>
243       <td><a href="rantest.php?testName=$result[Testcase]&runid=$result[_runid]&date=$result[Date]">$result[Testcase]</a></td>
244       <td align=right>$unit</td>
245       <td align=right>$time</td>
246     </tr>
247 END;
248
249     foreach ($result["Failures"] as $failure) {
250       $step     = $failure["Step"];
251       $reason   = $failure["Reason"];
252       $page .= <<<END
253     <tr class="failure">
254       <td>&nbsp;</td>
255       <td>$step</td>
256       <td>$reason</td>
257     </tr>
258 END;
259     } // foreach
260   } // foreach
261
262   $page .= <<<END
263   </tbody>
264 </table>
265 END;
266
267   return $page;
268 } // createPage
269
270 function displayReport ($day, $message = "") {
271   print createHeader    ($day);
272   print createPage      ($day, false, $message);
273
274   copyright ();
275 } // displayReport
276
277 function mailFailureAnalysisReport ($day, $pnbr, $username) {
278   $subject      = "Failure Analysis for $day";
279   $body         = createPage ($day, true);
280   $filename     = "FailureAnalysis.$day.csv";
281   $attachment   = createCSV ($day);
282
283   return mailReport ($pnbr, $username, $subject, $body, $filename, $attachment);
284 } // mailFailureAnalysisReport
285
286 openDB ();
287
288 switch ($action) {
289   case "Export":
290     exportStats ($day);
291     break;
292
293   case "Mail":
294     list ($pnbr, $username) = explode (":", $user);
295     displayReport ($day, mailFailureAnalysisReport ($day, $pnbr, $username));
296     break;
297
298   default:
299     displayReport ($day);
300     break;
301 } // switch  
302 ?>
303 </body>
304 </html>