forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌊 Streams routing UI (elastic#201427)
First iteration of the streams partitioning/routing UI: <img width="1455" alt="Screenshot 2024-11-27 at 21 31 13" src="https://github.com/user-attachments/assets/9768b0a0-6143-41a2-9cc0-ed25bbf0d38b"> ## Changes ### Unified search bar An empty `FlexItem` would be rendered even if there is no query input which will stick around as a 320px wide empty element. This change avoids rendering this empty wrapper. ### Streams API * Add logic to extract fields (and their types) from a condition * Add logic to turn the streams condition dialect into query dsl * Add dot expander to the root logs pipeline to normalize incoming docs for consistent access in painless and querydsl (this is a stopgap solution) * Add some additional validation to incoming definitions * Add a sample API which takes a stream and a condition, turns the condition into querydsl and searches for a bunch of docs and returns their sources (also sets runtime mappings so non-mapped fields can be used in the condition) ### Streams app * Adjust page height based on whether the new nav is enabled * Add a condition editor to show and change conditions (can also be reused in other UIs) * Add UI to show child routing conditions, change existing routings, partition new streams and delete streams as well --------- Co-authored-by: Chris Cowan <chris@elastic.co> Co-authored-by: Dario Gieselaar <dario.gieselaar@elastic.co> Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
927bb4b
commit 4495e74
Showing
32 changed files
with
1,787 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
x-pack/plugins/streams/server/lib/streams/helpers/condition_fields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Condition, FilterCondition } from '../../../../common/types'; | ||
import { isAndCondition, isFilterCondition, isOrCondition } from './condition_guards'; | ||
|
||
export function isComplete(condition: Condition): boolean { | ||
if (isFilterCondition(condition)) { | ||
return condition.field !== undefined && condition.field !== ''; | ||
} | ||
if (isAndCondition(condition)) { | ||
return condition.and.every(isComplete); | ||
} | ||
if (isOrCondition(condition)) { | ||
return condition.or.every(isComplete); | ||
} | ||
return false; | ||
} | ||
|
||
export function getFields( | ||
condition: Condition | ||
): Array<{ name: string; type: 'number' | 'string' }> { | ||
const fields = collectFields(condition); | ||
// deduplicate fields, if mapped as string and number, keep as number | ||
const uniqueFields = new Map<string, 'number' | 'string'>(); | ||
fields.forEach((field) => { | ||
const existing = uniqueFields.get(field.name); | ||
if (existing === 'number') { | ||
return; | ||
} | ||
if (existing === 'string' && field.type === 'number') { | ||
uniqueFields.set(field.name, 'number'); | ||
return; | ||
} | ||
uniqueFields.set(field.name, field.type); | ||
}); | ||
|
||
return Array.from(uniqueFields).map(([name, type]) => ({ name, type })); | ||
} | ||
|
||
function collectFields(condition: Condition): Array<{ name: string; type: 'number' | 'string' }> { | ||
if (isFilterCondition(condition)) { | ||
return [{ name: condition.field, type: getFieldTypeForFilterCondition(condition) }]; | ||
} | ||
if (isAndCondition(condition)) { | ||
return condition.and.flatMap(collectFields); | ||
} | ||
if (isOrCondition(condition)) { | ||
return condition.or.flatMap(collectFields); | ||
} | ||
return []; | ||
} | ||
|
||
function getFieldTypeForFilterCondition(condition: FilterCondition): 'number' | 'string' { | ||
switch (condition.operator) { | ||
case 'gt': | ||
case 'gte': | ||
case 'lt': | ||
case 'lte': | ||
return 'number'; | ||
case 'neq': | ||
case 'eq': | ||
case 'exists': | ||
case 'contains': | ||
case 'startsWith': | ||
case 'endsWith': | ||
case 'notExists': | ||
return 'string'; | ||
default: | ||
return 'string'; | ||
} | ||
} | ||
|
||
export function validateCondition(condition: Condition) { | ||
if (isFilterCondition(condition)) { | ||
// check whether a field is specified | ||
if (!condition.field.trim()) { | ||
throw new Error('Field is required in conditions'); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
x-pack/plugins/streams/server/lib/streams/helpers/condition_guards.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { | ||
AndCondition, | ||
conditionSchema, | ||
FilterCondition, | ||
filterConditionSchema, | ||
OrCondition, | ||
} from '../../../../common/types'; | ||
|
||
export function isFilterCondition(subject: any): subject is FilterCondition { | ||
const result = filterConditionSchema.safeParse(subject); | ||
return result.success; | ||
} | ||
|
||
export function isAndCondition(subject: any): subject is AndCondition { | ||
const result = conditionSchema.safeParse(subject); | ||
return result.success && subject.and != null; | ||
} | ||
|
||
export function isOrCondition(subject: any): subject is OrCondition { | ||
const result = conditionSchema.safeParse(subject); | ||
return result.success && subject.or != null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
x-pack/plugins/streams/server/lib/streams/helpers/condition_to_query_dsl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Condition, FilterCondition } from '../../../../common/types'; | ||
import { isAndCondition, isFilterCondition, isOrCondition } from './condition_guards'; | ||
|
||
function conditionToClause(condition: FilterCondition) { | ||
switch (condition.operator) { | ||
case 'neq': | ||
return { bool: { must_not: { match: { [condition.field]: condition.value } } } }; | ||
case 'eq': | ||
return { match: { [condition.field]: condition.value } }; | ||
case 'exists': | ||
return { exists: { field: condition.field } }; | ||
case 'gt': | ||
return { range: { [condition.field]: { gt: condition.value } } }; | ||
case 'gte': | ||
return { range: { [condition.field]: { gte: condition.value } } }; | ||
case 'lt': | ||
return { range: { [condition.field]: { lt: condition.value } } }; | ||
case 'lte': | ||
return { range: { [condition.field]: { lte: condition.value } } }; | ||
case 'contains': | ||
return { wildcard: { [condition.field]: `*${condition.value}*` } }; | ||
case 'startsWith': | ||
return { prefix: { [condition.field]: condition.value } }; | ||
case 'endsWith': | ||
return { wildcard: { [condition.field]: `*${condition.value}` } }; | ||
case 'notExists': | ||
return { bool: { must_not: { exists: { field: condition.field } } } }; | ||
default: | ||
return { match_none: {} }; | ||
} | ||
} | ||
|
||
export function conditionToQueryDsl(condition: Condition): any { | ||
if (isFilterCondition(condition)) { | ||
return conditionToClause(condition); | ||
} | ||
if (isAndCondition(condition)) { | ||
const and = condition.and.map((filter) => conditionToQueryDsl(filter)); | ||
return { | ||
bool: { | ||
must: and, | ||
}, | ||
}; | ||
} | ||
if (isOrCondition(condition)) { | ||
const or = condition.or.map((filter) => conditionToQueryDsl(filter)); | ||
return { | ||
bool: { | ||
should: or, | ||
}, | ||
}; | ||
} | ||
return { | ||
match_none: {}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.