|
8 | 8 | import React from 'react';
|
9 | 9 | import { shallow } from 'enzyme';
|
10 | 10 | import { createDatatableUtilitiesMock } from '@kbn/data-plugin/common/mocks';
|
11 |
| -import { getPrecisionErrorWarningMessages, cloneLayer } from './utils'; |
| 11 | +import { |
| 12 | + getPrecisionErrorWarningMessages, |
| 13 | + cloneLayer, |
| 14 | + getUnsupportedOperationsWarningMessage, |
| 15 | +} from './utils'; |
12 | 16 | import type { FormBasedPrivateState, GenericIndexPatternColumn } from './types';
|
13 |
| -import type { FramePublicAPI } from '../../types'; |
| 17 | +import type { FramePublicAPI, IndexPattern } from '../../types'; |
14 | 18 | import type { DocLinksStart } from '@kbn/core/public';
|
15 | 19 | import { EuiLink } from '@elastic/eui';
|
16 | 20 | import { TermsIndexPatternColumn } from './operations';
|
17 | 21 | import { mountWithIntl } from '@kbn/test-jest-helpers';
|
18 | 22 | import { FormattedMessage } from '@kbn/i18n-react';
|
19 | 23 | import { FormBasedLayer } from './types';
|
| 24 | +import { createMockedIndexPatternWithAdditionalFields } from './mocks'; |
20 | 25 |
|
21 | 26 | describe('indexpattern_datasource utils', () => {
|
22 | 27 | describe('getPrecisionErrorWarningMessages', () => {
|
@@ -240,4 +245,195 @@ describe('indexpattern_datasource utils', () => {
|
240 | 245 | ).toMatchSnapshot();
|
241 | 246 | });
|
242 | 247 | });
|
| 248 | + |
| 249 | + describe('getUnsupportedOperationsWarningMessage', () => { |
| 250 | + let docLinks: DocLinksStart; |
| 251 | + const affectedOperations = [ |
| 252 | + 'sum', |
| 253 | + 'average', |
| 254 | + 'percentile', |
| 255 | + 'percentile_rank', |
| 256 | + 'count', |
| 257 | + 'unique_count', |
| 258 | + 'standard_deviation', |
| 259 | + ]; |
| 260 | + |
| 261 | + function createColumnsForField(field: string, colOffset: number = 0) { |
| 262 | + return Object.fromEntries( |
| 263 | + affectedOperations.map((operationType, i) => [ |
| 264 | + `col_${i + colOffset}`, |
| 265 | + { operationType, sourceField: field, label: `${operationType} of ${field}` }, |
| 266 | + ]) |
| 267 | + ); |
| 268 | + } |
| 269 | + |
| 270 | + function createState(fields: string[]) { |
| 271 | + return { |
| 272 | + layers: { |
| 273 | + id: { |
| 274 | + indexPatternId: '0', |
| 275 | + columns: Object.assign( |
| 276 | + {}, |
| 277 | + ...fields.map((field, i) => |
| 278 | + createColumnsForField(field, i * affectedOperations.length) |
| 279 | + ) |
| 280 | + ), |
| 281 | + }, |
| 282 | + }, |
| 283 | + } as unknown as FormBasedPrivateState; |
| 284 | + } |
| 285 | + |
| 286 | + function createFramePublic(indexPattern: IndexPattern): FramePublicAPI { |
| 287 | + return { |
| 288 | + dataViews: { |
| 289 | + indexPatterns: Object.fromEntries([indexPattern].map((dataView, i) => [i, dataView])), |
| 290 | + }, |
| 291 | + } as unknown as FramePublicAPI; |
| 292 | + } |
| 293 | + |
| 294 | + function createFormulaColumns(formulaParts: string[], field: string, colOffset: number = 0) { |
| 295 | + const fullFormula = formulaParts.map((part) => `${part}(${field})`).join(' + '); |
| 296 | + // just assume it's a sum of all the parts for testing |
| 297 | + const rootId = `col-formula${colOffset}`; |
| 298 | + return Object.fromEntries([ |
| 299 | + [ |
| 300 | + rootId, |
| 301 | + { |
| 302 | + operationType: 'formula', |
| 303 | + label: `Formula: ${fullFormula}`, |
| 304 | + params: { formula: fullFormula }, |
| 305 | + }, |
| 306 | + ], |
| 307 | + ...formulaParts.map((part, i) => [ |
| 308 | + `${rootId}X${i}`, |
| 309 | + { operationType: part, sourceField: field, label: 'Part of formula' }, |
| 310 | + ]), |
| 311 | + [ |
| 312 | + `${rootId}X${formulaParts.length}`, |
| 313 | + { operationType: 'math', references: formulaParts.map((_, i) => `${rootId}X${i}`) }, |
| 314 | + ], |
| 315 | + ]); |
| 316 | + } |
| 317 | + |
| 318 | + beforeEach(() => { |
| 319 | + docLinks = { |
| 320 | + links: { |
| 321 | + fleet: { |
| 322 | + datastreamsTSDSMetrics: 'http://tsdb_metric_doc', |
| 323 | + }, |
| 324 | + }, |
| 325 | + } as DocLinksStart; |
| 326 | + }); |
| 327 | + |
| 328 | + it.each([['bytes'], ['bytes_gauge']])( |
| 329 | + 'should return no warning for non-counter fields: %s', |
| 330 | + (fieldName: string) => { |
| 331 | + const warnings = getUnsupportedOperationsWarningMessage( |
| 332 | + createState([fieldName]), |
| 333 | + createFramePublic( |
| 334 | + createMockedIndexPatternWithAdditionalFields([ |
| 335 | + { |
| 336 | + name: 'bytes_gauge', |
| 337 | + displayName: 'bytes_gauge', |
| 338 | + type: 'number', |
| 339 | + aggregatable: true, |
| 340 | + searchable: true, |
| 341 | + timeSeriesMetric: 'gauge', |
| 342 | + }, |
| 343 | + ]) |
| 344 | + ), |
| 345 | + docLinks |
| 346 | + ); |
| 347 | + expect(warnings).toHaveLength(0); |
| 348 | + } |
| 349 | + ); |
| 350 | + |
| 351 | + it('should return a warning for a counter field grouped by field', () => { |
| 352 | + const warnings = getUnsupportedOperationsWarningMessage( |
| 353 | + createState(['bytes_counter']), |
| 354 | + createFramePublic( |
| 355 | + createMockedIndexPatternWithAdditionalFields([ |
| 356 | + { |
| 357 | + name: 'bytes_counter', |
| 358 | + displayName: 'bytes_counter', |
| 359 | + type: 'number', |
| 360 | + aggregatable: true, |
| 361 | + searchable: true, |
| 362 | + timeSeriesMetric: 'counter', |
| 363 | + }, |
| 364 | + ]) |
| 365 | + ), |
| 366 | + docLinks |
| 367 | + ); |
| 368 | + expect(warnings).toHaveLength(1); |
| 369 | + }); |
| 370 | + |
| 371 | + it('should group multiple warnings by field', () => { |
| 372 | + const warnings = getUnsupportedOperationsWarningMessage( |
| 373 | + createState(['bytes_counter', 'bytes_counter2']), |
| 374 | + createFramePublic( |
| 375 | + createMockedIndexPatternWithAdditionalFields([ |
| 376 | + { |
| 377 | + name: 'bytes_counter', |
| 378 | + displayName: 'bytes_counter', |
| 379 | + type: 'number', |
| 380 | + aggregatable: true, |
| 381 | + searchable: true, |
| 382 | + timeSeriesMetric: 'counter', |
| 383 | + }, |
| 384 | + { |
| 385 | + name: 'bytes_counter2', |
| 386 | + displayName: 'bytes_counter2', |
| 387 | + type: 'number', |
| 388 | + aggregatable: true, |
| 389 | + searchable: true, |
| 390 | + timeSeriesMetric: 'counter', |
| 391 | + }, |
| 392 | + ]) |
| 393 | + ), |
| 394 | + docLinks |
| 395 | + ); |
| 396 | + expect(warnings).toHaveLength(2); |
| 397 | + }); |
| 398 | + |
| 399 | + it('should handle formula reporting only the top visible dimension', () => { |
| 400 | + const warnings = getUnsupportedOperationsWarningMessage( |
| 401 | + { |
| 402 | + layers: { |
| 403 | + id: { |
| 404 | + indexPatternId: '0', |
| 405 | + columns: Object.assign( |
| 406 | + {}, |
| 407 | + ...['bytes_counter', 'bytes_counter2'].map((field, i) => |
| 408 | + createFormulaColumns(affectedOperations, field, i * affectedOperations.length) |
| 409 | + ) |
| 410 | + ), |
| 411 | + }, |
| 412 | + }, |
| 413 | + } as unknown as FormBasedPrivateState, |
| 414 | + createFramePublic( |
| 415 | + createMockedIndexPatternWithAdditionalFields([ |
| 416 | + { |
| 417 | + name: 'bytes_counter', |
| 418 | + displayName: 'bytes_counter', |
| 419 | + type: 'number', |
| 420 | + aggregatable: true, |
| 421 | + searchable: true, |
| 422 | + timeSeriesMetric: 'counter', |
| 423 | + }, |
| 424 | + { |
| 425 | + name: 'bytes_counter2', |
| 426 | + displayName: 'bytes_counter2', |
| 427 | + type: 'number', |
| 428 | + aggregatable: true, |
| 429 | + searchable: true, |
| 430 | + timeSeriesMetric: 'counter', |
| 431 | + }, |
| 432 | + ]) |
| 433 | + ), |
| 434 | + docLinks |
| 435 | + ); |
| 436 | + expect(warnings).toHaveLength(2); |
| 437 | + }); |
| 438 | + }); |
243 | 439 | });
|
0 commit comments