Skip to content

Commit

Permalink
[Logs UI] Support inline Log Views in routes (#151760)
Browse files Browse the repository at this point in the history
## Summary

Closes #151489

This is the server side portion of support for transient / inline Log
Views. Alerting and ML based functionality is scoped to the persisted
type only, as we won't be supporting inline Log Views in those contexts.

In terms of UI, changes have been made as close to the edge / boundary
to the server (e.g. where we actually make network requests) as
possible. This is because the bulk of the UI changes will come in
#142840.

## Testing

This is predominantly a type-driven refactoring (the best kind), so we
should be relatively safe here (especially since inline views aren't in
the UI yet). But, a quick test of each page and a high level functional
test would still be good.
  • Loading branch information
Kerry350 authored Mar 2, 2023
1 parent bdb4f87 commit e772fa1
Show file tree
Hide file tree
Showing 54 changed files with 167 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { i18n } from '@kbn/i18n';
import * as rt from 'io-ts';
import { logViewReferenceRT } from '../../../log_views';
import { persistedLogViewReferenceRT } from '../../../log_views';
import { commonSearchSuccessResponseFieldsRT } from '../../../utils/elasticsearch_runtime_types';

export const LOG_DOCUMENT_COUNT_RULE_TYPE_ID = 'logs.alert.document.count';
Expand Down Expand Up @@ -181,7 +181,7 @@ const RequiredRuleParamsRT = rt.type({
count: ThresholdRT,
timeUnit: timeUnitRT,
timeSize: timeSizeRT,
logView: logViewReferenceRT, // In future, this should be a union of logViewReferenceRT and inlineLogViewRT
logView: persistedLogViewReferenceRT, // Alerts are only compatible with persisted Log Views
});

const partialRequiredRuleParamsRT = rt.partial(RequiredRuleParamsRT.props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
timeSizeRT,
groupByRT,
} from '../../alerting/logs/log_threshold/types';
import { persistedLogViewReferenceRT } from '../../log_views';

export const LOG_ALERTS_CHART_PREVIEW_DATA_PATH = '/api/infra/log_alerts/chart_preview_data';

Expand Down Expand Up @@ -81,7 +82,7 @@ export type GetLogAlertsChartPreviewDataAlertParamsSubset = rt.TypeOf<

export const getLogAlertsChartPreviewDataRequestPayloadRT = rt.type({
data: rt.type({
sourceId: rt.string,
logView: persistedLogViewReferenceRT,
alertParams: getLogAlertsChartPreviewDataAlertParamsSubsetRT,
buckets: rt.number,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import * as rt from 'io-ts';

import { persistedLogViewReferenceRT } from '../../../log_views';
import { timeRangeRT, routeTimingMetadataRT } from '../../shared';
import {
logEntryAnomalyRT,
Expand Down Expand Up @@ -51,8 +52,8 @@ export type GetLogEntryAnomaliesSuccessResponsePayload = rt.TypeOf<
export const getLogEntryAnomaliesRequestPayloadRT = rt.type({
data: rt.intersection([
rt.type({
// the ID of the source configuration
sourceId: rt.string,
// log view
logView: persistedLogViewReferenceRT,
// the time range to fetch the log entry anomalies from
timeRange: timeRangeRT,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as rt from 'io-ts';
import { persistedLogViewReferenceRT } from '../../../log_views';

import {
badRequestErrorRT,
Expand All @@ -23,8 +24,8 @@ export const LOG_ANALYSIS_GET_LOG_ENTRY_ANOMALIES_DATASETS_PATH =

export const getLogEntryAnomaliesDatasetsRequestPayloadRT = rt.type({
data: rt.type({
// the id of the source configuration
sourceId: rt.string,
// log view
logView: persistedLogViewReferenceRT,
// the time range to fetch the anomalies datasets from
timeRange: timeRangeRT,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import * as rt from 'io-ts';

import { persistedLogViewReferenceRT } from '../../../log_views';
import {
badRequestErrorRT,
forbiddenErrorRT,
Expand Down Expand Up @@ -38,8 +39,8 @@ export const getLogEntryCategoriesRequestPayloadRT = rt.type({
rt.type({
// the number of categories to fetch
categoryCount: rt.number,
// the id of the source configuration
sourceId: rt.string,
// log view
logView: persistedLogViewReferenceRT,
// the time range to fetch the categories from
timeRange: timeRangeRT,
// a list of histograms to create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
timeRangeRT,
routeTimingMetadataRT,
} from '../../shared';

import { persistedLogViewReferenceRT } from '../../../log_views';
export const LOG_ANALYSIS_GET_LOG_ENTRY_CATEGORY_DATASETS_PATH =
'/api/infra/log_analysis/results/log_entry_category_datasets';

Expand All @@ -23,8 +23,8 @@ export const LOG_ANALYSIS_GET_LOG_ENTRY_CATEGORY_DATASETS_PATH =

export const getLogEntryCategoryDatasetsRequestPayloadRT = rt.type({
data: rt.type({
// the id of the source configuration
sourceId: rt.string,
// log view
logView: persistedLogViewReferenceRT,
// the time range to fetch the category datasets from
timeRange: timeRangeRT,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import * as rt from 'io-ts';

import { persistedLogViewReferenceRT } from '../../../log_views';
import {
badRequestErrorRT,
forbiddenErrorRT,
Expand All @@ -28,8 +29,8 @@ export const getLogEntryCategoryExamplesRequestPayloadRT = rt.type({
categoryId: rt.number,
// the number of examples to fetch
exampleCount: rt.number,
// the id of the source configuration
sourceId: rt.string,
// log view
logView: persistedLogViewReferenceRT,
// the time range to fetch the category examples from
timeRange: timeRangeRT,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as rt from 'io-ts';
import { persistedLogViewReferenceRT } from '../../../log_views';
import { logEntryExampleRT } from '../../../log_analysis';
import {
badRequestErrorRT,
Expand All @@ -28,8 +29,8 @@ export const getLogEntryExamplesRequestPayloadRT = rt.type({
dataset: rt.string,
// the number of examples to fetch
exampleCount: rt.number,
// the id of the source configuration
sourceId: rt.string,
// logView
logView: persistedLogViewReferenceRT,
// the time range to fetch the log rate examples from
timeRange: timeRangeRT,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import * as rt from 'io-ts';
import { logEntryCursorRT, logEntryRT } from '../../log_entry';
import { logViewColumnConfigurationRT } from '../../log_views';
import { logViewReferenceRT } from '../../log_views';

export const LOG_ENTRIES_HIGHLIGHTS_PATH = '/api/log_entries/highlights';

export const logEntriesHighlightsBaseRequestRT = rt.intersection([
rt.type({
sourceId: rt.string,
logView: logViewReferenceRT,
startTimestamp: rt.number,
endTimestamp: rt.number,
highlightTerms: rt.array(rt.string),
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/infra/common/http_api/log_entries/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
*/

import * as rt from 'io-ts';
import { logViewReferenceRT } from '../../log_views';

export const LOG_ENTRIES_SUMMARY_PATH = '/api/log_entries/summary';

export const logEntriesSummaryRequestRT = rt.type({
sourceId: rt.string,
logView: logViewReferenceRT,
startTimestamp: rt.number,
endTimestamp: rt.number,
bucketSize: rt.number,
Expand Down
12 changes: 11 additions & 1 deletion x-pack/plugins/infra/common/log_views/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,19 @@ export const logViewStatusRT = rt.strict({
});
export type LogViewStatus = rt.TypeOf<typeof logViewStatusRT>;

export const logViewReferenceRT = rt.type({
export const persistedLogViewReferenceRT = rt.type({
logViewId: rt.string,
type: rt.literal('log-view-reference'),
});

export type PersistedLogViewReference = rt.TypeOf<typeof persistedLogViewReferenceRT>;

export const inlineLogViewReferenceRT = rt.type({
type: rt.literal('log-view-inline'),
id: rt.string,
attributes: logViewAttributesRT,
});

export const logViewReferenceRT = rt.union([persistedLogViewReferenceRT, inlineLogViewReferenceRT]);

export type LogViewReference = rt.TypeOf<typeof logViewReferenceRT>;
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import {
logEntryCursorRT,
logEntryRT,
} from '../../log_entry';
import { logViewColumnConfigurationRT } from '../../log_views';
import { logViewColumnConfigurationRT, logViewReferenceRT } from '../../log_views';
import { jsonObjectRT } from '../../typed_json';
import { searchStrategyErrorRT } from '../common/errors';

export const LOG_ENTRIES_SEARCH_STRATEGY = 'infra-log-entries';

const logEntriesBaseSearchRequestParamsRT = rt.intersection([
rt.type({
sourceId: rt.string,
logView: logViewReferenceRT,
startTimestamp: rt.number,
endTimestamp: rt.number,
size: rt.number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

import * as rt from 'io-ts';
import { logEntryCursorRT, logEntryFieldRT } from '../../log_entry';
import { logViewReferenceRT } from '../../log_views';
import { searchStrategyErrorRT } from '../common/errors';

export const LOG_ENTRY_SEARCH_STRATEGY = 'infra-log-entry';

export const logEntrySearchRequestParamsRT = rt.type({
sourceId: rt.string,
logView: logViewReferenceRT,
logEntryId: rt.string,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ForLastExpression,
RuleTypeParamsExpressionProps,
} from '@kbn/triggers-actions-ui-plugin/public';
import { LogViewReference, ResolvedLogViewField } from '../../../../../common/log_views';
import { PersistedLogViewReference, ResolvedLogViewField } from '../../../../../common/log_views';
import {
Comparator,
isOptimizableGroupedThreshold,
Expand Down Expand Up @@ -54,7 +54,7 @@ const DEFAULT_BASE_EXPRESSION = {

const DEFAULT_FIELD = 'log.level';

const createLogViewReference = (logViewId: string): LogViewReference => ({
const createLogViewReference = (logViewId: string): PersistedLogViewReference => ({
logViewId,
type: 'log-view-reference',
});
Expand All @@ -69,7 +69,7 @@ const createDefaultCriterion = (

const createDefaultCountRuleParams = (
availableFields: ResolvedLogViewField[],
logView: LogViewReference
logView: PersistedLogViewReference
): PartialCountRuleParams => ({
...DEFAULT_BASE_EXPRESSION,
logView,
Expand All @@ -82,7 +82,7 @@ const createDefaultCountRuleParams = (

const createDefaultRatioRuleParams = (
availableFields: ResolvedLogViewField[],
logView: LogViewReference
logView: PersistedLogViewReference
): PartialRatioRuleParams => ({
...DEFAULT_BASE_EXPRESSION,
logView,
Expand Down Expand Up @@ -226,24 +226,24 @@ export const Editor: React.FC<RuleTypeParamsExpressionProps<PartialRuleParams, L
[setRuleParams]
);

const logViewReferemnce = useMemo(() => createLogViewReference(logViewId), [logViewId]);
const logViewReference = useMemo(() => createLogViewReference(logViewId), [logViewId]);

const defaultCountAlertParams = useMemo(
() => createDefaultCountRuleParams(supportedFields, logViewReferemnce),
[supportedFields, logViewReferemnce]
() => createDefaultCountRuleParams(supportedFields, logViewReference),
[supportedFields, logViewReference]
);

const updateType = useCallback(
(type: ThresholdType) => {
const defaults =
type === 'count'
? defaultCountAlertParams
: createDefaultRatioRuleParams(supportedFields, logViewReferemnce);
: createDefaultRatioRuleParams(supportedFields, logViewReference);
// Reset properties that don't make sense switching from one context to the other
setRuleParams('count', defaults.count);
setRuleParams('criteria', defaults.criteria);
},
[defaultCountAlertParams, setRuleParams, supportedFields, logViewReferemnce]
[defaultCountAlertParams, setRuleParams, supportedFields, logViewReference]
);

useMount(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const callGetChartPreviewDataAPI = async (
body: JSON.stringify(
getLogAlertsChartPreviewDataRequestPayloadRT.encode({
data: {
sourceId,
logView: { type: 'log-view-reference', logViewId: sourceId },
alertParams,
buckets,
},
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/infra/public/containers/logs/log_entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export const useLogEntry = ({
return !!logEntryId && !!sourceId
? {
request: {
params: logEntrySearchRequestParamsRT.encode({ sourceId, logEntryId }),
params: logEntrySearchRequestParamsRT.encode({
logView: { type: 'log-view-reference', logViewId: sourceId },
logEntryId,
}),
},
options: { strategy: LOG_ENTRY_SEARCH_STRATEGY },
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const useLogEntryHighlights = (

return await fetchLogEntriesHighlights(
{
sourceId,
logView: { type: 'log-view-reference', logViewId: sourceId },
startTimestamp,
endTimestamp,
center: centerPoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const useLogSummaryHighlights = (

return await fetchLogSummaryHighlights(
{
sourceId,
logView: { type: 'log-view-reference', logViewId: sourceId },
startTimestamp,
endTimestamp,
bucketSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const useLogEntriesAfterRequest = ({
highlightPhrase,
query: query as JsonObject,
size: params.size,
sourceId,
logView: { type: 'log-view-reference', logViewId: sourceId },
startTimestamp,
}),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const useLogEntriesBeforeRequest = ({
highlightPhrase,
query: query as JsonObject,
size: params.size,
sourceId,
logView: { type: 'log-view-reference', logViewId: sourceId },
startTimestamp: params.extendTo ?? startTimestamp,
}),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('useLogSummary hook', () => {
expect(fetchLogSummaryMock).toHaveBeenCalledTimes(1);
expect(fetchLogSummaryMock).toHaveBeenLastCalledWith(
expect.objectContaining({
sourceId: 'INITIAL_SOURCE_ID',
logView: { logViewId: 'INITIAL_SOURCE_ID', type: 'log-view-reference' },
}),
expect.anything()
);
Expand All @@ -74,7 +74,7 @@ describe('useLogSummary hook', () => {
expect(fetchLogSummaryMock).toHaveBeenCalledTimes(2);
expect(fetchLogSummaryMock).toHaveBeenLastCalledWith(
expect.objectContaining({
sourceId: 'CHANGED_SOURCE_ID',
logView: { logViewId: 'CHANGED_SOURCE_ID', type: 'log-view-reference' },
}),
expect.anything()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useLogSummary = (

pushLogSummaryBucketsArgs([
{
sourceId,
logView: { type: 'log-view-reference', logViewId: sourceId },
startTimestamp,
endTimestamp,
bucketSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const callGetLogEntryCategoryDatasetsAPI = async (
body: JSON.stringify(
getLogEntryCategoryDatasetsRequestPayloadRT.encode({
data: {
sourceId,
logView: { type: 'log-view-reference', logViewId: sourceId },
timeRange: {
startTime,
endTime,
Expand Down
Loading

0 comments on commit e772fa1

Please sign in to comment.