[Metrics UI / Logs UI] Remove field filtering in Source API call (#58553)
* [Metrics UI / Logs UI] Remove field filtering in Source API call. * Fixing type_check issues
This commit is contained in:
parent
e97b451cc1
commit
0764380ffd
|
@ -9,8 +9,7 @@ import { RequestHandlerContext } from 'src/core/server';
|
|||
export interface FieldsAdapter {
|
||||
getIndexFields(
|
||||
requestContext: RequestHandlerContext,
|
||||
indices: string,
|
||||
timefield: string
|
||||
indices: string
|
||||
): Promise<IndexFieldDescriptor[]>;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
@ -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 };
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,8 +31,7 @@ export class InfraFieldsDomain {
|
|||
requestContext,
|
||||
`${includeMetricIndices ? configuration.metricAlias : ''},${
|
||||
includeLogIndices ? configuration.logAlias : ''
|
||||
}`,
|
||||
configuration.fields.timestamp
|
||||
}`
|
||||
);
|
||||
|
||||
return fields;
|
||||
|
|
Loading…
Reference in a new issue