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

move dataset_split and task filter to system table header #528

Merged
merged 3 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
58 changes: 58 additions & 0 deletions frontend/src/components/SystemsTable/SystemTableContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import { SystemModel } from "../../models";
import { DeleteOutlined, EditOutlined } from "@ant-design/icons";
import { PrivateIcon, useUser } from "..";
import { generateLeaderboardURL } from "../../utils";
import type { FilterValue, SorterResult } from "antd/lib/table/interface";
import type { TablePaginationConfig } from "antd/lib/table";
import { FilterUpdate, SystemFilter } from "./SystemFilter";
import { TaskCategory } from "../../clients/openapi";
const { Text } = Typography;

interface Props {
Expand All @@ -30,6 +34,9 @@ interface Props {
setSelectedSystemIDs: React.Dispatch<React.SetStateAction<string[]>>;
onActiveSystemChange: (ids: string[]) => void;
showEditDrawer: (systemIDToEdit: string) => void;
onFilterChange: (value: FilterUpdate) => void;
filterValue: SystemFilter;
taskCategories: TaskCategory[];
}

export function SystemTableContent({
Expand All @@ -44,6 +51,9 @@ export function SystemTableContent({
setSelectedSystemIDs,
onActiveSystemChange,
showEditDrawer,
onFilterChange,
filterValue,
taskCategories,
}: Props) {
const { userInfo } = useUser();
const metricColumns: ColumnsType<SystemModel> = metricNames.map((metric) => ({
Expand All @@ -70,6 +80,12 @@ export function SystemTableContent({
}
}

const taskFilterList = taskCategories.flatMap((category) => {
return category.tasks.map((task) => {
return { text: task.name, value: task.name };
});
});

const columns: ColumnsType<SystemModel> = [
{
dataIndex: "idx",
Expand Down Expand Up @@ -100,6 +116,9 @@ export function SystemTableContent({
fixed: "left",
title: "Task",
render: (value) => <Tag style={{ whiteSpace: "normal" }}>{value}</Tag>,
filters: taskFilterList,
filterMultiple: false,
filteredValue: filterValue.task ? [filterValue.task] : null,
},
{
dataIndex: "dataset_name",
Expand Down Expand Up @@ -132,6 +151,13 @@ export function SystemTableContent({
title: "Dataset Split",
fixed: "left",
align: "center",
filterMultiple: false,
filters: [
{ text: "train", value: "train" },
{ text: "validation", value: "validation" },
{ text: "test", value: "test" },
],
filteredValue: filterValue.split ? [filterValue.split] : null,
},
{
dataIndex: "source_language",
Expand Down Expand Up @@ -220,6 +246,37 @@ export function SystemTableContent({
},
};

const handleTableChange = (
_pagination: TablePaginationConfig,
filters: Record<string, FilterValue | null>,
_sorter: SorterResult<SystemModel> | SorterResult<SystemModel>[]
) => {
console.log(filters);
Copy link
Member

Choose a reason for hiding this comment

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

Could you please remove the console log?

for (const k in filters) {
Copy link
Member

@lyuyangh lyuyangh Nov 25, 2022

Choose a reason for hiding this comment

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

This for loop has a couple of minor bugs/typos. I think we can fix them and simplify the code to something like this

const filterUpdate: Partial<SystemFilter> = {};
const dataset_split = filters["dataset.split"]
  ? (filters["dataset.split"][0] as string)
  : undefined;
if (dataset_split !== filterValue.split) {
  filterUpdate.split = dataset_split;
}
const task = filters["task"] ? (filters["task"][0] as string) : undefined;
if (task !== filterValue.task) {
  filterUpdate.task = task;
}
onFilterChange(filterUpdate);

Note that I changed the following things:

  1. I don't think we need a for loop here. Loops are for repetitive things but we want to handle each key differently here.
  2. filters["task"] and filters["dataset.split"] are arrays if the filters are active. I don't think we can typecast them to string.
  3. To check whether the new filter value is different from the current one, I converted filters[key] to the value we want with an assignment and then this check is just an equality check.
  4. onFilterChange() is only called once at the end. This makes it easier to track the states because filterValue is only updated once.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thank you very much! This looks much clearer. I add another boolean flag before onFilterChange because onFilterChange will set the pagination to page one even when filterUpdate is empty.

if (
k === "dataset.split" &&
((filters[k] === null && filterValue.split !== undefined) || // Reset filter
(filters[k] !== null &&
(filters[k] as unknown as string) !== filterValue.split))
) {
onFilterChange({
split:
filters[k] === null ? undefined : (filters[k] as unknown as string),
});
} else if (
k === "task" &&
((filters[k] === null && filterValue.split !== undefined) || // Reset filter
(filters[k] !== null &&
(filters[k] as unknown as string) !== filterValue.split))
) {
onFilterChange({
task:
filters[k] === null ? undefined : (filters[k] as unknown as string),
});
}
}
};

return (
<div>
<Table
Expand All @@ -237,6 +294,7 @@ export function SystemTableContent({
onChange: (newPage, newPageSize) =>
onPageChange(newPage - 1, newPageSize),
}}
onChange={handleTableChange}
sticky={false}
loading={loading}
scroll={{ x: 100 }}
Expand Down
19 changes: 0 additions & 19 deletions frontend/src/components/SystemsTable/SystemTableTools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
Popconfirm,
Radio,
} from "antd";
import { TaskSelect } from "..";
import { TaskCategory } from "../../clients/openapi";
import {
ArrowDownOutlined,
Expand Down Expand Up @@ -208,24 +207,6 @@ export function SystemTableTools({
</Space>
<Space style={{ width: "fit-content", float: "right" }}>
{mineVsAllSystemsToggle}
<Select
options={["test", "validation", "train", "all"].map((opt) => ({
value: opt,
label: opt,
}))}
value={value.split || undefined}
placeholder="Dataset split"
onChange={(value) => onChange({ split: value })}
style={{ minWidth: "120px" }}
/>
<TaskSelect
taskCategories={taskCategories}
allowClear
value={value.task || undefined}
onChange={(value) => onChange({ task: value || "" })}
placeholder="All Tasks"
style={{ minWidth: "150px" }}
/>
<div style={{ display: "flex", flexDirection: "row" }}>
<Select
options={[
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/SystemsTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ export function SystemsTable() {
setSelectedSystemIDs={setSelectedSystemIDs}
onActiveSystemChange={onActiveSystemChange}
showEditDrawer={showEditDrawer}
onFilterChange={onFilterChange}
filterValue={filters}
taskCategories={taskCategories}
/>
<AnalysisDrawer
systems={systems.filter((sys) =>
Expand Down