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 | |
| 24 | def _get_group_query_sql(self, query, group_by, extra_select_fields): |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 25 | group_fields = self._get_field_names(group_by, extra_select_fields) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 26 | |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 27 | select_fields = [field for field in group_fields |
| 28 | if field not in extra_select_fields] |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 29 | for field_name, field_sql in extra_select_fields.iteritems(): |
| 30 | field_sql = self._get_key_unless_is_function(field_sql) |
| 31 | select_fields.append(field_sql + ' AS ' + field_name) |
| 32 | # add the extra fields to the query selects, so they'll be sortable |
| 33 | # and Django won't mess with any of them |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 34 | query._select[field_name] = field_sql |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 35 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 36 | _, where, params = query._get_sql_clause() |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 37 | |
| 38 | # insert GROUP BY clause into query |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 39 | group_by_clause = ' GROUP BY ' + ', '.join(group_fields) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 40 | group_by_position = where.rfind('ORDER BY') |
| 41 | if group_by_position == -1: |
| 42 | group_by_position = len(where) |
| 43 | where = (where[:group_by_position] + |
| 44 | group_by_clause + ' ' + |
| 45 | where[group_by_position:]) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 46 | |
| 47 | return ('SELECT ' + ', '.join(select_fields) + where), params |
| 48 | |
| 49 | |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 50 | def execute_group_query(self, query, group_by, extra_select_fields=[]): |
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 | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 53 | given extra select fields added. extra_select_fields should be a dict |
| 54 | mapping field alias to field SQL. Usually, the extra fields will use |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 55 | group aggregation functions. Returns a list of dicts, where each dict |
| 56 | corresponds to single row and contains a key for each grouped field as |
| 57 | well as all of the extra select fields. |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 58 | """ |
| 59 | sql, params = self._get_group_query_sql(query, group_by, |
| 60 | extra_select_fields) |
showard | 56e9377 | 2008-10-06 10:06:22 +0000 | [diff] [blame] | 61 | cursor = readonly_connection.connection().cursor() |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 62 | cursor.execute(sql, params) |
| 63 | field_names = [column_info[0] for column_info in cursor.description] |
| 64 | row_dicts = [dict(zip(field_names, row)) for row in cursor.fetchall()] |
| 65 | return row_dicts |
| 66 | |
| 67 | |
| 68 | def get_count_sql(self, query): |
| 69 | """ |
| 70 | 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] | 71 | a grouped query. Returns a tuple (field alias, field SQL) |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 72 | """ |
| 73 | if query._distinct: |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 74 | pk_field = self.get_key_on_this_table() |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 75 | count_sql = 'COUNT(DISTINCT %s)' % pk_field |
| 76 | else: |
| 77 | count_sql = 'COUNT(1)' |
showard | 7c199df | 2008-10-03 10:17:15 +0000 | [diff] [blame] | 78 | return self._GROUP_COUNT_NAME, count_sql |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | def _get_num_groups_sql(self, query, group_by): |
| 82 | group_fields = self._get_field_names(group_by) |
showard | 8a6eb0c | 2008-10-01 11:38:59 +0000 | [diff] [blame] | 83 | query._order_by = None # this can mess up the query and isn't needed |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 84 | _, where, params = query._get_sql_clause() |
| 85 | return ('SELECT COUNT(DISTINCT %s) %s' % (','.join(group_fields), |
| 86 | where), |
| 87 | params) |
| 88 | |
| 89 | |
| 90 | def get_num_groups(self, query, group_by): |
| 91 | """ |
| 92 | Returns the number of distinct groups for the given query grouped by the |
| 93 | fields in group_by. |
| 94 | """ |
| 95 | sql, params = self._get_num_groups_sql(query, group_by) |
showard | 56e9377 | 2008-10-06 10:06:22 +0000 | [diff] [blame] | 96 | cursor = readonly_connection.connection().cursor() |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 97 | cursor.execute(sql, params) |
| 98 | return cursor.fetchone()[0] |
| 99 | |
| 100 | |
| 101 | class Machine(dbmodels.Model): |
| 102 | machine_idx = dbmodels.IntegerField(primary_key=True) |
| 103 | hostname = dbmodels.CharField(unique=True, maxlength=300) |
| 104 | machine_group = dbmodels.CharField(blank=True, maxlength=240) |
| 105 | owner = dbmodels.CharField(blank=True, maxlength=240) |
| 106 | |
| 107 | class Meta: |
| 108 | db_table = 'machines' |
| 109 | |
| 110 | |
| 111 | class Kernel(dbmodels.Model): |
| 112 | kernel_idx = dbmodels.IntegerField(primary_key=True) |
| 113 | kernel_hash = dbmodels.CharField(maxlength=105, editable=False) |
| 114 | base = dbmodels.CharField(maxlength=90) |
| 115 | printable = dbmodels.CharField(maxlength=300) |
| 116 | |
| 117 | class Meta: |
| 118 | db_table = 'kernels' |
| 119 | |
| 120 | |
| 121 | class Patch(dbmodels.Model): |
| 122 | kernel = dbmodels.ForeignKey(Kernel, db_column='kernel_idx') |
| 123 | name = dbmodels.CharField(blank=True, maxlength=240) |
| 124 | url = dbmodels.CharField(blank=True, maxlength=900) |
| 125 | hash_ = dbmodels.CharField(blank=True, maxlength=105, db_column='hash') |
| 126 | |
| 127 | class Meta: |
| 128 | db_table = 'patches' |
| 129 | |
| 130 | |
| 131 | class Status(dbmodels.Model): |
| 132 | status_idx = dbmodels.IntegerField(primary_key=True) |
| 133 | word = dbmodels.CharField(maxlength=30) |
| 134 | |
| 135 | class Meta: |
| 136 | db_table = 'status' |
| 137 | |
| 138 | |
| 139 | class Job(dbmodels.Model): |
| 140 | job_idx = dbmodels.IntegerField(primary_key=True) |
| 141 | tag = dbmodels.CharField(unique=True, maxlength=300) |
| 142 | label = dbmodels.CharField(maxlength=300) |
| 143 | username = dbmodels.CharField(maxlength=240) |
| 144 | machine = dbmodels.ForeignKey(Machine, db_column='machine_idx') |
| 145 | queued_time = dbmodels.DateTimeField(null=True, blank=True) |
| 146 | started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 147 | finished_time = dbmodels.DateTimeField(null=True, blank=True) |
| 148 | |
| 149 | class Meta: |
| 150 | db_table = 'jobs' |
| 151 | |
| 152 | |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 153 | class Test(dbmodels.Model, model_logic.ModelExtensions): |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 154 | test_idx = dbmodels.IntegerField(primary_key=True) |
| 155 | job = dbmodels.ForeignKey(Job, db_column='job_idx') |
| 156 | test = dbmodels.CharField(maxlength=90) |
| 157 | subdir = dbmodels.CharField(blank=True, maxlength=180) |
| 158 | kernel = dbmodels.ForeignKey(Kernel, db_column='kernel_idx') |
| 159 | status = dbmodels.ForeignKey(Status, db_column='status') |
| 160 | reason = dbmodels.CharField(blank=True, maxlength=3072) |
| 161 | machine = dbmodels.ForeignKey(Machine, db_column='machine_idx') |
| 162 | finished_time = dbmodels.DateTimeField(null=True, blank=True) |
| 163 | started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 164 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 165 | class Meta: |
| 166 | db_table = 'tests' |
| 167 | |
| 168 | |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 169 | class TestAttribute(dbmodels.Model, model_logic.ModelExtensions): |
| 170 | # this isn't really a primary key, but it's necessary to appease Django |
| 171 | # and is harmless as long as we're careful |
| 172 | test = dbmodels.ForeignKey(Test, db_column='test_idx', primary_key=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 173 | attribute = dbmodels.CharField(maxlength=90) |
| 174 | value = dbmodels.CharField(blank=True, maxlength=300) |
| 175 | |
| 176 | class Meta: |
| 177 | db_table = 'test_attributes' |
| 178 | |
| 179 | |
jadmanski | 430dca9 | 2008-12-16 20:56:53 +0000 | [diff] [blame] | 180 | class IterationAttribute(dbmodels.Model, model_logic.ModelExtensions): |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 181 | # see comment on TestAttribute regarding primary_key=True |
| 182 | test = dbmodels.ForeignKey(Test, db_column='test_idx', primary_key=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 183 | iteration = dbmodels.IntegerField() |
| 184 | attribute = dbmodels.CharField(maxlength=90) |
| 185 | value = dbmodels.CharField(blank=True, maxlength=300) |
| 186 | |
| 187 | class Meta: |
| 188 | db_table = 'iteration_attributes' |
| 189 | |
| 190 | |
jadmanski | 430dca9 | 2008-12-16 20:56:53 +0000 | [diff] [blame] | 191 | class IterationResult(dbmodels.Model, model_logic.ModelExtensions): |
| 192 | # see comment on TestAttribute regarding primary_key=True |
| 193 | test = dbmodels.ForeignKey(Test, db_column='test_idx', primary_key=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 194 | iteration = dbmodels.IntegerField() |
| 195 | attribute = dbmodels.CharField(maxlength=90) |
| 196 | value = dbmodels.FloatField(null=True, max_digits=12, decimal_places=31, |
| 197 | blank=True) |
| 198 | |
| 199 | class Meta: |
| 200 | db_table = 'iteration_result' |
| 201 | |
| 202 | |
| 203 | class TestLabel(dbmodels.Model, model_logic.ModelExtensions): |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 204 | name = dbmodels.CharField(maxlength=80, unique=True) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 205 | description = dbmodels.TextField(blank=True) |
| 206 | tests = dbmodels.ManyToManyField(Test, blank=True, |
| 207 | filter_interface=dbmodels.HORIZONTAL) |
| 208 | |
| 209 | name_field = 'name' |
| 210 | |
| 211 | class Meta: |
| 212 | db_table = 'test_labels' |
| 213 | |
| 214 | |
| 215 | class SavedQuery(dbmodels.Model, model_logic.ModelExtensions): |
| 216 | # TODO: change this to foreign key once DBs are merged |
| 217 | owner = dbmodels.CharField(maxlength=80) |
| 218 | name = dbmodels.CharField(maxlength=100) |
| 219 | url_token = dbmodels.TextField() |
| 220 | |
| 221 | class Meta: |
| 222 | db_table = 'saved_queries' |
| 223 | |
| 224 | |
showard | ce12f55 | 2008-09-19 00:48:59 +0000 | [diff] [blame] | 225 | class EmbeddedGraphingQuery(dbmodels.Model, model_logic.ModelExtensions): |
| 226 | url_token = dbmodels.TextField(null=False, blank=False) |
| 227 | graph_type = dbmodels.CharField(maxlength=16, null=False, blank=False) |
| 228 | params = dbmodels.TextField(null=False, blank=False) |
| 229 | last_updated = dbmodels.DateTimeField(null=False, blank=False, |
| 230 | editable=False) |
| 231 | # refresh_time shows the time at which a thread is updating the cached |
| 232 | # image, or NULL if no one is updating the image. This is used so that only |
| 233 | # one thread is updating the cached image at a time (see |
| 234 | # graphing_utils.handle_plot_request) |
| 235 | refresh_time = dbmodels.DateTimeField(editable=False) |
| 236 | cached_png = dbmodels.TextField(editable=False) |
| 237 | |
| 238 | class Meta: |
| 239 | db_table = 'embedded_graphing_queries' |
| 240 | |
| 241 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 242 | # views |
| 243 | |
| 244 | class TestViewManager(TempManager): |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 245 | def get_query_set(self): |
| 246 | query = super(TestViewManager, self).get_query_set() |
showard | 5bf7c50 | 2008-08-20 01:22:22 +0000 | [diff] [blame] | 247 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 248 | # add extra fields to selects, using the SQL itself as the "alias" |
| 249 | extra_select = dict((sql, sql) |
| 250 | for sql in self.model.extra_fields.iterkeys()) |
| 251 | return query.extra(select=extra_select) |
| 252 | |
| 253 | |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 254 | def add_join(self, query_set, join_table, join_key, |
| 255 | join_condition='', suffix='', exclude=False, |
| 256 | force_left_join=False): |
| 257 | return super(TestViewManager, self).add_join( |
| 258 | query_set, join_table, join_key, local_join_key='test_idx', |
| 259 | join_condition=join_condition, suffix=suffix, exclude=exclude, |
| 260 | force_left_join=force_left_join) |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 261 | |
| 262 | |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 263 | def _get_include_exclude_suffix(self, exclude): |
| 264 | if exclude: |
| 265 | suffix = '_exclude' |
| 266 | else: |
| 267 | suffix = '_include' |
| 268 | return suffix |
| 269 | |
| 270 | |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 271 | def _add_label_joins(self, query_set, suffix=''): |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 272 | query_set = self.add_join(query_set, 'test_labels_tests', |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 273 | join_key='test_id', suffix=suffix, |
| 274 | force_left_join=True) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 275 | |
| 276 | second_join_alias = 'test_labels' + suffix |
| 277 | second_join_condition = ('%s.id = %s.testlabel_id' % |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 278 | (second_join_alias, |
| 279 | 'test_labels_tests' + suffix)) |
| 280 | filter_object = self._CustomSqlQ() |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 281 | filter_object.add_join('test_labels', |
| 282 | second_join_condition, |
| 283 | 'LEFT JOIN', |
| 284 | alias=second_join_alias) |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 285 | return query_set.filter(filter_object) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 286 | |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 287 | |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 288 | def _add_attribute_join(self, query_set, join_condition='', suffix=None, |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 289 | exclude=False): |
showard | 93caf2d | 2009-05-08 18:24:22 +0000 | [diff] [blame^] | 290 | join_condition = self.escape_user_sql(join_condition) |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 291 | if suffix is None: |
| 292 | suffix = self._get_include_exclude_suffix(exclude) |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 293 | return self.add_join(query_set, 'test_attributes', |
| 294 | join_key='test_idx', |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 295 | join_condition=join_condition, |
| 296 | suffix=suffix, exclude=exclude) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 297 | |
| 298 | |
| 299 | def _get_label_ids_from_names(self, label_names): |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 300 | if not label_names: |
| 301 | return [] |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 302 | query = TestLabel.objects.filter(name__in=label_names).values('id') |
showard | fc8c6ae | 2008-11-11 19:06:01 +0000 | [diff] [blame] | 303 | return [str(label['id']) for label in query] |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 304 | |
| 305 | |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 306 | def get_query_set_with_joins(self, filter_data, include_host_labels=False): |
showard | fc8c6ae | 2008-11-11 19:06:01 +0000 | [diff] [blame] | 307 | include_labels = filter_data.pop('include_labels', []) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 308 | exclude_labels = filter_data.pop('exclude_labels', []) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 309 | query_set = self.get_query_set() |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 310 | joined = False |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 311 | # TODO: make this check more thorough if necessary |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 312 | extra_where = filter_data.get('extra_where', '') |
| 313 | if 'test_labels' in extra_where: |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 314 | query_set = self._add_label_joins(query_set) |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 315 | joined = True |
| 316 | |
showard | fc8c6ae | 2008-11-11 19:06:01 +0000 | [diff] [blame] | 317 | include_label_ids = self._get_label_ids_from_names(include_labels) |
| 318 | if include_label_ids: |
| 319 | # TODO: Factor this out like what's done with attributes |
| 320 | condition = ('test_labels_tests_include.testlabel_id IN (%s)' % |
| 321 | ','.join(include_label_ids)) |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 322 | query_set = self.add_join(query_set, 'test_labels_tests', |
showard | fc8c6ae | 2008-11-11 19:06:01 +0000 | [diff] [blame] | 323 | join_key='test_id', |
| 324 | suffix='_include', |
| 325 | join_condition=condition) |
| 326 | joined = True |
| 327 | |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 328 | exclude_label_ids = self._get_label_ids_from_names(exclude_labels) |
| 329 | if exclude_label_ids: |
| 330 | condition = ('test_labels_tests_exclude.testlabel_id IN (%s)' % |
showard | fc8c6ae | 2008-11-11 19:06:01 +0000 | [diff] [blame] | 331 | ','.join(exclude_label_ids)) |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 332 | query_set = self.add_join(query_set, 'test_labels_tests', |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 333 | join_key='test_id', |
| 334 | suffix='_exclude', |
| 335 | join_condition=condition, |
| 336 | exclude=True) |
| 337 | joined = True |
| 338 | |
| 339 | include_attributes_where = filter_data.pop('include_attributes_where', |
| 340 | '') |
| 341 | exclude_attributes_where = filter_data.pop('exclude_attributes_where', |
| 342 | '') |
| 343 | if include_attributes_where: |
| 344 | query_set = self._add_attribute_join( |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 345 | query_set, join_condition=include_attributes_where) |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 346 | joined = True |
| 347 | if exclude_attributes_where: |
| 348 | query_set = self._add_attribute_join( |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 349 | query_set, join_condition=exclude_attributes_where, |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 350 | exclude=True) |
| 351 | joined = True |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 352 | |
| 353 | if not joined: |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 354 | filter_data['no_distinct'] = True |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 355 | |
showard | f248952 | 2008-10-23 23:08:00 +0000 | [diff] [blame] | 356 | if include_host_labels or 'test_attributes_host_labels' in extra_where: |
| 357 | query_set = self._add_attribute_join( |
| 358 | query_set, suffix='_host_labels', |
| 359 | join_condition='test_attributes_host_labels.attribute = ' |
| 360 | '"host-labels"') |
| 361 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 362 | return query_set |
| 363 | |
| 364 | |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 365 | def query_test_ids(self, filter_data): |
| 366 | dicts = self.model.query_objects(filter_data).values('test_idx') |
| 367 | return [item['test_idx'] for item in dicts] |
| 368 | |
| 369 | |
| 370 | def _custom_select_query(self, query_set, selects): |
| 371 | query_selects, where, params = query_set._get_sql_clause() |
| 372 | if query_set._distinct: |
| 373 | distinct = 'DISTINCT ' |
| 374 | else: |
| 375 | distinct = '' |
| 376 | sql_query = 'SELECT ' + distinct + ','.join(selects) + where |
showard | 56e9377 | 2008-10-06 10:06:22 +0000 | [diff] [blame] | 377 | cursor = readonly_connection.connection().cursor() |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 378 | cursor.execute(sql_query, params) |
| 379 | return cursor.fetchall() |
| 380 | |
| 381 | |
| 382 | def query_test_label_ids(self, filter_data): |
showard | d50ffb4 | 2008-09-04 02:47:45 +0000 | [diff] [blame] | 383 | query_set = self.model.query_objects(filter_data) |
| 384 | query_set = self._add_label_joins(query_set, suffix='_list') |
| 385 | rows = self._custom_select_query(query_set, ['test_labels_list.id']) |
| 386 | return [row[0] for row in rows if row[0] is not None] |
showard | 0281350 | 2008-08-20 20:52:56 +0000 | [diff] [blame] | 387 | |
| 388 | |
showard | eaccf8f | 2009-04-16 03:11:33 +0000 | [diff] [blame] | 389 | def escape_user_sql(self, sql): |
| 390 | sql = super(TestViewManager, self).escape_user_sql(sql) |
| 391 | return sql.replace('test_idx', self.get_key_on_this_table('test_idx')) |
| 392 | |
| 393 | |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 394 | class TestView(dbmodels.Model, model_logic.ModelExtensions): |
| 395 | extra_fields = { |
| 396 | 'DATE(test_finished_time)' : 'test finished day', |
| 397 | } |
| 398 | |
| 399 | group_fields = [ |
| 400 | 'test_name', |
| 401 | 'status', |
| 402 | 'kernel', |
| 403 | 'hostname', |
| 404 | 'job_tag', |
| 405 | 'job_name', |
| 406 | 'platform', |
| 407 | 'reason', |
| 408 | 'job_owner', |
showard | 35bcba2 | 2008-10-07 11:07:27 +0000 | [diff] [blame] | 409 | 'test_started_time', |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 410 | 'test_finished_time', |
| 411 | 'DATE(test_finished_time)', |
| 412 | ] |
| 413 | |
| 414 | test_idx = dbmodels.IntegerField('test index', primary_key=True) |
| 415 | job_idx = dbmodels.IntegerField('job index', null=True, blank=True) |
| 416 | test_name = dbmodels.CharField(blank=True, maxlength=90) |
| 417 | subdir = dbmodels.CharField('subdirectory', blank=True, maxlength=180) |
| 418 | kernel_idx = dbmodels.IntegerField('kernel index') |
| 419 | status_idx = dbmodels.IntegerField('status index') |
| 420 | reason = dbmodels.CharField(blank=True, maxlength=3072) |
| 421 | machine_idx = dbmodels.IntegerField('host index') |
| 422 | test_started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 423 | test_finished_time = dbmodels.DateTimeField(null=True, blank=True) |
| 424 | job_tag = dbmodels.CharField(blank=True, maxlength=300) |
| 425 | job_name = dbmodels.CharField(blank=True, maxlength=300) |
| 426 | job_owner = dbmodels.CharField('owner', blank=True, maxlength=240) |
| 427 | job_queued_time = dbmodels.DateTimeField(null=True, blank=True) |
| 428 | job_started_time = dbmodels.DateTimeField(null=True, blank=True) |
| 429 | job_finished_time = dbmodels.DateTimeField(null=True, blank=True) |
| 430 | hostname = dbmodels.CharField(blank=True, maxlength=300) |
| 431 | platform = dbmodels.CharField(blank=True, maxlength=240) |
| 432 | machine_owner = dbmodels.CharField(blank=True, maxlength=240) |
| 433 | kernel_hash = dbmodels.CharField(blank=True, maxlength=105) |
| 434 | kernel_base = dbmodels.CharField(blank=True, maxlength=90) |
| 435 | kernel = dbmodels.CharField(blank=True, maxlength=300) |
| 436 | status = dbmodels.CharField(blank=True, maxlength=30) |
| 437 | |
| 438 | objects = TestViewManager() |
| 439 | |
| 440 | def save(self): |
| 441 | raise NotImplementedError('TestView is read-only') |
| 442 | |
| 443 | |
| 444 | def delete(self): |
| 445 | raise NotImplementedError('TestView is read-only') |
| 446 | |
| 447 | |
| 448 | @classmethod |
| 449 | def query_objects(cls, filter_data, initial_query=None): |
| 450 | if initial_query is None: |
showard | 64aeecd | 2008-09-19 21:32:58 +0000 | [diff] [blame] | 451 | initial_query = cls.objects.get_query_set_with_joins(filter_data) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 452 | return super(TestView, cls).query_objects(filter_data, |
| 453 | initial_query=initial_query) |
| 454 | |
| 455 | |
| 456 | @classmethod |
showard | e732ee7 | 2008-09-23 19:15:43 +0000 | [diff] [blame] | 457 | def list_objects(cls, filter_data, initial_query=None, fields=None): |
| 458 | # include extra fields |
| 459 | if fields is None: |
| 460 | fields = cls.get_field_dict().keys() + cls.extra_fields.keys() |
| 461 | return super(TestView, cls).list_objects(filter_data, initial_query, |
| 462 | fields) |
showard | 3544486 | 2008-08-07 22:35:30 +0000 | [diff] [blame] | 463 | |
| 464 | |
| 465 | class Meta: |
| 466 | db_table = 'test_view_2' |