diff --git a/packages/diracx-web-components/components/JobMonitor/JobDataService.ts b/packages/diracx-web-components/components/JobMonitor/JobDataService.ts index caa4b18c..623be8fd 100644 --- a/packages/diracx-web-components/components/JobMonitor/JobDataService.ts +++ b/packages/diracx-web-components/components/JobMonitor/JobDataService.ts @@ -1,8 +1,23 @@ import useSWR, { mutate } from "swr"; import { fetcher } from "@/hooks/utils"; -import { Filter } from "@/types"; import dayjs from "dayjs"; +function processSearchBody(searchBody: any) { + searchBody.search = searchBody.search?.map((filter: any) => { + if (filter.operator == "last") { + return { + parameter: filter.parameter, + operator: "gt", + value: dayjs() + .subtract(1, filter.value as "hour" | "day" | "month" | "year") + .toISOString(), + values: filter.values, + }; + } + return filter; + }); +} + /** * Custom hook for fetching jobs data. * @@ -18,19 +33,7 @@ export const useJobs = ( page: number, rowsPerPage: number, ) => { - searchBody.search = searchBody.search?.map((filter: any) => { - if (filter.operator == "last") { - return { - column: filter.column, - operator: "gt", - value: dayjs() - .subtract(1, filter.value as "hour" | "day" | "month" | "year") - .toISOString(), - values: filter.values, - }; - } - return filter; - }); + processSearchBody(searchBody); const urlGetJobs = `/api/jobs/search?page=${page + 1}&per_page=${rowsPerPage}`; return useSWR([urlGetJobs, accessToken, "POST", searchBody], fetcher); }; @@ -49,19 +52,7 @@ export const refreshJobs = ( page: number, rowsPerPage: number, ) => { - searchBody.search = searchBody.search?.map((filter: any) => { - if (filter.operator == "last") { - return { - column: filter.column, - operator: "gt", - value: dayjs() - .subtract(1, filter.value as "hour" | "day" | "month" | "year") - .toISOString(), - values: filter.values, - }; - } - return filter; - }); + processSearchBody(searchBody); const urlGetJobs = `/api/jobs/search?page=${page + 1}&per_page=${rowsPerPage}`; mutate([urlGetJobs, accessToken, "POST", searchBody]); }; diff --git a/packages/diracx-web-components/components/JobMonitor/JobDataTable.tsx b/packages/diracx-web-components/components/JobMonitor/JobDataTable.tsx index 73ee1f39..04e22993 100644 --- a/packages/diracx-web-components/components/JobMonitor/JobDataTable.tsx +++ b/packages/diracx-web-components/components/JobMonitor/JobDataTable.tsx @@ -40,28 +40,28 @@ import { useJobs, } from "./JobDataService"; +const statusColors: { [key: string]: string } = { + Submitting: purple[500], + Received: blueGrey[500], + Checking: teal[500], + Staging: lightBlue[500], + Waiting: amber[600], + Matched: blue[300], + Running: blue[900], + Rescheduled: lime[700], + Completing: orange[500], + Completed: green[300], + Done: green[500], + Failed: red[500], + Stalled: amber[900], + Killed: red[900], + Deleted: grey[500], +}; + /** * Renders the status cell with colors */ const renderStatusCell = (status: string) => { - const statusColors: { [key: string]: string } = { - Submitting: purple[500], - Received: blueGrey[500], - Checking: teal[500], - Staging: lightBlue[500], - Waiting: amber[600], - Matched: blue[300], - Running: blue[900], - Rescheduled: lime[700], - Completing: orange[500], - Completed: green[300], - Done: green[500], - Failed: red[500], - Stalled: amber[900], - Killed: red[900], - Deleted: grey[500], - }; - return ( c.id == tempFilter.column); + const types: { + [type: string]: { operators: string[]; defaultOperator: string }; + } = { + DateTime: { operators: ["last", "gt", "lt"], defaultOperator: "last" }, + category: { + operators: ["eq", "neq", "in", "not in", "like"], + defaultOperator: "eq", + }, + number: { + operators: ["eq", "neq", "gt", "lt", "like"], + defaultOperator: "eq", + }, + default: { + operators: ["eq", "neq", "gt", "lt", "like"], + defaultOperator: "eq", + }, + }; + + const operatorText: { [operator: string]: string } = { + eq: "equals to", + neq: "not equals to", + last: "in the last", + gt: "is greater than", + lt: "is lower than", + in: "is in", + "not in": "is not in", + like: "like", + }; + + function operatorSelector() { + if (tempFilter) + return ( + + Operator + + + ); + } + + function valueSelector() { + if (!tempFilter) return null; + if (selectedColumn?.type == "DateTime") { + if (tempFilter.operator != "last") + return ( + + + onChange("value", e?.toISOString() || "")} + views={["year", "day", "hours", "minutes", "seconds"]} + /> + + + ); + else + return ( + + Value + + + ); + } + + if ( + typeof selectedColumn?.type == "object" && + tempFilter.operator != "like" + ) + return ( + + Value + + + ); + if (selectedColumn?.type == "number") { + if (!["in", "not in", "like"].includes(tempFilter.operator)) + return ( + + onChange("value", e.target.value)} + type="number" + /> + + ); + else if (["in", "not in"].includes(tempFilter.operator)) + return ( + + onChange("values", e.target.value.split(" "))} + /> + + ); + } + return ( + + onChange("value", e.target.value)} + /> + + ); + } + return ( @@ -115,16 +294,16 @@ export function FilterForm(props: FilterFormProps) { value={tempFilter.column} onChange={(e) => { onChange("column", e.target.value); - console.log(columns.find((c) => c.id == e.target.value)?.type); - if ( - columns.find((c) => c.id == e.target.value)?.type == - "DateTime" - ) - onChange("operator", "last"); - else if (tempFilter.operator == "last") { - onChange("operator", "eq"); - onChange("value", ""); - } + onChange( + "operator", + types[ + typeof columns.find((v) => v.id == e.target.value)?.type == + "object" + ? "category" + : (columns.find((v) => v.id == e.target.value) + ?.type as string) || "default" + ]?.defaultOperator, + ); }} label="Column" labelId="column" @@ -139,155 +318,9 @@ export function FilterForm(props: FilterFormProps) { - - Operator - - + {operatorSelector()} - - - {typeof selectedColumn?.type == "object" && - tempFilter.operator != "like" ? ( - <> - Value - - - ) : selectedColumn?.type == "DateTime" && - tempFilter.operator != "last" ? ( - onChange("value", e?.toISOString() || "")} - views={["year", "day", "hours", "minutes", "seconds"]} - /> - ) : selectedColumn?.type == "DateTime" && - tempFilter.operator == "last" ? ( - <> - Value - - - ) : selectedColumn?.type == "number" && - !["in", "not in", "like"].includes(tempFilter.operator) ? ( - onChange("value", e.target.value)} - type="number" - /> - ) : selectedColumn?.type == "number" && - ["in", "not in"].includes(tempFilter.operator) ? ( - - onChange("values", e.target.value.split(" ")) - } - /> - ) : ( - onChange("value", e.target.value)} - /> - )} - - + {valueSelector()} applyChanges()} color="success">