This repository has been archived by the owner on Mar 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
CloudWatchMetricsSelector.js
113 lines (102 loc) · 3.21 KB
/
CloudWatchMetricsSelector.js
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
import React, { PropTypes } from 'react'
import DropdownList from 'components/DropdownList'
import classes from './CloudWatchMetricsSelector.scss'
export default class CloudWatchMetricsSelector extends React.Component {
static propTypes = {
onChange: PropTypes.func.isRequired,
metrics: PropTypes.arrayOf(PropTypes.object.isRequired),
props: PropTypes.object,
fetchExternalMetrics: PropTypes.func.isRequired
}
constructor (props) {
super(props)
this.state = {
message: ''
}
}
componentDidMount () {
const fetchCallbacks = {
onFailure: (msg) => {
this.setState({message: msg})
}
}
this.props.fetchExternalMetrics('CloudWatch', fetchCallbacks)
}
handleChangeNamespace = (value) => {
this.props.onChange({
Namespace: value
})
}
handleChangeMetric = (value) => {
const splitStr = ' - ['
const splitIndex = value.indexOf(splitStr)
const metricName = value.substr(0, splitIndex)
const rawDims = value.slice(splitIndex + splitStr.length, -1)
const dimensions = rawDims.split(', ').map((rawDim) => {
const splitStr = ': '
const splitIndex = rawDim.indexOf(splitStr)
const dimName = rawDim.substr(0, splitIndex)
const dimValue = rawDim.substr(splitIndex + splitStr.length)
return {Name: dimName, Value: dimValue}
})
this.props.onChange({
MetricName: metricName,
Dimensions: dimensions
})
}
buildMetricExpression = (metric) => {
const dimensions = metric.Dimensions.map((dim) => {
return `${dim.Name}: ${dim.Value}`
})
return `${metric.MetricName} - [${dimensions.join(', ')}]`
}
render () {
let initialNamespace = ''
let initialMetric = ''
if (this.props.props) {
const props = this.props.props
initialNamespace = props.Namespace
if (props.Dimensions) {
initialMetric = this.buildMetricExpression(props)
}
}
let namespaces = []
const metrics = []
if (this.props.metrics) {
const namespaceSet = new Set([''])
this.props.metrics.forEach((metric) => {
namespaceSet.add(metric.Namespace)
})
namespaces = Array.from(namespaceSet).sort()
if (this.props.props && this.props.props.Namespace) {
this.props.metrics.forEach((metric) => {
if (metric.Namespace === this.props.props.Namespace) {
metrics.push(this.buildMetricExpression(metric))
}
})
}
metrics.sort()
} else {
namespaces.push(initialNamespace)
metrics.push(initialMetric)
}
return (
<div>
<label className={classes.label} htmlFor='metric'>
CloudWatch Namespace
</label>
<div id='metric' className={classes['dropdown-list']}>
<DropdownList onChange={this.handleChangeNamespace}
list={namespaces} initialValue={initialNamespace} />
</div>
<label className={classes.label} htmlFor='metric'>
CloudWatch MetricName & Dimensions
</label>
<div id='metric' className={classes['dropdown-list']}>
<DropdownList onChange={this.handleChangeMetric}
list={metrics} initialValue={initialMetric} />
</div>
</div>
)
}
}