Home | History | Annotate | Download | only in model
      1 <!--
      2 Copyright 2014 The Chromium Authors. All rights reserved.
      3 Use of this source code is governed by a BSD-style license that can be
      4 found in the LICENSE file.
      5 -->
      6 
      7 <link rel="import" href="ct-commit-list.html">
      8 
      9 <script>
     10 function CTFailureGroup(key, data, category) {
     11   this.key = key;
     12   this.data = data;
     13   this._annotation = CTFailureGroup._mergeAnnotations(data.getAnnotations());
     14   this._originalCategory = category || 'default';
     15   this._computeProperties();
     16 }
     17 
     18 CTFailureGroup.prototype.snoozeUntil = function(time) {
     19   return this._annotate({
     20     snoozeTime: time,
     21   });
     22 };
     23 
     24 CTFailureGroup.prototype.unsnooze = function() {
     25   return this._annotate({
     26     snoozeTime: undefined,
     27   });
     28 };
     29 
     30 CTFailureGroup.prototype.setBug = function(bug) {
     31   if (/^[0-9]+$/.test(bug))
     32     bug = 'https://crbug.com/' + bug;
     33   return this._annotate({
     34     bug: bug,
     35   });
     36 };
     37 
     38 CTFailureGroup.prototype.clearBug = function(bug) {
     39   return this._annotate({
     40     bug: undefined,
     41   });
     42 };
     43 
     44 CTFailureGroup.prototype._failedOnce = function() {
     45   return this.data.failedOnce && this.data.failedOnce();
     46 }
     47 
     48 CTFailureGroup.prototype._computeProperties = function() {
     49   this.isSnoozed = Date.now() < this._annotation.snoozeTime;
     50   if (this.isSnoozed) {
     51     this.category = 'snoozed';
     52   } else {
     53     if (this._failedOnce()) {
     54       this.category = 'failedOnce';
     55     } else {
     56       this.category = this._originalCategory;
     57     }
     58     // FIXME: crbug.com/400397 Split Tree closers into their own list.
     59   }
     60 
     61   this.bug = this._annotation.bug;
     62   // FIXME: Bug labels would be simpler to implement as a filter in the UI.
     63   if (this.bug != null)
     64     this.bugLabel = 'Bug ' + /([0-9]{3,})/.exec(this.bug)[0];
     65   else
     66     this.bugLabel = undefined;
     67 };
     68 
     69 CTFailureGroup._mergeAnnotations = function(failureAnnotations) {
     70   // FIXME: This should be a union of all bugs.
     71   var bug = failureAnnotations.map('bug').compact().first();
     72 
     73   // The group is only snoozed if all the failures specify a snooze-time, and only
     74   // until the first has elapsed.
     75   var snoozeTimes = failureAnnotations.map('snoozeTime').compact();
     76   var snoozeTime = snoozeTimes.length < failureAnnotations.length ? undefined : snoozeTimes.min();
     77 
     78   var annotation = {};
     79   if (bug != null) {
     80     annotation.bug = bug;
     81   }
     82   if (snoozeTime != null) {
     83     annotation.snoozeTime = snoozeTime;
     84   }
     85   return annotation;
     86 };
     87 
     88 CTFailureGroup.prototype._annotate = function(newAnnotation) {
     89   var failureAnnotations = [];
     90   // FIXME: Post the new annotation to frontend rather than storing locally.
     91   return CTFailureGroup.fetchAnnotations().then(function(annotations) {
     92     this.data.failureKeys().forEach(function(failureKey) {
     93       var annotation = annotations[failureKey] || {};
     94 
     95       Object.keys(newAnnotation, function(key, value) {
     96         if (value === undefined) {
     97           delete annotation[key];
     98         } else {
     99           annotation[key] = value;
    100         }
    101       });
    102 
    103       if (Object.size(annotation) == 0) {
    104         delete annotations[failureKey];
    105       } else {
    106         annotations[failureKey] = annotation;
    107         failureAnnotations.push(annotation);
    108       }
    109     });
    110 
    111     localStorage.CTFailureGroupAnnotations = JSON.stringify(annotations);
    112     this._annotation = CTFailureGroup._mergeAnnotations(failureAnnotations);
    113     this._computeProperties();
    114   }.bind(this));
    115 };
    116 
    117 CTFailureGroup.fetchAnnotations = function() {
    118   // FIXME: Fetch annotations from frontend.
    119   var stored = localStorage.CTFailureGroupAnnotations;
    120   var annotations = stored ? JSON.parse(stored) : {};
    121   return Promise.resolve(annotations);
    122 };
    123 </script>
    124