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

[cueweb] Fix autoload and caching #1623

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
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