|
1 | | -import { formatQuery, luceneEscape } from './base'; |
| 1 | +import { AdHocVariableFilter } from '@grafana/data'; |
| 2 | +import { from } from 'rxjs'; |
| 3 | + |
| 4 | +import { addAddHocFilter } from '../modifyQuery'; |
| 5 | +import { ElasticsearchQuery } from '../types'; |
| 6 | +import { BaseQuickwitDataSource, formatQuery, luceneEscape } from './base'; |
2 | 7 |
|
3 | 8 | describe('BaseQuickwitDataSource', () => { |
4 | 9 | describe('luceneEscape', () => { |
@@ -171,4 +176,150 @@ describe('BaseQuickwitDataSource', () => { |
171 | 176 | }); |
172 | 177 | }); |
173 | 178 | }); |
| 179 | + |
| 180 | + describe('quick filters', () => { |
| 181 | + const addFilterToQuery = ( |
| 182 | + fieldTypes: Record<string, string>, |
| 183 | + query: ElasticsearchQuery, |
| 184 | + key: string, |
| 185 | + value: string, |
| 186 | + negate = false |
| 187 | + ) => { |
| 188 | + return (BaseQuickwitDataSource.prototype as any).addFilterToQuery.call( |
| 189 | + { fieldTypes }, |
| 190 | + query, |
| 191 | + key, |
| 192 | + value, |
| 193 | + negate |
| 194 | + ) as ElasticsearchQuery; |
| 195 | + }; |
| 196 | + |
| 197 | + const renderAdHocFilters = ( |
| 198 | + fieldTypes: Record<string, string>, |
| 199 | + filters: AdHocVariableFilter[] |
| 200 | + ) => { |
| 201 | + return (BaseQuickwitDataSource.prototype as any).addAdHocFilters.call( |
| 202 | + { fieldTypes }, |
| 203 | + '', |
| 204 | + filters |
| 205 | + ) as string; |
| 206 | + }; |
| 207 | + |
| 208 | + it('adds text filters with whitespace as phrase filters', () => { |
| 209 | + const query = { refId: 'A', query: '', metrics: [], bucketAggs: [], filters: [] } as any; |
| 210 | + |
| 211 | + const updatedQuery = addFilterToQuery( |
| 212 | + { 'attributes.grpc_message': 'text' }, |
| 213 | + query, |
| 214 | + 'attributes.grpc_message', |
| 215 | + 'Error:[(0) invalid token, ]' |
| 216 | + ); |
| 217 | + |
| 218 | + expect(updatedQuery.filters?.[0].filter).toEqual({ |
| 219 | + key: 'attributes.grpc_message', |
| 220 | + operator: '=', |
| 221 | + value: 'Error:[(0) invalid token, ]', |
| 222 | + }); |
| 223 | + }); |
| 224 | + |
| 225 | + it('renders text phrase filters with quoted Quickwit syntax', () => { |
| 226 | + const result = renderAdHocFilters( |
| 227 | + { 'attributes.grpc_message': 'text' }, |
| 228 | + [{ |
| 229 | + key: 'attributes.grpc_message', |
| 230 | + operator: '=', |
| 231 | + value: 'Error:[(0) invalid token, ]', |
| 232 | + }] |
| 233 | + ); |
| 234 | + |
| 235 | + expect(result).toBe('attributes.grpc_message:"Error:[(0) invalid token, ]"'); |
| 236 | + }); |
| 237 | + |
| 238 | + it('renders negative text phrase filters with quoted Quickwit syntax', () => { |
| 239 | + const result = renderAdHocFilters( |
| 240 | + { 'attributes.grpc_message': 'text' }, |
| 241 | + [{ |
| 242 | + key: 'attributes.grpc_message', |
| 243 | + operator: '!=', |
| 244 | + value: 'Error:[(0) invalid token, ]', |
| 245 | + }] |
| 246 | + ); |
| 247 | + |
| 248 | + expect(result).toBe('-attributes.grpc_message:"Error:[(0) invalid token, ]"'); |
| 249 | + }); |
| 250 | + |
| 251 | + it('keeps simple-token text filters as term filters', () => { |
| 252 | + const query = { refId: 'A', query: '', metrics: [], bucketAggs: [], filters: [] } as any; |
| 253 | + |
| 254 | + const updatedQuery = addFilterToQuery( |
| 255 | + { 'attributes.grpc_message': 'text' }, |
| 256 | + query, |
| 257 | + 'attributes.grpc_message', |
| 258 | + 'unavailable' |
| 259 | + ); |
| 260 | + |
| 261 | + expect(updatedQuery.filters?.[0].filter.operator).toBe('term'); |
| 262 | + }); |
| 263 | + |
| 264 | + it('keeps punctuated text filters as phrase filters', () => { |
| 265 | + const query = { refId: 'A', query: '', metrics: [], bucketAggs: [], filters: [] } as any; |
| 266 | + |
| 267 | + const updatedQuery = addFilterToQuery( |
| 268 | + { service_name: 'text' }, |
| 269 | + query, |
| 270 | + 'service_name', |
| 271 | + 'auth-api' |
| 272 | + ); |
| 273 | + |
| 274 | + expect(updatedQuery.filters?.[0].filter).toEqual({ |
| 275 | + key: 'service_name', |
| 276 | + operator: '=', |
| 277 | + value: 'auth-api', |
| 278 | + }); |
| 279 | + }); |
| 280 | + |
| 281 | + it('renders punctuated text filters with quoted Quickwit syntax', () => { |
| 282 | + const result = renderAdHocFilters( |
| 283 | + { service_name: 'text' }, |
| 284 | + [{ |
| 285 | + key: 'service_name', |
| 286 | + operator: '=', |
| 287 | + value: 'auth-api', |
| 288 | + }] |
| 289 | + ); |
| 290 | + |
| 291 | + expect(result).toBe('service_name:"auth-api"'); |
| 292 | + }); |
| 293 | + |
| 294 | + it('escapes special characters in unquoted term filters', () => { |
| 295 | + const result = addAddHocFilter('', { |
| 296 | + key: 'attributes.grpc_message', |
| 297 | + operator: 'term', |
| 298 | + value: 'error:foo', |
| 299 | + }); |
| 300 | + |
| 301 | + expect(result).toBe('attributes.grpc_message:error\\:foo'); |
| 302 | + }); |
| 303 | + |
| 304 | + it('applies prior filters when loading tag values', async () => { |
| 305 | + const getTerms = jest.fn(() => from([[]])); |
| 306 | + |
| 307 | + await (BaseQuickwitDataSource.prototype as any).getTagValues.call( |
| 308 | + { |
| 309 | + fieldTypes: {}, |
| 310 | + addAdHocFilters: BaseQuickwitDataSource.prototype.addAdHocFilters, |
| 311 | + getTerms, |
| 312 | + }, |
| 313 | + { |
| 314 | + key: 'status', |
| 315 | + filters: [{ key: 'service', operator: '=', value: 'frontend' }], |
| 316 | + } |
| 317 | + ); |
| 318 | + |
| 319 | + expect(getTerms).toHaveBeenCalledWith( |
| 320 | + { field: 'status', query: 'service:"frontend"' }, |
| 321 | + undefined |
| 322 | + ); |
| 323 | + }); |
| 324 | + }); |
174 | 325 | }); |
0 commit comments