<commit_before>/* global _, localForage, Howl, console, get_gravatar, serializeObject, filesize, document,
   POP_SOUNDS, isAllDigits, window, angularForage, angular, alert, moment,
   setTimeout, showCloakDialog, closeCloakDialog */



// saves typing
var L = function() { console.log.apply(console, arguments); };
var D = function() { console.dir.apply(console, arguments); };

var BUGZILLA_URL = 'https://bugzilla.mozilla.org/rest/';
var MAX_BACKGROUND_DOWNLOADS = 10;
var FETCH_NEW_BUGS_FREQUENCY = 30;
var FETCH_CHANGED_BUGS_FREQUENCY = 35;
var CLEAN_LOCAL_STORAGE_FREQUENCY = 120;

var _INCLUDE_FIELDS = 'assigned_to,assigned_to_detail,product,component,creator,creator_detail,status,id,resolution,last_change_time,creation_time,summary';
var _ALL_POSSIBLE_STATUSES = 'UNCONFIRMED,NEW,ASSIGNED,REOPENED,RESOLVED,VERIFIED,CLOSED'.split(',');
// utils stuff
function makeCommentExtract(comment, max_length) {
  max_length = max_length || 75;
  if (comment.text.length > max_length) {
    return comment.text.substring(0, max_length - 1) + '\u2026';
  }
  return comment.text;
}

var app = angular.module('buggyApp', ['ngSanitize']);

app.filter('stringArraySort', function() {
  return function(input) {
    return input.sort();
  };
});

app.directive('whenScrolled', function() {
  return function(scope, elm, attr) {
    var raw = elm[0];
    var funCheckBounds = function(evt) {
      //console.log("event fired: " + evt.type);
      var rectObject = raw.getBoundingClientRect();
      //console.log(rectObject.bottom, window.innerHeight);
      if (rectObject.bottom < window.innerHeight) {
        //console.log('**At the bottom');
        scope.$apply(attr.whenScrolled);
      }

    };
    angular.element(window).bind('scroll load', funCheckBounds);
  };
});


BugsController.$inject = ['$scope', '$timeout', '$http', '$interval'];

