VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testmanager/core/failurereason.py@ 62548

Last change on this file since 62548 was 62484, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.6 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: failurereason.py 62484 2016-07-22 18:35:33Z vboxsync $
3
4"""
5Test Manager - Failure Reasons.
6"""
7
8__copyright__ = \
9"""
10Copyright (C) 2012-2016 Oracle Corporation
11
12This file is part of VirtualBox Open Source Edition (OSE), as
13available from http://www.virtualbox.org. This file is free software;
14you can redistribute it and/or modify it under the terms of the GNU
15General Public License (GPL) as published by the Free Software
16Foundation, in version 2 as it comes in the "COPYING" file of the
17VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19
20The contents of this file may alternatively be used under the terms
21of the Common Development and Distribution License Version 1.0
22(CDDL) only, as it comes in the "COPYING.CDDL" file of the
23VirtualBox OSE distribution, in which case the provisions of the
24CDDL are applicable instead of those of the GPL.
25
26You may elect to license modified versions of this file under the
27terms and conditions of either the GPL or the CDDL or both.
28"""
29__version__ = "$Revision: 62484 $"
30
31
32# Validation Kit imports.
33from testmanager.core.base import ModelDataBase, ModelLogicBase, TMRowNotFound, TMInvalidData, TMRowInUse, \
34 AttributeChangeEntry, ChangeLogEntry;
35from testmanager.core.useraccount import UserAccountLogic;
36
37
38
39class FailureReasonData(ModelDataBase):
40 """
41 Failure Reason Data.
42 """
43
44 ksIdAttr = 'idFailureReason';
45
46 ksParam_idFailureReason = 'FailureReasonData_idFailureReason'
47 ksParam_tsEffective = 'FailureReasonData_tsEffective'
48 ksParam_tsExpire = 'FailureReasonData_tsExpire'
49 ksParam_uidAuthor = 'FailureReasonData_uidAuthor'
50 ksParam_idFailureCategory = 'FailureReasonData_idFailureCategory'
51 ksParam_sShort = 'FailureReasonData_sShort'
52 ksParam_sFull = 'FailureReasonData_sFull'
53 ksParam_iTicket = 'FailureReasonData_iTicket'
54 ksParam_asUrls = 'FailureReasonData_asUrls'
55
56 kasAllowNullAttributes = [ 'idFailureReason', 'tsEffective', 'tsExpire',
57 'uidAuthor', 'iTicket', 'asUrls' ]
58
59 def __init__(self):
60 ModelDataBase.__init__(self);
61
62 #
63 # Initialize with defaults.
64 # See the database for explanations of each of these fields.
65 #
66
67 self.idFailureReason = None
68 self.tsEffective = None
69 self.tsExpire = None
70 self.uidAuthor = None
71 self.idFailureCategory = None
72 self.sShort = None
73 self.sFull = None
74 self.iTicket = None
75 self.asUrls = None
76
77 def initFromDbRow(self, aoRow):
78 """
79 Re-initializes the data with a row from a SELECT * FROM FailureReasons.
80
81 Returns self. Raises exception if the row is None or otherwise invalid.
82 """
83
84 if aoRow is None:
85 raise TMRowNotFound('Failure Reason not found.');
86
87 self.idFailureReason = aoRow[0]
88 self.tsEffective = aoRow[1]
89 self.tsExpire = aoRow[2]
90 self.uidAuthor = aoRow[3]
91 self.idFailureCategory = aoRow[4]
92 self.sShort = aoRow[5]
93 self.sFull = aoRow[6]
94 self.iTicket = aoRow[7]
95 self.asUrls = aoRow[8]
96
97 return self;
98
99 def initFromDbWithId(self, oDb, idFailureReason, tsNow = None, sPeriodBack = None):
100 """
101 Initialize from the database, given the ID of a row.
102 """
103 oDb.execute(self.formatSimpleNowAndPeriodQuery(oDb,
104 'SELECT *\n'
105 'FROM FailureReasons\n'
106 'WHERE idFailureReason = %s\n'
107 , ( idFailureReason,), tsNow, sPeriodBack));
108 aoRow = oDb.fetchOne()
109 if aoRow is None:
110 raise TMRowNotFound('idFailureReason=%s not found (tsNow=%s sPeriodBack=%s)'
111 % (idFailureReason, tsNow, sPeriodBack,));
112 return self.initFromDbRow(aoRow);
113
114
115class FailureReasonDataEx(FailureReasonData):
116 """
117 Failure Reason Data, extended version that includes the category.
118 """
119
120 def __init__(self):
121 FailureReasonData.__init__(self);
122 self.oCategory = None;
123 self.oAuthor = None;
124
125 def initFromDbRowEx(self, aoRow, oCategoryLogic, oUserAccountLogic):
126 """
127 Re-initializes the data with a row from a SELECT * FROM FailureReasons.
128
129 Returns self. Raises exception if the row is None or otherwise invalid.
130 """
131
132 self.initFromDbRow(aoRow);
133 self.oCategory = oCategoryLogic.cachedLookup(self.idFailureCategory);
134 self.oAuthor = oUserAccountLogic.cachedLookup(self.uidAuthor);
135
136 return self;
137
138
139class FailureReasonLogic(ModelLogicBase): # pylint: disable=R0903
140 """
141 Failure Reason logic.
142 """
143
144 def __init__(self, oDb):
145 ModelLogicBase.__init__(self, oDb)
146 self.dCache = None;
147 self.dCacheNameAndCat = None;
148 self.oCategoryLogic = None;
149 self.oUserAccountLogic = None;
150
151 def fetchForListing(self, iStart, cMaxRows, tsNow):
152 """
153 Fetches Failure Category records.
154
155 Returns an array (list) of FailureReasonDataEx items, empty list if none.
156 Raises exception on error.
157 """
158 self._ensureCachesPresent();
159
160 if tsNow is None:
161 self._oDb.execute('SELECT FailureReasons.*,\n'
162 ' FailureCategories.sShort AS sCategory\n'
163 'FROM FailureReasons,\n'
164 ' FailureCategories\n'
165 'WHERE FailureReasons.tsExpire = \'infinity\'::TIMESTAMP\n'
166 ' AND FailureCategories.idFailureCategory = FailureReasons.idFailureCategory\n'
167 ' AND FailureCategories.tsExpire = \'infinity\'::TIMESTAMP\n'
168 'ORDER BY sCategory ASC, sShort ASC\n'
169 'LIMIT %s OFFSET %s\n'
170 , (cMaxRows, iStart,));
171 else:
172 self._oDb.execute('SELECT FailureReasons.*,\n'
173 ' FailureCategories.sShort AS sCategory\n'
174 'FROM FailureReasons,\n'
175 ' FailureCategories\n'
176 'WHERE FailureReasons.tsExpire > %s\n'
177 ' AND FailureReasons.tsEffective <= %s\n'
178 ' AND FailureCategories.idFailureCategory = FailureReasons.idFailureCategory\n'
179 ' AND FailureReasons.tsExpire > %s\n'
180 ' AND FailureReasons.tsEffective <= %s\n'
181 'ORDER BY sCategory ASC, sShort ASC\n'
182 'LIMIT %s OFFSET %s\n'
183 , (tsNow, tsNow, tsNow, tsNow, cMaxRows, iStart,));
184
185 aoRows = []
186 for aoRow in self._oDb.fetchAll():
187 aoRows.append(FailureReasonDataEx().initFromDbRowEx(aoRow, self.oCategoryLogic, self.oUserAccountLogic));
188 return aoRows
189
190 def fetchForListingInCategory(self, iStart, cMaxRows, tsNow, idFailureCategory):
191 """
192 Fetches Failure Category records.
193
194 Returns an array (list) of FailureReasonDataEx items, empty list if none.
195 Raises exception on error.
196 """
197 self._ensureCachesPresent();
198
199 if tsNow is None:
200 self._oDb.execute('SELECT *\n'
201 'FROM FailureReasons\n'
202 'WHERE tsExpire = \'infinity\'::TIMESTAMP\n'
203 ' AND idFailureCategory = %s\n'
204 'ORDER BY sShort ASC\n'
205 'LIMIT %s OFFSET %s\n'
206 , ( idFailureCategory, cMaxRows, iStart,));
207 else:
208 self._oDb.execute('SELECT *\n'
209 'FROM FailureReasons\n'
210 'WHERE idFailureCategory = %s\n'
211 ' AND tsExpire > %s\n'
212 ' AND tsEffective <= %s\n'
213 'ORDER BY sShort ASC\n'
214 'LIMIT %s OFFSET %s\n'
215 , ( tsNow, tsNow, idFailureCategory, cMaxRows, iStart,));
216
217 aoRows = []
218 for aoRow in self._oDb.fetchAll():
219 aoRows.append(FailureReasonDataEx().initFromDbRowEx(aoRow, self.oCategoryLogic, self.oUserAccountLogic));
220 return aoRows
221
222
223 def fetchForSheriffByNamedCategory(self, sFailureCategory):
224 """
225 Fetches the short names of the reasons in the named category.
226
227 Returns array of strings.
228 Raises exception on error.
229 """
230 self._oDb.execute('SELECT FailureReasons.sShort\n'
231 'FROM FailureReasons,\n'
232 ' FailureCategories\n'
233 'WHERE FailureReasons.tsExpire = \'infinity\'::TIMESTAMP\n'
234 ' AND FailureReasons.idFailureCategory = FailureCategories.idFailureCategory\n'
235 ' AND FailureCategories.sShort = %s\n'
236 'ORDER BY FailureReasons.sShort ASC\n'
237 , ( sFailureCategory,));
238 return [aoRow[0] for aoRow in self._oDb.fetchAll()];
239
240
241 def fetchForCombo(self, sFirstEntry = 'Select a failure reason', tsEffective = None):
242 """
243 Gets the list of Failure Reasons for a combo box.
244 Returns an array of (value [idFailureReason], drop-down-name [sShort],
245 hover-text [sFull]) tuples.
246 """
247 if tsEffective is None:
248 self._oDb.execute('SELECT fr.idFailureReason, CONCAT(fc.sShort, \' / \', fr.sShort) as sComboText, fr.sFull\n'
249 'FROM FailureReasons fr,\n'
250 ' FailureCategories fc\n'
251 'WHERE fr.idFailureCategory = fc.idFailureCategory\n'
252 ' AND fr.tsExpire = \'infinity\'::TIMESTAMP\n'
253 ' AND fc.tsExpire = \'infinity\'::TIMESTAMP\n'
254 'ORDER BY sComboText')
255 else:
256 self._oDb.execute('SELECT fr.idFailureReason, CONCAT(fc.sShort, \' / \', fr.sShort) as sComboText, fr.sFull\n'
257 'FROM FailureReasons fr,\n'
258 ' FailureCategories fc\n'
259 'WHERE fr.idFailureCategory = fc.idFailureCategory\n'
260 ' AND fr.tsExpire > %s\n'
261 ' AND fr.tsEffective <= %s\n'
262 ' AND fc.tsExpire > %s\n'
263 ' AND fc.tsEffective <= %s\n'
264 'ORDER BY sComboText'
265 , (tsEffective, tsEffective, tsEffective, tsEffective));
266 aoRows = self._oDb.fetchAll();
267 return [(-1, sFirstEntry, '')] + aoRows;
268
269
270 def fetchForChangeLog(self, idFailureReason, iStart, cMaxRows, tsNow): # pylint: disable=R0914
271 """
272 Fetches change log entries for a failure reason.
273
274 Returns an array of ChangeLogEntry instance and an indicator whether
275 there are more entries.
276 Raises exception on error.
277 """
278 self._ensureCachesPresent();
279
280 if tsNow is None:
281 tsNow = self._oDb.getCurrentTimestamp();
282
283 # 1. Get a list of the relevant changes.
284 self._oDb.execute('SELECT * FROM FailureReasons WHERE idFailureReason = %s AND tsEffective <= %s\n'
285 'ORDER BY tsEffective DESC\n'
286 'LIMIT %s OFFSET %s\n'
287 , ( idFailureReason, tsNow, cMaxRows + 1, iStart, ));
288 aoRows = [];
289 for aoChange in self._oDb.fetchAll():
290 aoRows.append(FailureReasonData().initFromDbRow(aoChange));
291
292 # 2. Calculate the changes.
293 aoEntries = [];
294 for i in xrange(0, len(aoRows) - 1):
295 oNew = aoRows[i];
296 oOld = aoRows[i + 1];
297
298 aoChanges = [];
299 for sAttr in oNew.getDataAttributes():
300 if sAttr not in [ 'tsEffective', 'tsExpire', 'uidAuthor', ]:
301 oOldAttr = getattr(oOld, sAttr);
302 oNewAttr = getattr(oNew, sAttr);
303 if oOldAttr != oNewAttr:
304 if sAttr == 'idFailureCategory':
305 oCat = self.oCategoryLogic.cachedLookup(oOldAttr);
306 if oCat is not None:
307 oOldAttr = '%s (%s)' % (oOldAttr, oCat.sShort, );
308 oCat = self.oCategoryLogic.cachedLookup(oNewAttr);
309 if oCat is not None:
310 oNewAttr = '%s (%s)' % (oNewAttr, oCat.sShort, );
311 aoChanges.append(AttributeChangeEntry(sAttr, oNewAttr, oOldAttr, str(oNewAttr), str(oOldAttr)));
312
313 aoEntries.append(ChangeLogEntry(oNew.uidAuthor, None, oNew.tsEffective, oNew.tsExpire, oNew, oOld, aoChanges));
314
315 # If we're at the end of the log, add the initial entry.
316 if len(aoRows) <= cMaxRows and len(aoRows) > 0:
317 oNew = aoRows[-1];
318 aoEntries.append(ChangeLogEntry(oNew.uidAuthor, None, oNew.tsEffective, oNew.tsExpire, oNew, None, []));
319
320 return (UserAccountLogic(self._oDb).resolveChangeLogAuthors(aoEntries), len(aoRows) > cMaxRows);
321
322
323 def getById(self, idFailureReason):
324 """Get Failure Reason data by idFailureReason"""
325
326 self._oDb.execute('SELECT *\n'
327 'FROM FailureReasons\n'
328 'WHERE tsExpire = \'infinity\'::timestamp\n'
329 ' AND idFailureReason = %s;', (idFailureReason,))
330 aRows = self._oDb.fetchAll()
331 if len(aRows) not in (0, 1):
332 raise self._oDb.integrityException(
333 'Found more than one failure reasons with the same credentials. Database structure is corrupted.')
334 try:
335 return FailureReasonData().initFromDbRow(aRows[0])
336 except IndexError:
337 return None
338
339
340 def addEntry(self, oData, uidAuthor, fCommit = False):
341 """
342 Add a failure reason.
343 """
344 #
345 # Validate.
346 #
347 dErrors = oData.validateAndConvert(self._oDb, oData.ksValidateFor_Add);
348 if len(dErrors) > 0:
349 raise TMInvalidData('addEntry invalid input: %s' % (dErrors,));
350
351 #
352 # Add the record.
353 #
354 self._readdEntry(uidAuthor, oData);
355 self._oDb.maybeCommit(fCommit);
356 return True;
357
358
359 def editEntry(self, oData, uidAuthor, fCommit = False):
360 """
361 Modifies a failure reason.
362 """
363
364 #
365 # Validate inputs and read in the old(/current) data.
366 #
367 assert isinstance(oData, FailureReasonData);
368 dErrors = oData.validateAndConvert(self._oDb, oData.ksValidateFor_Edit);
369 if len(dErrors) > 0:
370 raise TMInvalidData('editEntry invalid input: %s' % (dErrors,));
371
372 oOldData = FailureReasonData().initFromDbWithId(self._oDb, oData.idFailureReason);
373
374 #
375 # Update the data that needs updating.
376 #
377 if not oData.isEqualEx(oOldData, [ 'tsEffective', 'tsExpire', 'uidAuthor', ]):
378 self._historizeEntry(oData.idFailureReason);
379 self._readdEntry(uidAuthor, oData);
380 self._oDb.maybeCommit(fCommit);
381 return True;
382
383
384 def removeEntry(self, uidAuthor, idFailureReason, fCascade = False, fCommit = False):
385 """
386 Deletes a failure reason.
387 """
388 _ = fCascade; # too complicated for now.
389
390 #
391 # Check whether it's being used by other tables and bitch if it is .
392 # We currently do not implement cascading.
393 #
394 self._oDb.execute('SELECT CONCAT(idBlacklisting, \' - blacklisting\')\n'
395 'FROM BuildBlacklist\n'
396 'WHERE idFailureReason = %s\n'
397 ' AND tsExpire = \'infinity\'::TIMESTAMP\n'
398 'UNION\n'
399 'SELECT CONCAT(idTestResult, \' - test result failure reason\')\n'
400 'FROM TestResultFailures\n'
401 'WHERE idFailureReason = %s\n'
402 ' AND tsExpire = \'infinity\'::TIMESTAMP\n'
403 , (idFailureReason, idFailureReason,));
404 aaoRows = self._oDb.fetchAll();
405 if len(aaoRows) > 0:
406 raise TMRowInUse('Cannot remove failure reason %u because its being used by: %s'
407 % (idFailureReason, ', '.join(aoRow[0] for aoRow in aaoRows),));
408
409 #
410 # Do the job.
411 #
412 oData = FailureReasonData().initFromDbWithId(self._oDb, idFailureReason);
413 assert oData.idFailureReason == idFailureReason;
414 (tsCur, tsCurMinusOne) = self._oDb.getCurrentTimestamps();
415 if oData.tsEffective != tsCur and oData.tsEffective != tsCurMinusOne:
416 self._historizeEntry(idFailureReason, tsCurMinusOne);
417 self._readdEntry(uidAuthor, oData, tsCurMinusOne);
418 self._historizeEntry(idFailureReason);
419 self._oDb.maybeCommit(fCommit);
420 return True;
421
422
423 def cachedLookup(self, idFailureReason):
424 """
425 Looks up the most recent FailureReasonDataEx object for idFailureReason
426 via an object cache.
427
428 Returns a shared FailureReasonData object. None if not found.
429 Raises exception on DB error.
430 """
431 if self.dCache is None:
432 self.dCache = self._oDb.getCache('FailureReasonDataEx');
433 oEntry = self.dCache.get(idFailureReason, None);
434 if oEntry is None:
435 self._oDb.execute('SELECT *\n'
436 'FROM FailureReasons\n'
437 'WHERE idFailureReason = %s\n'
438 ' AND tsExpire = \'infinity\'::TIMESTAMP\n'
439 , (idFailureReason, ));
440 if self._oDb.getRowCount() == 0:
441 # Maybe it was deleted, try get the last entry.
442 self._oDb.execute('SELECT *\n'
443 'FROM FailureReasons\n'
444 'WHERE idFailureReason = %s\n'
445 'ORDER BY tsExpire DESC\n'
446 'LIMIT 1\n'
447 , (idFailureReason, ));
448 elif self._oDb.getRowCount() > 1:
449 raise self._oDb.integrityException('%s infinity rows for %s' % (self._oDb.getRowCount(), idFailureReason));
450
451 if self._oDb.getRowCount() == 1:
452 self._ensureCachesPresent();
453 oEntry = FailureReasonDataEx().initFromDbRowEx(self._oDb.fetchOne(), self.oCategoryLogic,
454 self.oUserAccountLogic);
455 self.dCache[idFailureReason] = oEntry;
456 return oEntry;
457
458
459 def cachedLookupByNameAndCategory(self, sName, sCategory):
460 """
461 Looks up a failure reason by it's name and category.
462
463 Should the request be ambigiuos, we'll return the oldest one.
464
465 Returns a shared FailureReasonData object. None if not found.
466 Raises exception on DB error.
467 """
468 if self.dCacheNameAndCat is None:
469 self.dCacheNameAndCat = self._oDb.getCache('FailureReasonDataEx-By-Name-And-Category');
470 sKey = '%s:::%s' % (sName, sCategory,);
471 oEntry = self.dCacheNameAndCat.get(sKey, None);
472 if oEntry is None:
473 self._oDb.execute('SELECT *\n'
474 'FROM FailureReasons,\n'
475 ' FailureCategories\n'
476 'WHERE FailureReasons.sShort = %s\n'
477 ' AND FailureReasons.tsExpire = \'infinity\'::TIMESTAMP\n'
478 ' AND FailureReasons.idFailureCategory = FailureCategories.idFailureCategory '
479 ' AND FailureCategories.sShort = %s\n'
480 ' AND FailureCategories.tsExpire = \'infinity\'::TIMESTAMP\n'
481 'ORDER BY FailureReasons.tsEffective\n'
482 , ( sName, sCategory));
483 if self._oDb.getRowCount() == 0:
484 sLikeSucks = self._oDb.formatBindArgs(
485 'SELECT *\n'
486 'FROM FailureReasons,\n'
487 ' FailureCategories\n'
488 'WHERE ( FailureReasons.sShort ILIKE @@@@@@@! %s !@@@@@@@\n'
489 ' OR FailureReasons.sFull ILIKE @@@@@@@! %s !@@@@@@@)\n'
490 ' AND FailureCategories.tsExpire = \'infinity\'::TIMESTAMP\n'
491 ' AND FailureReasons.idFailureCategory = FailureCategories.idFailureCategory\n'
492 ' AND ( FailureCategories.sShort = %s\n'
493 ' OR FailureCategories.sFull = %s)\n'
494 ' AND FailureReasons.tsExpire = \'infinity\'::TIMESTAMP\n'
495 'ORDER BY FailureReasons.tsEffective\n'
496 , ( sName, sName, sCategory, sCategory ));
497 sLikeSucks = sLikeSucks.replace('LIKE @@@@@@@! \'', 'LIKE \'%').replace('\' !@@@@@@@', '%\'');
498 self._oDb.execute(sLikeSucks);
499 if self._oDb.getRowCount() > 0:
500 self._ensureCachesPresent();
501 oEntry = FailureReasonDataEx().initFromDbRowEx(self._oDb.fetchOne(), self.oCategoryLogic,
502 self.oUserAccountLogic);
503 self.dCacheNameAndCat[sKey] = oEntry;
504 if sName != oEntry.sShort or sCategory != oEntry.oCategory.sShort:
505 sKey2 = '%s:::%s' % (oEntry.sShort, oEntry.oCategory.sShort,);
506 self.dCacheNameAndCat[sKey2] = oEntry;
507 return oEntry;
508
509
510 #
511 # Helpers.
512 #
513
514 def _readdEntry(self, uidAuthor, oData, tsEffective = None):
515 """
516 Re-adds the FailureReasons entry. Used by addEntry, editEntry and removeEntry.
517 """
518 if tsEffective is None:
519 tsEffective = self._oDb.getCurrentTimestamp();
520 self._oDb.execute('INSERT INTO FailureReasons (\n'
521 ' uidAuthor,\n'
522 ' tsEffective,\n'
523 ' idFailureReason,\n'
524 ' idFailureCategory,\n'
525 ' sShort,\n'
526 ' sFull,\n'
527 ' iTicket,\n'
528 ' asUrls)\n'
529 'VALUES (%s, %s, '
530 + ( 'DEFAULT' if oData.idFailureReason is None else str(oData.idFailureReason) )
531 + ', %s, %s, %s, %s, %s)\n'
532 , ( uidAuthor,
533 tsEffective,
534 oData.idFailureCategory,
535 oData.sShort,
536 oData.sFull,
537 oData.iTicket,
538 oData.asUrls,) );
539 return True;
540
541
542 def _historizeEntry(self, idFailureReason, tsExpire = None):
543 """ Historizes the current entry. """
544 if tsExpire is None:
545 tsExpire = self._oDb.getCurrentTimestamp();
546 self._oDb.execute('UPDATE FailureReasons\n'
547 'SET tsExpire = %s\n'
548 'WHERE idFailureReason = %s\n'
549 ' AND tsExpire = \'infinity\'::TIMESTAMP\n'
550 , (tsExpire, idFailureReason,));
551 return True;
552
553
554 def _ensureCachesPresent(self):
555 """ Ensures we've got the cache references resolved. """
556 if self.oCategoryLogic is None:
557 from testmanager.core.failurecategory import FailureCategoryLogic;
558 self.oCategoryLogic = FailureCategoryLogic(self._oDb);
559 if self.oUserAccountLogic is None:
560 self.oUserAccountLogic = UserAccountLogic(self._oDb);
561 return True;
562
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette