Skip to content

Commit

Permalink
[ui/agg_response/tabify] -> TypeScript & Jest & Shim (#57660) (#58198)
Browse files Browse the repository at this point in the history
* [ui/agg_response/tabify] -> TypeScript & Jest & Shim

[ui/agg_response/tabify] -> TypeScript & Jest & Shim

Part of #57394

* fix CI

* move tabify into new folder

* TypeScript _bucket.js

* rename _buckets -> bucket

* fix CI

* tabify.test.js -> tabify.test.ts

* tabify.js -> tabify.ts

* fix JEST

* Update src/legacy/core_plugins/data/public/search/tabify/types.ts

Co-Authored-By: Luke Elmers <[email protected]>

* fake_hierarchical_data.js -> fake_hierarchical_data.ts

* TimeRange -> TabbedRangeFilterParams

Co-authored-by: Elastic Machine <[email protected]>
Co-authored-by: Luke Elmers <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
Co-authored-by: Luke Elmers <[email protected]>
  • Loading branch information
3 people authored Feb 21, 2020
1 parent 325a4e3 commit 420e4d0
Show file tree
Hide file tree
Showing 32 changed files with 1,053 additions and 1,005 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
* under the License.
*/

const data = {};

data.metricOnly = {
export const metricOnly = {
hits: { total: 1000, hits: [], max_score: 0 },
aggregations: {
agg_1: { value: 412032 },
},
};

data.threeTermBuckets = {
export const threeTermBuckets = {
hits: { total: 1000, hits: [], max_score: 0 },
aggregations: {
agg_2: {
Expand Down Expand Up @@ -129,7 +127,7 @@ data.threeTermBuckets = {
},
};

data.oneTermOneHistogramBucketWithTwoMetricsOneTopHitOneDerivative = {
export const oneTermOneHistogramBucketWithTwoMetricsOneTopHitOneDerivative = {
hits: { total: 1000, hits: [], max_score: 0 },
aggregations: {
agg_3: {
Expand Down Expand Up @@ -520,7 +518,7 @@ data.oneTermOneHistogramBucketWithTwoMetricsOneTopHitOneDerivative = {
},
};

data.oneRangeBucket = {
export const oneRangeBucket = {
took: 35,
timed_out: false,
_shards: {
Expand Down Expand Up @@ -555,7 +553,7 @@ data.oneRangeBucket = {
},
};

data.oneFilterBucket = {
export const oneFilterBucket = {
took: 11,
timed_out: false,
_shards: {
Expand All @@ -582,7 +580,7 @@ data.oneFilterBucket = {
},
};

data.oneHistogramBucket = {
export const oneHistogramBucket = {
took: 37,
timed_out: false,
_shards: {
Expand Down Expand Up @@ -632,5 +630,3 @@ data.oneHistogramBucket = {
},
},
};

export default data;
2 changes: 2 additions & 0 deletions src/legacy/core_plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ export {
// search_source
getRequestInspectorStats,
getResponseInspectorStats,
tabifyAggResponse,
tabifyGetColumns,
} from './search';
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/

import _ from 'lodash';
import { AggConfig, AggConfigOptions } from './agg_config';
import { AggConfig, AggConfigOptions, IAggConfig } from './agg_config';
import { Schema } from './schemas';
import { AggGroupNames } from './agg_groups';
import {
Expand Down Expand Up @@ -63,7 +63,7 @@ export class AggConfigs {
public schemas: any;
public timeRange?: TimeRange;

aggs: AggConfig[];
aggs: IAggConfig[];

constructor(indexPattern: IndexPattern, configStates = [] as any, schemas?: any) {
configStates = AggConfig.ensureIds(configStates);
Expand All @@ -74,7 +74,7 @@ export class AggConfigs {

configStates.forEach((params: any) => this.createAggConfig(params));

if (this.schemas) {
if (schemas) {
this.initializeDefaultsFromSchemas(schemas);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export const writeParams = <
aggs?: IAggConfigs,
locals?: Record<string, any>
) => {
const output = { params: {} as Record<string, any> };
const output: Record<string, any> = {
params: {} as Record<string, any>,
};
locals = locals || {};

params.forEach(param => {
Expand Down
3 changes: 3 additions & 0 deletions src/legacy/core_plugins/data/public/search/aggs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ export { isValidJson, isValidInterval } from './utils';
export { BUCKET_TYPES } from './buckets/bucket_agg_types';
export { METRIC_TYPES } from './metrics/metric_agg_types';
export { ISchemas, Schema, Schemas } from './schemas';

// types
export { IAggConfig, IAggConfigs } from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ import {

import { buildTabularInspectorData } from './build_tabular_inspector_data';
import { calculateObjectHash } from '../../../../visualizations/public';
// @ts-ignore
import { tabifyAggResponse } from '../../../../../ui/public/agg_response/tabify/tabify';
import { tabifyAggResponse } from '../../../../../core_plugins/data/public';
import { PersistedState } from '../../../../../ui/public/persisted_state';
import { Adapters } from '../../../../../../plugins/inspector/public';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
Expand Down
1 change: 1 addition & 0 deletions src/legacy/core_plugins/data/public/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
export * from './aggs';
export { getRequestInspectorStats, getResponseInspectorStats } from './utils';
export { serializeAggConfig } from './expressions/utils';
export { tabifyAggResponse, tabifyGetColumns } from './tabify';
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,36 @@
* under the License.
*/

import expect from '@kbn/expect';
import { TabifyBuckets } from '../_buckets';
import { TabifyBuckets } from './buckets';
import { AggGroupNames } from '../aggs';

describe('Buckets wrapper', function() {
function test(aggResp, count, keys) {
it('reads the length', function() {
jest.mock('ui/new_platform');

describe('Buckets wrapper', () => {
const check = (aggResp: any, count: number, keys: string[]) => {
test('reads the length', () => {
const buckets = new TabifyBuckets(aggResp);
expect(buckets).to.have.length(count);
expect(buckets).toHaveLength(count);
});

it('iterates properly, passing in the key', function() {
test('iterates properly, passing in the key', () => {
const buckets = new TabifyBuckets(aggResp);
const keysSent = [];
buckets.forEach(function(bucket, key) {
keysSent.push(key);
const keysSent: any[] = [];

buckets.forEach((bucket, key) => {
if (key) {
keysSent.push(key);
}
});

expect(keysSent).to.have.length(count);
expect(keysSent).to.eql(keys);
expect(keysSent).toHaveLength(count);
expect(keysSent).toEqual(keys);
});
}
};

describe('with object style buckets', function() {
const aggResp = {
buckets: {
describe('with object style buckets', () => {
let aggResp: any = {
[AggGroupNames.Buckets]: {
'0-100': {},
'100-200': {},
'200-300': {},
Expand All @@ -51,11 +56,11 @@ describe('Buckets wrapper', function() {
const count = 3;
const keys = ['0-100', '100-200', '200-300'];

test(aggResp, count, keys);
check(aggResp, count, keys);

it('should accept filters agg queries with strings', () => {
const aggResp = {
buckets: {
test('should accept filters agg queries with strings', () => {
aggResp = {
[AggGroupNames.Buckets]: {
'response:200': {},
'response:404': {},
},
Expand All @@ -75,15 +80,17 @@ describe('Buckets wrapper', function() {
};

const buckets = new TabifyBuckets(aggResp, aggParams);
expect(buckets).to.have.length(2);

expect(buckets).toHaveLength(2);

buckets._keys.forEach(key => {
expect(key).to.be.a('string');
expect(typeof key).toBe('string');
});
});

it('should accept filters agg queries with query_string queries', () => {
const aggResp = {
buckets: {
test('should accept filters agg queries with query_string queries', () => {
aggResp = {
[AggGroupNames.Buckets]: {
'response:200': {},
'response:404': {},
},
Expand All @@ -103,15 +110,17 @@ describe('Buckets wrapper', function() {
};

const buckets = new TabifyBuckets(aggResp, aggParams);
expect(buckets).to.have.length(2);

expect(buckets).toHaveLength(2);

buckets._keys.forEach(key => {
expect(key).to.be.a('string');
expect(typeof key).toBe('string');
});
});

it('should accept filters agg queries with query dsl queries', () => {
const aggResp = {
buckets: {
test('should accept filters agg queries with query dsl queries', () => {
aggResp = {
[AggGroupNames.Buckets]: {
'{match_all: {}}': {},
},
};
Expand All @@ -126,16 +135,18 @@ describe('Buckets wrapper', function() {
};

const buckets = new TabifyBuckets(aggResp, aggParams);
expect(buckets).to.have.length(1);

expect(buckets).toHaveLength(1);

buckets._keys.forEach(key => {
expect(key).to.be.a('string');
expect(typeof key).toBe('string');
});
});
});

describe('with array style buckets', function() {
describe('with array style buckets', () => {
const aggResp = {
buckets: [
[AggGroupNames.Buckets]: [
{ key: '0-100', value: {} },
{ key: '100-200', value: {} },
{ key: '200-300', value: {} },
Expand All @@ -145,31 +156,32 @@ describe('Buckets wrapper', function() {
const count = 3;
const keys = ['0-100', '100-200', '200-300'];

test(aggResp, count, keys);
check(aggResp, count, keys);
});

describe('with single bucket aggregations (filter)', function() {
it('creates single bucket from agg content', function() {
describe('with single bucket aggregations (filter)', () => {
test('creates single bucket from agg content', () => {
const aggResp = {
single_bucket: {},
doc_count: 5,
};
const buckets = new TabifyBuckets(aggResp);
expect(buckets).to.have.length(1);

expect(buckets).toHaveLength(1);
});
});

describe('drop_partial option', function() {
describe('drop_partial option', () => {
const aggResp = {
buckets: [
[AggGroupNames.Buckets]: [
{ key: 0, value: {} },
{ key: 100, value: {} },
{ key: 200, value: {} },
{ key: 300, value: {} },
],
};

it('drops partial buckets when enabled', function() {
test('drops partial buckets when enabled', () => {
const aggParams = {
drop_partials: true,
field: {
Expand All @@ -182,10 +194,11 @@ describe('Buckets wrapper', function() {
name: 'date',
};
const buckets = new TabifyBuckets(aggResp, aggParams, timeRange);
expect(buckets).to.have.length(1);

expect(buckets).toHaveLength(1);
});

it('keeps partial buckets when disabled', function() {
test('keeps partial buckets when disabled', () => {
const aggParams = {
drop_partials: false,
field: {
Expand All @@ -198,10 +211,11 @@ describe('Buckets wrapper', function() {
name: 'date',
};
const buckets = new TabifyBuckets(aggResp, aggParams, timeRange);
expect(buckets).to.have.length(4);

expect(buckets).toHaveLength(4);
});

it('keeps aligned buckets when enabled', function() {
test('keeps aligned buckets when enabled', () => {
const aggParams = {
drop_partials: true,
field: {
Expand All @@ -214,10 +228,11 @@ describe('Buckets wrapper', function() {
name: 'date',
};
const buckets = new TabifyBuckets(aggResp, aggParams, timeRange);
expect(buckets).to.have.length(3);

expect(buckets).toHaveLength(3);
});

it('does not drop buckets for non-timerange fields', function() {
test('does not drop buckets for non-timerange fields', () => {
const aggParams = {
drop_partials: true,
field: {
Expand All @@ -230,7 +245,8 @@ describe('Buckets wrapper', function() {
name: 'date',
};
const buckets = new TabifyBuckets(aggResp, aggParams, timeRange);
expect(buckets).to.have.length(4);

expect(buckets).toHaveLength(4);
});
});
});
Loading

0 comments on commit 420e4d0

Please sign in to comment.