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

feat: introduce the decodeFilter function to refactor filter parsing #1005

Merged
merged 3 commits into from
Mar 1, 2024
Merged
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
2 changes: 1 addition & 1 deletion benchexec/tablegenerator/react-table/build/main.min.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import TaskFilterCard from "./TaskFilterCard";
import { faClose, faTrash } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import equals from "deep-equal";
import { isNil } from "../../utils/utils";
import { decodeFilter, isNil } from "../../utils/utils";
const classNames = require("classnames");

export default class FilterBox extends React.PureComponent {
Expand Down Expand Up @@ -70,12 +70,12 @@ export default class FilterBox extends React.PureComponent {
if (id === "id") {
continue;
}
const [tool, title, col] = id.split("_");
const {tool, name: title, column } = decodeFilter(id);
const toolArr = out[tool] || [];
if (!toolArr[col]) {
toolArr[col] = { title, values: [value] };
if (!toolArr[column]) {
toolArr[column] = { title, values: [value] };
} else {
toolArr[col].values.push(value);
toolArr[column].values.push(value);
}
out[tool] = toolArr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
setHashSearch,
getHashSearch,
getHiddenColIds,
decodeFilter,
} from "../utils/utils";
import deepEqual from "deep-equal";
import { statusForEmptyRows } from "../utils/filters";
Expand Down Expand Up @@ -168,7 +169,7 @@ const Table = (props) => {
let additionalFilters = [];

if (newFilter.type === "status") {
const [tool, name, column] = newFilter.id.split("_");
const { tool, name, column } = decodeFilter(newFilter.id);
const value = newFilter.value;

if (value.trim() === "all") {
Expand Down Expand Up @@ -629,7 +630,7 @@ const Table = (props) => {
if (id === "id") {
setDisableTaskText(!isNil(values));
}
const [runset, , column] = id.split("_");
const {tool: runset, column} = decodeFilter(id);
const currentRunsetFilters = newFilteredColumnValues[runset] || {};

const isCategory =
Expand Down
21 changes: 21 additions & 0 deletions benchexec/tablegenerator/react-table/src/tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
makeFilterSerializer,
makeFilterDeserializer,
splitUrlPathForMatchingPrefix,
decodeFilter
} from "../utils/utils";

describe("isStatusOk", () => {
Expand Down Expand Up @@ -118,6 +119,26 @@ describe("hashRouting helpers", () => {
});
});
});

describe("decodeFilter", () => {
test("should decode filter correctly", () => {
const filter = "0_cputime_1";
const expected = { tool: 0, name: "cputime", column: 1 };
expect(decodeFilter(filter)).toEqual(expected);
});

test("should handle empty filters", () => {
const filter = "__";
const expected = { tool: "", name: "", column: "" };
expect(decodeFilter(filter)).toEqual(expected);
});

test("should throw errors if there are not exactly two '_' in the filter id", () => {
expect(() => decodeFilter("0_cputime")).toThrow();
expect(() => decodeFilter("0_cputime_1_2")).toThrow();
});
})

describe("serialization", () => {
let serializer;
const statusValues = [
Expand Down
4 changes: 2 additions & 2 deletions benchexec/tablegenerator/react-table/src/utils/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
// SPDX-License-Identifier: Apache-2.0

import { isNil, getRawOrDefault, omit, isNumericColumn } from "./utils";
import { isNil, getRawOrDefault, omit, isNumericColumn, decodeFilter } from "./utils";
/* Status that will be used to identify whether empty rows should be shown. Currently,
filtering for either categories or statuses creates filters for the other one as well.
Since empty rows don't have a status, they will be filtered out all the time.
Expand Down Expand Up @@ -158,7 +158,7 @@ const buildMatcher = (filters) => {
acc.id = { value, values };
return acc;
}
const [tool, , columnIdx] = id.split("_");
const { tool, column: columnIdx } = decodeFilter(id);
if (value === "diff") {
// this branch is noop as of now
if (!acc.diff) {
Expand Down
18 changes: 17 additions & 1 deletion benchexec/tablegenerator/react-table/src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,22 @@ function makeStatusColumnFilter(
return statusColumnFilter.join(",");
}

/**
* Function to decode a filter ID string from the URL into its parts
* @param {String} filterID - The filter ID to be decoded
* @returns {Object} The decoded filter ID
* @throws {Error} If the filter ID is invalid
*/
export const decodeFilter = (filterID) => {
const splitedArray = filterID.split("_");
if (splitedArray.length !== 3) throw new Error("Invalid filter ID");
return {
tool: splitedArray[0],
name: splitedArray[1],
column: splitedArray[2]
};
}

const makeFilterSerializer =
({ statusValues: allStatusValues, categoryValues: allCategoryValues }) =>
(filter) => {
Expand All @@ -368,7 +384,7 @@ const makeFilterSerializer =
}
continue;
}
const [tool, name, column] = id.split("_");
const { tool, name, column } = decodeFilter(id);
const toolBucket = groupedFilters[tool] || {};
const columnBucket = toolBucket[column] || { name: escape(name) };

Expand Down
Loading