Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moving tabify to data.search #57453

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/fixtures/stubbed_logstash_index_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

import StubIndexPattern from 'test_utils/stub_index_pattern';
import stubbedLogstashFields from 'fixtures/logstash_fields';
import StubIndexPattern from '../test_utils/public/stub_index_pattern';
import stubbedLogstashFields from './logstash_fields';

import { getKbnFieldType } from '../plugins/data/common';
import { npSetup } from '../legacy/ui/public/new_platform/new_platform.karma_mock';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,26 @@
*/

import expect from '@kbn/expect';
import ngMock from 'ng_mock';
import { tabifyGetColumns } from '../_get_columns';
import { Vis } from '../../../../../core_plugins/visualizations/public';
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern';
import stubbedLogstashIndexPatternService from '../../../../../../fixtures/stubbed_logstash_index_pattern';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind dropping the __tests__ folder, if we're at it?

import { AggConfigs } from '../../../public/search/aggs';
describe('get columns', function() {
let indexPattern;

beforeEach(ngMock.module('kibana'));
beforeEach(
ngMock.inject(function(Private) {
indexPattern = Private(FixturesStubbedLogstashIndexPatternProvider);
})
);
const indexPattern = stubbedLogstashIndexPatternService();

it('should inject a count metric if no aggs exist', function() {
const vis = new Vis(indexPattern, {
const aggConfigs = new AggConfigs(indexPattern, {
type: 'pie',
});
while (vis.aggs.length) vis.aggs.pop();
const columns = tabifyGetColumns(
vis.getAggConfig().getResponseAggs(),
null,
vis.isHierarchical()
);
while (aggConfigs.length) aggConfigs.pop();
const columns = tabifyGetColumns(aggConfigs.getRequestAggs(), null, false);

expect(columns).to.have.length(1);
expect(columns[0]).to.have.property('aggConfig');
expect(columns[0].aggConfig.type).to.have.property('name', 'count');
});

it('should inject a count metric if only buckets exist', function() {
const vis = new Vis(indexPattern, {
const aggConfigs = new AggConfigs(indexPattern, {
type: 'pie',
aggs: [
{
Expand All @@ -60,15 +48,15 @@ describe('get columns', function() {
],
});

const columns = tabifyGetColumns(vis.getAggConfig().getResponseAggs(), !vis.isHierarchical());
const columns = tabifyGetColumns(aggConfigs.getResponseAggs(), true);

expect(columns).to.have.length(2);
expect(columns[1]).to.have.property('aggConfig');
expect(columns[1].aggConfig.type).to.have.property('name', 'count');
});

it('should inject the metric after each bucket if the vis is hierarchical', function() {
const vis = new Vis(indexPattern, {
const aggConfigs = new AggConfigs(indexPattern, {
type: 'pie',
aggs: [
{
Expand All @@ -94,7 +82,7 @@ describe('get columns', function() {
],
});

const columns = tabifyGetColumns(vis.getAggConfig().getResponseAggs(), !vis.isHierarchical());
const columns = tabifyGetColumns(aggConfigs.getResponseAggs(), true);

expect(columns).to.have.length(8);
columns.forEach(function(column, i) {
Expand All @@ -104,7 +92,7 @@ describe('get columns', function() {
});

it('should inject the multiple metrics after each bucket if the vis is hierarchical', function() {
const vis = new Vis(indexPattern, {
const aggConfigs = new AggConfigs(indexPattern, {
type: 'pie',
aggs: [
{
Expand Down Expand Up @@ -132,7 +120,7 @@ describe('get columns', function() {
],
});

const columns = tabifyGetColumns(vis.getAggConfig().getResponseAggs(), !vis.isHierarchical());
const columns = tabifyGetColumns(aggConfigs.getResponseAggs(), true);

function checkColumns(column, i) {
expect(column).to.have.property('aggConfig');
Expand All @@ -156,7 +144,7 @@ describe('get columns', function() {
});

it('should put all metrics at the end of the columns if the vis is not hierarchical', function() {
const vis = new Vis(indexPattern, {
const aggConfigs = new AggConfigs(indexPattern, {
type: 'histogram',
aggs: [
{
Expand Down Expand Up @@ -184,7 +172,7 @@ describe('get columns', function() {
],
});

const columns = tabifyGetColumns(vis.getAggConfig().getResponseAggs(), !vis.isHierarchical());
const columns = tabifyGetColumns(aggConfigs.getResponseAggs(), true);
expect(columns).to.have.length(6);

// sum should be last
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,30 @@
*/

import _ from 'lodash';
import fixtures from 'fixtures/fake_hierarchical_data';
import fixtures from '../../../../../../fixtures/fake_hierarchical_data';
import expect from '@kbn/expect';
import ngMock from 'ng_mock';
import { tabifyAggResponse } from '../tabify';
import { Vis } from '../../../../../core_plugins/visualizations/public';
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern';
import stubbedLogstashIndexPatternService from '../../../../../../fixtures/stubbed_logstash_index_pattern';
import { AggConfigs } from '../../../public/search/aggs';

describe('tabifyAggResponse Integration', function() {
let indexPattern;
const indexPattern = stubbedLogstashIndexPatternService();

beforeEach(ngMock.module('kibana'));
beforeEach(
ngMock.inject(function(Private) {
indexPattern = Private(FixturesStubbedLogstashIndexPatternProvider);
})
);

function normalizeIds(vis) {
vis.aggs.aggs.forEach(function(agg, i) {
function normalizeIds(aggConfigs) {
aggConfigs.aggs.forEach(function(agg, i) {
agg.id = 'agg_' + (i + 1);
});
}

it('transforms a simple response properly', function() {
const vis = new Vis(indexPattern, {
const aggConfigs = new AggConfigs(indexPattern, {
type: 'histogram',
aggs: [],
});
normalizeIds(vis);
normalizeIds(aggConfigs);

const resp = tabifyAggResponse(vis.getAggConfig(), fixtures.metricOnly, {
metricsAtAllLevels: vis.isHierarchical(),
const resp = tabifyAggResponse(aggConfigs, fixtures.metricOnly, {
metricsAtAllLevels: false,
});

expect(resp)
Expand All @@ -59,21 +51,21 @@ describe('tabifyAggResponse Integration', function() {
expect(resp.columns).to.have.length(1);

expect(resp.rows[0]).to.eql({ 'col-0-agg_1': 1000 });
expect(resp.columns[0]).to.have.property('aggConfig', vis.aggs[0]);
expect(resp.columns[0]).to.have.property('aggConfig', aggConfigs[0]);
});

describe('transforms a complex response', function() {
this.slow(1000);

let vis;
let aggConfigs;
let avg;
let ext;
let src;
let os;
let esResp;

beforeEach(function() {
vis = new Vis(indexPattern, {
aggConfigs = new AggConfigs(indexPattern, {
type: 'pie',
aggs: [
{ type: 'avg', schema: 'metric', params: { field: 'bytes' } },
Expand All @@ -82,12 +74,12 @@ describe('tabifyAggResponse Integration', function() {
{ type: 'terms', schema: 'segment', params: { field: 'machine.os' } },
],
});
normalizeIds(vis);
normalizeIds(aggConfigs);

avg = vis.aggs[0];
ext = vis.aggs[1];
src = vis.aggs[2];
os = vis.aggs[3];
avg = aggConfigs[0];
ext = aggConfigs[1];
src = aggConfigs[2];
os = aggConfigs[3];

esResp = _.cloneDeep(fixtures.threeTermBuckets);
// remove the buckets for css in MX
Expand Down Expand Up @@ -140,7 +132,7 @@ describe('tabifyAggResponse Integration', function() {
// the default for a non-hierarchical vis is to display
// only complete rows, and only put the metrics at the end.

const tabbed = tabifyAggResponse(vis.getAggConfig(), esResp, { metricsAtAllLevels: false });
const tabbed = tabifyAggResponse(aggConfigs, esResp, { metricsAtAllLevels: false });

expectColumns(tabbed, [ext, src, os, avg]);

Expand All @@ -155,8 +147,7 @@ describe('tabifyAggResponse Integration', function() {
// minimalColumns we should expect the partial row to be completely after
// the existing bucket and it's metric

vis.isHierarchical = _.constant(true);
const tabbed = tabifyAggResponse(vis.getAggConfig(), esResp, { metricsAtAllLevels: true });
const tabbed = tabifyAggResponse(aggConfigs, esResp, { metricsAtAllLevels: true });

expectColumns(tabbed, [ext, avg, src, avg, os, avg]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,12 @@
*/

import expect from '@kbn/expect';
import ngMock from 'ng_mock';
import { TabbedAggResponseWriter } from '../_response_writer';
import { Vis } from '../../../../../core_plugins/visualizations/public';
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern';
import stubbedLogstashIndexPatternService from '../../../../../../fixtures/stubbed_logstash_index_pattern';
import { AggConfigs } from '../../../public/search/aggs';

describe('TabbedAggResponseWriter class', function() {
let Private;
let indexPattern;

beforeEach(ngMock.module('kibana'));
beforeEach(
ngMock.inject(function($injector) {
Private = $injector.get('Private');

indexPattern = Private(FixturesStubbedLogstashIndexPatternProvider);
})
);
const indexPattern = stubbedLogstashIndexPatternService();

const splitAggConfig = [
{
Expand All @@ -61,8 +50,8 @@ describe('TabbedAggResponseWriter class', function() {
];

const createResponseWritter = (aggs = [], opts = {}) => {
const vis = new Vis(indexPattern, { type: 'histogram', aggs: aggs });
return new TabbedAggResponseWriter(vis.getAggConfig(), opts);
const aggConfigs = new AggConfigs(indexPattern, { type: 'histogram', aggs: aggs });
return new TabbedAggResponseWriter(aggConfigs, opts);
};

describe('Constructor', function() {
Expand All @@ -88,13 +77,13 @@ describe('TabbedAggResponseWriter class', function() {

describe('sets timeRange', function() {
it("to the first nested object's range", function() {
const vis = new Vis(indexPattern, { type: 'histogram', aggs: [] });
const aggConfigs = new AggConfigs(indexPattern, { type: 'histogram', aggs: [] });
const range = {
gte: 0,
lte: 100,
};

const writer = new TabbedAggResponseWriter(vis.getAggConfig(), {
const writer = new TabbedAggResponseWriter(aggConfigs, {
timeRange: {
'@timestamp': range,
},
Expand All @@ -106,9 +95,9 @@ describe('TabbedAggResponseWriter class', function() {
});

it('to undefined if no nested object', function() {
const vis = new Vis(indexPattern, { type: 'histogram', aggs: [] });
const aggConfigs = new AggConfigs(indexPattern, { type: 'histogram', aggs: [] });

const writer = new TabbedAggResponseWriter(vis.getAggConfig(), {
const writer = new TabbedAggResponseWriter(aggConfigs, {
timeRange: {},
});
expect(writer).to.have.property('timeRange', undefined);
Expand Down
72 changes: 72 additions & 0 deletions src/legacy/core_plugins/data/common/tabify/_get_columns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { groupBy } from 'lodash';
import { IAggConfig } from '../../public/search/aggs/agg_config';

export interface AggColumn {
aggConfig: IAggConfig;
id: string;
name: string;
}

const getColumn = (agg: IAggConfig, i: number): AggColumn => {
return {
aggConfig: agg,
id: `col-${i}-${agg.id}`,
name: agg.makeLabel(),
};
};

/**
* Builds tabify columns.
*
* @param {AggConfigs} aggs - the agg configs object to which the aggregation response correlates
* @param {boolean} minimalColumns - setting to true will only return a column for the last bucket/metric instead of one for each level
*/
export function tabifyGetColumns(aggs: IAggConfig[], minimalColumns: boolean) {
// pick the columns
if (minimalColumns) {
return aggs.map((agg, i) => getColumn(agg, i));
}

// supposed to be bucket,...metrics,bucket,...metrics
const columns: AggColumn[] = [];

// separate the metrics
const grouped = groupBy(aggs, agg => {
return agg.type.type;
});

if (!grouped.buckets) {
// return just the metrics, in column format
return grouped.metrics.map((agg, i) => getColumn(agg, i));
}

let columnIndex = 0;
// return the buckets, and after each place all of the metrics
grouped.buckets.forEach(agg => {
columns.push(getColumn(agg, columnIndex++));
grouped.metrics.forEach(metric => {
columns.push(getColumn(metric, columnIndex++));
});
});

return columns;
}
22 changes: 22 additions & 0 deletions src/legacy/core_plugins/data/common/tabify/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// @ts-ignore
export { tabifyAggResponse } from './tabify';
export { tabifyGetColumns } from './_get_columns';
Loading