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

script field support, fix field formatters #33910

Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import _ from 'lodash';

// FIXME shouldn't this really use a service provided by @kbn-es-query? The logic here can part from Discover
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: The logic here can part from Discover, for some reason I have a hard time reading this. Does this mean that the logic here can be separated from Discover? IE right now it's closely coupled to it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll fix the comment, but this is meaning that the logic can part ways with Discover, even though they do much of the same thing. That becomes the cause of bugs like #25068

export function createFlattenHit(fields, metaFields, conflictedTypesFields) {
const flattenSource = (flat, obj, keyPrefix) => {
keyPrefix = keyPrefix ? keyPrefix + '.' : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ interface CsvResultFromSearch {
}

type EndpointCaller = (method: string, params: any) => Promise<any>;
type FormatsMap = Map<string, any>;
type FormatsMap = Map<
string,
{
id: string;
params: {
pattern: string;
};
}
>;

interface GenerateCsvParams {
searchRequest: SearchRequest;
Expand Down Expand Up @@ -103,6 +111,13 @@ const getTimebasedParts = (
return { combinedFilter, includes, timezone };
};

interface IndexPatternField {
scripted: boolean;
lang?: string;
script?: string;
name: string;
}

export async function generateCsvSearch(
req: Request,
server: KbnServer,
Expand Down Expand Up @@ -132,6 +147,19 @@ export async function generateCsvSearch(
const esQueryConfig = await getEsQueryConfig(uiConfig);
const [sortField, sortOrder] = savedSearchObjectAttr.sort;

const scriptFields = indexPatternSavedObject.fields
.filter((f: IndexPatternField) => f.scripted === true)
Copy link
Contributor

Choose a reason for hiding this comment

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

.filter((f: IndexPatternField) => f.scripted)

.reduce((accum: any, curr: IndexPatternField) => {
return {
...accum,
[curr.name]: {
script: {
source: curr.script,
lang: curr.lang,
},
},
};
}, {});
const searchRequest: SearchRequest = {
index: indexPatternSavedObject.title,
body: {
Expand All @@ -143,7 +171,7 @@ export async function generateCsvSearch(
combinedFilter,
esQueryConfig
),
script_fields: {},
script_fields: scriptFields,
sort: [{ [sortField]: { order: sortOrder } }],
},
};
Expand All @@ -152,9 +180,9 @@ export async function generateCsvSearch(
const callCluster = (...params: any[]) => callWithRequest(req, ...params);
const config = server.config();
const [formatsMap, uiSettings] = await Promise.all([
fieldFormatServiceFactory(uiConfig).then((fieldFormats: any) =>
fieldFormatMapFactory(indexPatternSavedObject, fieldFormats)
),
fieldFormatServiceFactory(uiConfig).then((fieldFormats: any) => {
return fieldFormatMapFactory(indexPatternSavedObject, fieldFormats);
}),
getUiSettings(uiConfig),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,16 @@ export async function getDataSource(
}
}
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice cleanup here.

const { attributes: indexPatternAttributes } = await savedObjectsClient.get(
'index-pattern',
indexPatternId
);
let fields = '[]';
let fieldFormatMap = '{}';
if (indexPatternAttributes.fields) {
fields = JSON.parse(indexPatternAttributes.fields);
}
if (indexPatternAttributes.fieldFormatMap) {
fieldFormatMap = JSON.parse(indexPatternAttributes.fieldFormatMap);
}
indexPatternSavedObject = { ...indexPatternAttributes, fields, fieldFormatMap };
const { attributes } = await savedObjectsClient.get('index-pattern', indexPatternId);
const { fields, title, timeFieldName } = attributes;
const parsedFields = fields ? JSON.parse(fields) : [];

indexPatternSavedObject = {
fields: parsedFields,
title,
timeFieldName,
attributes,
};
} catch (err) {
throw new Error(`Could not get index pattern saved object! ${err}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ export interface SavedObject {
references: SavedObjectReference[];
}

/* This object is passed to different helpers in different parts of the code
- packages/kbn-es-query/src/es_query/build_es_query
- x-pack/plugins/reporting/export_types/csv/server/lib/field_format_map
The structure has redundant parts and json-parsed / json-unparsed versions of the same data
*/
export interface IndexPatternSavedObject {
title: string;
timeFieldName: string;
fields: any[];
fieldFormatMap: {
[key: string]: { id: string; params: { pattern: string } };
attributes: {
fieldFormatMap: string;
fields: string;
Copy link
Member Author

Choose a reason for hiding this comment

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

There was some structure tweaks to this type made, after realizing that buildEsQuery and getFieldFormatMap both require an index pattern object, but expect different structure :\

};
}

Expand Down
5 changes: 3 additions & 2 deletions x-pack/test/api_integration/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {
InfraOpsGraphQLProvider
} from './services';

export default async function ({ readConfigFile }) {

export async function getApiIntegrationConfig({ readConfigFile }) {
const kibanaAPITestsConfig = await readConfigFile(require.resolve('../../../test/api_integration/config.js'));
const xPackFunctionalTestsConfig = await readConfigFile(require.resolve('../functional/config.js'));
const kibanaCommonConfig = await readConfigFile(require.resolve('../../../test/common/config.js'));
Expand Down Expand Up @@ -47,3 +46,5 @@ export default async function ({ readConfigFile }) {
esTestCluster: xPackFunctionalTestsConfig.get('esTestCluster'),
};
}

export default getApiIntegrationConfig;
Copy link
Member Author

Choose a reason for hiding this comment

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

Just a little trick I saw in another test file to get my test config to "extend" this one in an easier way.

Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,6 @@
{
"type": "index",
"value": {
"aliases": {
},
"index": "sales",
"mappings": {
"properties": {
Expand Down
Binary file not shown.
Loading