Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Streams 🌊] Fix _sample API condition param and missing typing on processor forms #207213

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -12,12 +12,13 @@ import { CodeEditor } from '@kbn/code-editor';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { useKibana } from '../../../../hooks/use_kibana';
import { ProcessorFormState } from '../../types';

export const DissectPatternDefinition = () => {
const { core } = useKibana();
const esDocUrl = core.docLinks.links.ingest.dissectKeyModifiers;

const { field, fieldState } = useController({
const { field, fieldState } = useController<ProcessorFormState, 'pattern'>({
name: 'pattern',
rules: {
required: i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import { useController } from 'react-hook-form';
import { EuiFormRow } from '@elastic/eui';
import { CodeEditor } from '@kbn/code-editor';
import { i18n } from '@kbn/i18n';
import { ProcessorFormState } from '../../types';

export const GrokPatternDefinition = () => {
const { field, fieldState } = useController({ name: 'pattern_definitions' });
const { field, fieldState } = useController<ProcessorFormState, 'pattern_definitions'>({
name: 'pattern_definitions',
});

return (
<EuiFormRow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ const DraggablePatternInput = ({
color="transparent"
paddingSize="xs"
{...provided.dragHandleProps}
aria-label="Drag Handle"
aria-label={i18n.translate(
'xpack.streams.streamDetailView.managementTab.enrichment.processorFlyout.grokEditor.dragHandleLabel',
{ defaultMessage: 'Drag Handle' }
)}
>
<EuiIcon type="grab" />
</EuiPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import React from 'react';
import { useController } from 'react-hook-form';
import { ConditionEditor } from '../../condition_editor';
import { ProcessorFormState } from '../types';

export const ProcessorConditionEditor = () => {
const { field } = useController({ name: 'condition' });
const { field } = useController<ProcessorFormState, 'condition'>({ name: 'condition' });

return <ConditionEditor condition={field.value} onConditionChange={field.onChange} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { EuiFormRow, EuiFieldText } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import React from 'react';
import { useController } from 'react-hook-form';
import { ProcessorFormState } from '../types';

export const ProcessorFieldSelector = () => {
const { field, fieldState } = useController({
const { field, fieldState } = useController<ProcessorFormState, 'field'>({
name: 'field',
rules: {
required: i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
ReadStreamDefinition,
isWiredReadStream,
} from '@kbn/streams-schema';
import { useController, useFieldArray } from 'react-hook-form';
import { UseControllerProps, useController, useFieldArray } from 'react-hook-form';
import { css } from '@emotion/react';
import { flattenObject } from '@kbn/object-utils';
import { IHttpFetchError, ResponseErrorBody } from '@kbn/core/public';
Expand Down Expand Up @@ -287,17 +287,19 @@ const DetectedFields = ({ detectedFields }: { detectedFields: DetectedField[] })
>
<EuiFlexGroup gutterSize="s" wrap>
{fields.map((field, id) => (
<DetectedFieldSelector key={field.name} selectorId={`detected_fields.${id}`} />
<DetectedFieldSelector key={field.name} name={`detected_fields.${id}`} />
))}
</EuiFlexGroup>
</EuiFormRow>
);
};

const DetectedFieldSelector = ({ selectorId }: { selectorId: string }) => {
const { field } = useController({ name: selectorId });
const DetectedFieldSelector = (
props: UseControllerProps<ProcessorFormState, `detected_fields.${number}`>
) => {
const { field } = useController(props);

const options = useMemo(() => getDetectedFieldSelectOptions(field.value), [field]);
const options = useMemo(() => getDetectedFieldSelectOptions(field.value), [field.value]);

return (
<EuiSuperSelect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useController, useFormContext, useWatch } from 'react-hook-form';
import { ProcessorType } from '@kbn/streams-schema';
import { useKibana } from '../../../hooks/use_kibana';
import { getDefaultFormState } from '../utils';
import { ProcessorFormState } from '../types';

interface TAvailableProcessor {
value: ProcessorType;
Expand All @@ -28,8 +29,11 @@ export const ProcessorTypeSelector = ({
const { core } = useKibana();
const esDocUrl = core.docLinks.links.elasticsearch.docsBase;

const { control, reset } = useFormContext();
const { field, fieldState } = useController({ name: 'type', control, rules: { required: true } });
const { reset } = useFormContext();
const { field, fieldState } = useController<ProcessorFormState, 'type'>({
name: 'type',
rules: { required: true },
});

const processorType = useWatch<{ type: ProcessorType }>({ name: 'type' });

Expand Down Expand Up @@ -74,7 +78,9 @@ const availableProcessors: TAvailableProcessors = {
values={{
dissectLink: (
<EuiLink external target="_blank" href={esDocUrl + 'dissect-processor.html'}>
dissect
{i18n.translate('xpack.streams.availableProcessors.dissectLinkLabel', {
defaultMessage: 'dissect',
})}
</EuiLink>
),
}}
Expand All @@ -91,7 +97,9 @@ const availableProcessors: TAvailableProcessors = {
values={{
grokLink: (
<EuiLink external target="_blank" href={esDocUrl + 'grok-processor.html'}>
grok
{i18n.translate('xpack.streams.availableProcessors.grokLinkLabel', {
defaultMessage: 'grok',
})}
</EuiLink>
),
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
import React from 'react';
import { useController } from 'react-hook-form';
import { EuiFormRow, EuiFormRowProps, EuiSwitch, htmlIdGenerator } from '@elastic/eui';
import { ProcessorFormState } from '../types';

type ExtractBooleanFields<TInput> = NonNullable<
{
[K in keyof TInput]: boolean extends TInput[K] ? K : never;
}[keyof TInput]
>;

interface ToggleFieldProps {
helpText?: EuiFormRowProps['helpText'];
id?: string;
label: string;
name: string;
name: ExtractBooleanFields<ProcessorFormState>;
}

export const ToggleField = ({
Expand All @@ -23,14 +30,16 @@ export const ToggleField = ({
name,
...rest
}: ToggleFieldProps) => {
const { field } = useController({ name });
const { field } = useController<ProcessorFormState, ToggleFieldProps['name']>({
name,
});

return (
<EuiFormRow helpText={helpText} fullWidth describedByIds={id ? [id] : undefined} {...rest}>
<EuiSwitch
id={id}
label={label}
checked={field.value}
checked={field.value ?? false}
onChange={(e) => field.onChange(e.target.checked)}
/>
</EuiFormRow>
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