-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathchart-threshold.component.coffee
218 lines (196 loc) · 7.85 KB
/
chart-threshold.component.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
###
# @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: '<?'
thresholdColor: '@'
onComplete: '&?'
controller: ($timeout, $log, ImpacKpisSvc, ImpacUtilities, toastr)->
ctrl = this
ctrl.$onInit = ->
# Define properties
# -------
ctrl.kpi = {}
ctrl.showPanel = false
ctrl.isEditingKpi = false
ctrl.loading = false
ctrl.draftTarget = value: ''
ctrl.chartShrinkSize ||= 38
ctrl.disabled ||= false
ctrl.kpiTargetMode ||= 'min'
ctrl.kpiCreateLabel ||= 'Get alerted when the target threshold goes below'
ctrl.thresholdColor ||= 'rgba(0, 0, 0, 0.7)'
# 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)
# Widgets can have multiple possible attachable KPIs, only one is currently supported.
angular.extend(ctrl.kpi, angular.copy(templates[0]))
# The watchables are currently not selectable by the user, only one element_watched
# is supported.
ctrl.kpi.element_watched = ctrl.kpi.watchables[0]
->
disableAttachability()
)
# Register to chart changes (expects a highchart instance)
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.value)
ctrl.draftTarget.value = target
toggleKpiPanel()
return
ctrl.editKpi = (options)->
return if ctrl.showPanel || ctrl.disabled || _.isEmpty(ctrl.widget.kpis)
ctrl.isEditingKpi = true
angular.extend(ctrl.draftTarget, options)
toggleKpiPanel()
return
ctrl.cancelCreateKpi = ->
toggleKpiPanel()
$timeout(->
ctrl.draftTarget.value = ''
ctrl.isEditingKpi = false
ctrl.loading = false
, 100)
return
ctrl.saveKpi = ->
return if ctrl.loading
ctrl.loading = true
params = targets: {}, metadata: {}
params.targets[ctrl.kpi.element_watched] = [{
"#{ctrl.kpiTargetMode}": parseFloat(ctrl.draftTarget.value)
currency: ImpacKpisSvc.getCurrentDashboard().currency
}]
return unless ImpacKpisSvc.validateKpiTargets(params.targets)
promise = if ctrl.isEditingKpi
kpi = getKpi()
ImpacKpisSvc.update(kpi, params, false).then(
(updatedKpi)->
# Remove old threshold from chart
ctrl.chart.removeThreshold(kpi.id)
angular.extend(kpi, updatedKpi)
)
else
params.metadata.hist_parameters = {
from: moment.utc().format('YYYY-MM-DD')
to: moment.utc(getChartExtremes().xAxis.max).format('YYYY-MM-DD')
}
params.widget_id = ctrl.widget.id
ImpacKpisSvc.create(ctrl.kpi, params).then(
(kpi)->
ctrl.widget.kpis.push(kpi)
kpi
)
promise.then(
(kpi)->
ImpacKpisSvc.show(kpi).then(
(kpiData)->
dataKey = ImpacKpisSvc.getApiV2KpiDataKey(kpi)
angular.extend(kpi, kpiData[dataKey])
).finally(
->
ctrl.onComplete($event: { kpi: kpi }) if _.isFunction(ctrl.onComplete)
)
->
toastr.error("Failed to save #{ctrl.kpi.element_watched} KPI", getWidgetName())
).finally(->
ctrl.cancelCreateKpi()
)
ctrl.deleteKpi = ->
return if ctrl.loading
ctrl.loading = true
kpi = getKpi()
ImpacKpisSvc.delete(kpi).then(
->
toastr.success("Deleted #{ctrl.kpi.element_watched} KPI", getWidgetName())
_.remove(ctrl.widget.kpis, (k)-> k.id == kpi.id)
ctrl.chart.removeThreshold(kpi.id)
ctrl.onComplete($event: {}) if _.isFunction(ctrl.onComplete)
->
toastr.error("Failed to delete #{ctrl.kpi.element_watched} KPI", getWidgetName())
).finally(->
ctrl.cancelCreateKpi()
)
# Private
getKpi = ->
_.find(ctrl.widget.kpis, (k)-> k.id == ctrl.draftTarget.kpiId)
getWidgetName = ->
_.startCase "#{ctrl.widget.name} widget"
onChartNotify = (chart)->
ctrl.chart = chart
Highcharts.addEvent(chart.hc.container, 'click', onChartClick)
_.each buildThresholdsFromKpis(), (threshold)->
thresholdSerie = ctrl.chart.updateThreshold(threshold)
ctrl.chart.addThresholdEvent(thresholdSerie, 'click', onThresholdClick)
return
onChartClick = (event)->
return unless hasFutureChartMaxDate()
# Check whether click event fired is from the 'reset zoom' button
return if event.srcElement.textContent == 'Reset zoom'
# Guard for tooltips / other chart areas that don't return a yAxis value
return unless event.yAxis && event.yAxis[0]
value = event.yAxis[0].value
# Guard for click events fired outside of the yAxis values range
if !value || _.isNaN(value) then return else value = value.toFixed(2)
ctrl.createKpi(value)
onThresholdClick = (thresholdSerie)->
thresholdValue = (opts = thresholdSerie.options).data[opts.data.length - 1][1].toFixed(2)
ctrl.editKpi(kpiId: opts.kpiId, value: thresholdValue)
disableAttachability = (logMsg)->
ctrl.disabled = true
toastr.warning('Chart KPIs are disabled!', getWidgetName())
$log.warn("Impac! - #{getWidgetName()}: #{logMsg}") if logMsg
# As this method can be called from parent component or an event callback,
# $timeout to ensure value change is detected as per usual.
toggleKpiPanel = ->
$timeout(->
if ctrl.showPanel then growChart() else shrinkChart()
ctrl.showPanel = !ctrl.showPanel
)
shrinkChart = ->
return unless ctrl.chart
ctrl.chart.hc.setSize(null, ctrl.chart.hc.chartHeight - ctrl.chartShrinkSize, false)
ctrl.chart.hc.container.parentElement.style.height = "#{ctrl.chart.hc.chartHeight}px"
growChart = ->
return unless ctrl.chart
ctrl.chart.hc.setSize(null, ctrl.chart.hc.chartHeight + ctrl.chartShrinkSize, false)
ctrl.chart.hc.container.parentElement.style.height = "#{ctrl.chart.hc.chartHeight}px"
hasFutureChartMaxDate = ->
return false unless ctrl.chart && ctrl.chart.hc
moment.utc(getChartExtremes().xAxis.max) > moment()
getChartExtremes = ->
xAxis: ctrl.chart.hc.xAxis[0].getExtremes()
# No support for multiple KPIs & watchables yet.
buildThresholdsFromKpis = ->
return unless (kpi = ctrl.widget.kpis && ctrl.widget.kpis[0]) &&
(watchable = kpi.watchables && kpi.watchables[0]) &&
(targets = watchable && watchable.targets)
_.map(targets, (t)->
name: 'Alert Threshold'
kpiId: kpi.id
value: t.min
triggered: t.trigger_state
triggered_interval_index: t.triggered_interval_index
color: ctrl.thresholdColor
)
isCmpDisabled = ->
if _.isEmpty(ctrl.widget.metadata.bolt_path)
$log.error("chart-threshold.component not compatible with #{getWidgetName()} - no bolt path defined")
true
else
false
return ctrl
})