| 
									
										
										
										
											2016-09-22 22:05:20 +08:00
										 |  |  | ///<reference path="../../../headers/common.d.ts" />
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import kbn from 'app/core/utils/kbn'; | 
					
						
							|  |  |  | import _ from 'lodash'; | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  | import moment from 'moment'; | 
					
						
							| 
									
										
										
										
											2016-09-22 22:05:20 +08:00
										 |  |  | import TimeSeries from 'app/core/time_series2'; | 
					
						
							|  |  |  | import {colors} from 'app/core/core'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class DataProcessor { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(private panel) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getSeriesList(options) { | 
					
						
							| 
									
										
										
										
											2016-09-22 22:47:37 +08:00
										 |  |  |     if (!options.dataList || options.dataList.length === 0) { | 
					
						
							|  |  |  |       return []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // auto detect xaxis mode
 | 
					
						
							|  |  |  |     var firstItem; | 
					
						
							|  |  |  |     if (options.dataList && options.dataList.length > 0) { | 
					
						
							|  |  |  |       firstItem = options.dataList[0]; | 
					
						
							| 
									
										
										
										
											2016-09-24 19:08:58 +08:00
										 |  |  |       let autoDetectMode = this.getAutoDetectXAxisMode(firstItem); | 
					
						
							|  |  |  |       if (this.panel.xaxis.mode !== autoDetectMode) { | 
					
						
							|  |  |  |         this.panel.xaxis.mode = autoDetectMode; | 
					
						
							|  |  |  |         this.setPanelDefaultsForNewXAxisMode(); | 
					
						
							| 
									
										
										
										
											2016-09-22 22:47:37 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-22 22:05:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     switch (this.panel.xaxis.mode) { | 
					
						
							|  |  |  |       case 'series': | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  |       case 'time': { | 
					
						
							|  |  |  |         return options.dataList.map((item, index) => { | 
					
						
							|  |  |  |           return this.timeSeriesHandler(item, index, options); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2016-09-22 22:05:20 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-09-24 19:08:58 +08:00
										 |  |  |       case 'field': { | 
					
						
							| 
									
										
										
										
											2016-09-22 22:47:37 +08:00
										 |  |  |         return this.customHandler(firstItem); | 
					
						
							| 
									
										
										
										
											2016-09-22 22:05:20 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 19:08:58 +08:00
										 |  |  |   getAutoDetectXAxisMode(firstItem) { | 
					
						
							|  |  |  |     switch (firstItem.type) { | 
					
						
							|  |  |  |       case 'docs': return 'field'; | 
					
						
							|  |  |  |       case 'table': return 'field'; | 
					
						
							|  |  |  |       default: { | 
					
						
							|  |  |  |         if (this.panel.xaxis.mode === 'series') { | 
					
						
							|  |  |  |           return 'series'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return 'time'; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   setPanelDefaultsForNewXAxisMode() { | 
					
						
							|  |  |  |     switch (this.panel.xaxis.mode) { | 
					
						
							|  |  |  |       case 'time': { | 
					
						
							|  |  |  |         this.panel.bars = false; | 
					
						
							|  |  |  |         this.panel.lines = true; | 
					
						
							|  |  |  |         this.panel.points = false; | 
					
						
							|  |  |  |         this.panel.legend.show = true; | 
					
						
							|  |  |  |         this.panel.tooltip.shared = true; | 
					
						
							|  |  |  |         this.panel.xaxis.values = []; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       case 'series': { | 
					
						
							|  |  |  |         this.panel.bars = true; | 
					
						
							|  |  |  |         this.panel.lines = false; | 
					
						
							|  |  |  |         this.panel.points = false; | 
					
						
							|  |  |  |         this.panel.stack = false; | 
					
						
							|  |  |  |         this.panel.legend.show = false; | 
					
						
							|  |  |  |         this.panel.tooltip.shared = false; | 
					
						
							| 
									
										
										
										
											2016-09-26 20:04:13 +08:00
										 |  |  |         this.panel.xaxis.values = ['total']; | 
					
						
							| 
									
										
										
										
											2016-09-24 19:08:58 +08:00
										 |  |  |         break; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  |   timeSeriesHandler(seriesData, index, options) { | 
					
						
							| 
									
										
										
										
											2016-09-28 00:17:39 +08:00
										 |  |  |     var datapoints = seriesData.datapoints || []; | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  |     var alias = seriesData.target; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 22:05:20 +08:00
										 |  |  |     var colorIndex = index % colors.length; | 
					
						
							|  |  |  |     var color = this.panel.aliasColors[alias] || colors[colorIndex]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var series = new TimeSeries({datapoints: datapoints, alias: alias, color: color, unit: seriesData.unit}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  |     if (datapoints && datapoints.length > 0) { | 
					
						
							|  |  |  |       var last = datapoints[datapoints.length - 1][1]; | 
					
						
							|  |  |  |       var from = options.range.from; | 
					
						
							|  |  |  |       if (last - from < -10000) { | 
					
						
							|  |  |  |         series.isOutsideRange = true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-22 22:05:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return series; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 22:47:37 +08:00
										 |  |  |   customHandler(dataItem) { | 
					
						
							| 
									
										
										
										
											2016-09-23 23:12:10 +08:00
										 |  |  |     let nameField = this.panel.xaxis.name; | 
					
						
							|  |  |  |     if (!nameField) { | 
					
						
							|  |  |  |       throw {message: 'No field name specified to use for x-axis, check your axes settings'}; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-22 22:47:37 +08:00
										 |  |  |     return []; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 22:05:20 +08:00
										 |  |  |   validateXAxisSeriesValue() { | 
					
						
							|  |  |  |     switch (this.panel.xaxis.mode) { | 
					
						
							|  |  |  |       case 'series': { | 
					
						
							|  |  |  |         if (this.panel.xaxis.values.length === 0) { | 
					
						
							|  |  |  |           this.panel.xaxis.values = ['total']; | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var validOptions = this.getXAxisValueOptions({}); | 
					
						
							|  |  |  |         var found = _.find(validOptions, {value: this.panel.xaxis.values[0]}); | 
					
						
							|  |  |  |         if (!found) { | 
					
						
							|  |  |  |           this.panel.xaxis.values = ['total']; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 19:08:58 +08:00
										 |  |  |   getDataFieldNames(dataList, onlyNumbers) { | 
					
						
							| 
									
										
										
										
											2016-09-23 23:12:10 +08:00
										 |  |  |     if (dataList.length === 0) { | 
					
						
							|  |  |  |       return []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 19:08:58 +08:00
										 |  |  |     let fields = []; | 
					
						
							| 
									
										
										
										
											2016-09-23 23:12:10 +08:00
										 |  |  |     var firstItem = dataList[0]; | 
					
						
							|  |  |  |     if (firstItem.type === 'docs'){ | 
					
						
							|  |  |  |       if (firstItem.datapoints.length === 0) { | 
					
						
							|  |  |  |         return []; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 19:08:58 +08:00
										 |  |  |       let fieldParts = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       function getPropertiesRecursive(obj) { | 
					
						
							|  |  |  |         _.forEach(obj, (value, key) => { | 
					
						
							|  |  |  |           if (_.isObject(value)) { | 
					
						
							|  |  |  |             fieldParts.push(key); | 
					
						
							|  |  |  |             getPropertiesRecursive(value); | 
					
						
							|  |  |  |           } else { | 
					
						
							|  |  |  |             if (!onlyNumbers || _.isNumber(value)) { | 
					
						
							|  |  |  |               let field = fieldParts.concat(key).join('.'); | 
					
						
							|  |  |  |               fields.push(field); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         fieldParts.pop(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       getPropertiesRecursive(firstItem.datapoints[0]); | 
					
						
							|  |  |  |       return fields; | 
					
						
							| 
									
										
										
										
											2016-09-23 23:12:10 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 22:05:20 +08:00
										 |  |  |   getXAxisValueOptions(options) { | 
					
						
							|  |  |  |     switch (this.panel.xaxis.mode) { | 
					
						
							|  |  |  |       case 'time': { | 
					
						
							|  |  |  |         return []; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       case 'series': { | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |           {text: 'Avg', value: 'avg'}, | 
					
						
							|  |  |  |           {text: 'Min', value: 'min'}, | 
					
						
							|  |  |  |           {text: 'Max', value: 'min'}, | 
					
						
							|  |  |  |           {text: 'Total', value: 'total'}, | 
					
						
							|  |  |  |           {text: 'Count', value: 'count'}, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 23:12:10 +08:00
										 |  |  |   pluckDeep(obj: any, property: string) { | 
					
						
							|  |  |  |     let propertyParts = property.split('.'); | 
					
						
							|  |  |  |     let value = obj; | 
					
						
							|  |  |  |     for (let i = 0; i < propertyParts.length; ++i) { | 
					
						
							|  |  |  |       if (value[propertyParts[i]]) { | 
					
						
							|  |  |  |         value = value[propertyParts[i]]; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         return undefined; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return value; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-22 22:05:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 |