Skip to content

Commit

Permalink
[7.x] [Logs UI] Use fields api in log stream (#76919) (#77031)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Fernández Gómez authored Sep 9, 2020
1 parent a08e45e commit 4af50ab
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import { timeMilliseconds } from 'd3-time';
import * as runtimeTypes from 'io-ts';
import { compact, first, get, has } from 'lodash';
import { compact, first } from 'lodash';
import { pipe } from 'fp-ts/lib/pipeable';
import { map, fold } from 'fp-ts/lib/Either';
import { identity, constant } from 'fp-ts/lib/function';
import { RequestHandlerContext } from 'src/core/server';
import { JsonObject, JsonValue } from '../../../../common/typed_json';
import { JsonValue } from '../../../../common/typed_json';
import {
LogEntriesAdapter,
LogEntriesParams,
Expand All @@ -31,7 +31,7 @@ const TIMESTAMP_FORMAT = 'epoch_millis';
interface LogItemHit {
_index: string;
_id: string;
_source: JsonObject;
fields: { [key: string]: [value: unknown] };
sort: [number, number];
}

Expand Down Expand Up @@ -82,7 +82,8 @@ export class InfraKibanaLogEntriesAdapter implements LogEntriesAdapter {
body: {
size: typeof size !== 'undefined' ? size : LOG_ENTRIES_PAGE_SIZE,
track_total_hits: false,
_source: fields,
_source: false,
fields,
query: {
bool: {
filter: [
Expand Down Expand Up @@ -214,6 +215,8 @@ export class InfraKibanaLogEntriesAdapter implements LogEntriesAdapter {
values: [id],
},
},
fields: ['*'],
_source: false,
},
};

Expand All @@ -230,8 +233,8 @@ function mapHitsToLogEntryDocuments(hits: SortedSearchHit[], fields: string[]):
return hits.map((hit) => {
const logFields = fields.reduce<{ [fieldName: string]: JsonValue }>(
(flattenedFields, field) => {
if (has(hit._source, field)) {
flattenedFields[field] = get(hit._source, field);
if (field in hit.fields) {
flattenedFields[field] = hit.fields[field][0];
}
return flattenedFields;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const serializeValue = (value: any): string => {
}
return `${value}`;
};
export const convertESFieldsToLogItemFields = (fields: {
[field: string]: [value: unknown];
}): LogEntriesItemField[] => {
return Object.keys(fields).map((field) => ({ field, value: serializeValue(fields[field][0]) }));
};

export const convertDocumentSourceToLogItemFields = (
source: JsonObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
SavedSourceConfigurationFieldColumnRuntimeType,
} from '../../sources';
import { getBuiltinRules } from './builtin_rules';
import { convertDocumentSourceToLogItemFields } from './convert_document_source_to_log_item_fields';
import { convertESFieldsToLogItemFields } from './convert_document_source_to_log_item_fields';
import {
CompiledLogMessageFormattingRule,
Fields,
Expand Down Expand Up @@ -264,7 +264,7 @@ export class InfraLogEntriesDomain {
tiebreaker: document.sort[1],
},
fields: sortBy(
[...defaultFields, ...convertDocumentSourceToLogItemFields(document._source)],
[...defaultFields, ...convertESFieldsToLogItemFields(document.fields)],
'field'
),
};
Expand Down Expand Up @@ -313,7 +313,7 @@ export class InfraLogEntriesDomain {
interface LogItemHit {
_index: string;
_id: string;
_source: JsonObject;
fields: { [field: string]: [value: unknown] };
sort: [number, number];
}

Expand Down
19 changes: 3 additions & 16 deletions x-pack/plugins/infra/server/routes/log_entries/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import Boom from 'boom';

import { pipe } from 'fp-ts/lib/pipeable';
import { fold } from 'fp-ts/lib/Either';
import { identity } from 'fp-ts/lib/function';
import { schema } from '@kbn/config-schema';

import { throwErrors } from '../../../common/runtime_types';
import { createValidationFunction } from '../../../common/runtime_types';

import { InfraBackendLibs } from '../../lib/infra_types';
import {
Expand All @@ -22,22 +15,16 @@ import {
import { parseFilterQuery } from '../../utils/serialized_query';
import { LogEntriesParams } from '../../lib/domains/log_entries_domain';

const escapeHatch = schema.object({}, { unknowns: 'allow' });

export const initLogEntriesRoute = ({ framework, logEntries }: InfraBackendLibs) => {
framework.registerRoute(
{
method: 'post',
path: LOG_ENTRIES_PATH,
validate: { body: escapeHatch },
validate: { body: createValidationFunction(logEntriesRequestRT) },
},
async (requestContext, request, response) => {
try {
const payload = pipe(
logEntriesRequestRT.decode(request.body),
fold(throwErrors(Boom.badRequest), identity)
);

const payload = request.body;
const {
startTimestamp: startTimestamp,
endTimestamp: endTimestamp,
Expand Down
19 changes: 3 additions & 16 deletions x-pack/plugins/infra/server/routes/log_entries/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import Boom from 'boom';

import { pipe } from 'fp-ts/lib/pipeable';
import { fold } from 'fp-ts/lib/Either';
import { identity } from 'fp-ts/lib/function';
import { schema } from '@kbn/config-schema';

import { throwErrors } from '../../../common/runtime_types';
import { createValidationFunction } from '../../../common/runtime_types';

import { InfraBackendLibs } from '../../lib/infra_types';
import {
Expand All @@ -20,22 +13,16 @@ import {
logEntriesItemResponseRT,
} from '../../../common/http_api';

const escapeHatch = schema.object({}, { unknowns: 'allow' });

export const initLogEntriesItemRoute = ({ framework, sources, logEntries }: InfraBackendLibs) => {
framework.registerRoute(
{
method: 'post',
path: LOG_ENTRIES_ITEM_PATH,
validate: { body: escapeHatch },
validate: { body: createValidationFunction(logEntriesItemRequestRT) },
},
async (requestContext, request, response) => {
try {
const payload = pipe(
logEntriesItemRequestRT.decode(request.body),
fold(throwErrors(Boom.badRequest), identity)
);

const payload = request.body;
const { id, sourceId } = payload;
const sourceConfiguration = (
await sources.getSourceConfiguration(requestContext.core.savedObjects.client, sourceId)
Expand Down

0 comments on commit 4af50ab

Please sign in to comment.