Skip to content

Commit

Permalink
Change type of date ranges
Browse files Browse the repository at this point in the history
We will move the responsibility to parse the dates to the client. The
API will only take timestamps
  • Loading branch information
Alejandro Fernández Gómez committed Dec 24, 2019
1 parent 02f14e0 commit b17b7c2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ export const logEntriesCursorRT = rt.type({
tiebreaker: rt.number,
});
export type LogEntriesCursor = rt.TypeOf<typeof logEntriesCursorRT>;

export const esDateRT = rt.union([rt.string, rt.number]);
export type ESDate = rt.TypeOf<typeof esDateRT>;
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
*/

import * as rt from 'io-ts';
import { logEntriesCursorRT, esDateRT } from './common';
import { logEntriesCursorRT } from './common';

export const LOG_ENTRIES_PATH = '/api/log_entries/entries';

export const logEntriesBaseRequestRT = rt.intersection([
rt.type({
sourceId: rt.string,
startDate: esDateRT,
endDate: esDateRT,
startDate: rt.number,
endDate: rt.number,
}),
rt.partial({
query: rt.string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class InfraKibanaLogEntriesAdapter implements LogEntriesAdapter {
fields: string[],
params: LogEntriesParams
): Promise<LogEntryDocument[]> {
const { startTimestamp, endTimestamp, query, cursor } = params;
const { startDate, endDate, query, cursor } = params;

const { sortDirection, searchAfterClause } = processCursor(cursor);

Expand All @@ -112,8 +112,8 @@ export class InfraKibanaLogEntriesAdapter implements LogEntriesAdapter {
{
range: {
[sourceConfiguration.fields.timestamp]: {
gte: startTimestamp,
lte: endTimestamp,
gte: startDate,
lte: endDate,
format: TIMESTAMP_FORMAT,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import stringify from 'json-stable-stringify';
import { sortBy, get } from 'lodash';
import { sortBy } from 'lodash';

import { RequestHandlerContext } from 'src/core/server';
import { TimeKey } from '../../../../common/time';
Expand Down Expand Up @@ -35,8 +35,8 @@ import {
} from './message';

export interface LogEntriesParams {
startTimestamp: number;
endTimestamp: number;
startDate: number;
endDate: number;
query?: JsonObject;
cursor?: { before: LogEntriesCursor | 'last' } | { after: LogEntriesCursor | 'first' };
}
Expand Down
25 changes: 2 additions & 23 deletions x-pack/legacy/plugins/infra/server/routes/log_entries/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ 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 datemath from '@elastic/datemath';

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

import { InfraBackendLibs } from '../../lib/infra_types';
import {
ESDate,
LOG_ENTRIES_PATH,
logEntriesRequestRT,
logEntriesResponseRT,
Expand Down Expand Up @@ -48,16 +46,9 @@ export const initLogEntriesRoute = ({ framework, logEntries }: InfraBackendLibs)
cursor = { after: payload.after };
}

const startTimestamp = parseDate(startDate);
const endTimestamp = parseDate(endDate);

if (!startTimestamp || !endTimestamp) {
return response.badRequest();
}

const entries = await logEntries.getLogEntries(requestContext, sourceId, {
startTimestamp,
endTimestamp,
startDate,
endDate,
query: parseFilterQuery(query),
cursor,
});
Expand All @@ -79,15 +70,3 @@ export const initLogEntriesRoute = ({ framework, logEntries }: InfraBackendLibs)
}
);
};

function parseDate(date: ESDate): number | undefined {
if (typeof date === 'number') {
return date;
}

const parsedDate = datemath.parse(date);

if (parsedDate) {
return parsedDate.valueOf();
}
}

0 comments on commit b17b7c2

Please sign in to comment.