<commit_before>angular
  .module('impac.services.dashboards', [])
  .service 'ImpacDashboardsSvc', ($q, $http, $log, $timeout, ImpacMainSvc, ImpacRoutes, ImpacTheming, ImpacDeveloper) ->
    #====================================
    # Initialization and getters
    #====================================

    _self = @
    @config = {}

    @config.dashboards = []
    @getDashboards = ->
      return _self.config.dashboards

    @config.currentDashboard = {}
    @getCurrentDashboard = ->
      return _self.config.currentDashboard

    @config.widgetsTemplates = []
    @getWidgetsTemplates = ->
      return _self.config.widgetsTemplates

    #====================================
    # Callbacks
    #====================================

    @callbacks = {}

    @callbacks.dashboardChanged = $q.defer()
    @dashboardChanged = ->
      return _self.callbacks.dashboardChanged.promise

    @callbacks.widgetAdded = $q.defer()
    @widgetAdded = ->
      return _self.callbacks.widgetAdded.promise

    @callbacks.pdfModeEnabled = $q.defer()
    @pdfModeEnabled = ->
      return _self.callbacks.pdfModeEnabled.promise
    @callbacks.pdfModeCanceled = $q.defer()
    @pdfModeCanceled = ->
      return _self.callbacks.pdfModeCanceled.promise

    @togglePdfMode = (enabled) ->
      if enabled
        _self.callbacks.pdfModeEnabled.notify()
      else
        _self.callbacks.pdfModeCanceled.notify()

    @callbacks.ticked = $q.defer()
    @ticked = ->
      return _self.callbacks.ticked.promise
    @tick = ->
      _self.callbacks.ticked.notify()

    @callbacks.dhbLoader = $q.defer()
    @dhbLoader = ->
      return _self.callbacks.dhbLoader.promise
    @triggerDhbLoader = (bool=false)->
      _self.callbacks.dhbLoader.notify(bool)

    #====================================
    # Context helpers (return booleans: can be called but can't be bound!)
    #====================================

    needConfigurationLoad = ->
      return  _.isEmpty(_self.config.dashboards) || _.isEmpty(_self.config.currentDashboard)

    @isThereADashboard = ->
      !_.isEmpty _self.config.currentDashboard

    @areThereSeveralDashboards = ->
      _self.config.dashboards.length > 1

    @isCurrentDashboardEmpty = ->
      _self.isThereADashboard() && _.isEmpty(_self.config.currentDashboard.widgets)

    @areKpisEnabled = ->
      ImpacTheming.get().dhbKpisConfig.enableKpis


    #====================================
    # Loaders and setters
    #====================================

    @loadLocked=false
    @load = (force=false) ->
      deferred = $q.defer()

      # Singleton prevents concurrent calls of _self.load
      if !_self.loadLocked
        _self.loadLocked=true

        if (needConfigurationLoad() || force)

          ImpacMainSvc.load(force).then (success) ->
            orgId = success.currentOrganization.id

            $http.get(ImpacRoutes.dashboards.index(orgId)).then(
              (dashboards) ->
                _self.setDashboards(dashboards.data).then ->
                  _self.setCurrentDashboard()
                  deferred.resolve(_self.config)
                  $log.info("Impac! - DashboardsSvc: loaded (force=#{force})")

                .finally( -> _self.loadLocked=false )
              (error) ->
                _self.loadLocked=false<commit_msg>Add a @reload() method to dhb service<commit_after>angular
  .module('impac.services.dashboards', [])
  .service 'ImpacDashboardsSvc', ($q, $http, $log, $timeout, ImpacMainSvc, ImpacRoutes, ImpacTheming, ImpacDeveloper) ->
    #====================================
    # Initialization and getters
    #====================================

    _self = @
    @config = {}

    @config.dashboards = []
    @getDashboards = ->
      return _self.config.dashboards

    @config.currentDashboard = {}
    @getCurrentDashboard = ->
      return _self.config.currentDashboard

    @config.widgetsTemplates = []
    @getWidgetsTemplates = ->
      return _self.config.widgetsTemplates

    #====================================
    # Callbacks
    #====================================

    @callbacks = {}

    @callbacks.dashboardChanged = $q.defer()
    @dashboardChanged = ->
      return _self.callbacks.dashboardChanged.promise

    @callbacks.widgetAdded = $q.defer()
    @widgetAdded = ->
      return _self.callbacks.widgetAdded.promise

    @callbacks.pdfModeEnabled = $q.defer()
    @pdfModeEnabled = ->
      return _self.callbacks.pdfModeEnabled.promise
    @callbacks.pdfModeCanceled = $q.defer()
    @pdfModeCanceled = ->
      return _self.callbacks.pdfModeCanceled.promise

    @togglePdfMode = (enabled) ->
      if enabled
        _self.callbacks.pdfModeEnabled.notify()
      else
        _self.callbacks.pdfModeCanceled.notify()

    @callbacks.ticked = $q.defer()
    @ticked = ->
      return _self.callbacks.ticked.promise
    @tick = ->
      _self.callbacks.ticked.notify()

    @callbacks.dhbLoader = $q.defer()
    @dhbLoader = ->
      return _self.callbacks.dhbLoader.promise
    @triggerDhbLoader = (bool=false)->
      _self.callbacks.dhbLoader.notify(bool)

    #====================================
    # Context helpers (return booleans: can be called but can't be bound!)
    #====================================

    needConfigurationLoad = ->
      return  _.isEmpty(_self.config.dashboards) || _.isEmpty(_self.config.currentDashboard)

    @isThereADashboard = ->
      !_.isEmpty _self.config.currentDashboard

    @areThereSeveralDashboards = ->
      _self.config.dashboards.length > 1

    @isCurrentDashboardEmpty = ->
      _self.isThereADashboard() && _.isEmpty(_self.config.currentDashboard.widgets)

    @areKpisEnabled = ->
      ImpacTheming.get().dhbKpisConfig.enableKpis


    #====================================
    # Loaders and setters
    #====================================

    # Method used for reloading an already loaded dashboard, will reload it properly
    # while also triggering the dhbLoader spinner.
    @reload = (force=false) ->
      deferred = $q.defer()
      _self.triggerDhbLoader(true)
      _self.load(force).then(
        -> deferred.resolve()
        -> deferred.reject()
      ).finally(->
        _self.triggerDhbLoader(false)
      )
      return deferred.promise

    @loadLocked=false
    @load = (force=false) ->
      deferred = $q.defer()

      # Singleton prevents concurrent calls of _self.load
      if !_self.loadLocked
        _self.loadLocked=true

        if (needConfigurationLoad() || force)

          ImpacMainSvc.load(force).then (success) ->
            orgId = success.currentOrganization.id

            $http.get(ImpacRoutes.dashboards.index(orgId)).then(
              (dashboards) ->
                _self.setDashboards(dashboards.data).then ->
                  _self.setCurrentDashboard()
                  deferred.resolve(_self.config)
                  $log.info("Impac! - DashboardsSvc: loaded (force=#{force})")

                .finally( -> _self.loadLocked=false )
              (error) ->
                _self.loadLocked=false
{'fraction_non_alphanumeric': 0.1909697572057362, 'fraction_numerical': 0.0002839698991906858, 'mean_word_length': 2.791173304628633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4503161', 'n_tokens_mistral': 2116, 'n_tokens_neox': 2052, 'n_words': 307}