@@ -27,6 +27,19 @@ describe('Scalyr datasource tests', () => {
2727 ]
2828 } ;
2929
30+ const annotationQueryOptions = {
31+ range : {
32+ from : sixHoursAgo . toISOString ( ) ,
33+ to : now . toISOString ( )
34+ } ,
35+ interval : '5s' ,
36+ annotation : [
37+ {
38+ queryText : '$foo=\'bar\'' ,
39+ }
40+ ]
41+ } ;
42+
3043 const variables = [
3144 {
3245 multi : true ,
@@ -135,4 +148,79 @@ describe('Scalyr datasource tests', () => {
135148 } ) ;
136149 } ) ;
137150
151+ describe ( 'Annotation queries' , ( ) => {
152+ let results ;
153+ beforeEach ( ( ) => {
154+ results = [
155+ {
156+ timefield : 12345 ,
157+ messagefield : "testmessage1" ,
158+ timeendfield : 54321
159+ } ,
160+ {
161+ timefield : 12345 ,
162+ messagefield : "testmessage2" ,
163+ timeendfield : 54321 ,
164+ attributes : {
165+ timefield : 11111 ,
166+ messagefield : "wrong" ,
167+ timeendfield : 22222 ,
168+ }
169+ } ,
170+ {
171+ timefield : 12345 ,
172+ messagefield : "testmessage4" ,
173+ timeendfield : 54321 ,
174+ attributes : {
175+ timefield2 : 123456 ,
176+ messagefield2 : "testmessage5" ,
177+ timeendfield2 : 543211 ,
178+ }
179+ }
180+ ] ;
181+ } ) ;
182+ it ( 'Should create a query request' , ( ) => {
183+ const request = datasource . createLogsQueryForAnnotation ( annotationQueryOptions , variables ) ;
184+ expect ( request . url ) . toBe ( 'proxied/query' ) ;
185+ expect ( request . method ) . toBe ( 'POST' ) ;
186+ const requestBody = JSON . parse ( request . data ) ;
187+ expect ( requestBody . token ) . toBe ( '123' ) ;
188+ expect ( requestBody . queryType ) . toBe ( 'log' ) ;
189+ expect ( requestBody . startTime ) . toBe ( sixHoursAgo . toISOString ( ) ) ;
190+ expect ( requestBody . endTime ) . toBe ( now . toISOString ( ) ) ;
191+ } ) ;
192+
193+ it ( 'Should transform standard query results to annotations' , ( ) => {
194+ const transformedResults = GenericDatasource . transformAnnotationResults ( results , "timefield" , "timeendfield" , "messagefield" ) ;
195+ expect ( transformedResults . length ) . toBe ( 3 ) ;
196+ const resultEntry = transformedResults [ 0 ] ;
197+ expect ( resultEntry . text ) . toBe ( "testmessage1" ) ;
198+ expect ( resultEntry . time ) . toBe ( 0.012345 ) ;
199+ expect ( resultEntry . timeEnd ) . toBe ( 0.054321 ) ;
200+ } ) ;
201+
202+ it ( 'Should transform standard query results to annotations, falling back to attribute fields' , ( ) => {
203+ const transformedResults = GenericDatasource . transformAnnotationResults ( results , "timefield2" , "timeendfield2" , "messagefield2" ) ;
204+ expect ( transformedResults . length ) . toBe ( 1 ) ;
205+ const resultEntry = transformedResults [ 0 ] ;
206+ expect ( resultEntry . text ) . toBe ( "testmessage5" ) ;
207+ expect ( resultEntry . time ) . toBe ( 0.123456 ) ;
208+ expect ( resultEntry . timeEnd ) . toBe ( 0.543211 ) ;
209+ } ) ;
210+
211+ it ( 'Should transform standard query results to annotations not from attributes first' , ( ) => {
212+ const transformedResults = GenericDatasource . transformAnnotationResults ( results , "timefield" , "timeendfield" , "messagefield" ) ;
213+ expect ( transformedResults . length ) . toBe ( 3 ) ;
214+ const resultEntry = transformedResults [ 1 ] ;
215+ expect ( resultEntry . text ) . toBe ( "testmessage2" ) ;
216+ expect ( resultEntry . time ) . toBe ( 0.012345 ) ;
217+ expect ( resultEntry . timeEnd ) . toBe ( 0.054321 ) ;
218+ } ) ;
219+
220+ it ( 'Shouldn\'t transform standard query results to annotations with bad field names' , ( ) => {
221+ const transformedResults = GenericDatasource . transformAnnotationResults ( results , "missingField" , null , null ) ;
222+ expect ( transformedResults . length ) . toBe ( 0 ) ;
223+ } ) ;
224+ } ) ;
225+
138226} ) ;
0 commit comments