Skip to content

Commit

Permalink
feat: add filter by config tags for changes
Browse files Browse the repository at this point in the history
Fixes #1961

chore: remove console logs
  • Loading branch information
mainawycliffe committed Jun 7, 2024
1 parent 179023b commit 6bc5631
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 16 deletions.
5 changes: 1 addition & 4 deletions src/api/query-hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,7 @@ export function useGetConfigTagsListQuery(
return useQuery({
queryKey: ["configs", "tags", "list"],
queryFn: async () => {
const { error, data } = await getConfigTagsList();
if (error) {
throw error;
}
const { data } = await getConfigTagsList();
return data ?? [];
},
...options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ import {
getConfigsChanges
} from "../services/configs";

function useConfigChangesTagsFilter() {
const [params] = useSearchParams();

const tags = useMemo(() => {
const allTags = params.get("tags");
if (allTags) {
return allTags.split(",").map((tag) => {
const prefix = tag.split(":")[1] === "-1" ? "!" : ""; // expected: `key____value:-1`
const key = `${prefix}${tag.split("____")[0]}`; // expected: `key____value:-1`
const value = tag.split("____")[1].split(":")[0]; // expected: `key____value:-1`
return {
key,
value
} satisfies Required<GetConfigsRelatedChangesParams>["tags"][number];
});
}
}, [params]);

return tags;
}

export function useGetAllConfigsChangesQuery(
queryOptions: UseQueryOptions<CatalogChangesSearchResponse> = {
enabled: true,
Expand Down Expand Up @@ -38,6 +59,8 @@ export function useGetAllConfigsChangesQuery(
const createdBy = params.get("created_by") ?? undefined;
const externalCreatedBy = params.get("external_created_by") ?? undefined;

const tags = useConfigChangesTagsFilter();

const arbitraryFilter = useMemo(() => {
const filter = new Map<string, string>();
if (configId) {
Expand Down Expand Up @@ -70,7 +93,8 @@ export function useGetAllConfigsChangesQuery(
sortOrder: sortDirection === "desc" ? "desc" : "asc",
page: page,
pageSize: pageSize,
arbitraryFilter
arbitraryFilter,
tags
} satisfies GetConfigsRelatedChangesParams;

return useQuery({
Expand Down Expand Up @@ -108,6 +132,8 @@ export function useGetConfigChangesByIDQuery(
const downstream = params.get("downstream") === "true";
const all = upstream && downstream;

const tags = useConfigChangesTagsFilter();

const relationshipType = useMemo(() => {
if (all) {
return "all";
Expand All @@ -133,7 +159,8 @@ export function useGetConfigChangesByIDQuery(
sortBy,
sortOrder: sortDirection === "desc" ? "desc" : "asc",
page: page,
pageSize: pageSize
pageSize: pageSize,
tags
} satisfies GetConfigsRelatedChangesParams;

return useQuery({
Expand Down
19 changes: 14 additions & 5 deletions src/api/services/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ export type ConfigsTagList = {
value: any;
};

export const getConfigTagsList = () =>
resolve<ConfigsTagList[] | null>(ConfigDB.get(`/config_tags`));
export const getConfigTagsList = () => {
return ConfigDB.get<ConfigsTagList[] | null>(`/config_tags`);
};

export const getConfigLabelsList = () =>
resolve<ConfigsTagList[] | null>(ConfigDB.get(`/config_labels`));
Expand Down Expand Up @@ -211,6 +212,10 @@ export type GetConfigsRelatedChangesParams = {
sortBy?: string;
sortOrder?: "asc" | "desc";
arbitraryFilter?: Record<string, string>;
tags?: {
key: string;
value: string;
}[];
};

export async function getConfigsChanges({
Expand All @@ -227,7 +232,8 @@ export async function getConfigsChanges({
pageSize,
sortBy,
sortOrder,
arbitraryFilter
arbitraryFilter,
tags
}: GetConfigsRelatedChangesParams) {
const queryParams = new URLSearchParams();
if (id) {
Expand All @@ -253,7 +259,7 @@ export async function getConfigsChanges({
}
if (arbitraryFilter) {
Object.entries(arbitraryFilter).forEach(([key, value]) => {
const filterExpression = tristateOutputToQueryParamValue(value);
const filterExpression = tristateOutputToQueryParamValue(value, false);
if (filterExpression) {
queryParams.set(key, filterExpression);
}
Expand All @@ -276,7 +282,10 @@ export async function getConfigsChanges({
// for descending order, we need to add a "-" before the sortBy field
queryParams.set("sort_by", `${sortOrder === "desc" ? "-" : ""}${sortBy}`);
}

if (tags) {
const tagList = tags.map(({ key, value }) => `${key}=${value}`);
queryParams.set("tags", tagList.join(","));
}
if (type_filter) {
queryParams.set(
"recursive",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { paramsToReset } from "../ConfigChangeHistory";
import { ChangesTypesDropdown } from "./ChangeTypesDropdown";
import { ConfigChangeSeverity } from "./ConfigChangeSeverity";
import ConfigChangesDateRangeFilter from "./ConfigChangesDateRangeFIlter";
import { ConfigTagsDropdown } from "./ConfigTagsDropdown";
import ConfigTypesTristateDropdown from "./ConfigTypesTristateDropdown";

type FilterBadgeProps = {
Expand Down Expand Up @@ -82,7 +83,7 @@ export function ConfigChangeFilters({
<div className="flex flex-col gap-2">
<FormikFilterForm
paramsToReset={paramsToReset}
filterFields={["configTypes", "changeType", "severity"]}
filterFields={["configTypes", "changeType", "severity", "tags"]}
defaultFieldValues={{
...(configType && { configTypes: defaultConfigType })
}}
Expand All @@ -91,6 +92,7 @@ export function ConfigChangeFilters({
<ConfigTypesTristateDropdown />
<ChangesTypesDropdown />
<ConfigChangeSeverity />
<ConfigTagsDropdown />
<ConfigChangesDateRangeFilter paramsToReset={paramsToReset} />
</div>
</FormikFilterForm>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { useGetConfigTagsListQuery } from "@flanksource-ui/api/query-hooks";
import TristateReactSelect, {
TriStateOptions
} from "@flanksource-ui/ui/Dropdowns/TristateReactSelect";
import { useField } from "formik";
import { useMemo } from "react";

type Props = {
searchParamKey?: string;
};

export function ConfigTagsDropdown({ searchParamKey = "tags" }: Props) {
const [field] = useField({
name: searchParamKey
});

const { data, isLoading } = useGetConfigTagsListQuery();

const tagItems = useMemo(() => {
if (data) {
return data.map(
(tag) =>
({
id: `${tag.key}____${tag.value}`,
label: (
<div className="block space-x-1 text-sm">
<span className="w-auto text-gray-600">{tag.key}:</span>
<span className="w-full">{tag.value}</span>
</div>
),
value: `${tag.key}____${tag.value}`
} satisfies TriStateOptions)
);
}
}, [data]);

return (
<TristateReactSelect
options={tagItems ?? []}
onChange={(value) => {
if (value) {
field.onChange({
target: { name: searchParamKey, value: value }
});
} else {
field.onChange({
target: { name: searchParamKey, value: undefined }
});
}
}}
value={field.value}
className="w-auto max-w-[38rem]"
label={"Tags"}
isLoading={isLoading}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ChangesTypesDropdown } from "../../ConfigChangesFilters/ChangeTypesDrop
import { ConfigChangeSeverity } from "../../ConfigChangesFilters/ConfigChangeSeverity";
import ConfigChangesDateRangeFilter from "../../ConfigChangesFilters/ConfigChangesDateRangeFIlter";
import { ConfigRelatedChangesToggles } from "../../ConfigChangesFilters/ConfigRelatedChangesToggles";
import { ConfigTagsDropdown } from "../../ConfigChangesFilters/ConfigTagsDropdown";
import ConfigTypesTristateDropdown from "../../ConfigChangesFilters/ConfigTypesTristateDropdown";
import { ConfigChangesToggledDeletedItems } from "./ConfigChangesToggledDeletedItems";

Expand All @@ -25,6 +26,7 @@ export function ConfigRelatedChangesFilters({
<ConfigTypesTristateDropdown />
<ChangesTypesDropdown />
<ConfigChangeSeverity />
<ConfigTagsDropdown />
<ConfigRelatedChangesToggles />
<ConfigChangesDateRangeFilter paramsToReset={paramsToReset} />
<ConfigChangesToggledDeletedItems />
Expand Down
2 changes: 1 addition & 1 deletion src/pages/config/ConfigChangesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGetAllConfigsChangesQuery } from "@flanksource-ui/api/query-hooks/useGetConfigsChangesQuery";
import { useGetAllConfigsChangesQuery } from "@flanksource-ui/api/query-hooks/useConfigChangesHooks";
import { ConfigChangeHistory } from "@flanksource-ui/components/Configs/Changes/ConfigChangeHistory";
import { ConfigChangeFilters } from "@flanksource-ui/components/Configs/Changes/ConfigChangesFilters/ConfigChangesFilters";
import ConfigPageTabs from "@flanksource-ui/components/Configs/ConfigPageTabs";
Expand Down
2 changes: 1 addition & 1 deletion src/pages/config/details/ConfigDetailsChangesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGetConfigChangesByIDQuery } from "@flanksource-ui/api/query-hooks/useGetConfigsChangesQuery";
import { useGetConfigChangesByIDQuery } from "@flanksource-ui/api/query-hooks/useConfigChangesHooks";
import { ConfigChangeHistory } from "@flanksource-ui/components/Configs/Changes/ConfigChangeHistory";
import { ConfigRelatedChangesFilters } from "@flanksource-ui/components/Configs/Changes/ConfigsRelatedChanges/FilterBar/ConfigRelatedChangesFilters";
import { ConfigDetailsTabs } from "@flanksource-ui/components/Configs/ConfigDetailsTabs";
Expand Down
4 changes: 2 additions & 2 deletions src/ui/Dropdowns/TristateReactSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { Tooltip } from "react-tooltip";

export type TriStateOptions = {
id: string;
label: string;
label: React.ReactNode;
icon?: any;
value: string;
};
Expand Down Expand Up @@ -200,7 +200,7 @@ function ReactSelectTriStateSingleValue({
data-tooltip-content={`${
+value === 1 ? "Include" : "Exclude"
} ${v.label}`}
key={v.label}
key={v.id}
className={clsx(
"w-auto flex flex-row items-center gap-1 px-2 py-1 max-w-full overflow-hidden text-ellipsis text-nowrap bg-gray-200"
)}
Expand Down

1 comment on commit 6bc5631

@moshloop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the tags the expression should be label!=value instead of !label=value

Please sign in to comment.