From 1971e5c90e746388bcbb1f1efe7a98427fcea6a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20G=C3=B3mez?= Date: Thu, 2 Jan 2020 17:47:56 +0100 Subject: [PATCH] Refine `LogEntry` type --- .../common/http_api/log_entries/entries.ts | 31 +++++++++++++- .../log_entries_domain/log_entries_domain.ts | 41 ++++++++++--------- 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/x-pack/legacy/plugins/infra/common/http_api/log_entries/entries.ts b/x-pack/legacy/plugins/infra/common/http_api/log_entries/entries.ts index 3d9b32f47f5e..bc9db5722aa9 100644 --- a/x-pack/legacy/plugins/infra/common/http_api/log_entries/entries.ts +++ b/x-pack/legacy/plugins/infra/common/http_api/log_entries/entries.ts @@ -45,12 +45,41 @@ export const logEntriesRequestRT = rt.union([ export type LogEntriesRequest = rt.TypeOf; +// JSON value +const valueRT = rt.union([rt.string, rt.number, rt.boolean, rt.object, rt.null, rt.undefined]); + +export const logMessagePartRT = rt.union([ + rt.type({ + constant: rt.string, + }), + rt.type({ + field: rt.string, + value: valueRT, + highlights: rt.array(rt.string), + }), +]); + +export const logColumnRT = rt.union([ + rt.type({ columnId: rt.string, timestamp: rt.number }), + rt.type({ + columnId: rt.string, + field: rt.string, + value: valueRT, + }), + rt.type({ + columnId: rt.string, + message: rt.array(logMessagePartRT), + }), +]); + export const logEntryRT = rt.type({ id: rt.string, cursor: logEntriesCursorRT, - columns: rt.array(rt.any), + columns: rt.array(logColumnRT), }); +export type LogMessagepart = rt.TypeOf; +export type LogColumn = rt.TypeOf; export type LogEntry = rt.TypeOf; export const logEntriesResponseRT = rt.type({ diff --git a/x-pack/legacy/plugins/infra/server/lib/domains/log_entries_domain/log_entries_domain.ts b/x-pack/legacy/plugins/infra/server/lib/domains/log_entries_domain/log_entries_domain.ts index 78317fdaa823..2d5aacb1e484 100644 --- a/x-pack/legacy/plugins/infra/server/lib/domains/log_entries_domain/log_entries_domain.ts +++ b/x-pack/legacy/plugins/infra/server/lib/domains/log_entries_domain/log_entries_domain.ts @@ -16,6 +16,7 @@ import { LogEntry, LogEntriesItem, LogEntriesCursor, + LogColumn, } from '../../../../common/http_api'; import { InfraLogEntry, InfraLogMessageSegment } from '../../../graphql/types'; import { @@ -203,27 +204,27 @@ export class InfraLogEntriesDomain { return { id: doc.gid, cursor: doc.key, - columns: configuration.logColumns.map(column => { - if ('timestampColumn' in column) { - return { - columnId: column.timestampColumn.id, - timestamp: doc.key.time, - }; - } - if ('messageColumn' in column) { - return { - columnId: column.messageColumn.id, - message: messageFormattingRules.format(doc.fields, doc.highlights), - }; - } - if ('fieldColumn' in column) { - return { - columnId: column.fieldColumn.id, - field: column.fieldColumn.field, - value: doc.fields[column.fieldColumn.field], - }; + columns: configuration.logColumns.map( + (column): LogColumn => { + if ('timestampColumn' in column) { + return { + columnId: column.timestampColumn.id, + timestamp: doc.key.time, + }; + } else if ('messageColumn' in column) { + return { + columnId: column.messageColumn.id, + message: messageFormattingRules.format(doc.fields, doc.highlights), + }; + } else { + return { + columnId: column.fieldColumn.id, + field: column.fieldColumn.field, + value: doc.fields[column.fieldColumn.field], + }; + } } - }), + ), }; });