Skip to content

Commit

Permalink
[Metrics UI / Logs UI] Remove field filtering in Source API call (#58553
Browse files Browse the repository at this point in the history
)

* [Metrics UI / Logs UI] Remove field filtering in Source API call.

* Fixing type_check issues
  • Loading branch information
simianhacker committed Feb 26, 2020
1 parent e97b451 commit 0764380
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { RequestHandlerContext } from 'src/core/server';
export interface FieldsAdapter {
getIndexFields(
requestContext: RequestHandlerContext,
indices: string,
timefield: string
indices: string
): Promise<IndexFieldDescriptor[]>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { startsWith, uniq, first } from 'lodash';
import { RequestHandlerContext } from 'src/core/server';
import { InfraDatabaseSearchResponse } from '../framework';
import { KibanaFramework } from '../framework/kibana_framework_adapter';
import { FieldsAdapter, IndexFieldDescriptor } from './adapter_types';
import { getAllowedListForPrefix } from '../../../../common/ecs_allowed_list';
import { getAllCompositeData } from '../../../utils/get_all_composite_data';
import { createAfterKeyHandler } from '../../../utils/create_afterkey_handler';

interface Bucket {
key: { dataset: string };
doc_count: number;
}

interface DataSetResponse {
datasets: {
buckets: Bucket[];
after_key: {
dataset: string;
};
};
}

export class FrameworkFieldsAdapter implements FieldsAdapter {
private framework: KibanaFramework;
Expand All @@ -36,91 +17,15 @@ export class FrameworkFieldsAdapter implements FieldsAdapter {

public async getIndexFields(
requestContext: RequestHandlerContext,
indices: string,
timefield: string
indices: string
): Promise<IndexFieldDescriptor[]> {
const indexPatternsService = this.framework.getIndexPatternsService(requestContext);
const response = await indexPatternsService.getFieldsForWildcard({
pattern: indices,
});
const { dataSets, modules } = await this.getDataSetsAndModules(
requestContext,
indices,
timefield
);
const allowedList = modules.reduce(
(acc, name) => uniq([...acc, ...getAllowedListForPrefix(name)]),
[] as string[]
);
const dataSetsWithAllowedList = [...allowedList, ...dataSets];
return response.map(field => ({
...field,
displayable: dataSetsWithAllowedList.some(name => startsWith(field.name, name)),
displayable: true,
}));
}

private async getDataSetsAndModules(
requestContext: RequestHandlerContext,
indices: string,
timefield: string
): Promise<{ dataSets: string[]; modules: string[] }> {
const params = {
index: indices,
allowNoIndices: true,
ignoreUnavailable: true,
body: {
size: 0,
query: {
bool: {
filter: [
{
range: {
[timefield]: {
gte: 'now-24h',
lte: 'now',
},
},
},
],
},
},
aggs: {
datasets: {
composite: {
sources: [
{
dataset: {
terms: {
field: 'event.dataset',
},
},
},
],
},
},
},
},
};

const bucketSelector = (response: InfraDatabaseSearchResponse<{}, DataSetResponse>) =>
(response.aggregations && response.aggregations.datasets.buckets) || [];
const handleAfterKey = createAfterKeyHandler(
'body.aggs.datasets.composite.after',
input => input?.aggregations?.datasets?.after_key
);

const buckets = await getAllCompositeData<DataSetResponse, Bucket>(
this.framework,
requestContext,
params,
bucketSelector,
handleAfterKey
);
const dataSets = buckets.map(bucket => bucket.key.dataset);
const modules = dataSets.reduce((acc, dataset) => {
const module = first(dataset.split(/\./));
return module ? uniq([...acc, module]) : acc;
}, [] as string[]);
return { modules, dataSets };
}
}
3 changes: 1 addition & 2 deletions x-pack/plugins/infra/server/lib/domains/fields_domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export class InfraFieldsDomain {
requestContext,
`${includeMetricIndices ? configuration.metricAlias : ''},${
includeLogIndices ? configuration.logAlias : ''
}`,
configuration.fields.timestamp
}`
);

return fields;
Expand Down

0 comments on commit 0764380

Please sign in to comment.