@@ -7,7 +7,10 @@ import 'rxjs/add/operator/catch';
7
7
import 'rxjs/add/operator/map' ;
8
8
9
9
import { ErrorHandler , Page } from '../shared/model' ;
10
- import { Counter } from './counters/model/counter.model' ;
10
+ import { Counter } from './model/counter.model' ;
11
+ import { DashboardItem } from './model/dashboard-item.model' ;
12
+ import { MetricType } from './model/metric-type.model' ;
13
+
11
14
import { HttpUtils } from '../shared/support/http.utils' ;
12
15
import { ToastyService } from 'ng2-toasty' ;
13
16
/**
@@ -22,6 +25,11 @@ export class AnalyticsService {
22
25
public _counterInterval = 2 ;
23
26
public counterPoller : Subscription ;
24
27
28
+ public metricTypes : MetricType [ ] = MetricType . getMetricTypes ( ) ;
29
+
30
+ private rowId = 1 ; // For Dashboard
31
+ public dashboardItems : DashboardItem [ ] ;
32
+
25
33
constructor (
26
34
private http : Http ,
27
35
private errorHandler : ErrorHandler ,
@@ -89,10 +97,11 @@ export class AnalyticsService {
89
97
*
90
98
* @param detailed If true will request additional counter values from the REST endpoint
91
99
*/
92
- public getAllCounters ( detailed = false ) : Observable < Page < Counter > > {
100
+ private getAllCounters ( detailed = false ) : Observable < Page < Counter > > {
93
101
94
102
if ( ! this . counters ) {
95
103
this . counters = new Page < Counter > ( ) ;
104
+ this . counters . pageSize = 50 ;
96
105
}
97
106
98
107
const params = HttpUtils . getPaginationParams ( this . counters . pageNumber , this . counters . pageSize ) ;
@@ -104,28 +113,38 @@ export class AnalyticsService {
104
113
105
114
requestOptionsArgs . search = params ;
106
115
return this . http . get ( this . metricsCountersUrl , requestOptionsArgs )
107
- . map ( this . extractData . bind ( this ) )
116
+ . map ( response => this . extractData ( response , detailed ) )
108
117
. catch ( this . errorHandler . handleError ) ;
109
118
}
110
119
111
- private extractData ( response : Response ) : Page < Counter > {
120
+ private extractData ( response : Response , handleRates : boolean ) : Page < Counter > {
112
121
const body = response . json ( ) ;
113
122
const items : Counter [ ] = [ ] ;
114
123
const cache : Counter [ ] = [ ] ;
115
- for ( const oldCounter of this . counters . items ) {
116
- cache [ oldCounter . name ] = oldCounter ;
117
- }
118
- if ( body . _embedded && body . _embedded . counterResourceList ) {
119
- for ( const counterResourceListItems of body . _embedded . counterResourceList ) {
120
- const counter = new Counter ( ) . deserialize ( counterResourceListItems ) ;
121
-
122
- if ( cache [ counter . name ] ) {
123
- const cached = cache [ counter . name ] ;
124
- counter . rates = cached . rates ;
125
- counter . rates . push ( ( counter . value - cached . value ) / this . counterInterval ) ;
126
- counter . rates . splice ( 0 , counter . rates . length - this . totalCacheSize ( ) ) ;
124
+
125
+ if ( handleRates ) {
126
+ for ( const oldCounter of this . counters . items ) {
127
+ cache [ oldCounter . name ] = oldCounter ;
128
+ }
129
+ if ( body . _embedded && body . _embedded . counterResourceList ) {
130
+ for ( const counterResourceListItems of body . _embedded . counterResourceList ) {
131
+ const counter = new Counter ( ) . deserialize ( counterResourceListItems ) ;
132
+
133
+ if ( cache [ counter . name ] ) {
134
+ const cached = cache [ counter . name ] ;
135
+ counter . rates = cached . rates ;
136
+ counter . rates . push ( ( counter . value - cached . value ) / this . counterInterval ) ;
137
+ counter . rates . splice ( 0 , counter . rates . length - this . totalCacheSize ( ) ) ;
138
+ }
139
+ items . push ( counter ) ;
140
+ }
141
+ }
142
+ } else {
143
+ if ( body . _embedded && body . _embedded . metricResourceList ) {
144
+ for ( const metricResourceListItems of body . _embedded . metricResourceList ) {
145
+ const counter = new Counter ( ) . deserialize ( metricResourceListItems ) ;
146
+ items . push ( counter ) ;
127
147
}
128
- items . push ( counter ) ;
129
148
}
130
149
}
131
150
@@ -138,4 +157,129 @@ export class AnalyticsService {
138
157
this . counters . update ( page ) ;
139
158
return page ;
140
159
}
160
+
161
+ /**
162
+ * Adds a new empty dashboard item to the dashboardItems array.
163
+ * @param {number } index the location it should be added.
164
+ * @returns {DashboardItem } the new instance of the {DashboardItem}.
165
+ */
166
+ addNewDashboardItem ( index ?: number ) : DashboardItem {
167
+ if ( ! this . dashboardItems ) {
168
+ this . dashboardItems = [ ] ;
169
+ }
170
+ const dashboardItem = new DashboardItem ( ) ;
171
+ dashboardItem . id = this . rowId ++ ;
172
+ dashboardItem . refreshRate = 2 ;
173
+ dashboardItem . visualization = '' ;
174
+
175
+ if ( index ) {
176
+ this . dashboardItems . splice ( index , 0 , dashboardItem ) ;
177
+ } else {
178
+ this . dashboardItems . push ( dashboardItem ) ;
179
+ }
180
+ return dashboardItem ;
181
+ }
182
+
183
+ /**
184
+ * Remove dashboard item from dashboardItems array and splice the array.
185
+ * @param {number } index the offset of the dashboard item to remove.
186
+ */
187
+ removeDashboardItem ( index : number ) : void {
188
+ if ( ! this . dashboardItems || this . dashboardItems . length === 0 ) {
189
+ return ;
190
+ }
191
+ this . dashboardItems . splice ( index , 1 ) ;
192
+ }
193
+
194
+ /**
195
+ * Retrieve a list of all dashboardItems.
196
+ * @returns {Observable<DashboardItem> } observable of dashboard items.
197
+ */
198
+ getAllDashboardItems ( ) : Observable < DashboardItem > {
199
+ if ( ! this . dashboardItems ) {
200
+ this . addNewDashboardItem ( ) ;
201
+ }
202
+ return Observable . from ( this . dashboardItems ) ;
203
+ }
204
+
205
+ /**
206
+ * Retrieve all metrics for a specific type.
207
+ * @param {MetricType } metricType the specific metric type to retrieve.
208
+ * @returns {Observable<Page<Counter>> } Page containing the metrics.
209
+ */
210
+ getStreamsForMetricType ( metricType : MetricType ) {
211
+ if ( MetricType . COUNTER === metricType ) {
212
+ return this . getAllCounters ( ) ;
213
+ } else {
214
+ this . toastyService . error ( `Metric type ${ metricType . name } is not supported.` ) ;
215
+ }
216
+ }
217
+
218
+ resetDashboard ( ) {
219
+ this . dashboardItems . length = 0 ;
220
+ this . addNewDashboardItem ( ) ;
221
+ }
222
+
223
+ /**
224
+ * Starts the polling process for a single counters. Method
225
+ * will check if the poller is already running and will
226
+ * start the poller only if the poller is undefined or
227
+ * stopped. The subscription is store on the {@link DashboardItem}.
228
+ */
229
+ public startPollingForSingleDashboardItem ( dashboardItem : DashboardItem ) {
230
+ console . log ( dashboardItem ) ;
231
+ if ( ! dashboardItem . counterPoller || dashboardItem . counterPoller . closed ) {
232
+ dashboardItem . counterPoller = Observable . interval ( dashboardItem . refreshRate * 1000 )
233
+ . switchMap ( ( ) => this . getSingleCounter ( dashboardItem . counter . name ) ) . subscribe (
234
+ result => {
235
+ dashboardItem . counter . rates . push ( ( result . value - dashboardItem . counter . value ) / dashboardItem . refreshRate ) ;
236
+ dashboardItem . counter . rates . splice ( 0 , dashboardItem . counter . rates . length - this . totalCacheSize ( ) ) ;
237
+ dashboardItem . counter . value = result . value ;
238
+ } ,
239
+ error => {
240
+ console . log ( 'error' , error ) ;
241
+ this . toastyService . error ( error ) ;
242
+ } ) ;
243
+ }
244
+ }
245
+
246
+ /**
247
+ * Stops the polling process for counters if the poller
248
+ * is running and is defined.
249
+ */
250
+ public stopPollingOfSingleDashboardItem ( dashboardItem : DashboardItem ) {
251
+ if ( dashboardItem . counterPoller && ! dashboardItem . counterPoller . closed ) {
252
+ dashboardItem . counterPoller . unsubscribe ( ) ;
253
+ }
254
+ }
255
+
256
+ /**
257
+ * Restarts the polling process for counters if the poller
258
+ * is running and is defined. Will stop the poller first and restart
259
+ * the counter with the {@link DashboardItem}s refresh rate.
260
+ *
261
+ * Will NOT restart the poller if the refresh rate is zero.
262
+ */
263
+ public restartPollingOfSingleDashboardItem ( dashboardItem : DashboardItem ) {
264
+ this . stopPollingOfSingleDashboardItem ( dashboardItem ) ;
265
+ if ( dashboardItem . refreshRate > 0 ) {
266
+ this . startPollingForSingleDashboardItem ( dashboardItem ) ;
267
+ }
268
+ }
269
+
270
+ /**
271
+ * Retrieves all counters. Will take pagination into account.
272
+ *
273
+ * @param detailed If true will request additional counter values from the REST endpoint
274
+ */
275
+ private getSingleCounter ( counterName : string ) : Observable < Counter > {
276
+ const requestOptionsArgs : RequestOptionsArgs = HttpUtils . getDefaultRequestOptions ( ) ;
277
+ return this . http . get ( this . metricsCountersUrl + '/' + counterName , requestOptionsArgs )
278
+ . map ( response => {
279
+ const body = response . json ( ) ;
280
+ console . log ( 'body' , body ) ;
281
+ return new Counter ( ) . deserialize ( body ) ;
282
+ } )
283
+ . catch ( this . errorHandler . handleError ) ;
284
+ }
141
285
}
0 commit comments