<commit_before>angular
  .module('impac.services.widgets', [])
  .service 'ImpacWidgetsSvc', ($q, $http, $log, ImpacRoutes, ImpacMainSvc, ImpacDashboardsSvc) ->

    _self = @
    @config = {}
    @config.ssoSessionId = ""


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

      if _.isEmpty(_self.config.ssoSessionId) || force

        $q.all([ImpacMainSvc.loadUserData(force), ImpacDashboardsSvc.load(force)]).then(
          (results) ->
            _self.config.ssoSessionId = results[0].sso_session
            deferred.resolve(_self.config)
          (error) ->
            deferred.reject(error)
        )

      else
         deferred.resolve(_self.config)

      return deferred.promise


    @create = (opts) ->
      deferred = $q.defer()

      _self.load().then(
        (config) ->

          dashboard = ImpacDashboardsSvc.getCurrentDashboard()
          data = { widget: opts }

          $http.post(ImpacRoutes.createWidgetPath(dashboard.id), data).then(
            (success) ->
              newWidget = success.data
              dashboard.widgets.push(newWidget)
              deferred.resolve(newWidget)

            (error) ->
              $log.error("ImpacWidgetsSvc: cannot create widget on dashboard #{dashboard.id}")
              deferred.reject(error)
          )

        (error) ->
          $log.error("ImpacWidgetsSvc: error while trying to load the service")
          deferred.reject(error)
      )

      return deferred.promise


    isWidgetInCurrentDashboard = (widgetId) ->
      currentDhb = ImpacDashboardsSvc.getCurrentDashboard()
      return false if _.isEmpty(currentDhb) || _.isEmpty(currentDhb.widgets)
      return _.contains( _.pluck(currentDhb.widgets, 'id'), widgetId)


    @initWidgetSettings = (w) ->
      return if _.isEmpty(w.settings)
      for setting in w.settings
        setting.initialize()
      return true

    @updateWidgetSettings = (widget, needContentReload=true) ->
      deferred = $q.defer()

      if _.isEmpty(widget.settings)
        deferred.reject('no setting to update')

      else
        widget.isLoading = true if needContentReload
        meta = {}
        for setting in widget.settings
          angular.merge meta, setting.toMetadata()

        _self.update(widget, { metadata: meta }).then(
          (updatedSettingsWidget) ->
            if needContentReload
              _self.show(updatedSettingsWidget).then(
                (updatedContentWidget) ->
                  updatedContentWidget.isLoading = false
                  deferred.resolve(updatedContentWidget)
                (error) ->
                  updatedSettingsWidget.isLoading = false
                  deferred.reject(error)
              )
            else
              deferred.resolve(updatedSettingsWidget)

          (error) ->
            deferred.reject(error)
        )

      return deferred.promise

    @massAssignAll = (metadata) ->
      unless _.isEmpty(metadata)
        _self.load().then ->
          currentDhb = ImpacDashboardsSvc.getCurrentDashboard()
          promises = []

          if currentDhb? && currentDhb.widgets?
            if !_.isEmpty(currentDhb.widgets)
              for widget in currentDhb.widgets
                promises.push _self.update(widget, {metadata: metadata}).then(
                  (updatedWidget) -> 
                    updatedWidget.isLoading=true
                    _self.show(updatedWidget, true).then( (renderedWidget)-> renderedWidget.isLoading=false )
                )
              return $q.all(promises)

            else
              return $q.resolve([])

          else
            $log.error "ImpacWidgetsSvc - currentDhb.widgets is null", currentDhb
            return $q.reject(null)


    #====================================
    # CRUD methods
    #====================================

    @show = (widget, refreshCache=false) ->
      deferred = $q.defer()

      _self.load().then(
        (config) ->
          unless isWidgetInCurrentDashboard(widget.id)
            $log.info("ImpacWidgetsSvc: trying to load a widget (id: #{widget.id}) that is not in currentDashboard")
            deferred.reject("trying to load a widget (id: #{widget.id}) that is not in currentDashboard")

          else
            data = {
              owner: widget.owner
              sso_session: _self.config.ssoSessionId
              metadata: widget.metadata
              engine: widget.category
            }
            angular.extend(data, {refresh_cache: true}) if refreshCache

            $http.post(ImpacRoutes.showWidgetPath(widget.id), data).then(
              (success) ->
                # Pushes new content to widget
                content = success.data.content || {}
                name = success.data.name
                angular.extend widget, {content: content, originalName: name}
<commit_msg>added refreshAll widgets method to ImpacWidgetsSvc<commit_after>angular
  .module('impac.services.widgets', [])
  .service 'ImpacWidgetsSvc', ($q, $http, $log, ImpacRoutes, ImpacMainSvc, ImpacDashboardsSvc) ->

    _self = @
    @config = {}
    @config.ssoSessionId = ""


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

      if _.isEmpty(_self.config.ssoSessionId) || force

        $q.all([ImpacMainSvc.loadUserData(force), ImpacDashboardsSvc.load(force)]).then(
          (results) ->
            _self.config.ssoSessionId = results[0].sso_session
            deferred.resolve(_self.config)
          (error) ->
            deferred.reject(error)
        )

      else
         deferred.resolve(_self.config)

      return deferred.promise


    @create = (opts) ->
      deferred = $q.defer()

      _self.load().then(
        (config) ->

          dashboard = ImpacDashboardsSvc.getCurrentDashboard()
          data = { widget: opts }

          $http.post(ImpacRoutes.createWidgetPath(dashboard.id), data).then(
            (success) ->
              newWidget = success.data
              dashboard.widgets.push(newWidget)
              deferred.resolve(newWidget)

            (error) ->
              $log.error("ImpacWidgetsSvc: cannot create widget on dashboard #{dashboard.id}")
              deferred.reject(error)
          )

        (error) ->
          $log.error("ImpacWidgetsSvc: error while trying to load the service")
          deferred.reject(error)
      )

      return deferred.promise


    isWidgetInCurrentDashboard = (widgetId) ->
      currentDhb = ImpacDashboardsSvc.getCurrentDashboard()
      return false if _.isEmpty(currentDhb) || _.isEmpty(currentDhb.widgets)
      return _.contains( _.pluck(currentDhb.widgets, 'id'), widgetId)


    @initWidgetSettings = (w) ->
      return if _.isEmpty(w.settings)
      for setting in w.settings
        setting.initialize()
      return true

    @updateWidgetSettings = (widget, needContentReload=true) ->
      deferred = $q.defer()

      if _.isEmpty(widget.settings)
        deferred.reject('no setting to update')

      else
        widget.isLoading = true if needContentReload
        meta = {}
        for setting in widget.settings
          angular.merge meta, setting.toMetadata()

        _self.update(widget, { metadata: meta }).then(
          (updatedSettingsWidget) ->
            if needContentReload
              _self.show(updatedSettingsWidget).then(
                (updatedContentWidget) ->
                  updatedContentWidget.isLoading = false
                  deferred.resolve(updatedContentWidget)
                (error) ->
                  updatedSettingsWidget.isLoading = false
                  deferred.reject(error)
              )
            else
              deferred.resolve(updatedSettingsWidget)

          (error) ->
            deferred.reject(error)
        )

      return deferred.promise

    @massAssignAll = (metadata) ->
      unless _.isEmpty(metadata)
        _self.load().then ->
          currentDhb = ImpacDashboardsSvc.getCurrentDashboard()
          promises = []

          if currentDhb? && currentDhb.widgets?
            if !_.isEmpty(currentDhb.widgets)
              for widget in currentDhb.widgets
                promises.push _self.update(widget, {metadata: metadata}).then(
                  (updatedWidget) ->
                    updatedWidget.isLoading=true
                    _self.show(updatedWidget, true).then( (renderedWidget)-> renderedWidget.isLoading=false )
                )
              return $q.all(promises)

            else
              return $q.resolve([])

          else
            $log.error "ImpacWidgetsSvc - currentDhb.widgets is null", currentDhb
            return $q.reject(null)

    @refreshAll = ->
      ImpacDashboardsSvc.load().then(
        (config)->
          widgets = config.currentDashboard.widgets
          _.forEach(widgets, (w) ->
            w.isLoading ||= true
            _self.show(w).then(
              (updatedWidget) ->
                w.isLoading = false
              (errorResponse) ->
                w.isLoading = false
                # TODO: better error management
                $log.error(errorResponse.data.error) if errorResponse.data? && errorResponse.data.error
            )
          )
      )

    #====================================
    # CRUD methods
    #====================================

    @show = (widget, refreshCache=false) ->
      deferred = $q.defer()

      _self.load().then(
        (config) ->
          unless isWidgetInCurrentDashboard(widget.id)
            $log.info("ImpacWidgetsSvc: trying to load a widget (id: #{widget.id}) that is not in currentDashboard")
            deferred.reject("trying to load a widget (id: #{widget.id}) that is not in currentDashboard")

          else
            data = {
              owner: widget.owner
              sso_session: _self.config.ssoSessionId
              metadata: widget.metadata
              engine: widget.category
            }
            angular.extend(data, {refresh_cache: true}) if refreshCache

            $http.post(ImpacRoutes.showWidgetPath(widget.id), data).then(
              (success) ->
                # Pushes new content to widget
                content = success.data.content || {}
                name = success.data.name
                angular.extend widget, {content: content, originalName: name}

{'fraction_non_alphanumeric': 0.11762991743564838, 'fraction_numerical': 0.0001942690626517727, 'mean_word_length': 1.8759776536312849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 54, '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': '6632711', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2745, 'n_words': 537}