Added rantest and cqtool to repo
[clearscm.git] / rantest / web / Rantest / TestHistory.php
1 <?php
2 ////////////////////////////////////////////////////////////////////////////////
3 //
4 // File:        TestHistory.php
5 // Revision:    1.2
6 // Description: Produce a historical report about a testcase
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 $testcase       = $_REQUEST["testcase"];
31 $user           = $_REQUEST["user"];
32
33 $build          = (empty ($_REQUEST["build"]))     ? "*"         : $_REQUEST["build"];
34 $level          = (empty ($_REQUEST["level"]))     ? "*"         : $_REQUEST["level"];
35 $DUT            = (empty ($_REQUEST["DUT"]))       ? "*"         : $_REQUEST["DUT"];
36 $test           = (empty ($_REQUEST["test"]))      ? "*"         : $_REQUEST["test"];
37
38 $action         = (empty ($_REQUEST["action"]))    ? "Report"     : $_REQUEST["action"];
39 $type           = (empty ($_REQUEST["type"]))      ? "All"        : $_REQUEST["type"];
40 $sortBy         = (empty ($_REQUEST["sortBy"]))    ? "Date"       : $_REQUEST["sortBy"];
41 $direction      = (empty ($_REQUEST["direction"])) ? "descending" : $_REQUEST["direction"];
42
43 $historyFor     = setTestcase ($testcase);
44
45 // Sorting functions
46 function sortDUT ($a, $b) {
47   global $direction;
48
49   return cmpStr ($a, $b, "DUT", $direction);
50 } // sortDUT
51
52 function sortAvgRunTime ($a, $b) {
53   global $direction;
54
55   return cmpNbr ($a, $b, "AvgRunTime", $direction);
56 } // sortAvgRunTime
57
58 function getData ($testcase) {
59   global $sortBy;
60
61   $data = getTestHistory ($testcase);
62
63   // Sort data
64   if ($sortBy == "Passed") {
65     uasort ($data, "sortPassed");
66   } elseif ($sortBy == "Failed") {
67     uasort ($data, "sortFailed");
68   } elseif ($sortBy == "Total") {
69     uasort ($data, "sortTotal");
70   } elseif ($sortBy == "AvgRunTime") {
71     uasort ($data, "sortAvgRunTime");
72   } elseif ($sortBy == "DUT") {
73     uasort ($data, "sortDUT");
74   } elseif ($sortBy == "Type") {
75     uasort ($data, "sortType");
76   } elseif ($sortBy == "Start") {
77     uasort ($data, "sortStart");
78   } elseif ($sortBy == "End") {
79     uasort ($data, "sortEnd");
80   } elseif ($sortBy == "Duration") {
81     uasort ($data, "sortDuration");
82   } elseif ($sortBy == "Status") {
83     uasort ($data, "sortStatus");
84   } else {
85     uasort ($data, "sortTestcase");
86   } // if    
87
88   return $data;
89 } // getData
90
91 function createHeader () {
92   global $historyFor;
93
94   $header = <<<END
95 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
96    "http://www.w3.org/TR/html4/strict.dtd">
97 <html>
98 <head>
99   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
100   <link rel="stylesheet" type="text/css" media="screen" href="/css/Testing.css">
101   <link rel="stylesheet" type="text/css" media="screen" href="/css/Tables.css">
102   <title>Test history for $historyFor</title>
103 </head>
104
105 <body>
106 END;
107
108   $header .= banner ();
109   $header .= <<<END
110 <h1 align="center">Test history for $historyFor</h1>
111 END;
112
113   return $header;
114 } // createHeader
115
116 function createPage ($testcase, $forEmail = false, $message = "") {
117   global $webdir, $direction, $sortBy, $script;
118
119   $data = getData ($testcase);
120
121   if (!$forEmail) {
122     // Flip direction
123     $direction = ($direction == "ascending") ? "descending" : "ascending";
124
125     if ($sortBy == "DUT") {
126       $DUTDirection = ($direction == "ascending") 
127         ? "<img src=/images/down.png border=0>"
128         : "<img src=/images/up.png border=0>";
129     } elseif ($sortBy == "Type") {
130       $typeDirection = ($direction == "ascending") 
131         ? "<img src=/images/down.png border=0>"
132         : "<img src=/images/up.png border=0>";
133     } elseif ($sortBy == "Start") {
134       $startDirection = ($direction == "ascending") 
135         ? "<img src=/images/down.png border=0>"
136         : "<img src=/images/up.png border=0>";
137     } elseif ($sortBy == "End") {
138       $endDirection = ($direction == "ascending") 
139         ? "<img src=/images/down.png border=0>"
140         : "<img src=/images/up.png border=0>";
141     } elseif ($sortBy == "Duration") {
142       $durationDirection = ($direction == "ascending") 
143         ? "<img src=/images/down.png border=0>"
144         : "<img src=/images/up.png border=0>";
145     } elseif ($sortBy == "Status") {
146       $statusDirection = ($direction == "ascending") 
147         ? "<img src=/images/down.png border=0>"
148         : "<img src=/images/up.png border=0>";
149     } elseif ($sortBy == "Passed") {
150       $passedDirection = ($direction == "ascending") 
151         ? "<img src=/images/down.png border=0>"
152         : "<img src=/images/up.png border=0>";
153     } elseif ($sortBy == "Failed") {
154       $failedDirection = ($direction == "ascending") 
155         ? "<img src=/images/down.png border=0>"
156         : "<img src=/images/up.png border=0>";
157     } elseif ($sortBy == "Total") {
158       $totalDirection = ($direction == "ascending") 
159         ? "<img src=/images/down.png border=0>"
160         : "<img src=/images/up.png border=0>";
161     } elseif ($sortBy == "AvgRunTime") {
162       $avgRunTimeDirection = ($direction == "ascending") 
163         ? "<img src=/images/down.png border=0>"
164         : "<img src=/images/up.png border=0>";
165     } else {
166       $testcaseDirection = ($direction == "ascending") 
167         ? "<img src=/images/down.png border=0>"
168         : "<img src=/images/up.png border=0>";
169     } // if
170       
171     if (isset ($message)) {
172       $page .= "<div align=center>$message</div>";
173     } // if
174   } // if
175
176   $page .= <<<END
177 <table align=center>
178   <thead>
179 END;
180
181   if (!$forEmail) {
182     if (isset ($testcase)) {
183       $page .= <<<END
184     <tr>
185       <th class=clear align="left" colspan="2"><a href="$script?action=Export&testcase=$testcase"><input type="submit" value="Export to CSV"></a></th>
186       <th class=clear align="right" colspan="7"><form action="$script?action=Mail&testcase=$testcase" method="post">
187 END;
188     } else {
189       $page .= <<<END
190     <tr>
191       <th class=clear align="left" colspan="2"><a href="$script?action=Export&testcase=$testcase"><input type="submit" value="Export to CSV"></a></th>
192       <th class=clear align="right" colspan="4"><form action="$script?action=Mail&testcase=$testcase" method="post">
193 END;
194     } // if
195
196     $page .= emailUsersDropdown ();
197     $page .= <<<END
198         </select>
199         <input type="submit" value="Send"></form>
200       </th>
201     </tr>
202 END;
203   } // if
204
205   if (isset ($testcase)) {
206     $urlParms  = "$script?testcase=$testcase&action=$action&direction=$direction&sortBy";
207     $page .= <<<END
208     <tr>
209       <th class=left>#</th>
210       <th><a href=$urlParms=Testcase>Testcase&nbsp;$testcaseDirection</a></th>
211       <th><a href=$urlParms=DUT>DUT&nbsp;$DUTDirection</a></th>
212       <th><a href=$urlParms=Type>Type&nbsp;$typeDirection</a></th>
213       <th><a href=$urlParms=Start>Start&nbsp;$startDirection</a></th>
214       <th><a href=$urlParms=End>End&nbsp;$endDirection</a></th>
215       <th><a href=$urlParms=Duration>Duration&nbsp;$durationDirection</a></th>
216       <th>Logs</th>
217       <th class=right><a href=$urlParms=Status>Status&nbsp;$statusDirection</a></th> 
218 END;
219   } else {
220     $urlParms  = "$script?build=$build&level=$level&DUT=$DUT&test=$test&action=$action&direction=$direction&sortBy";
221     $page .= <<<END
222     <tr>
223       <th class=left>#</th>
224       <th><a href=$urlParms=Testcase>Testcase&nbsp;$testcaseDirection</a></th>
225       <th><a href=$urlParms=Passed>Passed&nbsp;$passedDirection</a></th>
226       <th><a href=$urlParms=Failed>Failed&nbsp;$failedDirection</a></th>
227       <th><a href=$urlParms=Total>Total&nbsp;$totalDirection</a></th>
228       <th class=right><a href=$urlParms=AvgRunTime>Avg Run Time&nbsp;$avgRunTimeDirection</a></th>
229 END;
230   } // if
231
232   $page .= <<<END
233     </tr>
234   </thead>
235   <tbody>
236 END;
237
238   $total_passed =
239   $total_failed =
240   $total_total  = 0;
241
242   foreach ($data as $line) {
243     $row_nbr++;
244
245     if (isset ($testcase)) {
246       $class    = SetRowColor ($line["Status"]);
247       $status   = colorResult ($line["Status"]);
248       $date     = YMD2MDY (substr ($line["Start"], 0, 10));
249       $duration = FormatDuration ($line["Duration"]);
250       $logs     = logs ($line["_eastlogs"]);
251
252       $page .= <<<END
253       <tr $class>
254         <td align=center>$row_nbr</td>
255         <td><a href="$webdir/rantest.php?testName=$line[Testcase]&runid=$line[_runid]&date=$date">$line[Testcase]</a></td>
256         <td align=center>$line[DUT]</td>
257         <td align=center>$line[Type]</td>
258         <td align=center>$line[Start]</td>
259         <td align=center>$line[End]</td>
260         <td>$duration</td>
261         <td align=center>$logs</td>
262         <td>$status</td>
263       </tr>
264 END;
265     } else {
266       $row_color = ($row_nbr % 2 == 0) ? " class=other" : " class=white";
267
268       $page .= <<<END
269         <tr $row_color>
270           <td align=center>$row_nbr</td>
271           <td><a href="$script?testcase=$line[Testcase]">$line[Testcase]</a></td>
272           <td align=right>$line[Passed]</td>
273           <td align=right>$line[Failed]</td>
274           <td align=right>$line[Total]</td>
275           <td align=right>
276 END;
277       $page .= FormatDuration ($line[AvgRunTime]);
278       $page .= "</td></tr>";
279       $total_passed     += $line["Passed"];
280       $total_failed     += $line["Failed"];
281       $total_total      += $line["Total"];
282     } // if
283   } // foreach  
284
285   // Print total
286   if (empty ($testcase)) {
287     $page .= <<<END
288   <tfoot>
289     <tr $row_color>
290       <th colspan=2>Total</th>
291       <th align=right>$total_passed</th>
292       <th align=right>$total_failed</th>
293       <th align=right>$total_total</th>
294       <th>&nbsp;</th>
295     </tr>
296   </tfoot>
297 END;
298   } // if
299
300   $page .= <<<END
301   </tbody>
302 </table>
303 END;
304
305   return $page;
306 } // createPage
307
308 function exportTestHistoryCSV ($testcase) {
309   global $historyFor;
310
311   if (isset ($testcase)) {
312     $title      = "Test History for $historyFor";
313     $filename   = "Test History." . $testcase . ".csv";
314   } else {
315     $title      = "Test History for All Tests";
316     $filename   = "Test History.All Tests.csv";
317   } // if
318
319   // Protect $filename from wildcards
320   $filename = preg_replace ("/\*/", "%", $filename);
321
322   header ("Content-Type: application/octect-stream");
323   header ("Content-Disposition: attachment; filename=\"$filename\"");
324
325   print exportCSV (getData ($testcase), $title);
326
327   exit;
328 } // exportTestHistoryCSV
329
330 function setTestcase () {
331   global $testcase, $build, $level, $DUT, $test;
332
333   if (empty ($testcase)) {
334     if (empty ($test)) {
335       $test = "*";
336     } // if
337
338     if ($build == "*" and
339         $level == "*" and
340         $DUT   == "*" and
341         $test  == "*") {
342       unset ($testcase);
343       return "All Tests";
344     } else {
345       $testcase  = "${build}_${level}_${DUT}_${test}";
346     } // if
347   } // if
348
349   return $testcase;
350 } // setTestcase
351
352 function displayReport ($testcase, $message = "") {
353   print createHeader ();
354   print createPage   ($testcase, false, $message);
355
356   copyright ();
357 } // displayReport
358
359 function mailTestHistoryReport ($testcase, $pnbr, $username) {
360   global $historyFor;
361
362   if (isset ($testcase)) {
363     $subject    = "Test History for $historyFor";
364     $filename   = "Test History." . $testcase . ".csv";
365   } else {
366     $subject    = "Test History for All Tests";
367     $filename   = "Test History.All Tests.csv";
368   } // if
369
370   // Protect $filename from wildcards
371   $filename = preg_replace ("/\*/", "%", $filename);
372
373   $body         = createPage ($testcase, true);
374   $attachment   = exportCSV (getData ($testcase, true), $subject);
375
376   return mailReport ($pnbr, $username, $subject, $body, $filename, $attachment);
377 } // mailTestHistoryReport
378
379 openDB ();
380
381 $historyFor = setTestcase ();
382
383 switch ($action) {
384   case "Export":
385     exportTestHistoryCSV ($testcase);
386     break;
387
388    case "Mail":
389      list ($pnbr, $username) = explode (":", $user);
390      displayReport ($testcase, mailTestHistoryReport ($testcase, $pnbr, $username));
391      break;
392
393   default:
394     displayReport ($testcase);
395     break;
396 } // switch  
397 ?>
398 </body>
399 </html>