Skip to content

Commit

Permalink
applying review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Oct 21, 2020
1 parent fa043ab commit 8ed73fa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions src/plugins/data/common/search/tabify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@
* under the License.
*/

import { SearchResponse } from 'elasticsearch';
import { SearchSource } from '../search_source';
import { tabifyAggResponse } from './tabify';
import { tabifyDocs } from './tabify_docs';
import { tabifyDocs, TabifyDocsOptions } from './tabify_docs';
import { TabbedResponseWriterOptions } from './types';

export const tabify = (searchSource: SearchSource, esResponse: any, opts: any) => {
export const tabify = (
searchSource: SearchSource,
esResponse: SearchResponse<unknown>,
opts: Partial<TabbedResponseWriterOptions> | TabifyDocsOptions
) => {
return !esResponse.aggregations
? tabifyDocs(esResponse, searchSource.getField('index'), opts)
: tabifyAggResponse(searchSource.getField('aggs'), esResponse, opts);
? tabifyDocs(esResponse, searchSource.getField('index'), opts as TabifyDocsOptions)
: tabifyAggResponse(
searchSource.getField('aggs'),
esResponse,
opts as Partial<TabbedResponseWriterOptions>
);
};

export { tabifyAggResponse } from './tabify';
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/data/common/search/tabify/tabify_docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ describe('tabifyDocs', () => {

it('converts fields by default', () => {
const table = tabifyDocs(response, index);
expect(table).toMatchSnapshot('converts fields by default');
expect(table).toMatchSnapshot();
});

it('converts source if option is set', () => {
const table = tabifyDocs(response, index, { source: true });
expect(table).toMatchSnapshot('converts source if option is set');
expect(table).toMatchSnapshot();
});

it('skips nested fields if option is set', () => {
const table = tabifyDocs(response, index, { shallow: true });
expect(table).toMatchSnapshot('skips nested fields if option is set');
expect(table).toMatchSnapshot();
});

it('works without provided index pattern', () => {
const table = tabifyDocs(response);
expect(table).toMatchSnapshot('works without provided index pattern');
expect(table).toMatchSnapshot();
});
});
17 changes: 8 additions & 9 deletions src/plugins/data/common/search/tabify/tabify_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/

import { SearchResponse } from 'elasticsearch';
import { first, each, isPlainObject } from 'lodash';
import { isPlainObject } from 'lodash';
import { IndexPattern } from '../../index_patterns/index_patterns';
import { Datatable, DatatableColumn } from '../../../../expressions/common';
import { Datatable, DatatableColumn, DatatableColumnType } from '../../../../expressions/common';

export function flattenHit(
hit: Record<string, any>,
Expand All @@ -37,12 +37,11 @@ export function flattenHit(

if (!shallow) {
const isNestedField = field?.type === 'nested';
const isArrayOfObjects = Array.isArray(val) && isPlainObject(first(val));
if (isArrayOfObjects && !isNestedField) {
each(val as object, (v) => flatten(v, key + '.'));
if (Array.isArray(val) && !isNestedField) {
val.forEach((v) => isPlainObject(v) && flatten(v, key + '.'));
continue;
}
} else if (flat[key] !== void 0) {
} else if (flat[key] !== undefined) {
continue;
}

Expand Down Expand Up @@ -86,9 +85,9 @@ export const tabifyDocs = (
const flat = flattenHit(toConvert, index, params.shallow);
for (const [key, value] of Object.entries(flat)) {
const field = index?.fields.getByName(key);
if (!columns.find((c) => c.id === (field?.name || key))) {
const fieldName = field?.name || key;
const fieldType = (field?.type as any) || typeof value;
const fieldName = field?.name || key;
if (!columns.find((c) => c.id === fieldName)) {
const fieldType = (field?.type as DatatableColumnType) || typeof value;
const formatter = field && index?.getFormatterForField(field);
columns.push({
id: fieldName,
Expand Down

0 comments on commit 8ed73fa

Please sign in to comment.