Skip to content

Commit

Permalink
Simplify and rebalance caching/refetching
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasbruvik committed Nov 25, 2024
1 parent 6fe99ca commit 8ccf8f9
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Button } from "components/StyledComponents/Button";
import {
refreshObjectQuery,
refreshObjectsQuery,
refreshWellQuery,
refreshWellboresQuery,
refreshWellsQuery
} from "hooks/query/queryRefreshHelpers";
import useExport, { encloseCell } from "hooks/useExport";
Expand Down Expand Up @@ -81,7 +81,7 @@ const Panel = (props: PanelProps) => {
if (!wellUid) {
refreshWellsQuery(queryClient, serverUrl);
} else if (!wellboreUid) {
refreshWellQuery(queryClient, serverUrl, wellUid);
refreshWellboresQuery(queryClient, serverUrl, wellUid);
} else if (!objectUid) {
refreshObjectsQuery(
queryClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ import {
HideModalAction
} from "contexts/operationStateReducer";
import OperationType from "contexts/operationType";
import {
refreshWellQuery,
refreshWellsQuery
} from "hooks/query/queryRefreshHelpers";
import { refreshWellsQuery } from "hooks/query/queryRefreshHelpers";
import { useOpenInQueryView } from "hooks/useOpenInQueryView";
import { useServerFilter } from "hooks/useServerFilter";
import { DeleteWellJob } from "models/jobs/deleteJobs";
Expand Down Expand Up @@ -79,11 +76,6 @@ const WellContextMenu = (props: WellContextMenuProps): React.ReactElement => {
};

const onClickRefresh = async () => {
dispatchOperation({ type: OperationType.HideContextMenu });
refreshWellQuery(queryClient, connectedServer?.url, well.uid);
};

const onClickRefreshAll = async () => {
dispatchOperation({ type: OperationType.HideContextMenu });
refreshWellsQuery(queryClient, connectedServer?.url);
};
Expand Down Expand Up @@ -191,18 +183,7 @@ const WellContextMenu = (props: WellContextMenuProps): React.ReactElement => {
name="refresh"
color={colors.interactive.primaryResting}
/>
<Typography color={"primary"}>Refresh well</Typography>
</MenuItem>,
<MenuItem
key={"refreshallwells"}
onClick={onClickRefreshAll}
disabled={!connectedServer?.url}
>
<StyledIcon
name="refresh"
color={colors.interactive.primaryResting}
/>
<Typography color={"primary"}>Refresh all wells</Typography>
<Typography color={"primary"}>Refresh wells</Typography>
</MenuItem>,
<MenuItem key={"newWell"} onClick={onClickNewWell}>
<StyledIcon name="add" color={colors.interactive.primaryResting} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import { useConnectedServer } from "contexts/connectedServerContext";
import { DisplayModalAction } from "contexts/operationStateReducer";
import OperationType from "contexts/operationType";
import { refreshWellboreQuery } from "hooks/query/queryRefreshHelpers";
import { refreshWellboresQuery } from "hooks/query/queryRefreshHelpers";
import { useGetCapObjects } from "hooks/query/useGetCapObjects";
import { useOpenInQueryView } from "hooks/useOpenInQueryView";
import { useOperationState } from "hooks/useOperationState";
Expand Down Expand Up @@ -151,12 +151,7 @@ const WellboreContextMenu = (

const onClickRefresh = async () => {
dispatchOperation({ type: OperationType.HideContextMenu });
refreshWellboreQuery(
queryClient,
connectedServer?.url,
wellbore.wellUid,
wellbore.uid
);
refreshWellboresQuery(queryClient, connectedServer?.url, wellbore.wellUid);
};

const onClickMissingDataAgent = () => {
Expand Down Expand Up @@ -196,12 +191,12 @@ const WellboreContextMenu = (
return (
<ContextMenu
menuItems={[
<MenuItem key={"refreshwellbore"} onClick={onClickRefresh}>
<MenuItem key={"refreshwellbores"} onClick={onClickRefresh}>
<StyledIcon
name="refresh"
color={colors.interactive.primaryResting}
/>
<Typography color={"primary"}>Refresh wellbore</Typography>
<Typography color={"primary"}>Refresh wellbores</Typography>
</MenuItem>,
<MenuItem key={"newwellbore"} onClick={onClickNewWellbore}>
<StyledIcon name="add" color={colors.interactive.primaryResting} />
Expand Down
42 changes: 9 additions & 33 deletions Src/WitsmlExplorer.Frontend/components/RefreshHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import {
refreshObjectQuery,
refreshObjectsQuery,
refreshSearchQuery,
refreshWellboreQuery,
refreshWellboresQuery,
refreshWellQuery,
refreshWellsQuery
} from "hooks/query/queryRefreshHelpers";
import EntityType from "models/entityType";
Expand Down Expand Up @@ -68,40 +66,18 @@ const RefreshHandler = (): React.ReactElement => {
}, []);

function refreshWell(refreshAction: RefreshAction) {
if (
!refreshAction.wellUids &&
refreshAction.refreshType === RefreshType.Update
) {
refreshWellQuery(
queryClient,
refreshAction.serverUrl.toString().toLowerCase(),
refreshAction.wellUid
);
} else {
// A well has been removed or added, so we need to refresh the entire well list to ensure that it is added or removed from the list.
refreshWellsQuery(
queryClient,
refreshAction.serverUrl.toString().toLowerCase()
);
}
refreshWellsQuery(
queryClient,
refreshAction.serverUrl.toString().toLowerCase()
);
}

function refreshWellbore(refreshAction: RefreshAction) {
if (refreshAction.refreshType === RefreshType.Update) {
refreshWellboreQuery(
queryClient,
refreshAction.serverUrl.toString().toLowerCase(),
refreshAction.wellUid,
refreshAction.wellboreUid
);
} else {
// A wellbore has been removed or added, so we need to refresh the entire wellbore list to ensure that it is added or removed from the list.
refreshWellboresQuery(
queryClient,
refreshAction.serverUrl.toString().toLowerCase(),
refreshAction.wellUid
);
}
refreshWellboresQuery(
queryClient,
refreshAction.serverUrl.toString().toLowerCase(),
refreshAction.wellUid
);
}

function refreshWellboreObject(refreshAction: RefreshAction) {
Expand Down
65 changes: 0 additions & 65 deletions Src/WitsmlExplorer.Frontend/hooks/query/queryRefreshHelpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
QUERY_KEY_OBJECTS,
QUERY_KEY_OBJECT_SEARCH,
QUERY_KEY_SERVERS,
QUERY_KEY_WELL,
QUERY_KEY_WELLBORE,
QUERY_KEY_WELLBORES,
QUERY_KEY_WELLS
} from "./queryKeys";
Expand Down Expand Up @@ -39,37 +37,6 @@ export const refreshWellsQuery = (
});
};

export const refreshWellQuery = (
queryClient: QueryClient,
serverUrl: string,
wellUid: string
) => {
queryClient.invalidateQueries({
queryKey: [QUERY_KEY_WELL, serverUrl.toLowerCase(), wellUid.toLowerCase()]
});
queryClient.invalidateQueries({
queryKey: [
QUERY_KEY_WELLBORES,
serverUrl.toLowerCase(),
wellUid.toLowerCase()
]
});
queryClient.invalidateQueries({
queryKey: [
QUERY_KEY_OBJECTS,
serverUrl.toLowerCase(),
wellUid.toLowerCase()
]
});
queryClient.invalidateQueries({
queryKey: [
QUERY_KEY_COMPONENTS,
serverUrl.toLowerCase(),
wellUid.toLowerCase()
]
});
};

export const refreshWellboresQuery = (
queryClient: QueryClient,
serverUrl: string,
Expand Down Expand Up @@ -98,38 +65,6 @@ export const refreshWellboresQuery = (
});
};

export const refreshWellboreQuery = (
queryClient: QueryClient,
serverUrl: string,
wellUid: string,
wellboreUid: string
) => {
queryClient.invalidateQueries({
queryKey: [
QUERY_KEY_WELLBORE,
serverUrl.toLowerCase(),
wellUid.toLowerCase(),
wellboreUid.toLowerCase()
]
});
queryClient.invalidateQueries({
queryKey: [
QUERY_KEY_OBJECTS,
serverUrl.toLowerCase(),
wellUid.toLowerCase(),
wellboreUid.toLowerCase()
]
});
queryClient.invalidateQueries({
queryKey: [
QUERY_KEY_COMPONENTS,
serverUrl.toLowerCase(),
wellUid.toLowerCase(),
wellboreUid.toLowerCase()
]
});
};

export const refreshObjectsQuery = (
queryClient: QueryClient,
serverUrl: string,
Expand Down
7 changes: 1 addition & 6 deletions Src/WitsmlExplorer.Frontend/hooks/query/useGetCapObjects.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { QueryObserverResult, useQuery } from "@tanstack/react-query";
import {
MILLIS_IN_SECOND,
SECONDS_IN_MINUTE
} from "../../components/Constants";
import { ObjectType } from "../../models/objectType";
import { Server } from "../../models/server";
import CapService from "../../services/capService";
Expand All @@ -19,8 +15,7 @@ export const capObjectsQuery = (server: Server, options?: QueryOptions) => ({
return getCapObjects(server);
},
...options,
enabled: !!server && !(options?.enabled === false),
gcTime: 12 * SECONDS_IN_MINUTE * MILLIS_IN_SECOND // capObjects won't change, so we can cache it for a long time.
enabled: !!server && !(options?.enabled === false)
});

type CapObjectsQueryResult = Omit<
Expand Down
67 changes: 10 additions & 57 deletions Src/WitsmlExplorer.Frontend/hooks/query/useGetWell.tsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,22 @@
import {
QueryClient,
QueryObserverResult,
useQuery,
useQueryClient
} from "@tanstack/react-query";
import { QueryObserverResult } from "@tanstack/react-query";
import { Server } from "../../models/server";
import Well from "../../models/well";
import WellService from "../../services/wellService";
import { QUERY_KEY_WELL } from "./queryKeys";
import { QueryOptions } from "./queryOptions";
import { getWellsQueryKey } from "./useGetWells";
import { useGetWells } from "./useGetWells";

export const getWellQueryKey = (serverUrl: string, wellUid: string) => {
return [QUERY_KEY_WELL, serverUrl?.toLowerCase(), wellUid?.toLowerCase()];
};

const updatePartialWells = (
queryClient: QueryClient,
server: Server,
well: Well
) => {
const existingWells = queryClient.getQueryData<Well[]>(
getWellsQueryKey(server?.url)
);
if (existingWells) {
const existingWellIndex = existingWells.findIndex(
(w) => w.uid === well.uid
);
existingWells[existingWellIndex] = well;
queryClient.setQueryData<Well[]>(
getWellsQueryKey(server?.url),
existingWells
);
}
};

export const wellQuery = (
queryClient: QueryClient,
server: Server,
wellUid: string,
options?: QueryOptions
) => ({
queryKey: getWellQueryKey(server?.url, wellUid),
queryFn: async () => {
const well = await WellService.getWell(wellUid, null, server);
updatePartialWells(queryClient, server, well);
return well;
},
...options,
enabled: !!server && !!wellUid && !(options?.enabled === false),
gcTime: Infinity // We don't want to garbage collect wells
});

type WellQueryResult = Omit<QueryObserverResult<Well, unknown>, "data"> & {
well: Well;
type WellQueryResult = Omit<
QueryObserverResult<Well, unknown>,
"data" | "refetch"
> & {
well: Well | undefined;
};

export const useGetWell = (
server: Server,
wellUid: string,
options?: QueryOptions
): WellQueryResult => {
const queryClient = useQueryClient();
const { data, ...state } = useQuery<Well>(
wellQuery(queryClient, server, wellUid, options)
);
return { well: data, ...state };
const { wells, ...state } = useGetWells(server, options);
const well = wells?.find((w) => w.uid === wellUid);
return { well, ...state };
};
Loading

0 comments on commit 8ccf8f9

Please sign in to comment.