Merge database-common.js and utility.js into run-tests.js.
[WebKit-https.git] / Websites / perf.webkit.org / public / include / test-name-resolver.php
1 <?php
2
3 class TestNameResolver {
4
5     function __construct($db) {
6         $this->db = $db;
7         $this->full_name_to_test = array();
8         $this->test_id_to_child_metrics = array();
9         $this->test_to_metrics = array();
10         $this->tests_sorted_by_full_name = array();
11         $this->metric_to_configurations = array();
12         $this->id_to_metric = array();
13         $this->id_to_aggregator = array();
14
15         $test_table = $db->fetch_table('tests');
16         if (!$test_table)
17             return;
18
19         $test_id_to_name = array();
20         $test_id_to_parent = array();
21         foreach ($test_table as $test_row) {
22             $test_id = $test_row['test_id'];
23             $test_id_to_name[$test_id] = $test_row['test_name'];
24             $test_id_to_parent[$test_id] = $test_row['test_parent'];
25         }
26
27         $this->full_name_to_test = $this->compute_full_name($test_table, $test_id_to_name, $test_id_to_parent);
28         $this->test_to_metrics = $this->map_metrics_to_tests($db->fetch_table('test_metrics'), $test_id_to_parent);
29         $this->tests_sorted_by_full_name = $this->sort_tests_by_full_name($this->full_name_to_test);
30
31         if ($configurations = $db->fetch_table('test_configurations')) {
32             foreach ($configurations as $config) {
33                 $metric_id = $config['config_metric'];
34                 $platform_id = $config['config_platform'];
35                 array_set_default($this->metric_to_configurations, $metric_id, array());
36                 array_set_default($this->metric_to_configurations[$metric_id], $platform_id, array());
37                 array_push($this->metric_to_configurations[$metric_id][$platform_id], $config);
38             }
39         }
40
41         if ($aggregator_table = $db->fetch_table('aggregators')) {
42             foreach ($aggregator_table as $aggregator)
43                 $this->id_to_aggregator[$aggregator['aggregator_id']] = $aggregator;
44         }
45     }
46
47     private function compute_full_name($test_table, $test_id_to_name, $test_id_to_parent) {
48         $full_name_to_test = array();
49         foreach ($test_table as $test_row) {
50             $test_path = array();
51             $test_id = $test_row['test_id'];
52             do {
53                 array_push($test_path, $test_id_to_name[$test_id]);
54                 $test_id = $test_id_to_parent[$test_id];
55             } while ($test_id);
56             $test_row['full_name'] = join('/', array_reverse($test_path));
57             $full_name_to_test[$test_row['full_name']] = $test_row;
58         }
59         return $full_name_to_test;
60     }
61
62     private function map_metrics_to_tests($metrics_table, $test_id_to_parent) {
63         $test_to_metrics = array();
64         if (!$metrics_table)
65             return $test_to_metrics;
66
67         foreach ($metrics_table as $metric_row) {
68             $this->id_to_metric[$metric_row['metric_id']] = $metric_row;
69
70             $test_id = $metric_row['metric_test'];
71             array_set_default($test_to_metrics, $test_id, array());
72             array_push($test_to_metrics[$test_id], $metric_row);
73
74             $parent_id = $test_id_to_parent[$test_id];
75             if ($parent_id) {
76                 array_set_default($this->test_id_to_child_metrics, $parent_id, array());
77                 $parent_metrics = &$this->test_id_to_child_metrics[$parent_id];
78                 if (!in_array($metric_row['metric_name'], $parent_metrics))
79                     array_push($parent_metrics, $metric_row['metric_name']);
80             }
81         }
82         return $test_to_metrics;
83     }
84
85     private function sort_tests_by_full_name($full_name_to_test) {
86         $tests_sorted_by_full_name = array();
87         $full_names = array_keys($full_name_to_test);
88         asort($full_names);
89         foreach ($full_names as $name)
90             array_push($tests_sorted_by_full_name, $full_name_to_test[$name]);
91         return $tests_sorted_by_full_name;
92     }
93
94     function tests() {
95         return $this->tests_sorted_by_full_name;
96     }
97
98     function test_id_for_full_name($full_name) {
99         return array_get($this->full_name_to_test, $full_name);
100     }
101
102     function full_name_for_test($test_id) {
103         foreach ($this->full_name_to_test as $full_name => $test) {
104             if ($test['test_id'] == $test_id)
105                 return $full_name;
106         }
107         return NULL;
108     }
109
110     function full_name_for_metric($metric_id) {
111         $metric_row = array_get($this->id_to_metric, $metric_id);
112         if (!$metric_row)
113             return NULL;
114         $full_name = $this->full_name_for_test($metric_row['metric_test']) . ':' . $metric_row['metric_name'];
115         if ($aggregator_id = $metric_row['metric_aggregator'])
116             $full_name .= ':' . $this->id_to_aggregator[$aggregator_id]['aggregator_name'];
117         return $full_name;
118     }
119
120     function metrics_for_test_id($test_id) {
121         return array_get($this->test_to_metrics, $test_id, array());
122     }
123
124     function child_metrics_for_test_id($test_id) {
125         return array_get($this->test_id_to_child_metrics, $test_id, array());
126     }
127
128     function configurations_for_metric_and_platform($metric_id, $platform_id) {
129         $metric_configurations = array_get($this->metric_to_configurations, $metric_id, array());
130         return array_get($metric_configurations, $platform_id);
131     }
132 }
133
134 ?>