@@ -3,47 +3,79 @@ import { InlineLabel, QueryField, Select, Switch } from '@grafana/ui';
3
3
import { QueryEditorProps } from '@grafana/data' ;
4
4
import { DataSource } from '../datasource' ;
5
5
import { MyDataSourceOptions , MyQuery } from '../types' ;
6
- import { getStreams } from '../streams' ;
6
+ import { getStreams } from '../services/streams' ;
7
+ import { getOrganizations } from '../services/organizations' ;
7
8
import { css } from '@emotion/css' ;
8
9
9
10
type Props = QueryEditorProps < DataSource , MyQuery , MyDataSourceOptions > ;
10
11
11
12
export function QueryEditor ( { query, onChange, onRunQuery, datasource } : Props ) {
12
13
const [ streams , setStreams ] : any = useState ( { } ) ;
13
14
const [ streamOptions , setStreamOptions ] : any = useState ( [ ] ) ;
15
+ const [ orgOptions , setOrgOptions ] : any = useState ( [ ] ) ;
14
16
15
17
useEffect ( ( ) => {
16
- getStreams ( datasource . url ) . then ( ( response : any ) => {
17
- const streams : { [ key : string ] : any } = { } ;
18
- response . list . forEach ( ( stream : any ) => {
19
- streams [ stream . name ] = stream ;
20
- } ) ;
21
- setStreams ( { ...streams } ) ;
22
- onChange ( {
23
- ...query ,
24
- query : query . query || '' ,
25
- stream : response . list [ 0 ] . name ,
26
- streamFields : response . list [ 0 ] . schema ,
27
- sqlMode : false ,
28
- } ) ;
29
- setStreamOptions ( [
30
- ...response . list . map ( ( stream : any ) => ( {
31
- label : stream . name ,
32
- value : stream . name ,
33
- } ) ) ,
34
- ] ) ;
35
- onRunQuery ( ) ;
36
- } ) ;
18
+ getOrganizations ( { url : datasource . url , page_num : 0 , page_size : 1000 , sort_by : 'id' } )
19
+ . then ( ( orgs : any ) => {
20
+ setOrgOptions ( [
21
+ ...orgs . data . map ( ( org : any ) => ( {
22
+ label : org . name ,
23
+ value : org . name ,
24
+ } ) ) ,
25
+ ] ) ;
26
+ setupStreams ( orgs . data [ 0 ] . name ) . then ( ( streams : any ) => {
27
+ onChange ( {
28
+ ...query ,
29
+ query : '' ,
30
+ stream : streams [ 0 ] . name ,
31
+ organization : orgs . data [ 0 ] . name ,
32
+ streamFields : streams [ 0 ] . schema ,
33
+ sqlMode : false ,
34
+ } ) ;
35
+ setStreamOptions ( [
36
+ ...Object . values ( streams ) . map ( ( stream : any ) => ( {
37
+ label : stream . name ,
38
+ value : stream . name ,
39
+ } ) ) ,
40
+ ] ) ;
41
+ onRunQuery ( ) ;
42
+ } ) ;
43
+ } )
44
+ . catch ( ( err ) => console . log ( err ) ) ;
37
45
// eslint-disable-next-line react-hooks/exhaustive-deps
38
46
} , [ ] ) ;
39
47
40
48
useEffect ( ( ) => {
41
- if ( query . sqlMode !== undefined ) {
49
+ if ( query . sqlMode !== undefined && query . stream ) {
42
50
updateQuery ( ) ;
43
51
}
44
52
45
53
// eslint-disable-next-line react-hooks/exhaustive-deps
46
- } , [ query . sqlMode ] ) ;
54
+ } , [ query . sqlMode , query . organization , query . stream ] ) ;
55
+
56
+ useEffect ( ( ) => {
57
+ if ( query . stream && query . organization ) {
58
+ updateQuery ( ) ;
59
+ onRunQuery ( ) ;
60
+ }
61
+
62
+ // eslint-disable-next-line react-hooks/exhaustive-deps
63
+ } , [ query . organization , query . stream ] ) ;
64
+
65
+ const setupStreams = ( orgName : string ) => {
66
+ return new Promise ( ( resolve ) => {
67
+ getStreams ( datasource . url , orgName )
68
+ . then ( ( response : any ) => {
69
+ const streams : { [ key : string ] : any } = { } ;
70
+ response . list . forEach ( ( stream : any ) => {
71
+ streams [ stream . name ] = stream ;
72
+ } ) ;
73
+ setStreams ( { ...streams } ) ;
74
+ resolve ( response . list ) ;
75
+ } )
76
+ . catch ( ( err ) => console . log ( err ) ) ;
77
+ } ) ;
78
+ } ;
47
79
48
80
const updateQuery = ( ) => {
49
81
let newQuery = query . query ;
@@ -69,12 +101,31 @@ export function QueryEditor({ query, onChange, onRunQuery, datasource }: Props)
69
101
const streamUpdated = ( stream : any ) => {
70
102
onChange ( {
71
103
...query ,
104
+ query : '' ,
72
105
stream : stream . value ,
73
106
streamFields : streams [ stream . value ] . schema ,
74
107
} ) ;
75
108
onRunQuery ( ) ;
76
109
} ;
77
110
111
+ const orgUpdated = ( organization : any ) => {
112
+ setupStreams ( organization . value ) . then ( ( streams : any ) => {
113
+ onChange ( {
114
+ ...query ,
115
+ query : '' ,
116
+ stream : streams [ 0 ] . name ,
117
+ organization : organization . value ,
118
+ streamFields : streams [ 0 ] . schema ,
119
+ } ) ;
120
+ setStreamOptions ( [
121
+ ...streams . map ( ( stream : any ) => ( {
122
+ label : stream . name ,
123
+ value : stream . name ,
124
+ } ) ) ,
125
+ ] ) ;
126
+ } ) ;
127
+ } ;
128
+
78
129
const toggleSqlMode = ( ) => {
79
130
onChange ( {
80
131
...query ,
@@ -106,23 +157,55 @@ export function QueryEditor({ query, onChange, onRunQuery, datasource }: Props)
106
157
align-items : center;
107
158
` }
108
159
>
109
- < InlineLabel
160
+ < div
110
161
className = { css `
111
- width : fit-content;
162
+ display : flex;
163
+ align-items : center;
164
+ padding-right : 1rem ;
112
165
` }
113
- transparent
114
166
>
115
- Select Stream
116
- </ InlineLabel >
117
- < Select
167
+ < InlineLabel
168
+ className = { css `
169
+ width : fit-content;
170
+ ` }
171
+ transparent
172
+ >
173
+ Select Stream
174
+ </ InlineLabel >
175
+ < Select
176
+ className = { css `
177
+ width : 200px !important ;
178
+ margin : 8px 0px ;
179
+ ` }
180
+ options = { streamOptions }
181
+ value = { query . stream }
182
+ onChange = { streamUpdated }
183
+ />
184
+ </ div >
185
+ < div
118
186
className = { css `
119
- width : 200 px !important ;
120
- margin : 8 px 0 px ;
187
+ display : flex ;
188
+ align-items : center ;
121
189
` }
122
- options = { streamOptions }
123
- value = { query . stream }
124
- onChange = { streamUpdated }
125
- />
190
+ >
191
+ < InlineLabel
192
+ className = { css `
193
+ width : fit-content;
194
+ ` }
195
+ transparent
196
+ >
197
+ Select Organization
198
+ </ InlineLabel >
199
+ < Select
200
+ className = { css `
201
+ width : 200px !important ;
202
+ margin : 8px 0px ;
203
+ ` }
204
+ options = { orgOptions }
205
+ value = { query . organization }
206
+ onChange = { orgUpdated }
207
+ />
208
+ </ div >
126
209
</ div >
127
210
< QueryField
128
211
query = { query . query }
0 commit comments