Skip to content

Commit

Permalink
[cueweb] Fix autoload and caching (#1623)
Browse files Browse the repository at this point in the history
- Fixed autoload user jobs by not including finished user jobs
- Added a reinitialization of data table state values on re-mount using
local storage values
  • Loading branch information
Zach-Fong authored Dec 16, 2024
1 parent a0b9313 commit bde7b07
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
60 changes: 35 additions & 25 deletions cueweb/app/jobs/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,24 @@ const initialColumnVisibility = {
maxRss: false,
}

// Initial state
const initialState: State = {
autoloadMine: getItemFromLocalStorage("autoloadMine", "true"),
tableData: getItemFromLocalStorage("tableData", "[]"),
tableDataUnfiltered: getItemFromLocalStorage("tableDataUnfiltered", "[]"),
sorting: getItemFromLocalStorage("sorting", "[]"),
columnFilters: getItemFromLocalStorage("columnFilters", "[]"),
stateSelectValue: getItemFromLocalStorage("stateSelectValue", JSON.stringify("All States")),
jobSearchResults: [],
filteredJobSearchResults: [],
searchQuery: "",
apiQuery: "",
rowSelection: {},
columnVisibility: getItemFromLocalStorage("columnVisibility", JSON.stringify(initialColumnVisibility)),
error: null,
username: UNKNOWN_USER,
// The initial state of the data table on remount using local storage to preserve states
function getInitialState(): State {
return {
autoloadMine: getItemFromLocalStorage("autoloadMine", "true"),
tableData: getItemFromLocalStorage("tableData", "[]"),
tableDataUnfiltered: getItemFromLocalStorage("tableDataUnfiltered", "[]"),
sorting: getItemFromLocalStorage("sorting", "[]"),
columnFilters: getItemFromLocalStorage("columnFilters", "[]"),
stateSelectValue: getItemFromLocalStorage("stateSelectValue", JSON.stringify("All States")),
jobSearchResults: [],
filteredJobSearchResults: [],
searchQuery: "",
apiQuery: "",
rowSelection: {},
columnVisibility: getItemFromLocalStorage("columnVisibility", JSON.stringify(initialColumnVisibility)),
error: null,
username: UNKNOWN_USER,
}
};

// Reducer function
Expand Down Expand Up @@ -174,10 +176,10 @@ function reducer(state: State, action: Action): State {
case "RESET_COLUMN_VISIBILITY":
return {
...state,
columnVisibility: initialState.columnVisibility,
columnVisibility: initialColumnVisibility,
};
case "RESET_STATE":
return initialState;
return getInitialState();
default:
return state;
}
Expand Down Expand Up @@ -215,7 +217,11 @@ export function DataTable({ columns, username }: DataTableProps) {
const { theme, setTheme } = useTheme();

// useReducer hook to manage state
const [state, dispatch] = useReducer(reducer, initialState);
const [state, dispatch] = useReducer(reducer, getInitialState());

useEffect(() => {
dispatch({ type: "RESET_STATE"});
}, []);

useEffect(() => {
addUsersJobs();
Expand Down Expand Up @@ -380,17 +386,16 @@ export function DataTable({ columns, username }: DataTableProps) {
// Filter out any of the old data in the data table which no longer exists (has been finished for over 48 hours)
updatedTableDataUnfiltered = updatedTableDataUnfiltered.filter((oldJob: Job) => newData.some((newJob: Job) => oldJob.id === newJob.id));
updatedTableData = updatedTableData.filter((oldJob: Job) => newData.some((newJob: Job) => oldJob.id === newJob.id));

// Update table data as both a variable and in local storage
dispatch({ type: "SET_TABLE_DATA_UNFILTERED", payload: updatedTableDataUnfiltered });
dispatch({ type: "SET_TABLE_DATA", payload: updatedTableData });
}
};

// Trigger table updates every 5000ms
interval = setInterval(() => {
interval = setInterval(async () => {
updateData();
addUsersJobs();
await addUsersJobs();
}, 5000);
} catch (error) {
handleError(error, "Error updating table");
Expand Down Expand Up @@ -633,9 +638,14 @@ export function DataTable({ columns, username }: DataTableProps) {
const jobsToAdd = userJobs.filter(userJob => {
return !state.tableData.some(existingJob => existingJob.name === userJob.name)
});

dispatch({ type: "SET_TABLE_DATA", payload: [...state.tableDataUnfiltered, ...jobsToAddUnfiltered] });
dispatch({ type: "SET_TABLE_DATA_UNFILTERED", payload: [...state.tableData, ...jobsToAdd] });

if (jobsToAddUnfiltered.length > 0) {
dispatch({ type: "SET_TABLE_DATA", payload: [...state.tableDataUnfiltered, ...jobsToAddUnfiltered] });
}

if (jobsToAdd.length > 0) {
dispatch({ type: "SET_TABLE_DATA_UNFILTERED", payload: [...state.tableData, ...jobsToAdd] });
}
};

const handleStateFiltering = (stateFilter: string) => {
Expand Down
2 changes: 1 addition & 1 deletion cueweb/app/utils/get_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function getLayers(body: string): Promise<Layer[]> {

// Fetch jobs for a specific user, including finished jobs
export async function getJobsForUser(user: string): Promise<Job[]> {
const body = { r: { include_finished: true, users: [`${user}`] } };
const body = { r: { include_finished: false, users: [`${user}`] } };
return await getJobs(JSON.stringify(body));
}

Expand Down

0 comments on commit bde7b07

Please sign in to comment.