<commit_before>timeTracker.controller 'MainCtrl', ($rootScope, $scope, $timeout, $location, $anchorScroll, $window, $q, Ticket, Project, Redmine, Account, State, DataAdapter, Message, Chrome, Resource, Option, Log, Analytics) ->

  DATA_SYNC = "DATA_SYNC"
  MINUTE_5 = 5
  TICKET_CLOSED = 5
  NOT_FOUND = 404
  UNAUTHORIZED = 401
  # http request canceled.
  STATUS_CANCEL = 0
  # don't use query
  QUERY_ALL_ID = 0

  # list for toast Message.
  $rootScope.messages = []

  State.title = Resource.string("extName")
  $scope.state = State

  ###
   Initialize.
  ###
  init = () ->
    deferred = $q.defer()
    deferred.promise
      .then(_initializeGoogleAnalytics)
      .then(_initializeDataFromChrome)
      .then(_initializeDataFromRedmine)
      .then(_initializeEvents)
      .then(_setDataSyncAlarms)
    deferred.resolve()

  ###
   initialize events.
  ###
  _initializeEvents = () ->
    Log.debug "[4] initializeEvents start"
    DataAdapter.addEventListener DataAdapter.ACCOUNT_ADDED, _loadRedmine
    DataAdapter.addEventListener DataAdapter.PROJECTS_CHANGED, () ->
      Project.syncLocal(DataAdapter.getProjects())
    DataAdapter.addEventListener DataAdapter.TICKETS_CHANGED, () ->
      Ticket.syncLocal(DataAdapter.tickets)
    Option.onChanged('reportUsage', (e) -> Analytics.setPermission(e) )
    Option.onChanged('hideNonTicketProject',  _toggleProjectHidden)
    Log.debug "[4] initializeEvents success"

  ###
   load projects from redmine.
  ###
  _loadRedmine = (accounts) ->
    for a in accounts
      _loadActivities(a)
      _loadQueries(a)
      $q.all([_loadProjects(a), _loadStatuses(a)])
        .then(_loadIssues(a))
        .then(_loadIssueCount(a))

  ###
   load projects from redmine.
  ###
  _loadProjects = (a) ->
    redmine = Redmine.get(a)
    promises = []
    a.numProjects > 0 and promises.push(redmine.loadProjectsRange({}, 0, a.numProjects))
    if a.projectList
      promises.add a.projectList.map (id) ->
        if Object.isNumber(id)
          return redmine.loadProjectById(id)
        if Object.isArray(id)
          return redmine.loadProjectsRange({}, id[0], id[1])
    $q.all(promises)
      .then(_successLoadProject, _errorLoadProject)

  ###
   load projects from redmine.
  ###
  _successLoadProject = (data) =>
    loaded = _mergeResult(data)
    if loaded
      # update user specified settings using saved data on chrome.
      projects = DataAdapter.getProjects(loaded.account.url)
      loaded.projects.map (p) ->
        saved = projects.find (n) -> n.equals p
        return if not saved
        p.show = saved.show
        p.queryId = saved.queryId
        # On chrome, project doesn't have text. Update it here.
        if p.equals DataAdapter.selectedProject
          DataAdapter.selectedProject.text = p.text
      # delete saved data.
      DataAdapter.removeProjects(projects)
      # update new data.
      DataAdapter.addProjects(loaded.projects)
      Message.toast Resource.string("msgLoadProjectSuccess").format(loaded.account.name), 3000
    else
      _errorLoadProject data

  ###
   merge projects.
  ###
  _mergeResult = (data) ->
    return null if not data[0]
    if data[0].project
      data[0].projects = [data[0].project]

    data.reduce (a, b) ->
      if b.projects
        a.projects.push b.projects
      else if b.project
        a.projects.push b.project<commit_msg>if numProjects is 0, don't fetch Projects. and not, fetch Projects.<commit_after>timeTracker.controller 'MainCtrl', ($rootScope, $scope, $timeout, $location, $anchorScroll, $window, $q, Ticket, Project, Redmine, Account, State, DataAdapter, Message, Chrome, Resource, Option, Log, Analytics) ->

  DATA_SYNC = "DATA_SYNC"
  MINUTE_5 = 5
  TICKET_CLOSED = 5
  NOT_FOUND = 404
  UNAUTHORIZED = 401
  # http request canceled.
  STATUS_CANCEL = 0
  # don't use query
  QUERY_ALL_ID = 0

  # list for toast Message.
  $rootScope.messages = []

  State.title = Resource.string("extName")
  $scope.state = State

  ###
   Initialize.
  ###
  init = () ->
    deferred = $q.defer()
    deferred.promise
      .then(_initializeGoogleAnalytics)
      .then(_initializeDataFromChrome)
      .then(_initializeDataFromRedmine)
      .then(_initializeEvents)
      .then(_setDataSyncAlarms)
    deferred.resolve()

  ###
   initialize events.
  ###
  _initializeEvents = () ->
    Log.debug "[4] initializeEvents start"
    DataAdapter.addEventListener DataAdapter.ACCOUNT_ADDED, _loadRedmine
    DataAdapter.addEventListener DataAdapter.PROJECTS_CHANGED, () ->
      Project.syncLocal(DataAdapter.getProjects())
    DataAdapter.addEventListener DataAdapter.TICKETS_CHANGED, () ->
      Ticket.syncLocal(DataAdapter.tickets)
    Option.onChanged('reportUsage', (e) -> Analytics.setPermission(e) )
    Option.onChanged('hideNonTicketProject',  _toggleProjectHidden)
    Log.debug "[4] initializeEvents success"

  ###
   load projects from redmine.
  ###
  _loadRedmine = (accounts) ->
    for a in accounts
      _loadActivities(a)
      _loadQueries(a)
      $q.all([_loadProjects(a), _loadStatuses(a)])
        .then(_loadIssues(a))
        .then(_loadIssueCount(a))

  ###
   load projects from redmine.
  ###
  _loadProjects = (a) ->
    redmine = Redmine.get(a)
    promises = []
    if a.numProjects is 0
    else if not a.numProjects
      promises.push(redmine.loadProjectsRange({}, 0))
    else
      promises.push(redmine.loadProjectsRange({}, 0, a.numProjects))
    if a.projectList
      promises.add a.projectList.map (id) ->
        if Object.isNumber(id)
          return redmine.loadProjectById(id)
        if Object.isArray(id)
          return redmine.loadProjectsRange({}, id[0], id[1])
    $q.all(promises)
      .then(_successLoadProject, _errorLoadProject)

  ###
   load projects from redmine.
  ###
  _successLoadProject = (data) =>
    loaded = _mergeResult(data)
    if loaded
      # update user specified settings using saved data on chrome.
      projects = DataAdapter.getProjects(loaded.account.url)
      loaded.projects.map (p) ->
        saved = projects.find (n) -> n.equals p
        return if not saved
        p.show = saved.show
        p.queryId = saved.queryId
        # On chrome, project doesn't have text. Update it here.
        if p.equals DataAdapter.selectedProject
          DataAdapter.selectedProject.text = p.text
      # delete saved data.
      DataAdapter.removeProjects(projects)
      # update new data.
      DataAdapter.addProjects(loaded.projects)
      Message.toast Resource.string("msgLoadProjectSuccess").format(loaded.account.name), 3000
    else
      _errorLoadProject data

  ###
   merge projects.
  ###
  _mergeResult = (data) ->
    return null if not data[0]
    if data[0].project
      data[0].projects = [data[0].project]

    data.reduce (a, b) ->
      if b.projects
        a.projects.add b.projects
      else if b.project
        a.projects.push b.project
{'fraction_non_alphanumeric': 0.11552975326560232, 'fraction_numerical': 0.007547169811320755, 'mean_word_length': 2.9671848013816926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1537624', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2144, 'n_words': 465}