1
1
import React , { PureComponent } from 'react' ;
2
2
import { PanelProps } from '@grafana/data' ;
3
- import { getTemplateSrv , getLocationSrv } from '@grafana/runtime'
3
+ import { getTemplateSrv , getLocationSrv } from '@grafana/runtime' ;
4
4
import { SimpleOptions , defaults } from 'types' ;
5
5
import merge from 'deepmerge' ;
6
6
import _ from 'lodash' ;
@@ -19,73 +19,67 @@ declare global {
19
19
window . Plotly = Plotly ;
20
20
//window.LocationSrv = getLocationSrv();
21
21
22
- let templateSrv :any = getTemplateSrv ( ) ;
22
+ let templateSrv : any = getTemplateSrv ( ) ;
23
23
24
24
interface Props extends PanelProps < SimpleOptions > { }
25
25
26
26
export class SimplePanel extends PureComponent < Props > {
27
27
render ( ) {
28
-
29
28
//Get all variables
30
29
const context = {
31
30
//interval: templateSrv.getBuiltInIntervalValue(),//dataSource.templateSrv.builtIns.__interval.value,
32
- __from :this . props . replaceVariables ( '$__from' ) ,
33
- __to :this . props . replaceVariables ( '$__to' ) ,
34
- __interval :this . props . replaceVariables ( '$__interval' ) ,
35
- __interval_ms :this . props . replaceVariables ( '$__interval_ms' )
31
+ __from : this . props . replaceVariables ( '$__from' ) ,
32
+ __to : this . props . replaceVariables ( '$__to' ) ,
33
+ __interval : this . props . replaceVariables ( '$__interval' ) ,
34
+ __interval_ms : this . props . replaceVariables ( '$__interval_ms' ) ,
36
35
} as any ;
37
- templateSrv . getVariables ( ) . forEach ( ( elt : any ) => {
38
- context [ elt . name ] = elt . current . text ;
39
- } )
40
-
41
-
36
+ templateSrv . getVariables ( ) . forEach ( ( elt : any ) => {
37
+ context [ elt . name ] = elt . current . text ;
38
+ } ) ;
39
+
42
40
//const NbValues = data.series[0].rows.length;
43
41
44
- let config = this . props . options . config || defaults . config
45
- let data = this . props . options . data || defaults . data
42
+ let config = this . props . options . config || defaults . config ;
43
+ let data = this . props . options . data || defaults . data ;
46
44
let layout = this . props . options . layout || defaults . layout ;
47
45
let frames = this . props . options . frames || defaults . frames ;
48
46
49
47
let parameters : any ;
50
- parameters = [ this . props . options . data , layout , config ] ;
48
+ parameters = [ this . props . options . data , layout , config ] ;
51
49
52
50
let error : any ;
53
51
try {
54
52
if ( this . props . options . script !== '' && this . props . data . state !== 'Error' ) {
55
53
var f = new Function ( 'data,variables' , this . props . options . script ) ;
56
54
parameters = f ( this . props . data , context ) ;
57
- if ( ! parameters ) {
55
+ if ( ! parameters ) {
58
56
throw new Error ( 'Script must return values' ) ;
59
57
}
60
58
}
61
59
} catch ( e ) {
62
60
error = e ;
63
61
console . error ( e ) ;
64
-
62
+
65
63
//Can't update chart when script is changing if throw error?!?
66
64
//throw new Error('There\'s an error in your script. Check the console to see error\'s details');
67
-
68
65
}
69
66
67
+ const combineMerge = ( target , source , options ) => {
68
+ const destination = target . slice ( ) ;
70
69
71
-
72
-
73
- const combineMerge = ( target , source , options ) => {
74
- const destination = target . slice ( )
75
-
76
- source . forEach ( ( item , index ) => {
77
- if ( typeof destination [ index ] === 'undefined' ) {
78
- destination [ index ] = options . cloneUnlessOtherwiseSpecified ( item , options )
79
- } else if ( options . isMergeableObject ( item ) ) {
80
- destination [ index ] = merge ( target [ index ] , item , options )
81
- } else if ( target . indexOf ( item ) === - 1 ) {
82
- destination . push ( item )
83
- }
84
- } )
85
- return destination
86
- }
87
- //Merge data field and data transformed by script
88
- /*let series: any[] = [];
70
+ source . forEach ( ( item , index ) => {
71
+ if ( typeof destination [ index ] === 'undefined' ) {
72
+ destination [ index ] = options . cloneUnlessOtherwiseSpecified ( item , options ) ;
73
+ } else if ( options . isMergeableObject ( item ) ) {
74
+ destination [ index ] = merge ( target [ index ] , item , options ) ;
75
+ } else if ( target . indexOf ( item ) === - 1 ) {
76
+ destination . push ( item ) ;
77
+ }
78
+ } ) ;
79
+ return destination ;
80
+ } ;
81
+ //Merge data field and data transformed by script
82
+ /*let series: any[] = [];
89
83
if (data2.length && data2.length > 0) {
90
84
data2.forEach((serie, index) => {
91
85
let options = this.props.options.data[index];
@@ -95,35 +89,41 @@ export class SimplePanel extends PureComponent<Props> {
95
89
});
96
90
});
97
91
}*/
98
-
99
- layout = { ...layout , autosize : true , height : this . props . height } ;
100
- let display : any ;
101
- if ( error ) {
102
- let matches = error . stack . match ( / a n o n y m o u s > : .* \) / m) ;
103
- let lines = matches ?matches [ 0 ] . slice ( 0 , - 1 ) . split ( ':' ) :null
104
- display = < div > There's an error in your script : < br /> < span style = { { color :'#D00' } } > { error . toString ( ) } </ span > { lines ?"- line " + ( parseInt ( lines [ 1 ] ) - 2 ) + ":" + lines [ 2 ] :"" } (Check your console for more details)</ div >
105
- } else {
106
- display = < Plot
107
- style = { {
108
- width : '100%' ,
109
- height : '100%' ,
110
- } }
111
- data = { parameters . data ?merge ( data , parameters . data , { arrayMerge : combineMerge } ) :data }
112
- frames = { parameters . frames ?merge ( data , parameters . frames , { arrayMerge : combineMerge } ) :frames }
113
- onInitialized = { ( figure : any , graphDiv : any ) => this . setState ( { figure : figure , graphDiv : graphDiv } ) }
114
- //layout={ {autosize:true, height:this.props.height, title: this.props.options.title} }
115
- layout = { parameters . layout ?merge ( layout , parameters . layout ) :layout }
116
- config = { parameters . config ?merge ( config , parameters . config ) :config }
117
- useResizeHandler = { true }
118
- onClick = { data => {
119
- //console.log(data)
120
- var f = new Function ( 'data' , 'getLocationSrv' , 'getTemplateSrv' , this . props . options . onclick ) ;
121
- f ( data , getLocationSrv , getTemplateSrv ) ;
122
- } }
123
- > </ Plot >
124
-
125
- }
126
- return display ;
127
92
93
+ layout = { ...layout , autosize : true , height : this . props . height } ;
94
+ let display : any ;
95
+ if ( error ) {
96
+ let matches = error . stack . match ( / a n o n y m o u s > : .* \) / m) ;
97
+ let lines = matches ? matches [ 0 ] . slice ( 0 , - 1 ) . split ( ':' ) : null ;
98
+ display = (
99
+ < div >
100
+ There's an error in your script : < br />
101
+ < span style = { { color : '#D00' } } > { error . toString ( ) } </ span > { ' ' }
102
+ { lines ? '- line ' + ( parseInt ( lines [ 1 ] , 10 ) - 2 ) + ':' + lines [ 2 ] : '' } (Check your console for more details)
103
+ </ div >
104
+ ) ;
105
+ } else {
106
+ display = (
107
+ < Plot
108
+ style = { {
109
+ width : '100%' ,
110
+ height : '100%' ,
111
+ } }
112
+ data = { parameters . data ? merge ( data , parameters . data , { arrayMerge : combineMerge } ) : data }
113
+ frames = { parameters . frames ? merge ( data , parameters . frames , { arrayMerge : combineMerge } ) : frames }
114
+ onInitialized = { ( figure : any , graphDiv : any ) => this . setState ( { figure : figure , graphDiv : graphDiv } ) }
115
+ //layout={ {autosize:true, height:this.props.height, title: this.props.options.title} }
116
+ layout = { parameters . layout ? merge ( layout , parameters . layout ) : layout }
117
+ config = { parameters . config ? merge ( config , parameters . config ) : config }
118
+ useResizeHandler = { true }
119
+ onClick = { ( data ) => {
120
+ //console.log(data)
121
+ var f = new Function ( 'data' , 'getLocationSrv' , 'getTemplateSrv' , this . props . options . onclick ) ;
122
+ f ( data , getLocationSrv , getTemplateSrv ) ;
123
+ } }
124
+ > </ Plot >
125
+ ) ;
126
+ }
127
+ return display ;
128
128
}
129
129
}
0 commit comments