function BugsController($scope, $timeout, $http, $interval) {
  'use strict';

  var _inprogress_refreshing = false;

  $scope.bugs = [];
  $scope.in_config = false;
  $scope.in_about = false;
  $scope.email = '';
  $scope.auth_token = null;
  $scope.is_offline = false;
  $scope.all_possible_statuses = _ALL_POSSIBLE_STATUSES;
  $scope.selected_statuses = [];
  angularForage.getItem($scope, 'selected_statuses', function(value) {
    if (value !== null) {
      $scope.selected_statuses = value;
    }
  });
  angularForage.getItem($scope, 'auth_token', function(value) {
    if (value) {
      $scope.auth_token = value;
    }
  });
  angularForage.getItem($scope, 'email', function(value) {
    if (value) {
      $scope.email = value;
    }
  });
  $scope.products = [];
  $scope.play_sounds = true;
  angularForage.getItem($scope, 'play_sounds', function(value) {
    if (value !== null) $scope.play_sounds = value;
  });

  $scope.email_to_name = {};
  angularForage.getItem($scope, 'email_to_name', function(value) {
    if (value !== null) {
      $scope.email_to_name = value;
    }
  });

  $scope.config_stats = {
     data_downloaded_human: '',
     total_data_downloaded_human: '',
     total_comments: 0
  };

  $scope.data_downloaded = 0;  // this session
  $scope.total_data_downloaded = 0; // persistent with local storage
  angularForage.getItem($scope, 'total_data_downloaded', function(value) {
    if (value !== null) {
      $scope.total_data_downloaded = value;
    }
  });
  function logDataDownloaded(data) {
    var amount = JSON.stringify(data).length;
    $scope.data_downloaded += amount;
    $scope.total_data_downloaded += amount;
    localForage.setItem('total_data_downloaded', $scope.total_data_downloaded);
  }

  /* Used to put a notice loading message on the screen */
  $scope.loading = null;
  function startLoading(msg) {
    $scope.loading = {message: msg};
  }
  function stopLoading() {
    $scope.loading = null;
  }

  $scope.error_notice = null;
  function setErrorNotice(msg, options) {
    options = options || {};
    var delay = options.delay || 10;
    $scope.error_notice = msg;
    $timeout(function() {
      $scope.error_notice = null;
    }, delay * 1000);
  }

  var original_document_title = document.title;
  $scope.general_notice = null;
  function setGeneralNotice(msg, options) {
    options = options || {};
    var delay = options.delay || 10;
    $scope.general_notice = msg;
    document.title = msg;
    $timeout(function() {
      $scope.general_notice = null;
      document.title = original_document_title;
    }, delay * 1000);
  }

  $scope.filterByStatus = function(status) {
    if (status === 'ALL') {
      $scope.selected_statuses = [];
    } else if (_.contains($scope.selected_statuses, status)) {
      $scope.selected_statuses = _.filter($scope.selected_statuses, function(s) {
        return s !== status;
      });
    } else {
      $scope.selected_statuses.push(status);  // TODO uniqueness test
    }
    localForage.setItem('selected_statuses', $scope.selected_statuses);
  };

  $scope.isSelectedStatus = function(status) {
    return _.contains($scope.selected_statuses, status);
  };


  $scope.countByStatus = function(status) {
    return $scope.counts_by_status[status] || 0;
  };

  $scope.counts_by_status = {UNREAD: 0, ASSIGNED_TO: 0, CHANGED: 0};
  function reCountBugsByStatus(bugs) {
    if (!bugs) return;
    $scope.counts_by_status.ALL = bugs.length;
    $scope.counts_by_status.ASSIGNED_TO = 0;
    $scope.counts_by_status.UNREAD = 0;
    $scope.counts_by_status.CHANGED = 0;
    _.each(bugs, function(bug) {
      if ($scope.email == bug.assigned_to) {
        $scope.counts_by_status.ASSIGNED_TO++;
      }
      if (bug.unread) {
        $scope.counts_by_status.UNREAD++;
      }
      if (bug.is_changed) {
        $scope.counts_by_status.CHANGED++;
      }
      $scope.counts_by_status[bug.status] = 1 + ($scope.counts_by_status[bug.status] || 0);
    });
    console.log('BUGS ARE A CHANGING');
    console.log($scope.counts_by_status);
  }

  // not sure this works well
  //$scope.$watch('bugs', reCountBugsByStatus);

  $scope.toggleConfig = function() {
    if (!$scope.in_config) {
      // before opening the config pane, preload some stats
      $scope.config_stats.total_bugs = $scope.bugs.length;
      $scope.config_stats.data_downloaded_human = filesize($scope.data_downloaded);
      $scope.config_stats.total_data_downloaded_human = filesize($scope.total_data_downloaded);<commit_msg>counting by status didn't reset first<commit_after>/* global _, localForage, Howl, console, get_gravatar, serializeObject, filesize, document,
   POP_SOUNDS, isAllDigits, window, angularForage, angular, alert, moment,
   setTimeout, showCloakDialog, closeCloakDialog */



// saves typing
var L = function() { console.log.apply(console, arguments); };
var D = function() { console.dir.apply(console, arguments); };

var BUGZILLA_URL = 'https://bugzilla.mozilla.org/rest/';
var MAX_BACKGROUND_DOWNLOADS = 10;
var FETCH_NEW_BUGS_FREQUENCY = 30;
var FETCH_CHANGED_BUGS_FREQUENCY = 35;
var CLEAN_LOCAL_STORAGE_FREQUENCY = 120;

var _INCLUDE_FIELDS = 'assigned_to,assigned_to_detail,product,component,creator,creator_detail,status,id,resolution,last_change_time,creation_time,summary';
var _ALL_POSSIBLE_STATUSES = 'UNCONFIRMED,NEW,ASSIGNED,REOPENED,RESOLVED,VERIFIED,CLOSED'.split(',');
// utils stuff
function makeCommentExtract(comment, max_length) {
  max_length = max_length || 75;
  if (comment.text.length > max_length) {
    return comment.text.substring(0, max_length - 1) + '\u2026';
  }
  return comment.text;
}

var app = angular.module('buggyApp', ['ngSanitize']);

app.filter('stringArraySort', function() {
  return function(input) {
    return input.sort();
  };
});

app.directive('whenScrolled', function() {
  return function(scope, elm, attr) {
    var raw = elm[0];
    var funCheckBounds = function(evt) {
      //console.log("event fired: " + evt.type);
      var rectObject = raw.getBoundingClientRect();
      //console.log(rectObject.bottom, window.innerHeight);
      if (rectObject.bottom < window.innerHeight) {
        //console.log('**At the bottom');
        scope.$apply(attr.whenScrolled);
      }

    };
    angular.element(window).bind('scroll load', funCheckBounds);
  };
});


BugsController.$inject = ['$scope', '$timeout', '$http', '$interval'];

function BugsController($scope, $timeout, $http, $interval) {
  'use strict';

  var _inprogress_refreshing = false;

  $scope.bugs = [];
  $scope.in_config = false;
  $scope.in_about = false;
  $scope.email = '';
  $scope.auth_token = null;
  $scope.is_offline = false;
  $scope.all_possible_statuses = _ALL_POSSIBLE_STATUSES;
  $scope.selected_statuses = [];
  angularForage.getItem($scope, 'selected_statuses', function(value) {
    if (value !== null) {
      $scope.selected_statuses = value;
    }
  });
  angularForage.getItem($scope, 'auth_token', function(value) {
    if (value) {
      $scope.auth_token = value;
    }
  });
  angularForage.getItem($scope, 'email', function(value) {
    if (value) {
      $scope.email = value;
    }
  });
  $scope.products = [];
  $scope.play_sounds = true;
  angularForage.getItem($scope, 'play_sounds', function(value) {
    if (value !== null) $scope.play_sounds = value;
  });

  $scope.email_to_name = {};
  angularForage.getItem($scope, 'email_to_name', function(value) {
    if (value !== null) {
      $scope.email_to_name = value;
    }
  });

  $scope.config_stats = {
     data_downloaded_human: '',
     total_data_downloaded_human: '',
     total_comments: 0
  };

  $scope.data_downloaded = 0;  // this session
  $scope.total_data_downloaded = 0; // persistent with local storage
  angularForage.getItem($scope, 'total_data_downloaded', function(value) {
    if (value !== null) {
      $scope.total_data_downloaded = value;
    }
  });
  function logDataDownloaded(data) {
    var amount = JSON.stringify(data).length;
    $scope.data_downloaded += amount;
    $scope.total_data_downloaded += amount;
    localForage.setItem('total_data_downloaded', $scope.total_data_downloaded);
  }

  /* Used to put a notice loading message on the screen */
  $scope.loading = null;
  function startLoading(msg) {
    $scope.loading = {message: msg};
  }
  function stopLoading() {
    $scope.loading = null;
  }

  $scope.error_notice = null;
  function setErrorNotice(msg, options) {
    options = options || {};
    var delay = options.delay || 10;
    $scope.error_notice = msg;
    $timeout(function() {
      $scope.error_notice = null;
    }, delay * 1000);
  }

  var original_document_title = document.title;
  $scope.general_notice = null;
  function setGeneralNotice(msg, options) {
    options = options || {};
    var delay = options.delay || 10;
    $scope.general_notice = msg;
    document.title = msg;
    $timeout(function() {
      $scope.general_notice = null;
      document.title = original_document_title;
    }, delay * 1000);
  }

  $scope.filterByStatus = function(status) {
    if (status === 'ALL') {
      $scope.selected_statuses = [];
    } else if (_.contains($scope.selected_statuses, status)) {
      $scope.selected_statuses = _.filter($scope.selected_statuses, function(s) {
        return s !== status;
      });
    } else {
      $scope.selected_statuses.push(status);  // TODO uniqueness test
    }
    localForage.setItem('selected_statuses', $scope.selected_statuses);
  };

  $scope.isSelectedStatus = function(status) {
    return _.contains($scope.selected_statuses, status);
  };


  $scope.countByStatus = function(status) {
    return $scope.counts_by_status[status] || 0;
  };

  $scope.counts_by_status = {UNREAD: 0, ASSIGNED_TO: 0, CHANGED: 0};
  function reCountBugsByStatus(bugs) {
    if (!bugs) return;
    $scope.counts_by_status = {UNREAD: 0, ASSIGNED_TO: 0, CHANGED: 0, ALL: bugs.length};
    _.each(bugs, function(bug) {
      if ($scope.email == bug.assigned_to) {
        $scope.counts_by_status.ASSIGNED_TO++;
      }
      if (bug.unread) {
        $scope.counts_by_status.UNREAD++;
      }
      if (bug.is_changed) {
        $scope.counts_by_status.CHANGED++;
      }
      $scope.counts_by_status[bug.status] = 1 + ($scope.counts_by_status[bug.status] || 0);
    });
    //console.log('BUGS ARE A CHANGING');
    //console.log($scope.counts_by_status);
  }

  // not sure this works well
  //$scope.$watch('bugs', reCountBugsByStatus);

  $scope.toggleConfig = function() {
    if (!$scope.in_config) {
      // before opening the config pane, preload some stats
      $scope.config_stats.total_bugs = $scope.bugs.length;
      $scope.config_stats.data_downloaded_human = filesize($scope.data_downloaded);
      $scope.config_stats.total_data_downloaded_human = filesize($scope.total_data_downloaded);
{'fraction_non_alphanumeric': 0.1439860913545124, 'fraction_numerical': 0.006638217164532954, 'mean_word_length': 3.74859287054409, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5737600', 'n_tokens_mistral': 4392, 'n_tokens_neox': 4252, 'n_words': 759}