Skip to content
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
76 changes: 62 additions & 14 deletions packages/app/src/components/Slos/SloDefinitionFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
} from 'react-hook-form';
import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata';
import { buildSloAvailabilityCondition } from '@hyperdx/common-utils/dist/core/slo';
import { SloWindowSchema } from '@hyperdx/common-utils/dist/slo';
import {
SloTemplateSchema,
SloWindowSchema,
} from '@hyperdx/common-utils/dist/slo';
import { SourceKind, type TSource } from '@hyperdx/common-utils/dist/types';
import {
Button,
Expand All @@ -26,8 +29,10 @@ import SearchWhereInput from '@/components/SearchInput/SearchWhereInput';
import { SourceSelectControlled } from '@/components/SourceSelect';
import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor';

import { SloEligibleFilters } from './SloEligibleFilters';
import type { SloFormValues } from './sloForm';
import { SloFormRow } from './SloFormLayout';
import { SloLatencyFields } from './SloLatencyFields';
import { formatSloWindow } from './sloUtils';

const SLO_SOURCE_KINDS = [SourceKind.Trace, SourceKind.Log];
Expand All @@ -45,7 +50,10 @@ export function SloIndicatorFields({
setValue: UseFormSetValue<SloFormValues>;
source?: TSource;
}) {
const sourceId = useWatch({ control, name: 'source' });
const [sourceId, template] = useWatch({
control,
name: ['source', 'indicator.template'],
});
const { errors } = useFormState({ control, name: 'indicator.good' });
const tableConnection = tcFromSource(source);

Expand All @@ -67,6 +75,32 @@ export function SloIndicatorFields({

return (
<>
<SloFormRow
label="Type"
helpText="Availability: good events match a condition. Latency: good events finish within a threshold."
inline
>
<Controller
control={control}
name="indicator.template"
render={({ field }) => (
<Radio.Group
value={field.value}
onChange={value => {
const next = SloTemplateSchema.safeParse(value);
if (next.success) field.onChange(next.data);
}}
aria-label="Type"
data-testid="slo-template"
>
<Group>
<Radio value="availability" label="Availability" />
<Radio value="latency" label="Latency" />
</Group>
</Radio.Group>
)}
/>
</SloFormRow>
<SloFormRow label="Source">
{/* The select shows its own error. */}
<div data-testid="slo-source">
Expand All @@ -92,21 +126,35 @@ export function SloIndicatorFields({
lucenePlaceholder="For example ServiceName:checkout"
data-testid="slo-eligible-input"
/>
</SloFormRow>
<SloFormRow
label="Good events"
helpText="The SQL condition a good event matches. An event where it evaluates to NULL counts as bad."
>
<SQLInlineEditorControlled
tableConnection={tableConnection}
sourceId={sourceId}
<SloEligibleFilters
control={control}
name="indicator.good"
placeholder="For example StatusCode != 'Error'"
size="sm"
setValue={setValue}
source={source}
/>
<Input.Error mt={4}>{errors.indicator?.good?.message}</Input.Error>
</SloFormRow>
{template === 'latency' ? (
<SloLatencyFields
control={control}
getValues={getValues}
setValue={setValue}
source={source}
/>
) : (
<SloFormRow
label="Good events"
helpText="The SQL condition a good event matches. An event where it evaluates to NULL counts as bad."
>
<SQLInlineEditorControlled
tableConnection={tableConnection}
sourceId={sourceId}
control={control}
name="indicator.good"
placeholder="For example StatusCode != 'Error'"
size="sm"
/>
<Input.Error mt={4}>{errors.indicator?.good?.message}</Input.Error>
</SloFormRow>
)}
</>
);
}
Expand Down
134 changes: 134 additions & 0 deletions packages/app/src/components/Slos/SloEligibleFilters.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { useCallback, useMemo } from 'react';
import { Control, UseFormSetValue, useWatch } from 'react-hook-form';
import {
filterColumnMetaByType,
JSDataType,
} from '@hyperdx/common-utils/dist/clickhouse';
import { buildSearchChartConfig } from '@hyperdx/common-utils/dist/core/searchChartConfig';
import { describeFilter, parseQuery } from '@hyperdx/common-utils/dist/filters';
import type {
BuilderChartConfigWithDateRange,
Filter,
TSource,
} from '@hyperdx/common-utils/dist/types';
import { CloseButton, Code, Group, Text } from '@mantine/core';

import { ActiveFilterPills } from '@/components/ActiveFilterPills';
import { NOW } from '@/config';
import { useColumns } from '@/hooks/useMetadata';
import { useSearchPageFilterState } from '@/searchFilters';

import type { SloFormValues } from './sloForm';

const VALUE_PICKER_RANGE_MS = 60 * 60 * 1000;
// Stable, so a missing list does not hand the filter state a new array on
// every render.
const NO_FILTERS: Filter[] = [];

/** Filters the facet pills cannot show, such as raw SQL, kept as written. */
function isRawSloFilter(filter: Filter): boolean {
try {
return Object.keys(parseQuery([filter]).filters).length === 0;
} catch {
return true;
}
}

/**
* The eligible-event filters, shown and edited with Search's own pills, so
* filters carried over from Search stay visible in the SLO definition.
*/
export function SloEligibleFilters({
control,
setValue,
source,
}: {
control: Control<SloFormValues>;
setValue: UseFormSetValue<SloFormValues>;
source?: TSource;
}) {
const filters =
useWatch({ control, name: 'indicator.eligible.filters' }) ?? NO_FILTERS;
const { data: columns } = useColumns(
{
databaseName: source?.from.databaseName ?? '',
tableName: source?.from.tableName ?? '',
connectionId: source?.connection ?? '',
},
{ enabled: !!source },
);
const knownColumns = useMemo(
() => new Set((columns ?? []).map(c => c.name)),
[columns],
);
const jsonColumns = useMemo(
() =>
new Set(
(filterColumnMetaByType(columns ?? [], [JSDataType.JSON]) ?? []).map(
c => c.name,
),
),
[columns],
);
const onFilterChange = useCallback(
(next: Filter[]) =>
setValue('indicator.eligible.filters', next, { shouldDirty: true }),
[setValue],
);
const searchFilters = useSearchPageFilterState({
searchQuery: filters,
onFilterChange,
knownColumns,
jsonColumns,
isPreservedFilter: isRawSloFilter,
});

// Only used by the pill value picker to list a field's recent values.
const chartConfig = useMemo<BuilderChartConfigWithDateRange | undefined>(
() =>
source
? {
...buildSearchChartConfig(source, {
where: '',
whereLanguage: 'sql',
}),
dateRange: [new Date(NOW - VALUE_PICKER_RANGE_MS), new Date(NOW)],
}
: undefined,
[source],
);

const rawFilters = filters.filter(isRawSloFilter);
if (filters.length === 0) return null;

return (
<div data-testid="slo-eligible-filters">
{chartConfig && (
<ActiveFilterPills
searchFilters={searchFilters}
chartConfig={chartConfig}
mt={6}
/>
)}
{rawFilters.length > 0 && (
<Group gap={6} mt={6}>
{rawFilters.map(filter => (
<Group key={describeFilter(filter)} gap={2} wrap="nowrap">
<Code>{describeFilter(filter)}</Code>
<CloseButton
size="xs"
aria-label="Remove filter"
onClick={() =>
onFilterChange(filters.filter(f => f !== filter))
}
/>
</Group>
))}
</Group>
)}
<Text size="xs" c="dimmed" mt={4}>
Filters narrow the eligible events together with the where clause.
</Text>
</div>
);
}
Loading
Loading