<commit_before>###
#   @desc Chart Threshold - create Widget KPIs from Widget charts. (Highchart widgets only).
#   @todo support for multiple KPI watchables?
#   @todo support for multiple KPI targets?
#   @todo support for multiple attachable KPIs?
###
module = angular.module('impac.components.widgets-common.chart-threshold', [])
module.component('chartThreshold', {
  templateUrl: 'widgets-common/chart-threshold.tmpl.html'
  bindings:
    widget: '<'
    chartPromise: '<?'
    chartShrinkSize: '<?'
    disabled: '<?'
    kpiTargetMode: '<?'
    kpiCreateLabel: '<?'
    onComplete: '&?'
  controller: ($timeout, $log, ImpacKpisSvc, toastr)->
    ctrl = this

    ctrl.$onInit = ->
      # Define properties
      # -------
      ctrl.kpi = {}
      ctrl.showPanel = false
      ctrl.draftTarget = ''
      ctrl.chartShrinkSize ||= 38
      ctrl.disabled ||= false
      ctrl.kpiTargetMode ||= 'min'
      ctrl.kpiCreateLabel ||= 'Get alerted when the target threshold goes below'
      # Get attachable kpi templates
      ImpacKpisSvc.getAttachableKpis(ctrl.widget.endpoint).then(
        (templates)->
          return disableAttachability('No valid KPI Templates found') if _.isEmpty(templates) || _.isEmpty(templates[0].watchables)
          angular.extend(ctrl.kpi, angular.copy(templates[0]))
        ->
          disableAttachability()
      )
      # Register to chart changes
      ctrl.chartPromise.then(null, null, onChartNotify) if ctrl.chartPromise? && _.isFunction(ctrl.chartPromise.then)

    ctrl.createKpi = (target)->
      return if ctrl.disabled
      # Only 1 kpi per widget is supported & prevent panel showing if target is currently drafting
      return unless target && _.isEmpty(ctrl.widget.kpis) && _.isEmpty(ctrl.draftTarget)
      ctrl.draftTarget = target
      # As this method can be called from parent component, dirty checking in this ctrl
      # scope are not checked, this ensures value change is detected as per usual.
      $timeout(->
        ctrl.showPanel = true
        shrinkChart()
      )
      return

    ctrl.cancelCreateKpi = ->
      ctrl.draftTarget = ''
      ctrl.showPanel = false
      growChart()
      return

    ctrl.saveKpi = ->
      params = {}
      params.targets = {}
      params.targets[ctrl.kpi.watchables[0]] = [{
        "#{ctrl.kpiTargetMode}": ctrl.draftTarget
      }]
      return unless ImpacKpisSvc.validateKpiTargets(params.targets)
      params.widget_id = ctrl.widget.id
      ImpacKpisSvc.create('impac', ctrl.kpi.endpoint, ctrl.kpi.watchables[0], params).then(
        (kpi)->
          ctrl.widget.kpis.push(kpi)
          ctrl.onComplete($event: { kpi: kpi }) if _.isFunction(ctrl.onComplete)
      ).finally(->
        ctrl.cancelCreateKpi()
      )

    # Private

    onChartNotify = (chart)->
      ctrl.chart = chart
      Highcharts.addEvent(chart.container, 'click', onChartClick)
      return

    onChartClick = (event)->
      # Check whether click event fired is from the 'reset zoom' button
      return if event.srcElement.textContent == 'Reset zoom'
      value = event.yAxis[0].value
      # Gaurd for click events fired outside of the yAxis values range
      if !value || _.isNaN(value) then return else value = value.toFixed(2)
      ctrl.createKpi(value)

    disableAttachability = (logMsg)->
      ctrl.disabled = true
      toastr.warning("Chart threshold KPI disabled!", "#{ctrl.widget.name} Widget")
      $log.warn("Impac! - #{ctrl.widget.name} Widget: #{logMsg}") if logMsg<commit_msg>Add widget hist_params to kpi on create<commit_after>###
#   @desc Chart Threshold - create Widget KPIs from Widget charts. (Highchart widgets only).
#   @todo support for multiple KPI watchables?
#   @todo support for multiple KPI targets?
#   @todo support for multiple attachable KPIs?
###
module = angular.module('impac.components.widgets-common.chart-threshold', [])
module.component('chartThreshold', {
  templateUrl: 'widgets-common/chart-threshold.tmpl.html'
  bindings:
    widget: '<'
    chartPromise: '<?'
    chartShrinkSize: '<?'
    disabled: '<?'
    kpiTargetMode: '<?'
    kpiCreateLabel: '<?'
    onComplete: '&?'
  controller: ($timeout, $log, ImpacKpisSvc, toastr)->
    ctrl = this

    ctrl.$onInit = ->
      # Define properties
      # -------
      ctrl.kpi = {}
      ctrl.showPanel = false
      ctrl.draftTarget = ''
      ctrl.chartShrinkSize ||= 38
      ctrl.disabled ||= false
      ctrl.kpiTargetMode ||= 'min'
      ctrl.kpiCreateLabel ||= 'Get alerted when the target threshold goes below'
      # Get attachable kpi templates
      ImpacKpisSvc.getAttachableKpis(ctrl.widget.endpoint).then(
        (templates)->
          return disableAttachability('No valid KPI Templates found') if _.isEmpty(templates) || _.isEmpty(templates[0].watchables)
          angular.extend(ctrl.kpi, angular.copy(templates[0]))
        ->
          disableAttachability()
      )
      # Register to chart changes
      ctrl.chartPromise.then(null, null, onChartNotify) if ctrl.chartPromise? && _.isFunction(ctrl.chartPromise.then)

    ctrl.createKpi = (target)->
      return if ctrl.disabled
      # Only 1 kpi per widget is supported & prevent panel showing if target is currently drafting
      return unless target && _.isEmpty(ctrl.widget.kpis) && _.isEmpty(ctrl.draftTarget)
      ctrl.draftTarget = target
      # As this method can be called from parent component, dirty checking in this ctrl
      # scope are not checked, this ensures value change is detected as per usual.
      $timeout(->
        ctrl.showPanel = true
        shrinkChart()
      )
      return

    ctrl.cancelCreateKpi = ->
      ctrl.draftTarget = ''
      ctrl.showPanel = false
      growChart()
      return

    ctrl.saveKpi = ->
      params = {}
      params.targets = {}
      params.targets[ctrl.kpi.watchables[0]] = [{
        "#{ctrl.kpiTargetMode}": ctrl.draftTarget
      }]
      return unless ImpacKpisSvc.validateKpiTargets(params.targets)
      params.metadata = {}
      params.metadata.hist_parameters = ctrl.widget.metadata.hist_parameters
      params.widget_id = ctrl.widget.id
      ImpacKpisSvc.create('impac', ctrl.kpi.endpoint, ctrl.kpi.watchables[0], params).then(
        (kpi)->
          ctrl.widget.kpis.push(kpi)
          ctrl.onComplete($event: { kpi: kpi }) if _.isFunction(ctrl.onComplete)
      ).finally(->
        ctrl.cancelCreateKpi()
      )

    # Private

    onChartNotify = (chart)->
      ctrl.chart = chart
      Highcharts.addEvent(chart.container, 'click', onChartClick)
      return

    onChartClick = (event)->
      # Check whether click event fired is from the 'reset zoom' button
      return if event.srcElement.textContent == 'Reset zoom'
      value = event.yAxis[0].value
      # Gaurd for click events fired outside of the yAxis values range
      if !value || _.isNaN(value) then return else value = value.toFixed(2)
      ctrl.createKpi(value)

    disableAttachability = (logMsg)->
      ctrl.disabled = true
      toastr.warning("Chart threshold KPI disabled!", "#{ctrl.widget.name} Widget")
      $log.warn("Impac! - #{ctrl.widget.name} Widget: #{logMsg}") if logMsg
{'fraction_non_alphanumeric': 0.11995515695067265, 'fraction_numerical': 0.002522421524663677, 'mean_word_length': 2.7821939586645468, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 29, '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': '1921707', 'n_tokens_mistral': 2130, 'n_tokens_neox': 2107, 'n_words': 518}