showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 1 | from django.db import models as dbmodels, connection |
| 2 | from django.utils import datastructures |
| 3 | from autotest_lib.frontend.afe import model_logic, readonly_connection |
| 4 | |
| 5 | class TempManager(model_logic.ExtendedManager): |
| 6 | _GROUP_COUNT_NAME = 'group_count' |
| 7 | |
| 8 | def _get_key_unless_is_function(self, field): |
| 9 | if '(' in field: |
| 10 | return field |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 11 | return self.get_key_on_this_table(field) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 12 | |
| 13 | |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 14 | def _get_field_names(self, fields, extra_select_fields={}): |
| 15 | field_names = [] |
| 16 | for field in fields: |
| 17 | if field in extra_select_fields: |
| 18 | field_names.append(field) |
| 19 | else: |
| 20 | field_names.append(self._get_key_unless_is_function(field)) |
| 21 | return field_names |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 22 | |
| 23 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 24 | def _get_group_query_sql(self, query, group_by): |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 25 | sql, params = query.query.as_sql() |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 26 | |
| 27 | # insert GROUP BY clause into query |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 28 | group_fields = self._get_field_names(group_by, query.query.extra_select) |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 29 | group_by_clause = ' GROUP BY ' + ', '.join(group_fields) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 30 | group_by_position = sql.rfind('ORDER BY') |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 31 | if group_by_position == -1: |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 32 | group_by_position = len(sql) |
| 33 | sql = (sql[:group_by_position] + |
| 34 | group_by_clause + ' ' + |
| 35 | sql[group_by_position:]) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 36 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 37 | return sql, params |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 38 | |
| 39 | |
showard | 06b82fc | 2009-06-30 01:59:42 +0000 | [diff] [blame] | 40 | def _get_column_names(self, cursor): |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 41 | """ |
showard | 06b82fc | 2009-06-30 01:59:42 +0000 | [diff] [blame] | 42 | Gets the column names from the cursor description. This method exists |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 43 | so that it can be mocked in the unit test for sqlite3 compatibility. |
showard | 06b82fc | 2009-06-30 01:59:42 +0000 | [diff] [blame] | 44 | """ |
| 45 | return [column_info[0] for column_info in cursor.description] |
| 46 | |
| 47 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 48 | def execute_group_query(self, query, group_by): |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 49 | """ |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 50 | Performs the given query grouped by the fields in group_by with the |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 51 | given query's extra select fields added. Returns a list of dicts, where |
| 52 | each dict corresponds to single row and contains a key for each grouped |
| 53 | field as well as all of the extra select fields. |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 54 | """ |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 55 | sql, params = self._get_group_query_sql(query, group_by) |
showard | 56e9377 | 2008-10-06 10:06:22 +0000 | [diff] [blame] | 56 | cursor = readonly_connection.connection().cursor() |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 57 | cursor.execute(sql, params) |
showard | 06b82fc | 2009-06-30 01:59:42 +0000 | [diff] [blame] | 58 | field_names = self._get_column_names(cursor) |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 59 | row_dicts = [dict(zip(field_names, row)) for row in cursor.fetchall()] |
| 60 | return row_dicts |
| 61 | |
| 62 | |
| 63 | def get_count_sql(self, query): |
| 64 | """ |
| 65 | Get the SQL to properly select a per-group count of unique matches for |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 66 | a grouped query. Returns a tuple (field alias, field SQL) |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 67 | """ |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 68 | if query.query.distinct: |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 69 | pk_field = self.get_key_on_this_table() |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 70 | count_sql = 'COUNT(DISTINCT %s)' % pk_field |
| 71 | else: |
| 72 | count_sql = 'COUNT(1)' |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 73 | return self._GROUP_COUNT_NAME, count_sql |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | def _get_num_groups_sql(self, query, group_by): |
| 77 | group_fields = self._get_field_names(group_by) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 78 | query = query.order_by() # this can mess up the query and isn't needed |
| 79 | |
| 80 | sql, params = query.query.as_sql() |
| 81 | from_ = sql[sql.find(' FROM'):] |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 82 | return ('SELECT COUNT(DISTINCT %s) %s' % (','.join(group_fields), |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 83 | from_), |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 84 | params) |
| 85 | |
| 86 | |
| 87 | def get_num_groups(self, query, group_by): |
| 88 | """ |
| 89 | Returns the number of distinct groups for the given query grouped by the |
| 90 | fields in group_by. |
| 91 | """ |
| 92 | sql, params = self._get_num_groups_sql(query, group_by) |
showard | 56e9377 | 2008-10-06 10:06:22 +0000 | [diff] [blame] | 93 | cursor = readonly_connection.connection().cursor() |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 94 | cursor.execute(sql, params) |
| 95 | return cursor.fetchone()[0] |
| 96 | |
| 97 | |
| 98 | class Machine(dbmodels.Model): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 99 | machine_idx = dbmodels.AutoField(primary_key=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 100 | hostname = dbmodels.CharField(unique=True, max_length=300) |
| 101 | machine_group = dbmodels.CharField(blank=True, max_length=240) |
| 102 | owner = dbmodels.CharField(blank=True, max_length=240) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 103 | |
| 104 | class Meta: |
| 105 | db_table = 'machines' |
| 106 | |
| 107 | |
| 108 | class Kernel(dbmodels.Model): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 109 | kernel_idx = dbmodels.AutoField(primary_key=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 110 | kernel_hash = dbmodels.CharField(max_length=105, editable=False) |
| 111 | base = dbmodels.CharField(max_length=90) |
| 112 | printable = dbmodels.CharField(max_length=300) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 113 | |
| 114 | class Meta: |
| 115 | db_table = 'kernels' |
| 116 | |
| 117 | |
| 118 | class Patch(dbmodels.Model): |
| 119 | kernel = dbmodels.ForeignKey(Kernel, db_column='kernel_idx') |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 120 | name = dbmodels.CharField(blank=True, max_length=240) |
| 121 | url = dbmodels.CharField(blank=True, max_length=900) |
| 122 | the_hash = dbmodels.CharField(blank=True, max_length=105, db_column='hash') |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 123 | |
| 124 | class Meta: |
| 125 | db_table = 'patches' |
| 126 | |
| 127 | |
| 128 | class Status(dbmodels.Model): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 129 | status_idx = dbmodels.AutoField(primary_key=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 130 | word = dbmodels.CharField(max_length=30) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 131 | |
| 132 | class Meta: |
| 133 | db_table = 'status' |
| 134 | |
| 135 | |
| 136 | class Job(dbmodels.Model): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 137 | job_idx = dbmodels.AutoField(primary_key=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 138 | tag = dbmodels.CharField(unique=True, max_length=300) |
| 139 | label = dbmodels.CharField(max_length=300) |
| 140 | username = dbmodels.CharField(max_length=240) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 141 | machine = dbmodels.ForeignKey(Machine, db_column='machine_idx') |
| 142 | queued_time = dbmodels.DateTimeField(null=True, blank=True) |
| 143 | started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 144 | finished_time = dbmodels.DateTimeField(null=True, blank=True) |
showard | c1c1caf | 2009-09-08 16:26:50 +0000 | [diff] [blame] | 145 | afe_job_id = dbmodels.IntegerField(null=True, default=None) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 146 | |
| 147 | class Meta: |
| 148 | db_table = 'jobs' |
| 149 | |
| 150 | |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 151 | class Test(dbmodels.Model, model_logic.ModelExtensions, |
| 152 | model_logic.ModelWithAttributes): |
| 153 | test_idx = dbmodels.AutoField(primary_key=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 154 | job = dbmodels.ForeignKey(Job, db_column='job_idx') |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 155 | test = dbmodels.CharField(max_length=90) |
| 156 | subdir = dbmodels.CharField(blank=True, max_length=180) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 157 | kernel = dbmodels.ForeignKey(Kernel, db_column='kernel_idx') |
| 158 | status = dbmodels.ForeignKey(Status, db_column='status') |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 159 | reason = dbmodels.CharField(blank=True, max_length=3072) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 160 | machine = dbmodels.ForeignKey(Machine, db_column='machine_idx') |
| 161 | finished_time = dbmodels.DateTimeField(null=True, blank=True) |
| 162 | started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 163 | |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 164 | objects = model_logic.ExtendedManager() |
| 165 | |
| 166 | def _get_attribute_model_and_args(self, attribute): |
| 167 | return TestAttribute, dict(test=self, attribute=attribute, |
| 168 | user_created=True) |
| 169 | |
| 170 | |
| 171 | def set_attribute(self, attribute, value): |
| 172 | # ensure non-user-created attributes remain immutable |
| 173 | try: |
| 174 | TestAttribute.objects.get(test=self, attribute=attribute, |
| 175 | user_created=False) |
| 176 | raise ValueError('Attribute %s already exists for test %s and is ' |
| 177 | 'immutable' % (attribute, self.test_idx)) |
| 178 | except TestAttribute.DoesNotExist: |
| 179 | super(Test, self).set_attribute(attribute, value) |
| 180 | |
| 181 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 182 | class Meta: |
| 183 | db_table = 'tests' |
| 184 | |
| 185 | |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 186 | class TestAttribute(dbmodels.Model, model_logic.ModelExtensions): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 187 | test = dbmodels.ForeignKey(Test, db_column='test_idx') |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 188 | attribute = dbmodels.CharField(max_length=90) |
| 189 | value = dbmodels.CharField(blank=True, max_length=300) |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 190 | user_created = dbmodels.BooleanField(default=False) |
| 191 | |
| 192 | objects = model_logic.ExtendedManager() |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 193 | |
| 194 | class Meta: |
| 195 | db_table = 'test_attributes' |
| 196 | |
| 197 | |
jadmanski | 430dca9 | 2008-12-16 20:56:53 +0000 | [diff] [blame] | 198 | class IterationAttribute(dbmodels.Model, model_logic.ModelExtensions): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 199 | # this isn't really a primary key, but it's necessary to appease Django |
| 200 | # and is harmless as long as we're careful |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 201 | test = dbmodels.ForeignKey(Test, db_column='test_idx', primary_key=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 202 | iteration = dbmodels.IntegerField() |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 203 | attribute = dbmodels.CharField(max_length=90) |
| 204 | value = dbmodels.CharField(blank=True, max_length=300) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 205 | |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 206 | objects = model_logic.ExtendedManager() |
| 207 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 208 | class Meta: |
| 209 | db_table = 'iteration_attributes' |
| 210 | |
| 211 | |
jadmanski | 430dca9 | 2008-12-16 20:56:53 +0000 | [diff] [blame] | 212 | class IterationResult(dbmodels.Model, model_logic.ModelExtensions): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 213 | # see comment on IterationAttribute regarding primary_key=True |
jadmanski | 430dca9 | 2008-12-16 20:56:53 +0000 | [diff] [blame] | 214 | test = dbmodels.ForeignKey(Test, db_column='test_idx', primary_key=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 215 | iteration = dbmodels.IntegerField() |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 216 | attribute = dbmodels.CharField(max_length=90) |
| 217 | value = dbmodels.DecimalField(null=True, max_digits=12, decimal_places=31, |
| 218 | blank=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 219 | |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 220 | objects = model_logic.ExtendedManager() |
| 221 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 222 | class Meta: |
| 223 | db_table = 'iteration_result' |
| 224 | |
| 225 | |
| 226 | class TestLabel(dbmodels.Model, model_logic.ModelExtensions): |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 227 | name = dbmodels.CharField(max_length=80, unique=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 228 | description = dbmodels.TextField(blank=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 229 | tests = dbmodels.ManyToManyField(Test, blank=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 230 | |
| 231 | name_field = 'name' |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 232 | objects = model_logic.ExtendedManager() |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 233 | |
| 234 | class Meta: |
| 235 | db_table = 'test_labels' |
| 236 | |
| 237 | |
| 238 | class SavedQuery(dbmodels.Model, model_logic.ModelExtensions): |
| 239 | # TODO: change this to foreign key once DBs are merged |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 240 | owner = dbmodels.CharField(max_length=80) |
| 241 | name = dbmodels.CharField(max_length=100) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 242 | url_token = dbmodels.TextField() |
| 243 | |
| 244 | class Meta: |
| 245 | db_table = 'saved_queries' |
| 246 | |
| 247 | |
showard | ce12f55 | 2008-09-19 00:48:59 +0000 | [diff] [blame] | 248 | class EmbeddedGraphingQuery(dbmodels.Model, model_logic.ModelExtensions): |
| 249 | url_token = dbmodels.TextField(null=False, blank=False) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 250 | graph_type = dbmodels.CharField(max_length=16, null=False, blank=False) |
showard | ce12f55 | 2008-09-19 00:48:59 +0000 | [diff] [blame] | 251 | params = dbmodels.TextField(null=False, blank=False) |
| 252 | last_updated = dbmodels.DateTimeField(null=False, blank=False, |
| 253 | editable=False) |
| 254 | # refresh_time shows the time at which a thread is updating the cached |
| 255 | # image, or NULL if no one is updating the image. This is used so that only |
| 256 | # one thread is updating the cached image at a time (see |
| 257 | # graphing_utils.handle_plot_request) |
| 258 | refresh_time = dbmodels.DateTimeField(editable=False) |
| 259 | cached_png = dbmodels.TextField(editable=False) |
| 260 | |
| 261 | class Meta: |
| 262 | db_table = 'embedded_graphing_queries' |
| 263 | |
| 264 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 265 | # views |
| 266 | |
| 267 | class TestViewManager(TempManager): |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 268 | def get_query_set(self): |
| 269 | query = super(TestViewManager, self).get_query_set() |
showard | 5bf7c50 | 2008-08-20 01:22:22 +0000 | [diff] [blame] | 270 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 271 | # add extra fields to selects, using the SQL itself as the "alias" |
| 272 | extra_select = dict((sql, sql) |
| 273 | for sql in self.model.extra_fields.iterkeys()) |
| 274 | return query.extra(select=extra_select) |
| 275 | |
| 276 | |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 277 | def _get_include_exclude_suffix(self, exclude): |
| 278 | if exclude: |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 279 | return '_exclude' |
| 280 | return '_include' |
| 281 | |
| 282 | |
| 283 | def _add_attribute_join(self, query_set, join_condition, |
| 284 | suffix=None, exclude=False): |
| 285 | if suffix is None: |
| 286 | suffix = self._get_include_exclude_suffix(exclude) |
| 287 | return self.add_join(query_set, 'test_attributes', join_key='test_idx', |
| 288 | join_condition=join_condition, |
| 289 | suffix=suffix, exclude=exclude) |
| 290 | |
| 291 | |
| 292 | def _add_label_pivot_table_join(self, query_set, suffix, join_condition='', |
| 293 | exclude=False, force_left_join=False): |
| 294 | return self.add_join(query_set, 'test_labels_tests', join_key='test_id', |
| 295 | join_condition=join_condition, |
| 296 | suffix=suffix, exclude=exclude, |
| 297 | force_left_join=force_left_join) |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 298 | |
| 299 | |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 300 | def _add_label_joins(self, query_set, suffix=''): |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 301 | query_set = self._add_label_pivot_table_join( |
| 302 | query_set, suffix=suffix, force_left_join=True) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 303 | |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 304 | # since we're not joining from the original table, we can't use |
| 305 | # self.add_join() again |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 306 | second_join_alias = 'test_labels' + suffix |
| 307 | second_join_condition = ('%s.id = %s.testlabel_id' % |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 308 | (second_join_alias, |
| 309 | 'test_labels_tests' + suffix)) |
| 310 | filter_object = self._CustomSqlQ() |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 311 | filter_object.add_join('test_labels', |
| 312 | second_join_condition, |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 313 | query_set.query.LOUTER, |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 314 | alias=second_join_alias) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 315 | return self._add_customSqlQ(query_set, filter_object) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 316 | |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 317 | |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 318 | def _get_label_ids_from_names(self, label_names): |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 319 | assert label_names |
| 320 | label_ids = list( # listifying avoids a double query below |
| 321 | TestLabel.objects.filter(name__in=label_names).values('id')) |
| 322 | if len(label_ids) < len(set(label_names)): |
| 323 | raise ValueError('Not all labels found: %s' % |
| 324 | ', '.join(label_names)) |
| 325 | return [str(label['id']) for label in label_ids] |
| 326 | |
| 327 | |
| 328 | def _include_or_exclude_labels(self, query_set, label_names, exclude=False): |
| 329 | label_ids = self._get_label_ids_from_names(label_names) |
| 330 | suffix = self._get_include_exclude_suffix(exclude) |
| 331 | condition = ('test_labels_tests%s.testlabel_id IN (%s)' % |
| 332 | (suffix, ','.join(label_ids))) |
| 333 | return self._add_label_pivot_table_join(query_set, |
| 334 | join_condition=condition, |
| 335 | suffix=suffix, |
| 336 | exclude=exclude) |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 337 | |
| 338 | |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 339 | def get_query_set_with_joins(self, filter_data, include_host_labels=False): |
showard | fc8c6ae | 2008-11-11 19:06:01 +0000 | [diff] [blame] | 340 | include_labels = filter_data.pop('include_labels', []) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 341 | exclude_labels = filter_data.pop('exclude_labels', []) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 342 | query_set = self.get_query_set() |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 343 | joined = False |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 344 | |
| 345 | # TODO: make this feature obsolete in favor of include_labels and |
| 346 | # exclude_labels |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 347 | extra_where = filter_data.get('extra_where', '') |
| 348 | if 'test_labels' in extra_where: |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 349 | query_set = self._add_label_joins(query_set) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 350 | joined = True |
| 351 | |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 352 | if include_labels: |
| 353 | query_set = self._include_or_exclude_labels(query_set, |
| 354 | include_labels) |
showard | fc8c6ae | 2008-11-11 19:06:01 +0000 | [diff] [blame] | 355 | joined = True |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 356 | if exclude_labels: |
| 357 | query_set = self._include_or_exclude_labels(query_set, |
| 358 | exclude_labels, |
| 359 | exclude=True) |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 360 | joined = True |
| 361 | |
| 362 | include_attributes_where = filter_data.pop('include_attributes_where', |
| 363 | '') |
| 364 | exclude_attributes_where = filter_data.pop('exclude_attributes_where', |
| 365 | '') |
| 366 | if include_attributes_where: |
| 367 | query_set = self._add_attribute_join( |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 368 | query_set, |
| 369 | join_condition=self.escape_user_sql(include_attributes_where)) |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 370 | joined = True |
| 371 | if exclude_attributes_where: |
| 372 | query_set = self._add_attribute_join( |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 373 | query_set, |
| 374 | join_condition=self.escape_user_sql(exclude_attributes_where), |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 375 | exclude=True) |
| 376 | joined = True |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 377 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 378 | test_attributes = filter_data.pop('test_attributes', []) |
| 379 | for attribute in test_attributes: |
| 380 | query_set = self.join_attribute(query_set, attribute) |
| 381 | joined = True |
| 382 | |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 383 | if not joined: |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 384 | filter_data['no_distinct'] = True |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 385 | |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 386 | # TODO: make test_attributes_host_labels obsolete too |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 387 | if include_host_labels or 'test_attributes_host_labels' in extra_where: |
| 388 | query_set = self._add_attribute_join( |
| 389 | query_set, suffix='_host_labels', |
| 390 | join_condition='test_attributes_host_labels.attribute = ' |
| 391 | '"host-labels"') |
| 392 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 393 | return query_set |
| 394 | |
| 395 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 396 | def query_test_ids(self, filter_data, apply_presentation=True): |
| 397 | query = self.model.query_objects(filter_data, |
| 398 | apply_presentation=apply_presentation) |
| 399 | dicts = query.values('test_idx') |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 400 | return [item['test_idx'] for item in dicts] |
| 401 | |
| 402 | |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 403 | def query_test_label_ids(self, filter_data): |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 404 | query_set = self.model.query_objects(filter_data) |
| 405 | query_set = self._add_label_joins(query_set, suffix='_list') |
| 406 | rows = self._custom_select_query(query_set, ['test_labels_list.id']) |
| 407 | return [row[0] for row in rows if row[0] is not None] |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 408 | |
| 409 | |
showard | eaccf8f | 2009-04-16 03:11:33 +0000 | [diff] [blame] | 410 | def escape_user_sql(self, sql): |
| 411 | sql = super(TestViewManager, self).escape_user_sql(sql) |
| 412 | return sql.replace('test_idx', self.get_key_on_this_table('test_idx')) |
| 413 | |
| 414 | |
showard | c478040 | 2009-08-31 18:31:34 +0000 | [diff] [blame] | 415 | def _join_one_iteration_key(self, query_set, result_key, index): |
| 416 | suffix = '_%s' % index |
| 417 | table_name = IterationResult._meta.db_table |
| 418 | alias = table_name + suffix |
| 419 | condition_parts = ["%s.attribute = '%s'" % |
| 420 | (alias, self.escape_user_sql(result_key))] |
| 421 | if index > 0: |
| 422 | # after the first join, we need to match up iteration indices, |
| 423 | # otherwise each join will expand the query by the number of |
| 424 | # iterations and we'll have extraneous rows |
| 425 | first_alias = table_name + '_0' |
| 426 | condition_parts.append('%s.iteration = %s.iteration' % |
| 427 | (alias, first_alias)) |
| 428 | |
| 429 | condition = ' and '.join(condition_parts) |
| 430 | # add a join to IterationResult |
| 431 | query_set = self.add_join(query_set, table_name, join_key='test_idx', |
| 432 | join_condition=condition, suffix=suffix) |
| 433 | # select the iteration value for this join |
| 434 | query_set = query_set.extra(select={result_key: '%s.value' % alias}) |
| 435 | if index == 0: |
| 436 | # pull the iteration index from the first join |
| 437 | query_set = query_set.extra( |
| 438 | select={'iteration_index': '%s.iteration' % alias}) |
| 439 | |
| 440 | return query_set |
| 441 | |
| 442 | |
| 443 | def join_iterations(self, test_view_query_set, result_keys): |
| 444 | """ |
| 445 | Join the given TestView QuerySet to IterationResult. The resulting |
| 446 | query looks like a TestView query but has one row per iteration. Each |
| 447 | row includes all the attributes of TestView, an attribute for each key |
| 448 | in result_keys and an iteration_index attribute. |
| 449 | |
| 450 | We accomplish this by joining the TestView query to IterationResult |
| 451 | once per result key. Each join is restricted on the result key (and on |
| 452 | the test index, like all one-to-many joins). For the first join, this |
| 453 | is the only restriction, so each TestView row expands to a row per |
| 454 | iteration (per iteration that includes the key, of course). For each |
| 455 | subsequent join, we also restrict the iteration index to match that of |
| 456 | the initial join. This makes each subsequent join produce exactly one |
| 457 | result row for each input row. (This assumes each iteration contains |
| 458 | the same set of keys.) |
| 459 | """ |
| 460 | query_set = test_view_query_set |
| 461 | for index, result_key in enumerate(result_keys): |
| 462 | query_set = self._join_one_iteration_key(query_set, result_key, |
| 463 | index) |
| 464 | return query_set |
| 465 | |
| 466 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 467 | def join_attribute(self, test_view_query_set, attribute): |
| 468 | """ |
| 469 | Join the given TestView QuerySet to TestAttribute. The resulting query |
| 470 | has an additional column for the given attribute named |
| 471 | "attribute_<attribute name>". |
| 472 | """ |
| 473 | table_name = TestAttribute._meta.db_table |
| 474 | suffix = '_' + attribute |
| 475 | alias = table_name + suffix |
| 476 | condition = "%s.attribute = '%s'" % (alias, |
| 477 | self.escape_user_sql(attribute)) |
| 478 | query_set = self.add_join(test_view_query_set, table_name, |
| 479 | join_key='test_idx', join_condition=condition, |
| 480 | suffix=suffix, force_left_join=True) |
| 481 | |
| 482 | select_name = 'attribute_' + attribute |
| 483 | query_set = query_set.extra(select={select_name: '%s.value' % alias}) |
| 484 | return query_set |
| 485 | |
| 486 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 487 | class TestView(dbmodels.Model, model_logic.ModelExtensions): |
| 488 | extra_fields = { |
showard | f4c702e | 2009-07-08 21:14:27 +0000 | [diff] [blame] | 489 | 'DATE(job_queued_time)': 'job queued day', |
| 490 | 'DATE(test_finished_time)': 'test finished day', |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | group_fields = [ |
showard | f4c702e | 2009-07-08 21:14:27 +0000 | [diff] [blame] | 494 | 'test_name', |
| 495 | 'status', |
| 496 | 'kernel', |
| 497 | 'hostname', |
| 498 | 'job_tag', |
| 499 | 'job_name', |
| 500 | 'platform', |
| 501 | 'reason', |
| 502 | 'job_owner', |
| 503 | 'job_queued_time', |
| 504 | 'DATE(job_queued_time)', |
| 505 | 'test_started_time', |
| 506 | 'test_finished_time', |
| 507 | 'DATE(test_finished_time)', |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 508 | ] |
| 509 | |
| 510 | test_idx = dbmodels.IntegerField('test index', primary_key=True) |
| 511 | job_idx = dbmodels.IntegerField('job index', null=True, blank=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 512 | test_name = dbmodels.CharField(blank=True, max_length=90) |
| 513 | subdir = dbmodels.CharField('subdirectory', blank=True, max_length=180) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 514 | kernel_idx = dbmodels.IntegerField('kernel index') |
| 515 | status_idx = dbmodels.IntegerField('status index') |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 516 | reason = dbmodels.CharField(blank=True, max_length=3072) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 517 | machine_idx = dbmodels.IntegerField('host index') |
| 518 | test_started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 519 | test_finished_time = dbmodels.DateTimeField(null=True, blank=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 520 | job_tag = dbmodels.CharField(blank=True, max_length=300) |
| 521 | job_name = dbmodels.CharField(blank=True, max_length=300) |
| 522 | job_owner = dbmodels.CharField('owner', blank=True, max_length=240) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 523 | job_queued_time = dbmodels.DateTimeField(null=True, blank=True) |
| 524 | job_started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 525 | job_finished_time = dbmodels.DateTimeField(null=True, blank=True) |
showard | c1c1caf | 2009-09-08 16:26:50 +0000 | [diff] [blame] | 526 | afe_job_id = dbmodels.IntegerField(null=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 527 | hostname = dbmodels.CharField(blank=True, max_length=300) |
| 528 | platform = dbmodels.CharField(blank=True, max_length=240) |
| 529 | machine_owner = dbmodels.CharField(blank=True, max_length=240) |
| 530 | kernel_hash = dbmodels.CharField(blank=True, max_length=105) |
| 531 | kernel_base = dbmodels.CharField(blank=True, max_length=90) |
| 532 | kernel = dbmodels.CharField(blank=True, max_length=300) |
| 533 | status = dbmodels.CharField(blank=True, max_length=30) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 534 | |
| 535 | objects = TestViewManager() |
| 536 | |
| 537 | def save(self): |
| 538 | raise NotImplementedError('TestView is read-only') |
| 539 | |
| 540 | |
| 541 | def delete(self): |
| 542 | raise NotImplementedError('TestView is read-only') |
| 543 | |
| 544 | |
| 545 | @classmethod |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 546 | def query_objects(cls, filter_data, initial_query=None, |
| 547 | apply_presentation=True): |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 548 | if initial_query is None: |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 549 | initial_query = cls.objects.get_query_set_with_joins(filter_data) |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame^] | 550 | return super(TestView, cls).query_objects( |
| 551 | filter_data, initial_query=initial_query, |
| 552 | apply_presentation=apply_presentation) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 553 | |
| 554 | |
| 555 | class Meta: |
| 556 | db_table = 'test_view_2' |