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 | |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 5 | _quote_name = connection.ops.quote_name |
| 6 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 7 | class TempManager(model_logic.ExtendedManager): |
| 8 | _GROUP_COUNT_NAME = 'group_count' |
| 9 | |
| 10 | def _get_key_unless_is_function(self, field): |
| 11 | if '(' in field: |
| 12 | return field |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 13 | return self.get_key_on_this_table(field) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 14 | |
| 15 | |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 16 | def _get_field_names(self, fields, extra_select_fields={}): |
| 17 | field_names = [] |
| 18 | for field in fields: |
| 19 | if field in extra_select_fields: |
showard | d2b0c88 | 2009-10-19 18:34:11 +0000 | [diff] [blame] | 20 | field_names.append(extra_select_fields[field][0]) |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 21 | else: |
| 22 | field_names.append(self._get_key_unless_is_function(field)) |
| 23 | return field_names |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 24 | |
| 25 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 26 | def _get_group_query_sql(self, query, group_by): |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 27 | sql, params = query.query.as_sql() |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 28 | |
| 29 | # insert GROUP BY clause into query |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 30 | group_fields = self._get_field_names(group_by, query.query.extra_select) |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 31 | group_by_clause = ' GROUP BY ' + ', '.join(group_fields) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 32 | group_by_position = sql.rfind('ORDER BY') |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 33 | if group_by_position == -1: |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 34 | group_by_position = len(sql) |
| 35 | sql = (sql[:group_by_position] + |
| 36 | group_by_clause + ' ' + |
| 37 | sql[group_by_position:]) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 38 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 39 | return sql, params |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 40 | |
| 41 | |
showard | 06b82fc | 2009-06-30 01:59:42 +0000 | [diff] [blame] | 42 | def _get_column_names(self, cursor): |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 43 | """ |
showard | 06b82fc | 2009-06-30 01:59:42 +0000 | [diff] [blame] | 44 | Gets the column names from the cursor description. This method exists |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 45 | so that it can be mocked in the unit test for sqlite3 compatibility. |
showard | 06b82fc | 2009-06-30 01:59:42 +0000 | [diff] [blame] | 46 | """ |
| 47 | return [column_info[0] for column_info in cursor.description] |
| 48 | |
| 49 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 50 | def execute_group_query(self, query, group_by): |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 51 | """ |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 52 | Performs the given query grouped by the fields in group_by with the |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 53 | given query's extra select fields added. Returns a list of dicts, where |
| 54 | each dict corresponds to single row and contains a key for each grouped |
| 55 | field as well as all of the extra select fields. |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 56 | """ |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 57 | sql, params = self._get_group_query_sql(query, group_by) |
showard | 56e9377 | 2008-10-06 10:06:22 +0000 | [diff] [blame] | 58 | cursor = readonly_connection.connection().cursor() |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 59 | cursor.execute(sql, params) |
showard | 06b82fc | 2009-06-30 01:59:42 +0000 | [diff] [blame] | 60 | field_names = self._get_column_names(cursor) |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 61 | row_dicts = [dict(zip(field_names, row)) for row in cursor.fetchall()] |
| 62 | return row_dicts |
| 63 | |
| 64 | |
| 65 | def get_count_sql(self, query): |
| 66 | """ |
| 67 | 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] | 68 | a grouped query. Returns a tuple (field alias, field SQL) |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 69 | """ |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 70 | if query.query.distinct: |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 71 | pk_field = self.get_key_on_this_table() |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 72 | count_sql = 'COUNT(DISTINCT %s)' % pk_field |
| 73 | else: |
| 74 | count_sql = 'COUNT(1)' |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 75 | return self._GROUP_COUNT_NAME, count_sql |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | def _get_num_groups_sql(self, query, group_by): |
showard | d2b0c88 | 2009-10-19 18:34:11 +0000 | [diff] [blame] | 79 | group_fields = self._get_field_names(group_by, query.query.extra_select) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 80 | query = query.order_by() # this can mess up the query and isn't needed |
| 81 | |
| 82 | sql, params = query.query.as_sql() |
| 83 | from_ = sql[sql.find(' FROM'):] |
showard | d2b0c88 | 2009-10-19 18:34:11 +0000 | [diff] [blame] | 84 | return ('SELECT DISTINCT %s %s' % (','.join(group_fields), |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 85 | from_), |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 86 | params) |
| 87 | |
| 88 | |
showard | d2b0c88 | 2009-10-19 18:34:11 +0000 | [diff] [blame] | 89 | def _cursor_rowcount(self, cursor): |
| 90 | """To be stubbed by tests""" |
| 91 | return cursor.rowcount |
| 92 | |
| 93 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 94 | def get_num_groups(self, query, group_by): |
| 95 | """ |
| 96 | Returns the number of distinct groups for the given query grouped by the |
| 97 | fields in group_by. |
| 98 | """ |
| 99 | sql, params = self._get_num_groups_sql(query, group_by) |
showard | 56e9377 | 2008-10-06 10:06:22 +0000 | [diff] [blame] | 100 | cursor = readonly_connection.connection().cursor() |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 101 | cursor.execute(sql, params) |
showard | d2b0c88 | 2009-10-19 18:34:11 +0000 | [diff] [blame] | 102 | return self._cursor_rowcount(cursor) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 103 | |
| 104 | |
| 105 | class Machine(dbmodels.Model): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 106 | machine_idx = dbmodels.AutoField(primary_key=True) |
showard | 356d237 | 2009-11-10 01:29:53 +0000 | [diff] [blame] | 107 | hostname = dbmodels.CharField(unique=True, max_length=255) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 108 | machine_group = dbmodels.CharField(blank=True, max_length=240) |
| 109 | owner = dbmodels.CharField(blank=True, max_length=240) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 110 | |
| 111 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 112 | db_table = 'tko_machines' |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | class Kernel(dbmodels.Model): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 116 | kernel_idx = dbmodels.AutoField(primary_key=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 117 | kernel_hash = dbmodels.CharField(max_length=105, editable=False) |
| 118 | base = dbmodels.CharField(max_length=90) |
| 119 | printable = dbmodels.CharField(max_length=300) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 120 | |
| 121 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 122 | db_table = 'tko_kernels' |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 123 | |
| 124 | |
| 125 | class Patch(dbmodels.Model): |
| 126 | kernel = dbmodels.ForeignKey(Kernel, db_column='kernel_idx') |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 127 | name = dbmodels.CharField(blank=True, max_length=240) |
| 128 | url = dbmodels.CharField(blank=True, max_length=900) |
| 129 | the_hash = dbmodels.CharField(blank=True, max_length=105, db_column='hash') |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 130 | |
| 131 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 132 | db_table = 'tko_patches' |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 133 | |
| 134 | |
| 135 | class Status(dbmodels.Model): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 136 | status_idx = dbmodels.AutoField(primary_key=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 137 | word = dbmodels.CharField(max_length=30) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 138 | |
| 139 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 140 | db_table = 'tko_status' |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 141 | |
| 142 | |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 143 | class Job(dbmodels.Model, model_logic.ModelExtensions): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 144 | job_idx = dbmodels.AutoField(primary_key=True) |
showard | 356d237 | 2009-11-10 01:29:53 +0000 | [diff] [blame] | 145 | tag = dbmodels.CharField(unique=True, max_length=100) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 146 | label = dbmodels.CharField(max_length=300) |
| 147 | username = dbmodels.CharField(max_length=240) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 148 | machine = dbmodels.ForeignKey(Machine, db_column='machine_idx') |
| 149 | queued_time = dbmodels.DateTimeField(null=True, blank=True) |
| 150 | started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 151 | finished_time = dbmodels.DateTimeField(null=True, blank=True) |
showard | c1c1caf | 2009-09-08 16:26:50 +0000 | [diff] [blame] | 152 | afe_job_id = dbmodels.IntegerField(null=True, default=None) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 153 | |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 154 | objects = model_logic.ExtendedManager() |
| 155 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 156 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 157 | db_table = 'tko_jobs' |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 158 | |
| 159 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 160 | class JobKeyval(dbmodels.Model): |
| 161 | job = dbmodels.ForeignKey(Job) |
| 162 | key = dbmodels.CharField(max_length=90) |
| 163 | value = dbmodels.CharField(blank=True, max_length=300) |
| 164 | |
| 165 | |
| 166 | class Meta: |
| 167 | db_table = 'tko_job_keyvals' |
| 168 | |
| 169 | |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 170 | class Test(dbmodels.Model, model_logic.ModelExtensions, |
| 171 | model_logic.ModelWithAttributes): |
| 172 | test_idx = dbmodels.AutoField(primary_key=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 173 | job = dbmodels.ForeignKey(Job, db_column='job_idx') |
lmr | 7969728 | 2010-03-31 23:18:02 +0000 | [diff] [blame] | 174 | test = dbmodels.CharField(max_length=300) |
| 175 | subdir = dbmodels.CharField(blank=True, max_length=300) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 176 | kernel = dbmodels.ForeignKey(Kernel, db_column='kernel_idx') |
| 177 | status = dbmodels.ForeignKey(Status, db_column='status') |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 178 | reason = dbmodels.CharField(blank=True, max_length=3072) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 179 | machine = dbmodels.ForeignKey(Machine, db_column='machine_idx') |
| 180 | finished_time = dbmodels.DateTimeField(null=True, blank=True) |
| 181 | started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 182 | |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 183 | objects = model_logic.ExtendedManager() |
| 184 | |
| 185 | def _get_attribute_model_and_args(self, attribute): |
| 186 | return TestAttribute, dict(test=self, attribute=attribute, |
| 187 | user_created=True) |
| 188 | |
| 189 | |
| 190 | def set_attribute(self, attribute, value): |
| 191 | # ensure non-user-created attributes remain immutable |
| 192 | try: |
| 193 | TestAttribute.objects.get(test=self, attribute=attribute, |
| 194 | user_created=False) |
| 195 | raise ValueError('Attribute %s already exists for test %s and is ' |
| 196 | 'immutable' % (attribute, self.test_idx)) |
| 197 | except TestAttribute.DoesNotExist: |
| 198 | super(Test, self).set_attribute(attribute, value) |
| 199 | |
| 200 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 201 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 202 | db_table = 'tko_tests' |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 203 | |
| 204 | |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 205 | class TestAttribute(dbmodels.Model, model_logic.ModelExtensions): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 206 | test = dbmodels.ForeignKey(Test, db_column='test_idx') |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 207 | attribute = dbmodels.CharField(max_length=90) |
| 208 | value = dbmodels.CharField(blank=True, max_length=300) |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 209 | user_created = dbmodels.BooleanField(default=False) |
| 210 | |
| 211 | objects = model_logic.ExtendedManager() |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 212 | |
| 213 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 214 | db_table = 'tko_test_attributes' |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 215 | |
| 216 | |
jadmanski | 430dca9 | 2008-12-16 20:56:53 +0000 | [diff] [blame] | 217 | class IterationAttribute(dbmodels.Model, model_logic.ModelExtensions): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 218 | # this isn't really a primary key, but it's necessary to appease Django |
| 219 | # and is harmless as long as we're careful |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 220 | test = dbmodels.ForeignKey(Test, db_column='test_idx', primary_key=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 221 | iteration = dbmodels.IntegerField() |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 222 | attribute = dbmodels.CharField(max_length=90) |
| 223 | value = dbmodels.CharField(blank=True, max_length=300) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 224 | |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 225 | objects = model_logic.ExtendedManager() |
| 226 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 227 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 228 | db_table = 'tko_iteration_attributes' |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 229 | |
| 230 | |
jadmanski | 430dca9 | 2008-12-16 20:56:53 +0000 | [diff] [blame] | 231 | class IterationResult(dbmodels.Model, model_logic.ModelExtensions): |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 232 | # see comment on IterationAttribute regarding primary_key=True |
jadmanski | 430dca9 | 2008-12-16 20:56:53 +0000 | [diff] [blame] | 233 | test = dbmodels.ForeignKey(Test, db_column='test_idx', primary_key=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 234 | iteration = dbmodels.IntegerField() |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 235 | attribute = dbmodels.CharField(max_length=90) |
jamesren | 26f9a9f | 2010-04-02 17:44:55 +0000 | [diff] [blame^] | 236 | value = dbmodels.FloatField(null=True, blank=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 237 | |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 238 | objects = model_logic.ExtendedManager() |
| 239 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 240 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 241 | db_table = 'tko_iteration_result' |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 242 | |
| 243 | |
| 244 | class TestLabel(dbmodels.Model, model_logic.ModelExtensions): |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 245 | name = dbmodels.CharField(max_length=80, unique=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 246 | description = dbmodels.TextField(blank=True) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 247 | tests = dbmodels.ManyToManyField(Test, blank=True, |
| 248 | db_table='tko_test_labels_tests') |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 249 | |
| 250 | name_field = 'name' |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 251 | objects = model_logic.ExtendedManager() |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 252 | |
| 253 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 254 | db_table = 'tko_test_labels' |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 255 | |
| 256 | |
| 257 | class SavedQuery(dbmodels.Model, model_logic.ModelExtensions): |
| 258 | # TODO: change this to foreign key once DBs are merged |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 259 | owner = dbmodels.CharField(max_length=80) |
| 260 | name = dbmodels.CharField(max_length=100) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 261 | url_token = dbmodels.TextField() |
| 262 | |
| 263 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 264 | db_table = 'tko_saved_queries' |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 265 | |
| 266 | |
showard | ce12f55 | 2008-09-19 00:48:59 +0000 | [diff] [blame] | 267 | class EmbeddedGraphingQuery(dbmodels.Model, model_logic.ModelExtensions): |
| 268 | url_token = dbmodels.TextField(null=False, blank=False) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 269 | graph_type = dbmodels.CharField(max_length=16, null=False, blank=False) |
showard | ce12f55 | 2008-09-19 00:48:59 +0000 | [diff] [blame] | 270 | params = dbmodels.TextField(null=False, blank=False) |
| 271 | last_updated = dbmodels.DateTimeField(null=False, blank=False, |
| 272 | editable=False) |
| 273 | # refresh_time shows the time at which a thread is updating the cached |
| 274 | # image, or NULL if no one is updating the image. This is used so that only |
| 275 | # one thread is updating the cached image at a time (see |
| 276 | # graphing_utils.handle_plot_request) |
| 277 | refresh_time = dbmodels.DateTimeField(editable=False) |
| 278 | cached_png = dbmodels.TextField(editable=False) |
| 279 | |
| 280 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 281 | db_table = 'tko_embedded_graphing_queries' |
showard | ce12f55 | 2008-09-19 00:48:59 +0000 | [diff] [blame] | 282 | |
| 283 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 284 | # views |
| 285 | |
| 286 | class TestViewManager(TempManager): |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 287 | def get_query_set(self): |
| 288 | query = super(TestViewManager, self).get_query_set() |
showard | 5bf7c50 | 2008-08-20 01:22:22 +0000 | [diff] [blame] | 289 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 290 | # add extra fields to selects, using the SQL itself as the "alias" |
| 291 | extra_select = dict((sql, sql) |
| 292 | for sql in self.model.extra_fields.iterkeys()) |
| 293 | return query.extra(select=extra_select) |
| 294 | |
| 295 | |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 296 | def _get_include_exclude_suffix(self, exclude): |
| 297 | if exclude: |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 298 | return '_exclude' |
| 299 | return '_include' |
| 300 | |
| 301 | |
| 302 | def _add_attribute_join(self, query_set, join_condition, |
| 303 | suffix=None, exclude=False): |
| 304 | if suffix is None: |
| 305 | suffix = self._get_include_exclude_suffix(exclude) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 306 | return self.add_join(query_set, 'tko_test_attributes', |
| 307 | join_key='test_idx', |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 308 | join_condition=join_condition, |
| 309 | suffix=suffix, exclude=exclude) |
| 310 | |
| 311 | |
| 312 | def _add_label_pivot_table_join(self, query_set, suffix, join_condition='', |
| 313 | exclude=False, force_left_join=False): |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 314 | return self.add_join(query_set, 'tko_test_labels_tests', |
| 315 | join_key='test_id', |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 316 | join_condition=join_condition, |
| 317 | suffix=suffix, exclude=exclude, |
| 318 | force_left_join=force_left_join) |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 319 | |
| 320 | |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 321 | def _add_label_joins(self, query_set, suffix=''): |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 322 | query_set = self._add_label_pivot_table_join( |
| 323 | query_set, suffix=suffix, force_left_join=True) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 324 | |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 325 | # since we're not joining from the original table, we can't use |
| 326 | # self.add_join() again |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 327 | second_join_alias = 'tko_test_labels' + suffix |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 328 | second_join_condition = ('%s.id = %s.testlabel_id' % |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 329 | (second_join_alias, |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 330 | 'tko_test_labels_tests' + suffix)) |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 331 | query_set.query.add_custom_join('tko_test_labels', |
| 332 | second_join_condition, |
| 333 | query_set.query.LOUTER, |
| 334 | alias=second_join_alias) |
| 335 | return query_set |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 336 | |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 337 | |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 338 | def _get_label_ids_from_names(self, label_names): |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 339 | label_ids = list( # listifying avoids a double query below |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 340 | TestLabel.objects.filter(name__in=label_names) |
| 341 | .values_list('name', 'id')) |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 342 | if len(label_ids) < len(set(label_names)): |
lmr | 7969728 | 2010-03-31 23:18:02 +0000 | [diff] [blame] | 343 | raise ValueError('Not all labels found: %s' % |
| 344 | ', '.join(label_names)) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 345 | return dict(name_and_id for name_and_id in label_ids) |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 346 | |
| 347 | |
| 348 | def _include_or_exclude_labels(self, query_set, label_names, exclude=False): |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 349 | label_ids = self._get_label_ids_from_names(label_names).itervalues() |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 350 | suffix = self._get_include_exclude_suffix(exclude) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 351 | condition = ('tko_test_labels_tests%s.testlabel_id IN (%s)' % |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 352 | (suffix, |
| 353 | ','.join(str(label_id) for label_id in label_ids))) |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 354 | return self._add_label_pivot_table_join(query_set, |
| 355 | join_condition=condition, |
| 356 | suffix=suffix, |
| 357 | exclude=exclude) |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 358 | |
| 359 | |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 360 | def _add_custom_select(self, query_set, select_name, select_sql): |
| 361 | return query_set.extra(select={select_name: select_sql}) |
| 362 | |
| 363 | |
| 364 | def _add_select_value(self, query_set, alias): |
| 365 | return self._add_custom_select(query_set, alias, |
| 366 | _quote_name(alias) + '.value') |
| 367 | |
| 368 | |
| 369 | def _add_select_ifnull(self, query_set, alias, non_null_value): |
| 370 | select_sql = "IF(%s.id IS NOT NULL, '%s', NULL)" % (_quote_name(alias), |
| 371 | non_null_value) |
| 372 | return self._add_custom_select(query_set, alias, select_sql) |
| 373 | |
| 374 | |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 375 | def _join_test_label_column(self, query_set, label_name, label_id): |
| 376 | alias = 'test_label_' + label_name |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 377 | label_query = TestLabel.objects.filter(name=label_name) |
| 378 | query_set = Test.objects.join_custom_field(query_set, label_query, |
| 379 | alias) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 380 | |
| 381 | query_set = self._add_select_ifnull(query_set, alias, label_name) |
| 382 | return query_set |
| 383 | |
| 384 | |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 385 | def _join_test_label_columns(self, query_set, label_names): |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 386 | label_id_map = self._get_label_ids_from_names(label_names) |
| 387 | for label_name in label_names: |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 388 | query_set = self._join_test_label_column(query_set, label_name, |
| 389 | label_id_map[label_name]) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 390 | return query_set |
| 391 | |
| 392 | |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 393 | def _join_test_attribute(self, query_set, attribute, alias=None, |
| 394 | extra_join_condition=None): |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 395 | """ |
| 396 | Join the given TestView QuerySet to TestAttribute. The resulting query |
| 397 | has an additional column for the given attribute named |
| 398 | "attribute_<attribute name>". |
| 399 | """ |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 400 | if not alias: |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 401 | alias = 'test_attribute_' + attribute |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 402 | attribute_query = TestAttribute.objects.filter(attribute=attribute) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 403 | if extra_join_condition: |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 404 | attribute_query = attribute_query.extra( |
| 405 | where=[extra_join_condition]) |
| 406 | query_set = Test.objects.join_custom_field(query_set, attribute_query, |
| 407 | alias) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 408 | |
| 409 | query_set = self._add_select_value(query_set, alias) |
| 410 | return query_set |
| 411 | |
| 412 | |
| 413 | def _join_machine_label_columns(self, query_set, machine_label_names): |
| 414 | for label_name in machine_label_names: |
| 415 | alias = 'machine_label_' + label_name |
| 416 | condition = "FIND_IN_SET('%s', %s)" % ( |
| 417 | label_name, _quote_name(alias) + '.value') |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 418 | query_set = self._join_test_attribute( |
| 419 | query_set, 'host-labels', |
| 420 | alias=alias, extra_join_condition=condition) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 421 | query_set = self._add_select_ifnull(query_set, alias, label_name) |
| 422 | return query_set |
| 423 | |
| 424 | |
| 425 | def _join_one_iteration_key(self, query_set, result_key, first_alias=None): |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 426 | alias = 'iteration_result_' + result_key |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 427 | iteration_query = IterationResult.objects.filter(attribute=result_key) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 428 | if first_alias: |
| 429 | # after the first join, we need to match up iteration indices, |
| 430 | # otherwise each join will expand the query by the number of |
| 431 | # iterations and we'll have extraneous rows |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 432 | iteration_query = iteration_query.extra( |
| 433 | where=['%s.iteration = %s.iteration' |
| 434 | % (_quote_name(alias), _quote_name(first_alias))]) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 435 | |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 436 | query_set = Test.objects.join_custom_field(query_set, iteration_query, |
| 437 | alias, left_join=False) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 438 | # select the iteration value and index for this join |
| 439 | query_set = self._add_select_value(query_set, alias) |
| 440 | if not first_alias: |
| 441 | # for first join, add iteration index select too |
| 442 | query_set = self._add_custom_select( |
| 443 | query_set, 'iteration_index', |
| 444 | _quote_name(alias) + '.iteration') |
| 445 | |
| 446 | return query_set, alias |
| 447 | |
| 448 | |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 449 | def _join_iteration_results(self, test_view_query_set, result_keys): |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 450 | """Join the given TestView QuerySet to IterationResult for one result. |
| 451 | |
| 452 | The resulting query looks like a TestView query but has one row per |
| 453 | iteration. Each row includes all the attributes of TestView, an |
| 454 | attribute for each key in result_keys and an iteration_index attribute. |
| 455 | |
| 456 | We accomplish this by joining the TestView query to IterationResult |
| 457 | once per result key. Each join is restricted on the result key (and on |
| 458 | the test index, like all one-to-many joins). For the first join, this |
| 459 | is the only restriction, so each TestView row expands to a row per |
| 460 | iteration (per iteration that includes the key, of course). For each |
| 461 | subsequent join, we also restrict the iteration index to match that of |
| 462 | the initial join. This makes each subsequent join produce exactly one |
| 463 | result row for each input row. (This assumes each iteration contains |
| 464 | the same set of keys. Results are undefined if that's not true.) |
| 465 | """ |
| 466 | if not result_keys: |
| 467 | return test_view_query_set |
| 468 | |
| 469 | query_set, first_alias = self._join_one_iteration_key( |
| 470 | test_view_query_set, result_keys[0]) |
| 471 | for result_key in result_keys[1:]: |
| 472 | query_set, _ = self._join_one_iteration_key(query_set, result_key, |
| 473 | first_alias=first_alias) |
| 474 | return query_set |
| 475 | |
| 476 | |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 477 | def _join_job_keyvals(self, query_set, job_keyvals): |
| 478 | for job_keyval in job_keyvals: |
| 479 | alias = 'job_keyval_' + job_keyval |
| 480 | keyval_query = JobKeyval.objects.filter(key=job_keyval) |
| 481 | query_set = Job.objects.join_custom_field(query_set, keyval_query, |
| 482 | alias) |
| 483 | query_set = self._add_select_value(query_set, alias) |
| 484 | return query_set |
| 485 | |
| 486 | |
| 487 | def _join_iteration_attributes(self, query_set, iteration_attributes): |
| 488 | for attribute in iteration_attributes: |
| 489 | alias = 'iteration_attribute_' + attribute |
| 490 | attribute_query = IterationAttribute.objects.filter( |
| 491 | attribute=attribute) |
| 492 | query_set = Test.objects.join_custom_field(query_set, |
| 493 | attribute_query, alias) |
| 494 | query_set = self._add_select_value(query_set, alias) |
| 495 | return query_set |
| 496 | |
| 497 | |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 498 | def get_query_set_with_joins(self, filter_data): |
| 499 | """ |
| 500 | Add joins for querying over test-related items. |
| 501 | |
| 502 | These parameters are supported going forward: |
| 503 | * test_attribute_fields: list of attribute names. Each attribute will |
| 504 | be available as a column attribute_<name>.value. |
| 505 | * test_label_fields: list of label names. Each label will be available |
| 506 | as a column label_<name>.id, non-null iff the label is present. |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 507 | * iteration_result_fields: list of iteration result names. Each |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 508 | result will be available as a column iteration_<name>.value. |
| 509 | Note that this changes the semantics to return iterations |
| 510 | instead of tests -- if a test has multiple iterations, a row |
| 511 | will be returned for each one. The iteration index is also |
| 512 | available as iteration_<name>.iteration. |
| 513 | * machine_label_fields: list of machine label names. Each will be |
| 514 | available as a column machine_label_<name>.id, non-null iff the |
| 515 | label is present on the machine used in the test. |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 516 | * job_keyval_fields: list of job keyval names. Each value will be |
| 517 | available as a column job_keyval_<name>.id, non-null iff the |
| 518 | keyval is present in the AFE job. |
| 519 | * iteration_attribute_fields: list of iteration attribute names. Each |
| 520 | attribute will be available as a column |
| 521 | iteration_attribute<name>.id, non-null iff the attribute is |
| 522 | present. |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 523 | |
| 524 | These parameters are deprecated: |
| 525 | * include_labels |
| 526 | * exclude_labels |
| 527 | * include_attributes_where |
| 528 | * exclude_attributes_where |
| 529 | |
| 530 | Additionally, this method adds joins if the following strings are |
| 531 | present in extra_where (this is also deprecated): |
| 532 | * test_labels |
| 533 | * test_attributes_host_labels |
| 534 | """ |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 535 | query_set = self.get_query_set() |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 536 | |
| 537 | test_attributes = filter_data.pop('test_attribute_fields', []) |
| 538 | for attribute in test_attributes: |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 539 | query_set = self._join_test_attribute(query_set, attribute) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 540 | |
| 541 | test_labels = filter_data.pop('test_label_fields', []) |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 542 | query_set = self._join_test_label_columns(query_set, test_labels) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 543 | |
| 544 | machine_labels = filter_data.pop('machine_label_fields', []) |
| 545 | query_set = self._join_machine_label_columns(query_set, machine_labels) |
| 546 | |
jamesren | 708f1c0 | 2010-03-31 21:43:57 +0000 | [diff] [blame] | 547 | iteration_keys = filter_data.pop('iteration_result_fields', []) |
| 548 | query_set = self._join_iteration_results(query_set, iteration_keys) |
| 549 | |
| 550 | job_keyvals = filter_data.pop('job_keyval_fields', []) |
| 551 | query_set = self._join_job_keyvals(query_set, job_keyvals) |
| 552 | |
| 553 | iteration_attributes = filter_data.pop('iteration_attribute_fields', []) |
| 554 | query_set = self._join_iteration_attributes(query_set, |
| 555 | iteration_attributes) |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 556 | |
| 557 | # everything that follows is deprecated behavior |
| 558 | |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 559 | joined = False |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 560 | |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 561 | extra_where = filter_data.get('extra_where', '') |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 562 | if 'tko_test_labels' in extra_where: |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 563 | query_set = self._add_label_joins(query_set) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 564 | joined = True |
| 565 | |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 566 | include_labels = filter_data.pop('include_labels', []) |
| 567 | exclude_labels = filter_data.pop('exclude_labels', []) |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 568 | if include_labels: |
| 569 | query_set = self._include_or_exclude_labels(query_set, |
| 570 | include_labels) |
showard | fc8c6ae | 2008-11-11 19:06:01 +0000 | [diff] [blame] | 571 | joined = True |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 572 | if exclude_labels: |
| 573 | query_set = self._include_or_exclude_labels(query_set, |
| 574 | exclude_labels, |
| 575 | exclude=True) |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 576 | joined = True |
| 577 | |
| 578 | include_attributes_where = filter_data.pop('include_attributes_where', |
| 579 | '') |
| 580 | exclude_attributes_where = filter_data.pop('exclude_attributes_where', |
| 581 | '') |
| 582 | if include_attributes_where: |
| 583 | query_set = self._add_attribute_join( |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 584 | query_set, |
| 585 | join_condition=self.escape_user_sql(include_attributes_where)) |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 586 | joined = True |
| 587 | if exclude_attributes_where: |
| 588 | query_set = self._add_attribute_join( |
showard | 2aa318e | 2009-08-20 23:43:10 +0000 | [diff] [blame] | 589 | query_set, |
| 590 | join_condition=self.escape_user_sql(exclude_attributes_where), |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 591 | exclude=True) |
| 592 | joined = True |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 593 | |
| 594 | if not joined: |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 595 | filter_data['no_distinct'] = True |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 596 | |
showard | 8b0ea22 | 2009-12-23 19:23:03 +0000 | [diff] [blame] | 597 | if 'tko_test_attributes_host_labels' in extra_where: |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 598 | query_set = self._add_attribute_join( |
| 599 | query_set, suffix='_host_labels', |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 600 | join_condition='tko_test_attributes_host_labels.attribute = ' |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 601 | '"host-labels"') |
| 602 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 603 | return query_set |
| 604 | |
| 605 | |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 606 | def query_test_ids(self, filter_data, apply_presentation=True): |
| 607 | query = self.model.query_objects(filter_data, |
| 608 | apply_presentation=apply_presentation) |
| 609 | dicts = query.values('test_idx') |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 610 | return [item['test_idx'] for item in dicts] |
| 611 | |
| 612 | |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 613 | def query_test_label_ids(self, filter_data): |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 614 | query_set = self.model.query_objects(filter_data) |
| 615 | query_set = self._add_label_joins(query_set, suffix='_list') |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 616 | rows = self._custom_select_query(query_set, ['tko_test_labels_list.id']) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 617 | return [row[0] for row in rows if row[0] is not None] |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 618 | |
| 619 | |
showard | eaccf8f | 2009-04-16 03:11:33 +0000 | [diff] [blame] | 620 | def escape_user_sql(self, sql): |
| 621 | sql = super(TestViewManager, self).escape_user_sql(sql) |
| 622 | return sql.replace('test_idx', self.get_key_on_this_table('test_idx')) |
| 623 | |
| 624 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 625 | class TestView(dbmodels.Model, model_logic.ModelExtensions): |
| 626 | extra_fields = { |
showard | f4c702e | 2009-07-08 21:14:27 +0000 | [diff] [blame] | 627 | 'DATE(job_queued_time)': 'job queued day', |
| 628 | 'DATE(test_finished_time)': 'test finished day', |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | group_fields = [ |
showard | f4c702e | 2009-07-08 21:14:27 +0000 | [diff] [blame] | 632 | 'test_name', |
| 633 | 'status', |
| 634 | 'kernel', |
| 635 | 'hostname', |
| 636 | 'job_tag', |
| 637 | 'job_name', |
| 638 | 'platform', |
| 639 | 'reason', |
| 640 | 'job_owner', |
| 641 | 'job_queued_time', |
| 642 | 'DATE(job_queued_time)', |
| 643 | 'test_started_time', |
| 644 | 'test_finished_time', |
| 645 | 'DATE(test_finished_time)', |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 646 | ] |
| 647 | |
| 648 | test_idx = dbmodels.IntegerField('test index', primary_key=True) |
| 649 | job_idx = dbmodels.IntegerField('job index', null=True, blank=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 650 | test_name = dbmodels.CharField(blank=True, max_length=90) |
| 651 | subdir = dbmodels.CharField('subdirectory', blank=True, max_length=180) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 652 | kernel_idx = dbmodels.IntegerField('kernel index') |
| 653 | status_idx = dbmodels.IntegerField('status index') |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 654 | reason = dbmodels.CharField(blank=True, max_length=3072) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 655 | machine_idx = dbmodels.IntegerField('host index') |
| 656 | test_started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 657 | test_finished_time = dbmodels.DateTimeField(null=True, blank=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 658 | job_tag = dbmodels.CharField(blank=True, max_length=300) |
| 659 | job_name = dbmodels.CharField(blank=True, max_length=300) |
| 660 | job_owner = dbmodels.CharField('owner', blank=True, max_length=240) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 661 | job_queued_time = dbmodels.DateTimeField(null=True, blank=True) |
| 662 | job_started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 663 | job_finished_time = dbmodels.DateTimeField(null=True, blank=True) |
showard | c1c1caf | 2009-09-08 16:26:50 +0000 | [diff] [blame] | 664 | afe_job_id = dbmodels.IntegerField(null=True) |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 665 | hostname = dbmodels.CharField(blank=True, max_length=300) |
| 666 | platform = dbmodels.CharField(blank=True, max_length=240) |
| 667 | machine_owner = dbmodels.CharField(blank=True, max_length=240) |
| 668 | kernel_hash = dbmodels.CharField(blank=True, max_length=105) |
| 669 | kernel_base = dbmodels.CharField(blank=True, max_length=90) |
| 670 | kernel = dbmodels.CharField(blank=True, max_length=300) |
| 671 | status = dbmodels.CharField(blank=True, max_length=30) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 672 | |
| 673 | objects = TestViewManager() |
| 674 | |
| 675 | def save(self): |
| 676 | raise NotImplementedError('TestView is read-only') |
| 677 | |
| 678 | |
| 679 | def delete(self): |
| 680 | raise NotImplementedError('TestView is read-only') |
| 681 | |
| 682 | |
| 683 | @classmethod |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 684 | def query_objects(cls, filter_data, initial_query=None, |
| 685 | apply_presentation=True): |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 686 | if initial_query is None: |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 687 | initial_query = cls.objects.get_query_set_with_joins(filter_data) |
showard | 8bfb5cb | 2009-10-07 20:49:15 +0000 | [diff] [blame] | 688 | return super(TestView, cls).query_objects( |
| 689 | filter_data, initial_query=initial_query, |
| 690 | apply_presentation=apply_presentation) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 691 | |
| 692 | |
| 693 | class Meta: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 694 | db_table = 'tko_test_view_2' |