Skip to content

Commit

Permalink
Add log entries query params
Browse files Browse the repository at this point in the history
  • Loading branch information
weltenwort committed Jan 11, 2021
1 parent 003e22f commit b361648
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
16 changes: 15 additions & 1 deletion x-pack/plugins/infra/common/log_entry/log_entry_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,23 @@ export const logEntryCursorRT = rt.type({
time: rt.number,
tiebreaker: rt.number,
});

export type LogEntryCursor = rt.TypeOf<typeof logEntryCursorRT>;

export const logEntryBeforeCursorRT = rt.type({
before: rt.union([logEntryCursorRT, rt.literal('last')]),
});
export type LogEntryBeforeCursor = rt.TypeOf<typeof logEntryBeforeCursorRT>;

export const logEntryAfterCursorRT = rt.type({
after: rt.union([logEntryCursorRT, rt.literal('first')]),
});
export type LogEntryAfterCursor = rt.TypeOf<typeof logEntryAfterCursorRT>;

export const logEntryAroundCursorRT = rt.type({
center: logEntryCursorRT,
});
export type LogEntryAroundCursor = rt.TypeOf<typeof logEntryAroundCursorRT>;

export const getLogEntryCursorFromHit = (hit: { sort: [number, number] }) =>
decodeOrThrow(logEntryCursorRT)({
time: hit.sort[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

import * as rt from 'io-ts';
import { logSourceColumnConfigurationRT } from '../../http_api/log_sources';
import { logEntryCursorRT, logEntryRT } from '../../log_entry';
import {
logEntryAfterCursorRT,
logEntryAroundCursorRT,
logEntryBeforeCursorRT,
logEntryCursorRT,
logEntryRT,
} from '../../log_entry';
import { searchStrategyErrorRT } from '../common/errors';

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

export const logEntriesBaseSearchRequestParamsRT = rt.intersection([
const logEntriesBaseSearchRequestParamsRT = rt.intersection([
rt.type({
sourceId: rt.string,
startTimestamp: rt.number,
Expand All @@ -26,17 +32,17 @@ export const logEntriesBaseSearchRequestParamsRT = rt.intersection([

export const logEntriesBeforeSearchRequestParamsRT = rt.intersection([
logEntriesBaseSearchRequestParamsRT,
rt.type({ before: rt.union([logEntryCursorRT, rt.literal('last')]) }),
logEntryBeforeCursorRT,
]);

export const logEntriesAfterSearchRequestParamsRT = rt.intersection([
logEntriesBaseSearchRequestParamsRT,
rt.type({ after: rt.union([logEntryCursorRT, rt.literal('first')]) }),
logEntryAfterCursorRT,
]);

export const logEntriesCenteredSearchRequestParamsRT = rt.intersection([
logEntriesBaseSearchRequestParamsRT,
rt.type({ center: logEntryCursorRT }),
logEntryAroundCursorRT,
]);

export const logEntriesSearchRequestParamsRT = rt.union([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@

import type { RequestParams } from '@elastic/elasticsearch';
import * as rt from 'io-ts';
import { jsonArrayRT } from '../../../../common/typed_json';
import {
LogEntryAfterCursor,
LogEntryBeforeCursor,
LogEntryCursor,
} from '../../../../common/log_entry';
import { jsonArrayRT, JsonObject } from '../../../../common/typed_json';
import {
commonHitFieldsRT,
commonSearchSuccessResponseFieldsRT,
} from '../../../utils/elasticsearch_runtime_types';

export const createGetLogEntriesQuery = (
logEntryIndex: string
logEntriesIndex: string,
startTimestamp: number,
endTimestamp: number,
cursor: LogEntryBeforeCursor | LogEntryAfterCursor | null | undefined,
size: number,
timestampField: string,
tiebreakerField: string,
fields: string[],
query?: JsonObject,
highlightTerm?: string
): RequestParams.AsyncSearchSubmit<Record<string, any>> => ({
index: logEntryIndex,
index: logEntriesIndex,
terminate_after: 1,
track_scores: false,
track_total_hits: false,
Expand All @@ -28,6 +42,32 @@ export const createGetLogEntriesQuery = (
},
});

function createSortAndSearchAfterClause(
cursor: LogEntryBeforeCursor | LogEntryAfterCursor | null | undefined
): {
sortDirection: 'asc' | 'desc';
searchAfterClause: { search_after?: readonly [number, number] };
} {
if (cursor) {
if ('before' in cursor) {
return {
sortDirection: 'desc',
searchAfterClause:
cursor.before !== 'last'
? { search_after: [cursor.before.time, cursor.before.tiebreaker] as const }
: {},
};
} else if (cursor.after !== 'first') {
return {
sortDirection: 'asc',
searchAfterClause: { search_after: [cursor.after.time, cursor.after.tiebreaker] as const },
};
}
}

return { sortDirection: 'asc', searchAfterClause: {} };
}

export const logEntryHitRT = rt.intersection([
commonHitFieldsRT,
rt.type({
Expand Down

0 comments on commit b361648

Please sign in to comment.