@@ -7,36 +7,106 @@ function (angular, _) {
7
7
8
8
var module = angular . module ( 'grafana.controllers' ) ;
9
9
10
- module . controller ( 'CloudWatchQueryCtrl' , function ( $scope , templateSrv , uiSegmentSrv ) {
10
+ module . controller ( 'CloudWatchQueryCtrl' , function ( $scope , templateSrv , uiSegmentSrv , $q ) {
11
11
12
12
$scope . init = function ( ) {
13
13
$scope . target . namespace = $scope . target . namespace || '' ;
14
14
$scope . target . metricName = $scope . target . metricName || '' ;
15
+ $scope . target . statistics = $scope . target . statistics || { Average : true } ;
15
16
$scope . target . dimensions = $scope . target . dimensions || { } ;
16
- $scope . target . escapedDimensions = this . escapeDimensions ( $scope . target . dimensions ) ;
17
- $scope . target . statistics = $scope . target . statistics || { } ;
18
17
$scope . target . period = $scope . target . period || 60 ;
19
18
$scope . target . region = $scope . target . region || $scope . datasource . getDefaultRegion ( ) ;
20
- $scope . target . errors = validateTarget ( ) ;
21
19
22
20
$scope . regionSegment = uiSegmentSrv . getSegmentForValue ( $scope . target . region , 'select region' ) ;
23
21
$scope . namespaceSegment = uiSegmentSrv . getSegmentForValue ( $scope . target . namespace , 'select namespace' ) ;
24
22
$scope . metricSegment = uiSegmentSrv . getSegmentForValue ( $scope . target . metricName , 'select metric' ) ;
23
+
24
+ $scope . dimSegments = _ . reduce ( $scope . target . dimensions , function ( memo , value , key ) {
25
+ memo . push ( uiSegmentSrv . newKey ( key ) ) ;
26
+ memo . push ( uiSegmentSrv . newOperator ( "=" ) ) ;
27
+ memo . push ( uiSegmentSrv . newKeyValue ( value ) ) ;
28
+ return memo ;
29
+ } , [ ] ) ;
30
+
31
+ $scope . fixSegments ( ) ;
32
+ $scope . removeDimSegment = uiSegmentSrv . newSegment ( { fake : true , value : '-- remove dimension --' } ) ;
33
+ } ;
34
+
35
+ $scope . fixSegments = function ( ) {
36
+ var count = $scope . dimSegments . length ;
37
+ var lastSegment = $scope . dimSegments [ Math . max ( count - 1 , 0 ) ] ;
38
+
39
+ if ( ! lastSegment || lastSegment . type !== 'plus-button' ) {
40
+ $scope . dimSegments . push ( uiSegmentSrv . newPlusButton ( ) ) ;
41
+ }
42
+ } ;
43
+
44
+ $scope . getDimSegments = function ( segment ) {
45
+ if ( segment . type === 'operator' ) { return $q . when ( [ ] ) ; }
46
+
47
+ var target = $scope . target ;
48
+ var query = $q . when ( [ ] ) ;
49
+
50
+ if ( segment . type === 'key' || segment . type === 'plus-button' ) {
51
+ query = $scope . datasource . getDimensionKeys ( $scope . target . namespace ) ;
52
+ } else if ( segment . type === 'value' ) {
53
+ query = $scope . datasource . getDimensionValues ( target . region , target . namespace , target . metricName , { } ) ;
54
+ }
55
+
56
+ return query . then ( $scope . transformToSegments ( true ) ) . then ( function ( results ) {
57
+ if ( segment . type === 'key' ) {
58
+ results . splice ( 0 , 0 , angular . copy ( $scope . removeDimSegment ) ) ;
59
+ }
60
+ return results ;
61
+ } ) ;
62
+ } ;
63
+
64
+ $scope . dimSegmentChanged = function ( segment , index ) {
65
+ $scope . dimSegments [ index ] = segment ;
66
+
67
+ if ( segment . value === $scope . removeDimSegment . value ) {
68
+ $scope . dimSegments . splice ( index , 3 ) ;
69
+ }
70
+ else if ( segment . type === 'plus-button' ) {
71
+ $scope . dimSegments . push ( uiSegmentSrv . newOperator ( '=' ) ) ;
72
+ $scope . dimSegments . push ( uiSegmentSrv . newFake ( 'select dimension value' , 'value' , 'query-segment-value' ) ) ;
73
+ segment . type = 'key' ;
74
+ segment . cssClass = 'query-segment-key' ;
75
+ }
76
+
77
+ $scope . fixSegments ( ) ;
78
+ $scope . syncDimSegmentsWithModel ( ) ;
79
+ $scope . get_data ( ) ;
80
+ } ;
81
+
82
+ $scope . syncDimSegmentsWithModel = function ( ) {
83
+ var dims = { } ;
84
+ var length = $scope . dimSegments . length ;
85
+
86
+ for ( var i = 0 ; i < length - 2 ; i += 3 ) {
87
+ var keySegment = $scope . dimSegments [ i ] ;
88
+ var valueSegment = $scope . dimSegments [ i + 2 ] ;
89
+ if ( ! valueSegment . fake ) {
90
+ dims [ keySegment . value ] = valueSegment . value ;
91
+ }
92
+ }
93
+
94
+ $scope . target . dimensions = dims ;
25
95
} ;
26
96
27
97
$scope . getRegions = function ( ) {
28
98
return $scope . datasource . metricFindQuery ( 'regions()' )
29
- . then ( $scope . transformToSegments ( true ) ) ;
99
+ . then ( $scope . transformToSegments ( true ) ) ;
30
100
} ;
31
101
32
102
$scope . getNamespaces = function ( ) {
33
103
return $scope . datasource . metricFindQuery ( 'namespaces()' )
34
- . then ( $scope . transformToSegments ( true ) ) ;
104
+ . then ( $scope . transformToSegments ( true ) ) ;
35
105
} ;
36
106
37
107
$scope . getMetrics = function ( ) {
38
108
return $scope . datasource . metricFindQuery ( 'metrics(' + $scope . target . namespace + ')' )
39
- . then ( $scope . transformToSegments ( true ) ) ;
109
+ . then ( $scope . transformToSegments ( true ) ) ;
40
110
} ;
41
111
42
112
$scope . regionChanged = function ( ) {
@@ -71,40 +141,12 @@ function (angular, _) {
71
141
} ;
72
142
73
143
$scope . refreshMetricData = function ( ) {
74
- $scope . target . errors = validateTarget ( $scope . target ) ;
75
-
76
- // this does not work so good
77
- if ( ! _ . isEqual ( $scope . oldTarget , $scope . target ) && _ . isEmpty ( $scope . target . errors ) ) {
144
+ if ( ! _ . isEqual ( $scope . oldTarget , $scope . target ) ) {
78
145
$scope . oldTarget = angular . copy ( $scope . target ) ;
79
146
$scope . get_data ( ) ;
80
147
}
81
148
} ;
82
149
83
- $scope . suggestDimensionKeys = function ( query , callback ) { // jshint unused:false
84
- $scope . datasource . getDimensionKeys ( $scope . target . namespace ) . then ( function ( result ) {
85
- callback ( _ . pluck ( result , 'text' ) ) ;
86
- } ) ;
87
- } ;
88
-
89
- // TODO: Removed template variables from the suggest
90
- // add this feature back after improving the editor
91
- $scope . suggestDimensionValues = function ( query , callback ) {
92
- if ( ! $scope . target . namespace || ! $scope . target . metricName ) {
93
- return callback ( [ ] ) ;
94
- }
95
-
96
- return $scope . datasource . getDimensionValues (
97
- $scope . target . region ,
98
- $scope . target . namespace ,
99
- $scope . target . metricName ,
100
- $scope . target . dimensions
101
- ) . then ( function ( result ) {
102
- callback ( result ) ;
103
- } , function ( ) {
104
- callback ( [ ] ) ;
105
- } ) ;
106
- } ;
107
-
108
150
$scope . addDimension = function ( ) {
109
151
if ( ! $scope . addDimensionMode ) {
110
152
$scope . addDimensionMode = true ;
@@ -147,17 +189,6 @@ function (angular, _) {
147
189
$scope . refreshMetricData ( ) ;
148
190
} ;
149
191
150
- // TODO: validate target
151
- function validateTarget ( ) {
152
- var errs = { } ;
153
-
154
- if ( $scope . target . period < 60 || ( $scope . target . period % 60 ) !== 0 ) {
155
- errs . period = 'Period must be at least 60 seconds and must be a multiple of 60' ;
156
- }
157
-
158
- return errs ;
159
- }
160
-
161
192
$scope . init ( ) ;
162
193
163
194
} ) ;
0 commit comments