Skip to content

Commit

Permalink
fix(streams): update sample API to handle unexisting condition
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyghiani committed Jan 20, 2025
1 parent f0292b5 commit 66c3b82
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const sampleStreamRoute = createServerRoute({
condition: z.optional(conditionSchema),
start: z.optional(z.number()),
end: z.optional(z.number()),
number: z.optional(z.number()),
size: z.optional(z.number()),
}),
}),
handler: async ({ params, request, getScopedClients }): Promise<{ documents: unknown[] }> => {
Expand All @@ -146,18 +146,20 @@ export const sampleStreamRoute = createServerRoute({
if (!read) {
throw new DefinitionNotFound(`Stream definition for ${params.path.id} not found.`);
}

const { condition, start, end, size } = params.body;
const searchBody = {
query: {
bool: {
must: [
isCompleteCondition(params.body.condition)
? conditionToQueryDsl(params.body.condition)
Boolean(condition && isCompleteCondition(condition))
? conditionToQueryDsl(condition)
: { match_all: {} },
{
range: {
'@timestamp': {
gte: params.body.start,
lte: params.body.end,
gte: start,
lte: end,
format: 'epoch_millis',
},
},
Expand All @@ -170,20 +172,22 @@ export const sampleStreamRoute = createServerRoute({
// ingest in the painless condition checks.
// This is less efficient than it could be - in some cases, these fields _are_ indexed with the right type and we could use them directly.
// This can be optimized in the future.
runtime_mappings: Object.fromEntries(
getFields(params.body.condition).map((field) => [
field.name,
{ type: field.type === 'string' ? 'keyword' : 'double' },
])
),
runtime_mappings: condition
? Object.fromEntries(
getFields(condition).map((field) => [
field.name,
{ type: field.type === 'string' ? 'keyword' : 'double' },
])
)
: undefined,
sort: [
{
'@timestamp': {
order: 'desc',
},
},
],
size: params.body.number,
size,
};
const results = await scopedClusterClient.asCurrentUser.search({
index: params.path.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const useProcessingSimulator = ({
condition,
start: start?.valueOf(),
end: end?.valueOf(),
number: 100,
size: 100,
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ function PreviewPanel({
condition: routingAppState.debouncedChildUnderEdit.child.condition,
start: start?.valueOf(),
end: end?.valueOf(),
number: 100,
size: 100,
},
},
});
Expand Down

0 comments on commit 66c3b82

Please sign in to comment.