aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/rebaseline_server/static/loader.js
blob: ccd29c205aa640edec00b6b1ea33732d5043a63f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
 * Loader:
 * Reads GM result reports written out by results.py, and imports
 * them into $scope.categories and $scope.testData .
 */
var Loader = angular.module(
    'Loader',
    []
);


// TODO(epoger): Combine ALL of our filtering operations (including
// truncation) into this one filter, so that runs most efficiently?
// (We would have to make sure truncation still took place after
// sorting, though.)
Loader.filter(
  'removeHiddenItems',
  function() {
    return function(unfilteredItems, hiddenResultTypes, hiddenConfigs,
                    builderSubstring, testSubstring, viewingTab) {
      var filteredItems = [];
      for (var i = 0; i < unfilteredItems.length; i++) {
        var item = unfilteredItems[i];
        // For performance, we examine the "set" objects directly rather
        // than calling $scope.isValueInSet().
        // Besides, I don't think we have access to $scope in here...
        if (!(true == hiddenResultTypes[item.resultType]) &&
            !(true == hiddenConfigs[item.config]) &&
            !(-1 == item.builder.indexOf(builderSubstring)) &&
            !(-1 == item.test.indexOf(testSubstring)) &&
            (viewingTab == item.tab)) {
          filteredItems.push(item);
        }
      }
      return filteredItems;
    };
  }
);


