Skip to content

Commit

Permalink
Rename tag -> control_tag in benchmark filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBurgess committed Feb 28, 2024
1 parent 23d58d3 commit c857404
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const DashboardTagGroupSelect = () => {
<Listbox.Button className="relative w-full bg-dashboard-panel border border-table-border rounded-md pl-3 pr-7 md:pr-10 py-2 text-left text-sm md:text-base cursor-pointer focus:ring-1 focus:ring-text-link">
{/*@ts-ignore*/}
<span className="block truncate">
<span className="hidden md:inline mr-1">Group by:</span>
<span className="hidden lg:inline mr-1">Group by:</span>
{value.label}
</span>
<span className="absolute inset-y-0 right-0 flex items-center pr-1 md:pr-2 pointer-events-none">
Expand Down
8 changes: 7 additions & 1 deletion ui/dashboard/src/components/SaveSnapshotButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { saveAs } from "file-saver";
import { timestampForFilename } from "@powerpipe/utils/date";
import { useDashboard } from "@powerpipe/hooks/useDashboard";
import { validateFilter } from "@powerpipe/components/dashboards/check/CheckFilterEditor";

const SaveSnapshotButton = () => {
const { dashboard, dataMode, selectedDashboard, snapshot } = useDashboard();
Expand All @@ -33,7 +34,12 @@ const SaveSnapshotButton = () => {
const metadata: DashboardSnapshotMetadata = {
view: {},
};
if (!!filterConfig) {
// If a benchmark
if (
dashboard.artificial &&
!!filterConfig &&
validateFilter(filterConfig)
) {
// @ts-ignore
metadata.view.filter_by = filterToSnapshotMetadata(filterConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const filterTestCases: TestCase[] = [
},
{
name: "valid filter with type and value",
input: { operator: "equal", type: "tag", value: "production" },
input: { operator: "equal", type: "control_tag", value: "production" },
expected: true,
},
{
Expand All @@ -40,7 +40,7 @@ const filterTestCases: TestCase[] = [
{ operator: "equal", type: "resource", value: "*mybucket*" },
{
operator: "equal",
type: "tag",
type: "control_tag",
key: "environment",
value: "production",
},
Expand All @@ -64,7 +64,7 @@ const filterTestCases: TestCase[] = [
// input: {
// or: [
// { type: "resource", value: "*mybucket*" },
// { type: "tag", key: "environment", value: "production" },
// { type: "control_tag", key: "environment", value: "production" },
// ],
// },
// expected: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@ const CheckFilterTypeSelect = ({
const allTypes: SelectOption[] = [
{ value: "benchmark", label: "Benchmark" },
{ value: "control", label: "Control" },
{ value: "control_tag", label: "Control Tag" },
{ value: "dimension", label: "Dimension" },
{ value: "reason", label: "Reason" },
{ value: "resource", label: "Resource" },
{ value: "severity", label: "Severity" },
{ value: "status", label: "Status" },
{ value: "tag", label: "Tag" },
];
return allTypes.filter(
(t) =>
t.value === type ||
t.value === "dimension" ||
t.value === "tag" ||
t.value === "control_tag" ||
// @ts-ignore
!existingTypes.includes(t.value),
);
Expand Down Expand Up @@ -337,7 +337,7 @@ const CheckFilterEditorItem = ({
update={update}
/>
</div>
{(item.type === "dimension" || item.type === "tag") && (
{(item.type === "dimension" || item.type === "control_tag") && (
<>
<span>=</span>
<div className="grow min-w-40 max-w-72">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type CheckGroupingTitleLabelProps = {

const CheckGroupingTitleLabel = ({ item }: CheckGroupingTitleLabelProps) => {
switch (item.type) {
case "control_tag":
case "dimension":
case "tag":
return (
<div className="space-x-1">
<span className="capitalize">{item.type}</span>
Expand Down Expand Up @@ -46,7 +46,7 @@ const CheckGroupingConfig = ({ onClose }: CheckGroupingConfigProps) => {
"grouping",
toSave
.map((c) =>
c.type === "dimension" || c.type === "tag"
c.type === "dimension" || c.type === "control_tag"
? `${c.type}|${c.value}`
: c.type,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ const CheckGroupingTypeSelect = ({
{ value: "result", label: "Result" },
{ value: "severity", label: "Severity" },
{ value: "status", label: "Status" },
{ value: "tag", label: "Tag" },
{ value: "control_tag", label: "Control Tag" },
];
return allTypes.filter(
(t) =>
t.value === type ||
t.value === "dimension" ||
t.value === "tag" ||
t.value === "control_tag" ||
// @ts-ignore
!existingTypes.includes(t.value),
);
Expand Down Expand Up @@ -192,7 +192,7 @@ const CheckGroupingEditorItem = ({
update={update}
/>
</div>
{(item.type === "dimension" || item.type === "tag") && (
{(item.type === "dimension" || item.type === "control_tag") && (
<>
<span>=</span>
<div className="grow">
Expand Down Expand Up @@ -246,7 +246,7 @@ const CheckGroupingEditor = ({ config, onApply }: CheckGroupingEditorProps) => {
case "status":
return !c.value;
case "dimension":
case "tag":
case "control_tag":
return !!c.value;
case "result":
return true;
Expand Down
6 changes: 3 additions & 3 deletions ui/dashboard/src/components/dashboards/check/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DashboardRunState } from "@powerpipe/types";
export type CheckNodeType =
| "benchmark"
| "control"
| "control_tag"
| "dimension"
| "empty_result"
| "error"
Expand All @@ -18,8 +19,7 @@ export type CheckNodeType =
| "running"
| "root"
| "severity"
| "status"
| "tag";
| "status";

export type CheckNode = {
sort: string;
Expand Down Expand Up @@ -117,8 +117,8 @@ export type CheckControlRun = {
export type CheckDisplayGroupType =
| "benchmark"
| "control"
| "control_tag"
| "result"
| "tag"
| "dimension"
| "reason"
| "resource"
Expand Down
26 changes: 15 additions & 11 deletions ui/dashboard/src/hooks/useCheckGrouping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ type CheckGroupFilterStatusValuesMap = {

export type CheckGroupFilterValues = {
status: CheckGroupFilterStatusValuesMap;
control_tag: { key: {}; value: {} };
dimension: { key: {}; value: {} };
tag: { key: {}; value: {} };
};

type ICheckGroupingContext = {
Expand Down Expand Up @@ -249,7 +249,7 @@ const getCheckGroupingKey = (
switch (group.type) {
case "dimension":
return getCheckDimensionGroupingKey(group.value, checkResult.dimensions);
case "tag":
case "control_tag":
return getCheckTagGroupingKey(group.value, checkResult.tags);
case "reason":
return getCheckReasonGroupingKey(checkResult.reason);
Expand Down Expand Up @@ -295,11 +295,11 @@ const getCheckGroupingNode = (
dimensionValue,
children,
);
case "tag":
case "control_tag":
const value = getCheckTagGroupingKey(group.value, checkResult.tags);
return new KeyValuePairNode(
value,
"tag",
"control_tag",
group.value || "Tag key not set",
value,
children,
Expand Down Expand Up @@ -579,7 +579,7 @@ function recordFilterValues(
reason: { value: {} };
resource: { value: {} };
control: { value: {} };
tag: { value: {}; key: {} };
control_tag: { value: {}; key: {} };
dimension: { value: {}; key: {} };
benchmark: { value: {} };
status: {
Expand Down Expand Up @@ -650,15 +650,19 @@ function recordFilterValues(

// Record the dimension keys/values + value/key counts of this check result to allow assisted filtering later
for (const [tagKey, tagValue] of Object.entries(checkResult.tags || {})) {
filterValues.tag.key[tagKey] = filterValues.tag.key[tagKey] || {
filterValues.control_tag.key[tagKey] = filterValues.control_tag.key[
tagKey
] || {
[tagValue]: 0,
};
filterValues.tag.key[tagKey][tagValue] += 1;
filterValues.control_tag.key[tagKey][tagValue] += 1;

filterValues.tag.value[tagValue] = filterValues.tag.value[tagValue] || {
filterValues.control_tag.value[tagValue] = filterValues.control_tag.value[
tagValue
] || {
[tagKey]: 0,
};
filterValues.tag.value[tagValue][tagKey] += 1;
filterValues.control_tag.value[tagValue][tagKey] += 1;
}
}

Expand Down Expand Up @@ -743,7 +747,7 @@ const includeResult = (
matches.push(matchesDimensions);
break;
}
case "tag": {
case "control_tag": {
// @ts-ignore
const keyRegex = new RegExp(`^${wildcardToRegex(filter.key)}$`);
let matchesTags = false;
Expand Down Expand Up @@ -777,12 +781,12 @@ const useGrouping = (
const filterValues = {
benchmark: { value: {} },
control: { value: {} },
control_tag: { key: {}, value: {} },
dimension: { key: {}, value: {} },
reason: { value: {} },
resource: { value: {} },
severity: { value: {} },
status: { alarm: 0, empty: 0, error: 0, info: 0, ok: 0, skip: 0 },
tag: { key: {}, value: {} },
};

if (!definition || skip || !panelsMap) {
Expand Down
8 changes: 4 additions & 4 deletions ui/dashboard/src/hooks/useCheckGroupingConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { useSearchParams } from "react-router-dom";
const groupingKeys = [
"benchmark",
"control",
"control_tag",
"dimension",
"reason",
"resource",
"result",
"severity",
"status",
"tag",
];

const useCheckGroupingConfig = () => {
Expand Down Expand Up @@ -54,9 +54,9 @@ const useCheckGroupingConfig = () => {
// { type: "severity" },
// { type: "dimension", value: "account_id" },
// { type: "dimension", value: "region" },
// { type: "tag", value: "service" },
// { type: "tag", value: "cis_type" },
// { type: "tag", value: "cis_level" },
// { type: "control_tag", value: "service" },
// { type: "control_tag", value: "cis_type" },
// { type: "control_tag", value: "cis_level" },
{ type: "benchmark" },
{ type: "control" },
{ type: "result" },
Expand Down
2 changes: 1 addition & 1 deletion ui/dashboard/src/hooks/useDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const DashboardProvider = ({
"grouping",
state.snapshot.metadata.view.group_by
.map((c) =>
c.type === "dimension" || c.type === "tag"
c.type === "dimension" || c.type === "control_tag"
? `${c.type}|${c.value}`
: c.type,
)
Expand Down
8 changes: 5 additions & 3 deletions ui/dashboard/src/utils/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,16 @@ describe("snapshot utils", () => {
});

it("should omit tag with no value", () => {
const input: CheckDisplayGroup[] = [{ type: "tag" }];
const input: CheckDisplayGroup[] = [{ type: "control_tag" }];
const expected = [];
expect(groupingToSnapshotMetadata(input)).toEqual(expected);
});

it("should handle tag with value", () => {
const input: CheckDisplayGroup[] = [{ type: "tag", value: "category" }];
const expected = [{ type: "tag", value: "category" }];
const input: CheckDisplayGroup[] = [
{ type: "control_tag", value: "category" },
];
const expected = [{ type: "control_tag", value: "category" }];
expect(groupingToSnapshotMetadata(input)).toEqual(expected);
});

Expand Down
5 changes: 4 additions & 1 deletion ui/dashboard/src/utils/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ const groupingToSnapshotMetadata = (

return grouping
.filter((g) => {
return !((g.type === "dimension" || g.type === "tag") && !g.value);
return !(
(g.type === "dimension" || g.type === "control_tag") &&
!g.value
);
})
.map((g) => {
const mapped: { type: CheckDisplayGroupType; value?: string } = {
Expand Down

0 comments on commit c857404

Please sign in to comment.