1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-06-04 01:18:20 +02:00
unleash.unleash/src/lib/features/feature-search/search-utils.ts
Thomas Heartman 8da201aed8
feat: add potentiallyStale filter (#8784)
This PR adds support for the `potentiallyStale` value in the feature
search API. The value is added as a third option for `state` (in
addition to `stale` and `active`). Potentially stale is a subset of
active flags, so stale flags are never considered potentially stale,
even if they have the flag set in the db.

Because potentially stale is a separate column in the db, this
complicates the query a bit. As such, I've created a specialized
handling function in the feature search store: if the query doesn't
include `potentiallyStale`, handle it as we did before (the mapping has
just been moved). If the query *does* contain potentially stale, though,
the handling is quite a bit more involved because we need to check
multiple different columns against each other.

In essence, it's based on this logic:

when you’re searching for potentially stale flags, you should only get flags that are active and marked as potentially stale. You should not get stale flags.

This can cause some confusion, because in the db, we don’t clear the potentially stale status when we mark a flag as stale, so we can get flags that are both stale and potentially stale.

However, as a user, if you’re looking for potentially stale flags, I’d be surprised to also get (only some) stale flags, because if a flag is stale, it’s definitely stale, not potentially stale.

This leads us to these six different outcomes we need to handle when your search includes potentially stale and stale or active:

1. You filter for “potentially stale” flags only. The API will give you only flags that are active and marked as potentially stale. You will not get stale flags.
2. You filter only for flags that are not potentially stale. You will get all flags that are active and not potentially stale and all stale flags.
3. You search for “is any of stale, potentially stale”. This is our “unhealthy flags” metric. You get all stale flags and all flags that are active and potentially stale
4. You search for “is none of stale, potentially stale”: This gives you all flags that are active and not potentially stale. Healthy flags, if you will.
5. “is any of active, potentially stale”: you get all active flags. Because we treat potentially stale as a subset of active, this is the same as “is active”
6. “is none of active, potentially stale”: you get all stale flags. As in the previous point, this is the same as “is not active”
2024-11-19 14:53:01 +01:00

125 lines
3.6 KiB
TypeScript

import type { Knex } from 'knex';
import type {
IQueryOperator,
IQueryParam,
} from '../feature-toggle/types/feature-toggle-strategies-store-type';
export interface NormalizeParamsDefaults {
limitDefault: number;
maxLimit?: number; // Optional because you might not always want to enforce a max limit
typeDefault?: string; // Optional field for type, not required for every call
}
export type SearchParams = {
query?: string;
offset?: string | number;
limit?: string | number;
sortOrder?: 'asc' | 'desc';
};
export type NormalizedSearchParams = {
normalizedQuery?: string[];
normalizedLimit: number;
normalizedOffset: number;
normalizedSortOrder: 'asc' | 'desc';
};
export const applySearchFilters = (
qb: Knex.QueryBuilder,
searchParams: string[] | undefined,
columns: string[],
): void => {
const hasSearchParams = searchParams?.length;
if (hasSearchParams) {
const sqlParameters = searchParams.map((item) => `%${item}%`);
const sqlQueryParameters = sqlParameters.map(() => '?').join(',');
qb.where((builder) => {
columns.forEach((column) => {
builder.orWhereRaw(
`(${column}) ILIKE ANY (ARRAY[${sqlQueryParameters}])`,
sqlParameters,
);
});
});
}
};
export const applyGenericQueryParams = (
query: Knex.QueryBuilder,
queryParams: IQueryParam[],
): void => {
queryParams.forEach((param) => {
const isSingleParam = param.values.length === 1;
switch (param.operator) {
case 'IS':
case 'IS_ANY_OF':
query.whereIn(param.field, param.values);
break;
case 'IS_NOT':
case 'IS_NONE_OF':
if (isSingleParam) {
query.whereNot(param.field, param.values[0]);
} else {
query.whereNotIn(param.field, param.values);
}
break;
case 'IS_BEFORE':
query.where(param.field, '<', param.values[0]);
break;
case 'IS_ON_OR_AFTER':
query.where(param.field, '>=', param.values[0]);
break;
}
});
};
export const normalizeQueryParams = (
params: SearchParams,
defaults: NormalizeParamsDefaults,
): NormalizedSearchParams => {
const { query, offset, limit = defaults.limitDefault, sortOrder } = params;
const normalizedQuery = query
?.split(',')
.map((query) => query.trim())
.filter((query) => query);
const maxLimit = defaults.maxLimit || 1000;
const normalizedLimit =
Number(limit) > 0 && Number(limit) <= maxLimit
? Number(limit)
: defaults.limitDefault;
const normalizedOffset = Number(offset) > 0 ? Number(offset) : 0;
const normalizedSortOrder =
sortOrder === 'asc' || sortOrder === 'desc' ? sortOrder : 'asc';
return {
normalizedQuery,
normalizedLimit,
normalizedOffset,
normalizedSortOrder,
};
};
export const parseSearchOperatorValue = (
field: string,
value: string,
): IQueryParam | null => {
const pattern =
/^(IS|IS_NOT|IS_ANY_OF|IS_NONE_OF|INCLUDE|DO_NOT_INCLUDE|INCLUDE_ALL_OF|INCLUDE_ANY_OF|EXCLUDE_IF_ANY_OF|EXCLUDE_ALL|IS_BEFORE|IS_ON_OR_AFTER):(.+)$/;
const match = value.match(pattern);
if (match) {
return {
field,
operator: match[1] as IQueryOperator,
values: match[2].split(',').map((value) => value.trim()),
};
}
return null;
};