Skip to content

Commit

Permalink
handle column_names being undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Sep 17, 2020
1 parent 12efff0 commit 10e1a17
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/ml/common/types/file_datavisualizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface FindFileStructureResponse {
delimiter: string;
need_client_timezone: boolean;
num_lines_analyzed: number;
column_names: string[];
column_names?: string[];
explanation?: string[];
grok_pattern?: string;
multiline_start_pattern?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { Component } from 'react';
import React from 'react';
import { EuiText } from '@elastic/eui';

import { CombinedField } from './types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class CombinedFieldsForm extends Component<Props, State> {
}

hasNameCollision = (name: string) => {
if (this.props.results.column_names.includes(name)) {
if (this.props.results.column_names?.includes(name)) {
// collision with column name
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function CombinedFieldsReadOnlyForm({
defaultMessage: 'Combined fields',
})}
helpText={i18n.translate('xpack.ml.fileDatavisualizer.combinedFieldsReadOnlyHelpTextLabel', {
defaultMessage: 'Edit combined fields advanced tab',
defaultMessage: 'Edit combined fields in advanced tab',
})}
>
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
createGeoPointCombinedField,
isWithinLatRange,
isWithinLonRange,
getFieldNames,
getNameCollisionMsg,
} from './utils';
import { FindFileStructureResponse } from '../../../../../../common/types/file_datavisualizer';
Expand All @@ -51,7 +52,7 @@ export class GeoPointForm extends Component<Props, State> {

const latFields: EuiSelectOption[] = [{ value: '', text: '' }];
const lonFields: EuiSelectOption[] = [{ value: '', text: '' }];
props.results.column_names.forEach((columnName: string) => {
getFieldNames(props.results).forEach((columnName: string) => {
if (isWithinLatRange(columnName, props.results.field_stats)) {
latFields.push({ value: columnName, text: columnName });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,23 @@ export function getNameCollisionMsg(name: string) {
});
}

export function getFieldNames(results: FindFileStructureResponse): string[] {
return results.column_names !== undefined
? results.column_names
: Object.keys(results.field_stats);
}

function getGeoPointField(results: FindFileStructureResponse) {
const latField = results.column_names.find((columnName) => {
const fieldNames = getFieldNames(results);

const latField = fieldNames.find((columnName) => {
return (
COMMON_LAT_NAMES.includes(columnName.toLowerCase()) &&
isWithinLatRange(columnName, results.field_stats)
);
});

const lonField = results.column_names.find((columnName) => {
const lonField = fieldNames.find((columnName) => {
return (
COMMON_LON_NAMES.includes(columnName.toLowerCase()) &&
isWithinLonRange(columnName, results.field_stats)
Expand All @@ -159,7 +167,7 @@ function getGeoPointField(results: FindFileStructureResponse) {
];
// Use first combinedFieldNames that does not have a naming collision
const geoPointField = combinedFieldNames.find((name) => {
return !results.column_names.includes(name);
return !fieldNames.includes(name);
});

return geoPointField ? createGeoPointCombinedField(latField, lonField, geoPointField) : null;
Expand Down

0 comments on commit 10e1a17

Please sign in to comment.