Unreviewed typo fix. The prefix in triggerable_configurations is "trigconfig", not...
[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 == 'update-triggerable') {
36     $test_id = intval($_POST['test']);
37     $platform_id = intval($_POST['platform']);
38
39     $triggerable_id = array_get($_POST, 'triggerable');
40     if ($triggerable_id) {
41         $triggerable_id = intval($triggerable_id);
42         $association = array('test' => $test_id, 'platform' => $platform_id, 'triggerable' => $triggerable_id);
43         if (!$db->insert_row('triggerable_configurations', 'trigconfig', $association, NULL)) {
44             $suceeded = FALSE;
45             notice("Failed to associate triggerable $triggerable_id with test $test_id on platform $platform_id.");
46         }
47     } else {
48         $db->query_and_get_affected_rows('DELETE FROM triggerable_configurations WHERE trigconfig_test = $1 AND trigconfig_platform = $2',
49             array($test_id, $platform_id));
50     }
51 } else if ($action == 'add') {
52     if (array_key_exists('test_id', $_POST) && array_key_exists('metric_name', $_POST)) {
53         $id = intval($_POST['test_id']);
54         $aggregator = intval($_POST['metric_aggregator']);
55         if (!$aggregator)
56             notice('Invalid aggregator. You must specify one.');
57         else {
58             $metric_id = $db->insert_row('test_metrics', 'metric',
59                 array('test' => $id, 'name' => $_POST['metric_name'], 'aggregator' => $aggregator));
60             if (!$metric_id)
61                 notice("Could not insert the new metric for test $id");
62             else
63                 notice("Inserted the metric for test $id. Aggregation for $metric_id is needed.");
64         }
65     } else if (array_key_exists('metric_id', $_POST))
66         regenerate_manifest();
67     else
68         notice('Invalid parameters');
69 }
70
71 if ($db) {
72     $aggregators = array();
73     if ($aggregators_table = $db->fetch_table('aggregators')) {
74         foreach ($aggregators_table as $aggregator_row)
75             $aggregators[$aggregator_row['aggregator_id']] = $aggregator_row['aggregator_name'];
76     }
77     $build_triggerable_table = $db->fetch_table('build_triggerables', 'triggerable_name') or array();
78
79     $test_name_resolver = new TestNameResolver($db);
80     if ($test_name_resolver->tests()) {
81         $name_to_platform = array();
82
83         foreach ($db->fetch_table('platforms') as $platform)
84             $name_to_platform[$platform['platform_name']] = $platform;
85
86         $platform_names = array_keys($name_to_platform);
87         asort($platform_names);
88
89         $odd = false;
90         $selected_parent_full_name = trim(array_get($_SERVER, 'PATH_INFO', ''), '/');
91         $selected_parent = $test_name_resolver->test_id_for_full_name($selected_parent_full_name);
92         if ($selected_parent)
93             echo '<h2>' . htmlspecialchars($selected_parent_full_name) . '</h2>';
94
95 ?>
96 <table>
97 <thead>
98     <tr><td>Test ID</td><td>Full Name</td><td>Parent ID</td><td>URL</td><td>Triggerables</td>
99         <td>Metric ID</td><td>Metric Name</td><td>Aggregator</td><td>Dashboard</td>
100 </thead>
101 <tbody>
102 <?php
103
104         foreach ($test_name_resolver->tests() as $test) {
105             if ($test['test_parent'] != $selected_parent['test_id'])
106                 continue;
107
108             $test_id = $test['test_id'];
109             $test_metrics = $test_name_resolver->metrics_for_test_id($test_id);
110             $row_count = count($test_metrics);
111             $child_metrics = $test_name_resolver->child_metrics_for_test_id($test_id);
112             $linked_test_name = htmlspecialchars($test['full_name']);
113             if ($child_metrics) {
114                 $row_count++;
115                 $linked_test_name = '<a href="/admin/tests/' . htmlspecialchars($test['full_name']) . '">' . $linked_test_name . '</a>';
116             }
117
118             $tbody_class = $odd ? 'odd' : 'even';
119             $odd = !$odd;
120
121             $test_url = htmlspecialchars($test['test_url']);
122
123             $triggerable_platforms = $db->query_and_fetch_all('SELECT * FROM platforms
124                 LEFT OUTER JOIN triggerable_configurations ON trigconfig_platform = platform_id AND trigconfig_test = $1 ORDER BY platform_name', array($test_id));
125             $triggerables = '';
126             foreach ($triggerable_platforms as $platform_row) {
127                 if (!$test_name_resolver->test_exists_on_platform($test_id, $platform_row['platform_id']))
128                     continue;
129
130                 $triggerables .= <<< END
131 <form method="POST">
132     <input type="hidden" name="test" value="$test_id">
133     <input type="hidden" name="platform" value="{$platform_row['platform_id']}">
134     <input type="hidden" name="action" value="update-triggerable">
135     <label>
136         {$platform_row['platform_name']}
137         <select name="triggerable" onchange="this.form.submit();">
138             <option value="">None</option>
139 END;
140                 $selected_triggerable = array_get($platform_row, 'trigconfig_triggerable');
141                 foreach ($build_triggerable_table as $triggerable_row) {
142                     $triggerable_id = $triggerable_row['triggerable_id'];
143                     $selected = $triggerable_id == $selected_triggerable ? ' selected' : '';
144                     $triggerables .= "<option value=\"$triggerable_id\"$selected>{$triggerable_row['triggerable_name']}</option>";
145                 }
146                 $triggerables .= <<< END
147         </select>
148     </label>
149 </form>
150 <br>
151 END;
152             }
153
154             echo <<<EOF
155     <tbody class="$tbody_class">
156     <tr>
157         <td rowspan="$row_count">$test_id</td>
158         <td rowspan="$row_count">$linked_test_name</td>
159         <td rowspan="$row_count">{$test['test_parent']}</td>
160         <td rowspan="$row_count">
161         <form method="POST"><input type="hidden" name="id" value="$test_id">
162         <input type="hidden" name="action" value="update">
163         <input type="url" name="url" value="$test_url" size="30"></form></td>
164         <td rowspan="$row_count">$triggerables</td>
165 EOF;
166
167             if ($test_metrics) {
168                 $has_tr = true;
169                 foreach ($test_metrics as $metric) {
170                     $aggregator_name = array_get($aggregators, $metric['metric_aggregator'], '');
171                     if ($aggregator_name) {
172                         $aggregator_action = '<form method="POST"><input type="hidden" name="metric_id" value="'. $metric['metric_id']
173                             . '"><button type="submit" name="action" value="add">Regenerate</button></form>';
174                     } else
175                         $aggregator_action = '';
176
177                     if (!$has_tr)
178                         echo <<<EOF
179
180     <tr>
181 EOF;
182                     $has_tr = false;
183
184                     $metric_id = $metric['metric_id'];
185                     echo <<<EOF
186         <td><a href="/admin/test-configurations?metric=$metric_id">$metric_id</a></td>
187         <td>{$metric['metric_name']}</td>
188         <td>$aggregator_name $aggregator_action</td>
189         <td><form method="POST"><input type="hidden" name="metric_id" value="$metric_id">
190 EOF;
191
192                     foreach ($platform_names as $platform_name) {
193                         $platform_name = htmlspecialchars($platform_name);
194                         $platform = $name_to_platform[$platform_name];
195                         $configurations = $test_name_resolver->configurations_for_metric_and_platform($metric_id, $platform['platform_id']);
196                         if (!$configurations)
197                             continue;
198                         echo "<label><input type=\"checkbox\" name=\"metric_platforms[]\" value=\"{$platform['platform_id']}\"";
199                         if ($db->is_true($configurations[0]['config_is_in_dashboard']))
200                             echo ' checked';
201                         else if ($db->is_true($platform['platform_hidden']))
202                             echo 'disabled';
203                         echo ">$platform_name</label>";
204                     }
205
206                     echo <<<EOF
207         <button type="submit" name="action" value="dashboard">Save</button></form>
208         </td>
209     </tr>
210 EOF;
211                 }
212             }
213
214             if ($child_metrics) {
215                 echo <<<EOF
216         <td colspan="5"><form method="POST">
217         <input type="hidden" name="test_id" value="$test_id">
218         <label>Name<select name="metric_name">
219 EOF;
220
221                 foreach ($child_metrics as $metric) {
222                     $metric_name = htmlspecialchars($metric['metric_name']);
223                     echo "
224             <option>$metric_name</option>";
225                 }
226
227                 echo <<<EOF
228         </select></label>
229         <label>Aggregator
230         <select name="metric_aggregator">
231             <option value="">-</option>
232 EOF;
233             foreach ($aggregators as $id => $name) {
234                 $name = htmlspecialchars($name);
235                 echo "
236             <option value=\"$id\">$name</option>";
237             }
238             echo <<<EOF
239         </select></label>
240         <button type="submit" name="action" value="add">Add</button></form></td>
241     </tr>
242 EOF;
243             }
244             echo <<<EOF
245     </tbody>
246 EOF;
247         }
248
249         ?></tbody>
250 </table>
251
252 <?php
253
254     }
255
256 }
257
258 include('../include/admin-footer.php');
259
260 ?>