aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/rebaseline_server/static/new/js/app.js
blob: 0a1fac0a450f848083742c51e9ab3a159cab6ed9 (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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
'use strict';

/**
 * TODO (stephana@): This is still work in progress. 
 * It does not offer the same functionality as the current version, but 
 * will serve as the starting point for a new backend.
 * It works with the current backend, but does not support rebaselining.
 */

/*
 * Wrap everything into an IIFE to not polute the global namespace.
 */
(function () {

  // Declare app level module which contains everything of the current app.
  // ui.bootstrap refers to directives defined in the AngularJS Bootstrap 
  // UI package (http://angular-ui.github.io/bootstrap/).
  var app = angular.module('rbtApp', ['ngRoute', 'ui.bootstrap']);

  // Configure the different within app views.
  app.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/', {templateUrl: 'partials/index-view.html', 
                              controller: 'IndexCtrl'});
    $routeProvider.when('/view', {templateUrl: 'partials/rebaseline-view.html',
                                  controller: 'RebaselineCrtrl'});
    $routeProvider.otherwise({redirectTo: '/'});
  }]);


  // TODO (stephana): Some of these constants are 'gm' specific. In the 
  // next iteration we need to remove those as we move the more generic 
  // 'dm' testing tool. 
  // 
  // Shared constants used here and in the markup. These are exported when
  // when used by a controller.
  var c = {
    // Define different view states as we load the data.
    ST_LOADING: 1,
    ST_STILL_LOADING: 2,
    ST_READY: 3,

    // These column types are used by the Column class.
    COL_T_FILTER: 'filter',
    COL_T_IMAGE: 'image',
    COL_T_REGULAR: 'regular',

    // Request parameters used to select between subsets of results.
    RESULTS_ALL: 'all',
    RESULTS_FAILURES: 'failures',

    // Filter types are used by the Column class.
    FILTER_FREE_FORM: 'free_form',
    FILTER_CHECK_BOX: 'checkbox',

    // Columns either provided by the backend response or added in code. 
    // TODO (stephana): This should go away once we switch to 'dm'.
    COL_BUGS: 'bugs',
    COL_IGNORE_FAILURE: 'ignore-failure',
    COL_REVIEWED_BY_HUMANS: 'reviewed-by-human',

    // Defines the order in which image columns appear.
    // TODO (stephana@): needs to be driven by backend data.
    IMG_COL_ORDER: [
       {
        key: 'imageA', 
        urlField: ['imageAUrl']
      },
      {
        key: 'imageB', 
        urlField: ['imageBUrl']
      },
      {
        key: 'whiteDiffs',
        urlField: ['differenceData', 'whiteDiffUrl'],
        percentField: ['differenceData', 'percentDifferingPixels'],
        valueField: ['differenceData', 'numDifferingPixels']
      },
      {
        key: 'diffs',
        urlField: ['differenceData', 'diffUrl'],
        percentField: ['differenceData', 'perceptualDifference'],
        valueField: ['differenceData', 'maxDiffPerChannel']
      }
    ],

    // Choice of availabe image size selection.
    IMAGE_SIZES: [
      100,
      200,
      400
    ],

    // Choice of available number of records selection.
    MAX_RECORDS: [
      '100', 
      '200',
      '300'
    ]
  };  // end constants 

  /*
   * Index Controller 
   */
  // TODO (stephana): Remove $timeout since it only simulates loading delay.
  app.controller('IndexCtrl', ['$scope', '$timeout', 'dataService', 
  function($scope, $timeout, dataService) {
    // init the scope 
    $scope.c = c;
    $scope.state = c.ST_LOADING;
    $scope.qStr = dataService.getQueryString;

    // TODO (stephana): Remove and replace with index data generated by the 
    // backend to reflect the current "known" image sets to compare.
    $scope.allSKPs = [
    {
      params: {
        setBSection: 'actual-results',
        setASection: 'expected-results',
        setBDir: 'gs://chromium-skia-skp-summaries/' + 
                 'Test-Mac10.8-MacMini4.1-GeForce320M-x86_64-Debug',
        setADir: 'repo:expectations/skp/' +
                 'Test-Mac10.8-MacMini4.1-GeForce320M-x86_64-Debug'
      },
      title: 'expected vs actuals on ' +
             'Test-Mac10.8-MacMini4.1-GeForce320M-x86_64-Debug'
    }, 
    {
      params: {
        setBSection: 'actual-results',
        setASection: 'expected-results',
        setBDir: 'gs://chromium-skia-skp-summaries/' +
                 'Test-Ubuntu12-ShuttleA-GTX660-x86-Release',
        setADir: 'repo:expectations/skp/'+
                 'Test-Ubuntu12-ShuttleA-GTX660-x86-Release'
      },
      title: 'expected vs actuals on Test-Ubuntu12-ShuttleA-GTX660-x86-Release'
    },
    {
      params: {
        setBSection: 'actual-results',
        setASection: 'actual-results',
        setBDir: 'gs://chromium-skia-skp-summaries/' + 
                 'Test-Ubuntu12-ShuttleA-GTX660-x86-Release',
        setADir: 'gs://chromium-skia-skp-summaries/' + 
                 'Test-Mac10.8-MacMini4.1-GeForce320M-x86_64-Debug'
      },
      title: 'Actuals on Test-Mac10.8-MacMini4.1-GeForce320M-x86_64-Debug ' + 
             'vs Test-Ubuntu12-ShuttleA-GTX660-x86-Release'
    }
    ];

    // TODO (stephana): Remove this once we load index data from the server. 
    $timeout(function () { 
      $scope.state = c.ST_READY;
    }); 
  }]);

  /* 
   *  RebaselineCtrl
   *  Controls the main comparison view.
   *
   *  @param {service} dataService Service that encapsulates functions to 
   *                               retrieve data from the backend.
   *  
   */
  app.controller('RebaselineCrtrl', ['$scope', '$timeout', 'dataService', 
  function($scope, $timeout, dataService) {
    // determine which to request
    // TODO (stephana): This should be extracted from the query parameters.
    var target = c.TARGET_GM;

    // process the rquest arguments
    // TODO (stephana): This should be determined from the query parameters.
    var loadFn = dataService.loadAll;

    // controller state variables 
    var allData = null;
    var filterFuncs = null;
    var currentData = null;
    var selectedData = null;

    // Index of the column that should provide the sort key
    var sortByIdx = 0;

    // Sort in asending (true) or descending (false) order
    var sortOrderAsc = true; 

    // Array of functions for each column used for comparison during sort.
    var compareFunctions = null;

    // Variables to track load and render times
    var startTime;
    var loadStartTime;


    /** Load the data from the backend **/ 
    loadStartTime = Date.now();
    function loadData() { 
      loadFn().then(
        function (serverData) {
          $scope.header = serverData.header;
          $scope.loadTime = (Date.now() - loadStartTime)/1000;

          // keep polling if the data are not ready yet
          if ($scope.header.resultsStillLoading) {
            $scope.state = c.ST_STILL_LOADING;
            $timeout(loadData, 5000);
            return;
          }

          // get the filter colunms and an array to hold filter data by user
          var fcol = getFilterColumns(serverData);
          $scope.filterCols = fcol[0];
          $scope.filterVals = fcol[1];

          // Add extra columns and retrieve the image columns
          var otherCols = [ Column.regular(c.COL_BUGS) ];
          var imageCols = getImageColumns(serverData);

          // Concat to get all columns
          // NOTE: The order is important since filters are rendered first, 
          // followed by regular columns and images 
          $scope.allCols = $scope.filterCols.concat(otherCols, imageCols);

          // Pre-process the data and get the filter functions.
          var dataFilters = getDataAndFilters(serverData, $scope.filterCols, 
                                              otherCols, imageCols);
          allData = dataFilters[0];
          filterFuncs = dataFilters[1];

          // Get regular columns (== not image columns)
          var regularCols = $scope.filterCols.concat(otherCols);

          // Get the compare functions for regular and image columns. These
          // are then used to sort by the respective columns. 
          compareFunctions = DataRow.getCompareFunctions(regularCols, 
                                                         imageCols);

          // Filter and sort the results to get them ready for rendering
          updateResults();

          // Data are ready for display
          $scope.state = c.ST_READY;
        },
        function (httpErrResponse) {
          console.log(httpErrResponse);        
        });
    };

    /*
     * updateResults
     * Central render function. Everytime settings/filters/etc. changed
     * this function is called to filter, sort and splice the data. 
     *
     * NOTE (stephana): There is room for improvement here: before filtering
     * and sorting we could check if this is necessary. But this has not been
     * a bottleneck so far. 
     */
    function updateResults () {
      // run digest before we update the results. This allows
      // updateResults to be called from functions trigger by ngChange
      $scope.updating = true;
      startTime = Date.now();

      // delay by one render cycle so it can be called via ng-change
      $timeout(function() {
        // filter data 
        selectedData = filterData(allData, filterFuncs, $scope.filterVals);

        // sort the selected data.
        sortData(selectedData, compareFunctions, sortByIdx, sortOrderAsc);

        // only conside the elements that we really need
        var nRecords = $scope.settings.nRecords;
        currentData = selectedData.slice(0, parseInt(nRecords));

        DataRow.setRowspanValues(currentData, $scope.mergeIdenticalRows);

        // update the scope with relevant data for rendering.
        $scope.data = currentData;
        $scope.totalRecords = allData.length;
        $scope.showingRecords = currentData.length;
        $scope.selectedRecords = selectedData.length;
        $scope.updating = false;

        // measure the filter time and total render time (via timeout).
        $scope.filterTime = Date.now() - startTime;
        $timeout(function() { 
          $scope.renderTime = Date.now() - startTime;
        });
      });
    };

    /**
     * Generate the style value to set the width of images. 
     * 
     * @param {Column} col Column that we are trying to render. 
     * @param {int} paddingPx Number of padding pixels.
     * @param {string} defaultVal Default value if not an image column.
     *
     * @return {string} Value to be used in ng-style element to set the width 
     *                  of a image column.
     **/
    $scope.getImageWidthStyle = function (col, paddingPx, defaultVal) { 
      var result = (col.ctype === c.COL_T_IMAGE) ? 
                   ($scope.imageSize + paddingPx + 'px') : defaultVal;
      return result;
    };

    /**
     * Sets the column by which to sort the data. If called for the 
     * currently sorted column it will cause the sort to toggle between
     * ascending and descending. 
     * 
     * @param {int} colIdx Index of the column to use for sorting. 
     **/
    $scope.sortBy = function (colIdx) { 
      if (sortByIdx === colIdx) { 
        sortOrderAsc = !sortOrderAsc;
      } else {
        sortByIdx = colIdx;
        sortOrderAsc = true;
      }
      updateResults();
    };

    /**
     * Helper function to generate a CSS class indicating whether this column 
     * is the sort key. If it is a class name with the sort direction (Asc/Desc) is 
     * return otherwise the default value is returned. In markup we use this 
     * to display (or not display) an arrow next to the column name. 
     * 
     * @param {string} prefix Prefix of the classname to be generated. 
     * @param {int} idx Index of the target column.
     * @param {string} defaultVal Value to return if current column is not used
     *                            for sorting. 
     *
     * @return {string} CSS class name that a combination of the prefix and 
     *                  direction indicator ('Asc' or 'Desc') if the column is 
     *                  used for sorting. Otherwise the defaultVal is returned.
     **/
    $scope.getSortedClass = function (prefix, idx, defaultVal) {
      if (idx === sortByIdx) { 
        return prefix + ((sortOrderAsc) ? 'Asc' : 'Desc');
      }

      return defaultVal; 
    };

    /**
     * Checkbox to merge identical records has change. Force an update.
     **/
    $scope.mergeRowsChanged = function () {
      updateResults();
    }

    /**
     * Max number of records to display has changed. Force an update. 
     **/
    $scope.maxRecordsChanged = function () {
      updateResults();
    };

    /**
     * Filter settings changed. Force an update. 
     **/
    $scope.filtersChanged = function () { 
      updateResults();
    };

    /**
     * Sets all possible values of the specified values to the given value.
     * That means all checkboxes are eiter selected or unselected.
     * Then force an update.
     * 
     * @param {int} idx Index of the target filter column.
     * @param {boolean} val Value to set the filter values to. 
     *
     **/
    $scope.setFilterAll = function (idx, val) {
      for(var i=0, len=$scope.filterVals[idx].length; i<len; i++) {
        $scope.filterVals[idx][i] = val;
      }
      updateResults();
    };

    /**
     * Toggle the values of a filter. This toggles all values in a 
     * filter. 
     * 
     * @param {int} idx Index of the target filter column.
     **/
    $scope.setFilterToggle = function (idx) { 
      for(var i=0, len=$scope.filterVals[idx].length; i<len; i++) {
        $scope.filterVals[idx][i] = !$scope.filterVals[idx][i];
      }
      updateResults();
    };

    // ****************************************
    // Initialize the scope.
    // ****************************************

    // Inject the constants into the scope and set the initial state. 
    $scope.c = c;
    $scope.state = c.ST_LOADING;

    // Initial settings
    $scope.settings = {
      showThumbnails: true,
      imageSize: c.IMAGE_SIZES[0],
      nRecords: c.MAX_RECORDS[0],
      mergeIdenticalRows: true
    };

    // Initial values for filters set in loadData()
    $scope.filterVals = [];

    // Information about records - set in loadData()
    $scope.totalRecords = 0;
    $scope.showingRecords = 0;
    $scope.updating = false;

    // Trigger the data loading. 
    loadData();

  }]);

  // data structs to interface with markup and backend
  /**
   * Models a column. It aggregates attributes of all 
   * columns types. Some might be empty. See convenience 
   * factory methods below for different column types.
   * 
   * @param {string} key Uniquely identifies this columns
   * @param {string} ctype Type of columns. Use COL_* constants. 
   * @param {string} ctitle Human readable title of the column.
   * @param {string} ftype Filter type. Use FILTER_* constants.
   * @param {FilterOpt[]} foptions Filter options. For 'checkbox' filters this 
                                   is used to render all the checkboxes. 
                                   For freeform filters this is a list of all
                                   available values. 
   * @param {string} baseUrl Baseurl for image columns. All URLs are relative 
                             to this.
   *
   * @return {Column} Instance of the Column class. 
   **/
  function Column(key, ctype, ctitle, ftype, foptions, baseUrl) { 
    this.key = key; 
    this.ctype = ctype;
    this.ctitle = ctitle;
    this.ftype = ftype;
    this.foptions = foptions;
    this.baseUrl = baseUrl;
    this.foptionsArr = [];

    // get the array of filter options for lookup in indexOfOptVal
    if (this.foptions) {
      for(var i=0, len=foptions.length; i<len; i++) {
        this.foptionsArr.push(this.foptions[i].value);
      }
    }
  }

  /**
   * Find the index of an value in a column with a fixed set
   * of options. 
   * 
   * @param {string} optVal Value of the column.
   *
   * @return {int} Index of optVal in this column.
   **/
  Column.prototype.indexOfOptVal = function (optVal) {
    return this.foptionsArr.indexOf(optVal);
  };

  /**
   * Set filter options for this column.
   * 
   * @param {FilterOpt[]} foptions Possible values for this column. 
   **/
  Column.prototype.setFilterOptions = function (foptions) { 
    this.foptions = foptions;
  };

  /**
   * Factory function to create a filter column. Same args as Column()
   **/
  Column.filter = function(key, ctitle, ftype, foptions) { 
    return new Column(key, c.COL_T_FILTER, ctitle || key, ftype, foptions); 
  }

  /**
   * Factory function to create an image column. Same args as Column()
   **/
  Column.image = function (key, ctitle, baseUrl) { 
    return new Column(key, c.COL_T_IMAGE, ctitle || key, null, null, baseUrl); 
  };

  /**
   * Factory function to create a regular column. Same args as Column()
   **/
  Column.regular = function (key, ctitle) { 
    return new Column(key, c.COL_T_REGULAR, ctitle || key); 
  }; 

  /**
   * Helper class to wrap a single option in a filter. 
   * 
   * @param {string} value Option value. 
   * @param {int} count Number of instances of this option in the dataset.
   *
   * @return {} Instance of FiltertOpt
   **/
  function FilterOpt(value, count) { 
    this.value = value; 
    this.count = count;
  }

  /**
   * Container for a single row in the dataset.
   * 
   * @param {int} rowspan Number of rows (including this and following rows) 
                          that have identical values. 
   * @param {string[]} dataCols Values of the respective columns (combination
                                of filter and regular columns)
   * @param {ImgVal[]} imageCols Image meta data for the image columns.
   *
   * @return {DataRow} Instance of DataRow. 
   **/
  function DataRow(rowspan, dataCols, imageCols) { 
    this.rowspan = rowspan;
    this.dataCols = dataCols;
    this.imageCols = imageCols;
  }

  /**
   * Gets the comparator functions for the columns in this dataset.
   * The comparators are then used to sort the dataset by the respective
   * column. 
   *
   * @param {Column[]} dataCols Data columns (= non-image columns)
   * @param {Column[]} imgCols Image columns.
   *
   * @return {Function[]} Array of functions that can be used to sort by the 
   *                      respective column.
   **/
  DataRow.getCompareFunctions = function (dataCols, imgCols) {
    var result = [];
    for(var i=0, len=dataCols.length; i<len; i++) { 
      result.push(( function (col, idx) { 
        return function (a, b) {
          return (a.dataCols[idx] < b.dataCols[idx]) ? -1 : 
                 ((a.dataCols[idx] === b.dataCols[idx]) ? 0 : 1);
        };
      }(dataCols[i], i) ));
    }

    for(var i=0, len=imgCols.length; i<len; i++) { 
      result.push((function (col, idx) { 
        return function (a,b) {
          var aVal = a.imageCols[idx].percent;
          var bVal = b.imageCols[idx].percent;

          return (aVal < bVal) ? -1 : ((aVal === bVal) ? 0 : 1);
        };
      }(imgCols[i], i) ));
    }

    return result;
  };

  /**
  * Set the rowspan values of a given array of DataRow instances.
  * 
  * @param {DataRow[]} data Dataset in desired order (after sorting).
  * @param {mergeRows} mergeRows Indicate whether to sort 
   **/
  DataRow.setRowspanValues = function (data, mergeRows) {
    var curIdx, rowspan, cur;
    if (mergeRows) { 
      for(var i=0, len=data.length; i<len;) {
        curIdx = i;
        cur = data[i];
        rowspan = 1;
        for(i++; ((i<len) && (data[i].dataCols === cur.dataCols)); i++) {
          rowspan++;
          data[i].rowspan=0;
        }
        data[curIdx].rowspan = rowspan;
      }
    } else {
      for(var i=0, len=data.length; i<len; i++) { 
        data[i].rowspan = 1;
      }
    }
  };

  /**
   * Wrapper class for image related data.
   * 
   * @param {string} url Relative Url of the image or null if not available.
   * @param {float} percent Percent of pixels that are differing.
   * @param {int} value Absolute number of pixes differing.
   *
   * @return {ImgVal} Instance of ImgVal.
   **/
  function ImgVal(url, percent, value) {
    this.url = url;
    this.percent = percent;
    this.value = value;
  }

  /**
   * Extracts the filter columns from the JSON response of the server. 
   * 
   * @param {object} data Server response. 
   *
   * @return {Column[]} List of filter columns as described in 'header' field. 
   **/
  function getFilterColumns(data) {
    var result = [];
    var vals = [];
    var colOrder = data.extraColumnOrder;
    var colHeaders = data.extraColumnHeaders;
    var fopts, optVals, val;

    for(var i=0, len=colOrder.length; i<len; i++) {
      if (colHeaders[colOrder[i]].isFilterable) {
        if (colHeaders[colOrder[i]].useFreeformFilter) {
          result.push(Column.filter(colOrder[i], 
                                    colHeaders[colOrder[i]].headerText, 
                                    c.FILTER_FREE_FORM));
          vals.push('');
        }
        else {
          fopts = [];
          optVals = [];

          // extract the different options for this column
          for(var j=0, jlen=colHeaders[colOrder[i]].valuesAndCounts.length; 
              j<jlen; j++) {
                val = colHeaders[colOrder[i]].valuesAndCounts[j];
                fopts.push(new FilterOpt(val[0], val[1]));
                optVals.push(false);
          }

          // ad the column and values
          result.push(Column.filter(colOrder[i], 
                                    colHeaders[colOrder[i]].headerText, 
                                    c.FILTER_CHECK_BOX, 
                                    fopts));
          vals.push(optVals);
        }
      }
    }

    return [result, vals];
  }

  /**
   * Extracts the image columns from the JSON response of the server. 
   * 
   * @param {object} data Server response. 
   *
   * @return {Column[]} List of images columns as described in 'header' field. 
   **/
  function getImageColumns(data) {
    var CO = c.IMG_COL_ORDER;
    var imgSet;
    var result = [];
    for(var i=0, len=CO.length; i<len; i++) { 
      imgSet = data.imageSets[CO[i].key];
      result.push(Column.image(CO[i].key, 
                               imgSet.description, 
                               ensureTrailingSlash(imgSet.baseUrl)));
    }
    return result;
  }

  /**
   * Make sure Url has a trailing '/'. 
   * 
   * @param {string} url Base url. 
   * @return {string} Same url with a trailing '/' or same as input if it 
                      already contained '/'.
   **/
  function ensureTrailingSlash(url) { 
    var result = url.trim();

    // TODO: remove !!!
    result = fixUrl(url);
    if (result[result.length-1] !== '/') {
      result += '/';
    }
    return result;
  }

  // TODO: remove. The backend should provide absoute URLs
  function fixUrl(url) {
    url = url.trim();
    if ('http' === url.substr(0, 4)) {
      return url;
    }

    var idx = url.indexOf('static');
    if (idx != -1) {
      return '/' + url.substr(idx);
    }

    return url;
  };

  /**
   * Processes that data and returns filter functions. 
   * 
   * @param {object} Server response.
   * @param {Column[]} filterCols Filter columns. 
   * @param {Column[]} otherCols Columns that are neither filters nor images.
   * @param {Column[]} imageCols Image columns.
   *
   * @return {[]} Returns a pair [dataRows, filterFunctions] where:
   *       - dataRows is an array of DataRow instances.
   *       - filterFunctions is an array of functions that can be used to 
   *         filter the column at the corresponding index. 
   *
   **/
  function getDataAndFilters(data, filterCols, otherCols, imageCols) {
    var el;
    var result = [];
    var lookupIndices = [];
    var indexerFuncs = [];
    var temp;

    // initialize the lookupIndices
    var filterFuncs = initIndices(filterCols, lookupIndices, indexerFuncs);

    // iterate over the data and get the rows
    for(var i=0, len=data.imagePairs.length; i<len; i++) {
      el = data.imagePairs[i];
      temp = new DataRow(1, getColValues(el, filterCols, otherCols),
                                 getImageValues(el, imageCols));
      result.push(temp);

      // index the row
      for(var j=0, jlen=filterCols.length; j < jlen; j++) {
        indexerFuncs[j](lookupIndices[j], filterCols[j], temp.dataCols[j], i);
      }
    }

    setFreeFormFilterOptions(filterCols, lookupIndices);
    return [result, filterFuncs];
  }

  /**
   * Initiazile the lookup indices and indexer functions for the filter
   * columns. 
   * 
   * @param {Column} filterCols Filter columns
   * @param {[]} lookupIndices Will be filled with datastructures for 
                               fast lookup (output parameter)
   * @param {[]} lookupIndices Will be filled with functions to index data 
                               of the column with the corresponding column.
   *
   * @return {[]} Returns an array of filter functions that can be used to 
                  filter the respective column.  
   **/
  function initIndices(filterCols, lookupIndices, indexerFuncs) {
    var filterFuncs = [];
    var temp;

    for(var i=0, len=filterCols.length; i<len; i++) { 
      if (filterCols[i].ftype === c.FILTER_FREE_FORM) {
        lookupIndices.push({});
        indexerFuncs.push(indexFreeFormValue);
        filterFuncs.push(
          getFreeFormFilterFunc(lookupIndices[lookupIndices.length-1]));
      }
      else if (filterCols[i].ftype === c.FILTER_CHECK_BOX) { 
        temp = [];
        for(var j=0, jlen=filterCols[i].foptions.length; j<jlen; j++) {
          temp.push([]);
        }
        lookupIndices.push(temp);
        indexerFuncs.push(indexDiscreteValue);
        filterFuncs.push(
          getDiscreteFilterFunc(lookupIndices[lookupIndices.length-1]));
      }
    }

    return filterFuncs; 
  }

  /**
   * Helper function that extracts the values of free form columns from 
   * the lookupIndex and injects them into the Column object as FilterOpt 
   * objects.
   **/
  function setFreeFormFilterOptions(filterCols, lookupIndices) {
    var temp, k;
    for(var i=0, len=filterCols.length; i<len; i++) { 
      if (filterCols[i].ftype === c.FILTER_FREE_FORM) { 
        temp = []
        for(k in lookupIndices[i]) { 
          if (lookupIndices[i].hasOwnProperty(k)) { 
            temp.push(new FilterOpt(k, lookupIndices[i][k].length));
          }
        }
        filterCols[i].setFilterOptions(temp);
      }
    }
  }

  /**
   * Index a discrete column (column with fixed number of values). 
   *
   **/
  function indexDiscreteValue(lookupIndex, col, dataVal, dataRowIndex) {
    var i = col.indexOfOptVal(dataVal);
    lookupIndex[i].push(dataRowIndex);
  }

  /**
   * Index a column with free form text (= not fixed upfront)
   *
   **/
  function indexFreeFormValue(lookupIndex, col, dataVal, dataRowIndex) { 
    if (!lookupIndex[dataVal]) { 
      lookupIndex[dataVal] = [];
    }
    lookupIndex[dataVal].push(dataRowIndex);
  }


  /**
   * Get the function to filter a column with the given lookup index
   * for discrete (fixed upfront) values. 
   * 
   **/
  function getDiscreteFilterFunc(lookupIndex) { 
    return function(filterVal) {
      var result = [];
      for(var i=0, len=lookupIndex.length; i < len; i++) {
        if (filterVal[i]) { 
          // append the indices to the current array
          result.push.apply(result, lookupIndex[i]);
        }
      }
      return { nofilter: false, records: result };
    };
  }

  /**
   * Get the function to filter a column with the given lookup index
   * for free form values.
   * 
   **/
  function getFreeFormFilterFunc(lookupIndex) {
    return function(filterVal) {
      filterVal = filterVal.trim();
      if (filterVal === '') {
        return { nofilter: true };
      }
      return { 
        nofilter: false, 
        records: lookupIndex[filterVal] || []
      };
    };
  }

  /**
   * Filters the data based on the given filterColumns and 
   * corresponding filter values. 
   * 
   * @return {[]} Subset of the input dataset based on the 
   *              filter values.
   **/
  function filterData(data, filterFuncs, filterVals) {
    var recordSets = [];
    var filterResult;

    // run through all the filters
    for(var i=0, len=filterFuncs.length; i<len; i++) { 
      filterResult = filterFuncs[i](filterVals[i]);
      if (!filterResult.nofilter) { 
        recordSets.push(filterResult.records);
      }
    }

    // If there are no restrictions then return the whole dataset.
    if (recordSets.length === 0) {
      return data;
    } 

    // intersect the records returned by filters. 
    var targets = intersectArrs(recordSets);
    var result = [];
    for(var i=0, len=targets.length; i<len; i++) {
      result.push(data[targets[i]]);
    }

    return result; 
  }

  /**
   * Creates an object where the keys are the elements of the input array
   * and the values are true. To be used for set operations with integer.
   **/
  function arrToObj(arr) { 
    var o = {};
    var i,len;
    for(i=0, len=arr.length; i<len; i++) { 
      o[arr[i]] = true;
    }
    return o;
  }

  /**
   * Converts the keys of an object to an array after converting 
   * each key to integer. To be used for set operations with integers.  
   **/
  function objToArr(obj) { 
    var result = [];
    for(var k in obj) {
      if (obj.hasOwnProperty(k)) { 
        result.push(parseInt(k));
      }
    }
    return result;
  }

  /**
   * Find the intersection of a set of arrays.
   **/
  function intersectArrs(sets) {
    var temp, obj;

    if (sets.length === 1) { 
      return sets[0];
    }

    // sort by size and load the smallest into the object
    sets.sort(function(a,b) { return a.length - b.length; });
    obj = arrToObj(sets[0]); 

    // shrink the hash as we fail to find elements in the other sets
    for(var i=1, len=sets.length; i<len; i++) { 
      temp = arrToObj(sets[i]);
      for(var k in obj) {
        if (obj.hasOwnProperty(k) && !temp[k]) { 
          delete obj[k];
        }
      }
    }
    
    return objToArr(obj);
  }

  /**
   * Extract the column values from an ImagePair (contained in the server 
   * response) into filter and data columns. 
   *
   * @return {[]} Array of data contained in one data row.
   **/
  function getColValues(imagePair, filterCols, otherCols) { 
    var result = [];
    for(var i=0, len=filterCols.length; i<len; i++) { 
      result.push(imagePair.extraColumns[filterCols[i].key]);
    }

    for(var i=0, len=otherCols.length; i<len; i++) { 
      result.push(get_robust(imagePair, ['expectations', otherCols[i].key]));
    }

    return result;
  }

  /**
   * Extract the image meta data from an Image pair returned by the server.
   **/
  function getImageValues(imagePair, imageCols) {
    var result=[];
    var url, value, percent, diff;
    var CO = c.IMG_COL_ORDER;

    for(var i=0, len=imageCols.length; i<len; i++) {
      percent = get_robust(imagePair, CO[i].percentField);
      value = get_robust(imagePair, CO[i].valueField);
      url = get_robust(imagePair, CO[i].urlField);
      if (url) { 
        url = imageCols[i].baseUrl + url;
      }
      result.push(new ImgVal(url, percent, value));
    }

    return result;
  }

  /**
   * Given an object find sub objects for the given index without 
   * throwing an error if any of the sub objects do not exist. 
   **/
  function get_robust(obj, idx) {
    if (!idx) {
      return;
    }

    for(var i=0, len=idx.length; i<len; i++) {
      if ((typeof obj === 'undefined') || (!idx[i])) {
        return;  // returns 'undefined'
      }

      obj = obj[idx[i]];
    }

    return obj;
  }

  /**
   * Set all elements in the array to the given value. 
   **/
  function setArrVals(arr, newVal) { 
    for(var i=0, len=arr.length; i<len; i++) { 
      arr[i] = newVal;
    }
  }

  /**
   * Toggle the elements of a boolean array. 
   * 
   **/
  function toggleArrVals(arr) { 
    for(var i=0, len=arr.length; i<len; i++) { 
      arr[i] = !arr[i];
    }
  }

  /**
   * Sort the array of DataRow instances with the given compare functions 
   * and the column at the given index either in ascending or descending order.
   **/
  function sortData (allData, compareFunctions, sortByIdx, sortOrderAsc) {
    var cmpFn = compareFunctions[sortByIdx];
    var useCmp = cmpFn;
    if (!sortOrderAsc) {
      useCmp = function ( _ ) {
        return -cmpFn.apply(this, arguments);
      };
    }
    allData.sort(useCmp);
  }


  // *****************************  Services *********************************

  /**  
   *  Encapsulates all interactions with the backend by handling 
   *  Urls and HTTP requests. Also exposes some utility functions
   *  related to processing Urls. 
   */
  app.factory('dataService', [ '$http', function ($http) {
    /** Backend related constants  **/ 
    var c = {
      /** Url to retrieve failures */ 
      FAILURES: '/results/failures',

      /** Url to retrieve all GM results */ 
      ALL:      '/results/all'
    };

    /**
     * Convenience function to retrieve all results.
     * 
     * @return {Promise} Will resolve to either the data (success) or to 
     *                   the HTTP response (error).
     **/
    function loadAll() {
      return httpGetData(c.ALL);
    }

    /**
     * Make a HTTP get request with the given query parameters.
     * 
     * @param {} 
     * @param {}
     *
     * @return {} 
     **/
    function httpGetData(url, queryParams) {
      var reqConfig = {
        method: 'GET',
        url: url,
        params: queryParams
      };

      return $http(reqConfig).then(
        function(successResp) {
          return successResp.data;
        });
    }

    /**
     * Takes an arbitrary number of objects and generates a Url encoded
     * query string.
     *
     **/
    function getQueryString( _params_ ) {
      var result = [];
      for(var i=0, len=arguments.length; i < len; i++) {
        if (arguments[i]) {
          for(var k in arguments[i]) { 
            if (arguments[i].hasOwnProperty(k)) {
              result.push(encodeURIComponent(k) + '=' + 
                          encodeURIComponent(arguments[i][k]));
            }
          }
        }
      }
      return result.join("&");
    }

    // Interface of the service:
    return {
      getQueryString: getQueryString,
      loadAll: loadAll
    };

  }]);  

})();