Merge database-common.js and utility.js into run-tests.js.
[WebKit-https.git] / Websites / perf.webkit.org / public / admin / tests.php
1 <?php
2
3 require_once('../include/admin-header.php');
4 require_once('../include/test-name-resolver.php');
5
6 if ($action == 'dashboard') {
7     if (array_key_exists('metric_id', $_POST)) {
8         $metric_id = intval($_POST['metric_id']);
9         $config_ids = array();
10         $succeeded = TRUE;
11         if (!$db->query_and_get_affected_rows("UPDATE test_configurations SET config_is_in_dashboard = false WHERE config_metric = $1", array($metric_id))) {
12             $succeeded = FALSE;
13             notice("Failed to remove some configurations from the dashboard.");
14         }
15
16         else if (array_key_exists('metric_platforms', $_POST)) {
17             foreach ($_POST['metric_platforms'] as $platform_id) {
18                 if (!$db->query_and_get_affected_rows("UPDATE test_configurations SET config_is_in_dashboard = true
19                     WHERE config_metric = $1 AND config_platform = $2", array($metric_id, $platform_id))) {
20                     $succeeded = FALSE;
21                     notice("Failed to add configurations for metric $metric_id and platform $platform_id to the dashboard.");
22                 }
23             }
24         }
25
26         if ($succeeded) {
27             notice("Updated the dashboard.");
28             regenerate_manifest();
29         }
30     } else
31         notice('Invalid parameters');
32 } else if ($action == 'update') {
33     if (!update_field('tests', 'test', 'url'))
34         notice('Invalid parameters');
35 } else if ($action == 'add') {
36     if (array_key_exists('test_id', $_POST) && array_key_exists('metric_name', $_POST)) {
37         $id = intval($_POST['test_id']);
38         $aggregator = intval($_POST['metric_aggregator']);
39         if (!$aggregator)
40             notice('Invalid aggregator. You must specify one.');
41         else {
42             $metric_id = $db->insert_row('test_metrics', 'metric',
43                 array('test' => $id, 'name' => $_POST['metric_name'], 'aggregator' => $aggregator));
44             if (!$metric_id)
45                 notice("Could not insert the new metric for test $id");
46             else {
47                 add_job('aggregate', '{"metricIds": [ ' . $metric_id . ']}');
48                 notice("Inserted the metric for test $id");
49             }
50         }
51     } else if (array_key_exists('metric_id', $_POST))
52         regenerate_manifest();
53     else
54         notice('Invalid parameters');
55 }
56
57 if ($db) {
58     $aggregators = array();
59     if ($aggregators_table = $db->fetch_table('aggregators')) {
60         foreach ($aggregators_table as $aggregator_row)
61             $aggregators[$aggregator_row['aggregator_id']] = $aggregator_row['aggregator_name'];
62     }
63
64     $test_name_resolver = new TestNameResolver($db);
65     if ($test_name_resolver->tests()) {
66         $name_to_platform = array();
67
68         foreach ($db->fetch_table('platforms') as $platform)
69             $name_to_platform[$platform['platform_name']] = $platform;
70
71         $platform_names = array_keys($name_to_platform);
72         asort($platform_names);
73
74         $odd = false;
75         $selected_parent_full_name = trim(array_get($_SERVER, 'PATH_INFO', ''), '/');
76         $selected_parent = $test_name_resolver->test_id_for_full_name($selected_parent_full_name);
77         if ($selected_parent)
78             echo '<h2>' . htmlspecialchars($selected_parent_full_name) . '</h2>';
79
80 ?>
81 <table>
82 <thead>
83     <tr><td>Test ID</td><td>Full Name</td><td>Parent ID</td><td>URL</td>
84         <td>Metric ID</td><td>Metric Name</td><td>Aggregator</td><td>Dashboard</td>
85 </thead>
86 <tbody>
87 <?php
88
89         foreach ($test_name_resolver->tests() as $test) {
90             if ($test['test_parent'] != $selected_parent['test_id'])
91                 continue;
92
93             $test_id = $test['test_id'];
94             $test_metrics = $test_name_resolver->metrics_for_test_id($test_id);
95             $row_count = count($test_metrics);
96             $child_metrics = $test_name_resolver->child_metrics_for_test_id($test_id);
97             $linked_test_name = htmlspecialchars($test['full_name']);
98             if ($child_metrics) {
99                 $row_count++;
100                 $linked_test_name = '<a href="/admin/tests/' . htmlspecialchars($test['full_name']) . '">' . $linked_test_name . '</a>';
101             }
102
103             $tbody_class = $odd ? 'odd' : 'even';
104             $odd = !$odd;
105
106             $test_url = htmlspecialchars($test['test_url']);
107
108             echo <<<EOF
109     <tbody class="$tbody_class">
110     <tr>
111         <td rowspan="$row_count">$test_id</td>
112         <td rowspan="$row_count">$linked_test_name</td>
113         <td rowspan="$row_count">{$test['test_parent']}</td>
114         <td rowspan="$row_count">
115         <form method="POST"><input type="hidden" name="id" value="$test_id">
116         <input type="hidden" name="action" value="update">
117         <input type="url" name="url" value="$test_url" size="80"></form></td>
118 EOF;
119
120             if ($test_metrics) {
121                 $has_tr = true;
122                 foreach ($test_metrics as $metric) {
123                     $aggregator_name = array_get($aggregators, $metric['metric_aggregator'], '');
124                     if ($aggregator_name) {
125                         $aggregator_action = '<form method="POST"><input type="hidden" name="metric_id" value="'. $metric['metric_id']
126                             . '"><button type="submit" name="action" value="add">Regenerate</button></form>';
127                     } else
128                         $aggregator_action = '';
129
130                     if (!$has_tr)
131                         echo <<<EOF
132
133     <tr>
134 EOF;
135                     $has_tr = false;
136
137                     $metric_id = $metric['metric_id'];
138                     echo <<<EOF
139         <td><a href="/admin/test-configurations?metric=$metric_id">$metric_id</a></td>
140         <td>{$metric['metric_name']}</td>
141         <td>$aggregator_name $aggregator_action</td>
142         <td><form method="POST"><input type="hidden" name="metric_id" value="$metric_id">
143 EOF;
144
145                     foreach ($platform_names as $platform_name) {
146                         $platform_name = htmlspecialchars($platform_name);
147                         $platform = $name_to_platform[$platform_name];
148                         $configurations = $test_name_resolver->configurations_for_metric_and_platform($metric_id, $platform['platform_id']);
149                         if (!$configurations)
150                             continue;
151                         echo "<label><input type=\"checkbox\" name=\"metric_platforms[]\" value=\"{$platform['platform_id']}\"";
152                         if ($db->is_true($configurations[0]['config_is_in_dashboard']))
153                             echo ' checked';
154                         else if ($db->is_true($platform['platform_hidden']))
155                             echo 'disabled';
156                         echo ">$platform_name</label>";
157                     }
158
159                     echo <<<EOF
160         <button type="submit" name="action" value="dashboard">Save</button></form>
161         </td>
162     </tr>
163 EOF;
164                 }
165             }
166
167             if ($child_metrics) {
168                 echo <<<EOF
169         <td colspan="5"><form method="POST">
170         <input type="hidden" name="test_id" value="$test_id">
171         <label>Name<select name="metric_name">
172 EOF;
173
174                 foreach ($child_metrics as $metric_name) {
175                     $metric_name = htmlspecialchars($metric_name);
176                     echo "
177             <option>$metric_name</option>";
178                 }
179
180                 echo <<<EOF
181         </select></label>
182         <label>Aggregator
183         <select name="metric_aggregator">
184             <option value="">-</option>
185 EOF;
186             foreach ($aggregators as $id => $name) {
187                 $name = htmlspecialchars($name);
188                 echo "
189             <option value=\"$id\">$name</option>";
190             }
191             echo <<<EOF
192         </select></label>
193         <button type="submit" name="action" value="add">Add</button></form></td>
194     </tr>
195 EOF;
196             }
197             echo <<<EOF
198     </tbody>
199 EOF;
200         }
201
202         ?></tbody>
203 </table>
204
205 <?php
206
207     }
208
209 }
210
211 include('../include/admin-footer.php');
212
213 ?>