@@ -53,11 +53,9 @@ describe('runShardSplitQuery()', () => {
5353 request = createRequest ( [ { expr : '$SELECTOR' , refId : 'A' , direction : LokiQueryDirection . Scan } ] ) ;
5454 datasource = createLokiDatasource ( ) ;
5555 datasource . languageProvider . fetchLabelValues = jest . fn ( ) ;
56- datasource . interpolateVariablesInQueries = jest . fn ( ) . mockImplementation ( ( queries : LokiQuery [ ] ) => {
57- return queries . map ( ( query ) => {
58- query . expr = query . expr . replace ( '$SELECTOR' , '{a="b"}' ) ;
59- return query ;
60- } ) ;
56+ datasource . applyTemplateVariables = jest . fn ( ) . mockImplementation ( ( query : LokiQuery ) => {
57+ query . expr = query . expr . replace ( '$SELECTOR' , '{a="b"}' ) ;
58+ return query ;
6159 } ) ;
6260 jest . mocked ( datasource . languageProvider . fetchLabelValues ) . mockResolvedValue ( [ '1' , '10' , '2' , '20' , '3' ] ) ;
6361 const { metricFrameA } = getMockFrames ( ) ;
@@ -83,6 +81,25 @@ describe('runShardSplitQuery()', () => {
8381 } ) ;
8482 } ) ;
8583
84+ test ( 'Interpolates queries before execution' , async ( ) => {
85+ const request = createRequest ( [ { expr : 'count_over_time({a="b"}[$__auto])' , refId : 'A' , step : '$step' } ] ) ;
86+ datasource = createLokiDatasource ( {
87+ replace : ( input = '' ) => {
88+ return input . replace ( '$__auto' , '5m' ) . replace ( '$step' , '5m' ) ;
89+ } ,
90+ getVariables : ( ) => [ ] ,
91+ } ) ;
92+ jest . spyOn ( datasource , 'runQuery' ) . mockReturnValue ( of ( { data : [ ] } ) ) ;
93+ datasource . languageProvider . fetchLabelValues = jest . fn ( ) ;
94+ jest . mocked ( datasource . languageProvider . fetchLabelValues ) . mockResolvedValue ( [ '1' , '10' , '2' , '20' , '3' ] ) ;
95+ await expect ( runShardSplitQuery ( datasource , request ) ) . toEmitValuesWith ( ( ) => {
96+ expect ( jest . mocked ( datasource . runQuery ) . mock . calls [ 0 ] [ 0 ] . targets [ 0 ] . expr ) . toBe (
97+ 'count_over_time({a="b", __stream_shard__=~"20|10"} | drop __stream_shard__[5m])'
98+ ) ;
99+ expect ( jest . mocked ( datasource . runQuery ) . mock . calls [ 0 ] [ 0 ] . targets [ 0 ] . step ) . toBe ( '5m' ) ;
100+ } ) ;
101+ } ) ;
102+
86103 test ( 'Users query splitting for querying over a day' , async ( ) => {
87104 await expect ( runShardSplitQuery ( datasource , request ) ) . toEmitValuesWith ( ( ) => {
88105 // 5 shards, 3 groups + empty shard group, 4 requests
@@ -92,7 +109,7 @@ describe('runShardSplitQuery()', () => {
92109
93110 test ( 'Interpolates queries before running' , async ( ) => {
94111 await expect ( runShardSplitQuery ( datasource , request ) ) . toEmitValuesWith ( ( ) => {
95- expect ( datasource . interpolateVariablesInQueries ) . toHaveBeenCalledTimes ( 1 ) ;
112+ expect ( datasource . applyTemplateVariables ) . toHaveBeenCalledTimes ( 5 ) ;
96113
97114 expect ( datasource . runQuery ) . toHaveBeenCalledWith ( {
98115 intervalMs : expect . any ( Number ) ,
@@ -153,11 +170,9 @@ describe('runShardSplitQuery()', () => {
153170 } ) ;
154171
155172 test ( 'Sends the whole stream selector to fetch values' , async ( ) => {
156- datasource . interpolateVariablesInQueries = jest . fn ( ) . mockImplementation ( ( queries : LokiQuery [ ] ) => {
157- return queries . map ( ( query ) => {
158- query . expr = query . expr . replace ( '$SELECTOR' , '{service_name="test", filter="true"}' ) ;
159- return query ;
160- } ) ;
173+ datasource . applyTemplateVariables = jest . fn ( ) . mockImplementation ( ( query : LokiQuery ) => {
174+ query . expr = query . expr . replace ( '$SELECTOR' , '{service_name="test", filter="true"}' ) ;
175+ return query ;
161176 } ) ;
162177
163178 await expect ( runShardSplitQuery ( datasource , request ) ) . toEmitValuesWith ( ( ) => {
0 commit comments