Loader.controller(
  'Loader.Controller',
    function($scope, $http, $filter, $location, $timeout) {
    $scope.windowTitle = "Loading GM Results...";
    var resultsToLoad = $location.search().resultsToLoad;
    $scope.loadingMessage = "Loading results of type '" + resultsToLoad +
        "', please wait...";

    /**
     * On initial page load, load a full dictionary of results.
     * Once the dictionary is loaded, unhide the page elements so they can
     * render the data.
     */
    $http.get("/results/" + resultsToLoad).success(
      function(data, status, header, config) {
        if (data.header.resultsStillLoading) {
          $scope.loadingMessage =
              "Server is still loading initial results; will retry at " +
              $scope.localTimeString(data.header.timeNextUpdateAvailable);
          $timeout(
              function(){location.reload();},
              (data.header.timeNextUpdateAvailable * 1000) - new Date().getTime());
        } else {
          $scope.loadingMessage = "Processing data, please wait...";

          $scope.header = data.header;
          $scope.categories = data.categories;
          $scope.testData = data.testData;
          $scope.sortColumn = 'weightedDiffMeasure';
          $scope.showTodos = false;

          $scope.showSubmitAdvancedSettings = false;
          $scope.submitAdvancedSettings = {};
          $scope.submitAdvancedSettings['reviewed-by-human'] = true;
          $scope.submitAdvancedSettings['ignore-failure'] = false;
          $scope.submitAdvancedSettings['bug'] = '';

          // Create the list of tabs (lists into which the user can file each
          // test).  This may vary, depending on isEditable.
          $scope.tabs = [
            'Unfiled', 'Hidden'
          ];
          if (data.header.isEditable) {
            $scope.tabs = $scope.tabs.concat(
                ['Pending Approval']);
          }
          $scope.defaultTab = $scope.tabs[0];
          $scope.viewingTab = $scope.defaultTab;

          // Track the number of results on each tab.
          $scope.numResultsPerTab = {};
          for (var i = 0; i < $scope.tabs.length; i++) {
            $scope.numResultsPerTab[$scope.tabs[i]] = 0;
          }
          $scope.numResultsPerTab[$scope.defaultTab] = $scope.testData.length;

          // Add index and tab fields to all records.
          for (var i = 0; i < $scope.testData.length; i++) {
            $scope.testData[i].index = i;
            $scope.testData[i].tab = $scope.defaultTab;
          }

          // Arrays within which the user can toggle individual elements.
          $scope.selectedItems = [];

          // Sets within which the user can toggle individual elements.
          $scope.hiddenResultTypes = {
            'failure-ignored': true,
            'no-comparison': true,
            'succeeded': true,
          };
          $scope.allResultTypes = Object.keys(data.categories['resultType']);
          $scope.hiddenConfigs = {};
          $scope.allConfigs = Object.keys(data.categories['config']);

          // Associative array of partial string matches per category.
          $scope.categoryValueMatch = {};
          $scope.categoryValueMatch.builder = "";
          $scope.categoryValueMatch.test = "";

          $scope.updateResults();
          $scope.loadingMessage = "";
          $scope.windowTitle = "Current GM Results";
        }
      }
    ).error(
      function(data, status, header, config) {
        $scope.loadingMessage = "Failed to load results of type '"
            + resultsToLoad + "'";
        $scope.windowTitle = "Failed to Load GM Results";
      }
    );


    //
    // Select/Clear/Toggle all tests.
    //

    /**
     * Select all currently showing tests.
     */
    $scope.selectAllItems = function() {
      var numItemsShowing = $scope.limitedTestData.length;
      for (var i = 0; i < numItemsShowing; i++) {
        var index = $scope.limitedTestData[i].index;
        if (!$scope.isValueInArray(index, $scope.selectedItems)) {
          $scope.toggleValueInArray(index, $scope.selectedItems);
        }
      }
    }

    /**
     * Deselect all currently showing tests.
     */
    $scope.clearAllItems = function() {
      var numItemsShowing = $scope.limitedTestData.length;
      for (var i = 0; i < numItemsShowing; i++) {
        var index = $scope.limitedTestData[i].index;
        if ($scope.isValueInArray(index, $scope.selectedItems)) {
          $scope.toggleValueInArray(index, $scope.selectedItems);
        }
      }
    }

    /**
     * Toggle selection of all currently showing tests.
     */
    $scope.toggleAllItems = function() {
      var numItemsShowing = $scope.limitedTestData.length;
      for (var i = 0; i < numItemsShowing; i++) {
        var index = $scope.limitedTestData[i].index;
        $scope.toggleValueInArray(index, $scope.selectedItems);
      }
    }


    //
    // Tab operations.
    //

    /**
     * Change the selected tab.
     *
     * @param tab (string): name of the tab to select
     */
    $scope.setViewingTab = function(tab) {
      $scope.viewingTab = tab;
      $scope.updateResults();
    }

    /**
     * Move the items in $scope.selectedItems to a different tab,
     * and then clear $scope.selectedItems.
     *
     * @param newTab (string): name of the tab to move the tests to
     */
    $scope.moveSelectedItemsToTab = function(newTab) {
      $scope.moveItemsToTab($scope.selectedItems, newTab);
      $scope.selectedItems = [];
      $scope.updateResults();
    }

    /**
     * Move a subset of $scope.testData to a different tab.
     *
     * @param itemIndices (array of ints): indices into $scope.testData
     *        indicating which test results to move
     * @param newTab (string): name of the tab to move the tests to
     */
    $scope.moveItemsToTab = function(itemIndices, newTab) {
      var itemIndex;
      var numItems = itemIndices.length;
      for (var i = 0; i < numItems; i++) {
        itemIndex = itemIndices[i];
        $scope.numResultsPerTab[$scope.testData[itemIndex].tab]--;
        $scope.testData[itemIndex].tab = newTab;
      }
      $scope.numResultsPerTab[newTab] += numItems;
    }


    //
    // updateResults() and friends.
    //

    /**
     * Set $scope.areUpdatesPending (to enable/disable the Update Results
     * button).
     *
     * TODO(epoger): We could reduce the amount of code by just setting the
     * variable directly (from, e.g., a button's ng-click handler).  But when
     * I tried that, the HTML elements depending on the variable did not get
     * updated.
     * It turns out that this is due to variable scoping within an ng-repeat
     * element; see http://stackoverflow.com/questions/15388344/behavior-of-assignment-expression-invoked-by-ng-click-within-ng-repeat
     *
     * @param val boolean value to set $scope.areUpdatesPending to
     */
    $scope.setUpdatesPending = function(val) {
      $scope.areUpdatesPending = val;
    }

    /**
     * Update the displayed results, based on filters/settings.
     */
    $scope.updateResults = function() {
      $scope.displayLimit = $scope.displayLimitPending;
      // TODO(epoger): Every time we apply a filter, AngularJS creates
      // another copy of the array.  Is there a way we can filter out
      // the items as they are displayed, rather than storing multiple
      // array copies?  (For better performance.)

      if ($scope.viewingTab == $scope.defaultTab) {

        // TODO(epoger): Until we allow the user to reverse sort order,
        // there are certain columns we want to sort in a different order.
        var doReverse = (
            ($scope.sortColumn == 'percentDifferingPixels') ||
            ($scope.sortColumn == 'weightedDiffMeasure'));

        $scope.filteredTestData =
            $filter("orderBy")(
                $filter("removeHiddenItems")(
                    $scope.testData,
                    $scope.hiddenResultTypes,
                    $scope.hiddenConfigs,
                    $scope.categoryValueMatch.builder,
                    $scope.categoryValueMatch.test,
                    $scope.viewingTab
                ),
                $scope.sortColumn, doReverse);
        $scope.limitedTestData = $filter("limitTo")(
            $scope.filteredTestData, $scope.displayLimit);
      } else {
        $scope.filteredTestData =
            $filter("orderBy")(
                $filter("filter")(
                    $scope.testData,
                    {tab: $scope.viewingTab},
                    true
                ),
                $scope.sortColumn);
        $scope.limitedTestData = $scope.filteredTestData;
      }
      $scope.imageSize = $scope.imageSizePending;
      $scope.setUpdatesPending(false);
    }

    /**
     * Re-sort the displayed results.
     *
     * @param sortColumn (string): name of the column to sort on
     */
    $scope.sortResultsBy = function(sortColumn) {
      $scope.sortColumn = sortColumn;
      $scope.updateResults();
    }

    /**
     * Set $scope.categoryValueMatch[name] = value, and update results.
     *
     * @param name
     * @param value
     */
    $scope.setCategoryValueMatch = function(name, value) {
      $scope.categoryValueMatch[name] = value;
      $scope.updateResults();
    }

    /**
     * Update $scope.hiddenResultTypes so that ONLY this resultType is showing,
     * and update the visible results.
     *
     * @param resultType
     */
    $scope.showOnlyResultType = function(resultType) {
      $scope.hiddenResultTypes = {};
      // TODO(epoger): Maybe change $scope.allResultTypes to be a Set like
      // $scope.hiddenResultTypes (rather than an array), so this operation is
      // simpler (just assign or add allResultTypes to hiddenResultTypes).
      $scope.toggleValuesInSet($scope.allResultTypes, $scope.hiddenResultTypes);
      $scope.toggleValueInSet(resultType, $scope.hiddenResultTypes);
      $scope.updateResults();
    }

    /**
     * Update $scope.hiddenConfigs so that ONLY this config is showing,
     * and update the visible results.
     *
     * @param config
     */
    $scope.showOnlyConfig = function(config) {
      $scope.hiddenConfigs = {};
      $scope.toggleValuesInSet($scope.allConfigs, $scope.hiddenConfigs);
      $scope.toggleValueInSet(config, $scope.hiddenConfigs);
      $scope.updateResults();
    }


    //
    // Operations for sending info back to the server.
    //

    /**
     * Tell the server that the actual results of these particular tests
     * are acceptable.
     *
     * @param testDataSubset an array of test results, most likely a subset of
     *        $scope.testData (perhaps with some modifications)
     */
    $scope.submitApprovals = function(testDataSubset) {
      $scope.submitPending = true;

      // Convert bug text field to null or 1-item array.
      var bugs = null;
      var bugNumber = parseInt($scope.submitAdvancedSettings['bug']);
      if (!isNaN(bugNumber)) {
        bugs = [bugNumber];
      }

      // TODO(epoger): This is a suboptimal way to prevent users from
      // rebaselining failures in alternative renderModes, but it does work.
      // For a better solution, see
      // https://code.google.com/p/skia/issues/detail?id=1748 ('gm: add new
      // result type, RenderModeMismatch')
      var encounteredComparisonConfig = false;

      var newResults = [];
      for (var i = 0; i < testDataSubset.length; i++) {
        var actualResult = testDataSubset[i];
        var expectedResult = {
          builder: actualResult['builder'],
          test: actualResult['test'],
          config: actualResult['config'],
          expectedHashType: actualResult['actualHashType'],
          expectedHashDigest: actualResult['actualHashDigest'],
        };
        if (0 == expectedResult.config.indexOf('comparison-')) {
          encounteredComparisonConfig = true;
        }

        // Advanced settings...
        expectedResult['reviewed-by-human'] =
            $scope.submitAdvancedSettings['reviewed-by-human'];
        if (true == $scope.submitAdvancedSettings['ignore-failure']) {
          // if it's false, don't send it at all (just keep the default)
          expectedResult['ignore-failure'] = true;
        }
        expectedResult['bugs'] = bugs;

        newResults.push(expectedResult);
      }
      if (encounteredComparisonConfig) {
        alert("Approval failed -- you cannot approve results with config " +
            "type comparison-*");
        $scope.submitPending = false;
        return;
      }
      $http({
        method: "POST",
        url: "/edits",
        data: {
          oldResultsType: $scope.header.type,
          oldResultsHash: $scope.header.dataHash,
          modifications: newResults
        }
      }).success(function(data, status, headers, config) {
        var itemIndicesToMove = [];
        for (var i = 0; i < testDataSubset.length; i++) {
          itemIndicesToMove.push(testDataSubset[i].index);
        }
        $scope.moveItemsToTab(itemIndicesToMove,
                              "HackToMakeSureThisItemDisappears");
        $scope.updateResults();
        alert("New baselines submitted successfully!\n\n" +
            "You still need to commit the updated expectations files on " +
            "the server side to the Skia repo.\n\n" +
            "Also: in order to see the complete updated data, or to submit " +
            "more baselines, you will need to reload your client.");
        $scope.submitPending = false;
      }).error(function(data, status, headers, config) {
        alert("There was an error submitting your baselines.\n\n" +
            "Please see server-side log for details.");
        $scope.submitPending = false;
      });
    }


    //
    // Operations we use to mimic Set semantics, in such a way that
    // checking for presence within the Set is as fast as possible.
    // But getting a list of all values within the Set is not necessarily
    // possible.
    // TODO(epoger): move into a separate .js file?
    //

    /**
     * Returns true if value "value" is present within set "set".
     *
     * @param value a value of any type
     * @param set an Object which we use to mimic set semantics
     *        (this should make isValueInSet faster than if we used an Array)
     */
    $scope.isValueInSet = function(value, set) {
      return (true == set[value]);
    }

    /**
     * If value "value" is already in set "set", remove it; otherwise, add it.
     *
     * @param value a value of any type
     * @param set an Object which we use to mimic set semantics
     */
    $scope.toggleValueInSet = function(value, set) {
      if (true == set[value]) {
        delete set[value];
      } else {
        set[value] = true;
      }
    }

    /**
     * For each value in valueArray, call toggleValueInSet(value, set).
     *
     * @param valueArray
     * @param set
     */
    $scope.toggleValuesInSet = function(valueArray, set) {
      var arrayLength = valueArray.length;
      for (var i = 0; i < arrayLength; i++) {
        $scope.toggleValueInSet(valueArray[i], set);
      }
    }


    //
    // Array operations; similar to our Set operations, but operate on a
    // Javascript Array so we *can* easily get a list of all values in the Set.
    // TODO(epoger): move into a separate .js file?
    //

    /**
     * Returns true if value "value" is present within array "array".
     *
     * @param value a value of any type
     * @param array a Javascript Array
     */
    $scope.isValueInArray = function(value, array) {
      return (-1 != array.indexOf(value));
    }

    /**
     * If value "value" is already in array "array", remove it; otherwise,
     * add it.
     *
     * @param value a value of any type
     * @param array a Javascript Array
     */
    $scope.toggleValueInArray = function(value, array) {
      var i = array.indexOf(value);
      if (-1 == i) {
        array.push(value);
      } else {
        array.splice(i, 1);
      }
    }


    //
    // Miscellaneous utility functions.
    // TODO(epoger): move into a separate .js file?
    //

    /**
     * Returns a human-readable (in local time zone) time string for a
     * particular moment in time.
     *
     * @param secondsPastEpoch (numeric): seconds past epoch in UTC
     */
    $scope.localTimeString = function(secondsPastEpoch) {
      var d = new Date(secondsPastEpoch * 1000);
      return d.toString();
    }

  }
);