Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenthoms committed Jun 20, 2024
1 parent cb50584 commit 4334c7c
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 26 deletions.
12 changes: 12 additions & 0 deletions frontend/src/modules/Intersection/settings/atoms/derivedAtoms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EnsembleIdent } from "@framework/EnsembleIdent";
import { EnsembleSet } from "@framework/EnsembleSet";
import { EnsembleRealizationFilterFunctionAtom, EnsembleSetAtom } from "@framework/GlobalAtoms";
import { IntersectionPolylinesAtom } from "@framework/userCreatedItems/IntersectionPolylines";

Expand All @@ -12,6 +13,17 @@ import {
} from "./baseAtoms";
import { drilledWellboreHeadersQueryAtom } from "./queryAtoms";

export const filteredEnsembleSetAtom = atom((get) => {
const ensembleSet = get(EnsembleSetAtom);
const fieldIdentifier = get(userSelectedFieldIdentifierAtom);

if (fieldIdentifier === null) {
return ensembleSet;
}

return new EnsembleSet(ensembleSet.getEnsembleArr().filter((el) => el.getFieldIdentifier() === fieldIdentifier));
});

export const selectedFieldIdentifierAtom = atom((get) => {
const ensembleSet = get(EnsembleSetAtom);
const selectedFieldIdentifier = get(userSelectedFieldIdentifierAtom);
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/modules/Intersection/settings/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from "./atoms/baseAtoms";
import {
availableUserCreatedIntersectionPolylinesAtom,
filteredEnsembleSetAtom,
selectedCustomIntersectionPolylineIdAtom,
selectedFieldIdentifierAtom,
selectedWellboreAtom,
Expand All @@ -41,6 +42,7 @@ export function Settings(
props: ModuleSettingsProps<State, SettingsToViewInterface, Record<string, never>, ViewAtoms>
): JSX.Element {
const ensembleSet = useEnsembleSet(props.workbenchSession);
const filteredEnsembleSet = useAtomValue(filteredEnsembleSetAtom);
const statusWriter = useSettingsStatusWriter(props.settingsContext);

const selectedField = useAtomValue(selectedFieldIdentifierAtom);
Expand Down Expand Up @@ -192,7 +194,7 @@ export function Settings(
</CollapsibleGroup>
<div className="flex-grow flex flex-col min-h-0">
<Layers
ensembleSet={ensembleSet}
ensembleSet={filteredEnsembleSet}
workbenchSession={props.workbenchSession}
workbenchSettings={props.workbenchSettings}
/>
Expand Down
20 changes: 15 additions & 5 deletions frontend/src/modules/Intersection/utils/layers/BaseLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type LayerSettings = {

export class BaseLayer<TSettings extends LayerSettings, TData> {
private _subscribers: Map<LayerTopic, Set<() => void>> = new Map();
protected _queryClient: QueryClient;
protected _queryClient: QueryClient | null = null;
protected _status: LayerStatus = LayerStatus.IDLE;
private _id: string;
private _name: string;
Expand All @@ -46,12 +46,11 @@ export class BaseLayer<TSettings extends LayerSettings, TData> {
private _lastDataFetchSettings: TSettings;
private _queryKeys: unknown[][] = [];

constructor(name: string, settings: TSettings, queryClient: QueryClient) {
constructor(name: string, settings: TSettings) {
this._id = v4();
this._name = name;
this._settings = settings;
this._lastDataFetchSettings = cloneDeep(settings);
this._queryClient = queryClient;
}

getId(): string {
Expand All @@ -78,6 +77,10 @@ export class BaseLayer<TSettings extends LayerSettings, TData> {
this.notifySubscribers(LayerTopic.NAME);
}

setQueryClient(queryClient: QueryClient): void {
this._queryClient = queryClient;
}

getBoundingBox(): BoundingBox | null {
return this._boundingBox;
}
Expand Down Expand Up @@ -132,6 +135,10 @@ export class BaseLayer<TSettings extends LayerSettings, TData> {
}

private maybeCancelQuery(): void {
if (!this._queryClient) {
return;
}

if (this._queryKeys) {
for (const queryKey of this._queryKeys) {
this._queryClient.cancelQueries({ queryKey });
Expand Down Expand Up @@ -169,6 +176,9 @@ export class BaseLayer<TSettings extends LayerSettings, TData> {
}

async maybeRefetchData(): Promise<void> {
if (!this._queryClient) {
return;
}
if (this._isSuspended) {
return;
}
Expand All @@ -187,7 +197,7 @@ export class BaseLayer<TSettings extends LayerSettings, TData> {
this._status = LayerStatus.LOADING;
this.notifySubscribers(LayerTopic.STATUS);
try {
this._data = await this.fetchData();
this._data = await this.fetchData(this._queryClient);
if (this._queryKeys.length === null && isDevMode()) {
console.warn(
"Did you forget to use 'setQueryKeys' in your layer implementation of 'fetchData'? This will cause the queries to not be cancelled when settings change and might lead to undesired behaviour."
Expand All @@ -203,7 +213,7 @@ export class BaseLayer<TSettings extends LayerSettings, TData> {
this.notifySubscribers(LayerTopic.STATUS);
}

protected async fetchData(): Promise<TData> {
protected async fetchData(queryClient: QueryClient): Promise<TData> {
throw new Error("Not implemented");
}
}
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/modules/Intersection/utils/layers/GridLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class GridLayer extends BaseLayer<GridLayerSettings, AdjustedPolylineInte
private _useCustomColorScaleBoundariesParameterMap = new Map<string, boolean>();
private _defaultColorScale: ColorScale;

constructor(name: string, queryClient: QueryClient) {
constructor(name: string) {
const defaultSettings = {
ensembleIdent: null,
gridModelName: null,
Expand All @@ -50,7 +50,7 @@ export class GridLayer extends BaseLayer<GridLayerSettings, AdjustedPolylineInte
showMesh: false,
extensionLength: null,
};
super(name, defaultSettings, queryClient);
super(name, defaultSettings);

this._defaultColorScale = new ColorScale({
colorPalette: defaultContinuousSequentialColorPalettes[0],
Expand Down Expand Up @@ -151,13 +151,13 @@ export class GridLayer extends BaseLayer<GridLayerSettings, AdjustedPolylineInte
);
}

protected async fetchData(): Promise<AdjustedPolylineIntersection> {
protected async fetchData(queryClient: QueryClient): Promise<AdjustedPolylineIntersection> {
super.setBoundingBox(null);

const queryKey = ["getGridPolylineIntersection", ...Object.entries(this._settings)];
this.registerQueryKey(queryKey);

return this._queryClient
return queryClient
.fetchQuery({
queryKey,
queryFn: () =>
Expand Down
56 changes: 56 additions & 0 deletions frontend/src/modules/Intersection/utils/layers/LayerManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { QueryClient } from "@tanstack/query-core";

import { BaseLayer } from "./BaseLayer";

export enum LayerManagerTopics {
LAYERS_CHANGED = "layers-changed",
}

export class LayerManager {
private _queryClient: QueryClient;
private _layers: BaseLayer<any, any>[] = [];
private _subscribers: Map<LayerManagerTopics, Set<() => void>> = new Map();

constructor(queryClient: QueryClient) {
this._queryClient = queryClient;
}

addLayer(layer: BaseLayer<any, any>): void {
layer.setName(this.makeUniqueLayerName(layer.getName()));
layer.setQueryClient(this._queryClient);
this._layers.push(layer);
this.notifySubscribers(LayerManagerTopics.LAYERS_CHANGED);
}

removeLayer(id: string): void {
this._layers = this._layers.filter((layer) => layer.getId() !== id);
}

getLayer(id: string): BaseLayer<any, any> | undefined {
return this._layers.find((layer) => layer.getId() === id);
}

changeOrder(order: string[]): void {
this._layers = order
.map((id) => this._layers.find((layer) => layer.getId() === id))
.filter(Boolean) as BaseLayer<any, any>[];
this.notifySubscribers(LayerManagerTopics.LAYERS_CHANGED);
}

private notifySubscribers(topic: LayerManagerTopics): void {
const subscribers = this._subscribers.get(topic);
if (subscribers) {
subscribers.forEach((subscriber) => subscriber());
}
}

private makeUniqueLayerName(name: string): string {
let potentialName = name;
let i = 1;
while (this._layers.some((layer) => layer.getName() === potentialName)) {
potentialName = `${name} (${i})`;
i++;
}
return potentialName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class SeismicLayer extends BaseLayer<SeismicLayerSettings, SeismicLayerDa
private _colorScalesParameterMap: Map<string, ColorScale> = new Map();
private _useCustomColorScaleBoundariesParameterMap = new Map<string, boolean>();

constructor(name: string, queryClient: QueryClient) {
constructor(name: string) {
const defaultSettings = {
ensembleIdent: null,
realizationNum: null,
Expand All @@ -86,7 +86,7 @@ export class SeismicLayer extends BaseLayer<SeismicLayerSettings, SeismicLayerDa
dateOrInterval: null,
resolution: 1,
};
super(name, defaultSettings, queryClient);
super(name, defaultSettings);

this._defaultColorScale = new ColorScale({
colorPalette: defaultContinuousDivergingColorPalettes[0],
Expand Down Expand Up @@ -313,7 +313,7 @@ export class SeismicLayer extends BaseLayer<SeismicLayerSettings, SeismicLayerDa
return trajectory;
}

protected async fetchData(): Promise<SeismicLayerData> {
protected async fetchData(queryClient: QueryClient): Promise<SeismicLayerData> {
super.setBoundingBox(null);

const sampledPolyline = this.samplePolyline();
Expand All @@ -340,7 +340,7 @@ export class SeismicLayer extends BaseLayer<SeismicLayerSettings, SeismicLayerDa
];
this.registerQueryKey(queryKey);

return this._queryClient
return queryClient
.fetchQuery({
queryKey,
queryFn: () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type SurfaceLayerSettings = {
export class SurfaceLayer extends BaseLayer<SurfaceLayerSettings, SurfaceIntersectionData_api[]> {
private _colorSet: ColorSet;

constructor(name: string, queryClient: QueryClient) {
constructor(name: string) {
const defaultSettings = {
ensembleIdent: null,
realizationNum: null,
Expand All @@ -36,7 +36,7 @@ export class SurfaceLayer extends BaseLayer<SurfaceLayerSettings, SurfaceInterse
extensionLength: 0,
resolution: 1,
};
super(name, defaultSettings, queryClient);
super(name, defaultSettings);

this._colorSet = new ColorSet(defaultColorPalettes[0]);
}
Expand Down Expand Up @@ -112,7 +112,7 @@ export class SurfaceLayer extends BaseLayer<SurfaceLayerSettings, SurfaceInterse
);
}

protected async fetchData(): Promise<SurfaceIntersectionData_api[]> {
protected async fetchData(queryClient: QueryClient): Promise<SurfaceIntersectionData_api[]> {
const promises: Promise<SurfaceIntersectionData_api>[] = [];

super.setBoundingBox(null);
Expand Down Expand Up @@ -181,7 +181,7 @@ export class SurfaceLayer extends BaseLayer<SurfaceLayerSettings, SurfaceInterse
];
this.registerQueryKey(queryKey);

const promise = this._queryClient.fetchQuery({
const promise = queryClient.fetchQuery({
queryKey,
queryFn: () =>
apiService.surface.postGetSurfaceIntersection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function transformData(
export class SurfacesUncertaintyLayer extends BaseLayer<SurfacesUncertaintyLayerSettings, SurfaceUncertaintyData[]> {
private _colorSet: ColorSet;

constructor(name: string, queryClient: QueryClient) {
constructor(name: string) {
const defaultSettings = {
ensembleIdent: null,
realizationNums: [],
Expand All @@ -55,7 +55,7 @@ export class SurfacesUncertaintyLayer extends BaseLayer<SurfacesUncertaintyLayer
extensionLength: 0,
resolution: 1,
};
super(name, defaultSettings, queryClient);
super(name, defaultSettings);

this._colorSet = new ColorSet(defaultColorPalettes[0]);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ export class SurfacesUncertaintyLayer extends BaseLayer<SurfacesUncertaintyLayer
);
}

protected async fetchData(): Promise<SurfaceUncertaintyData[]> {
protected async fetchData(queryClient: QueryClient): Promise<SurfaceUncertaintyData[]> {
const promises: Promise<SurfaceUncertaintyData>[] = [];

super.setBoundingBox(null);
Expand Down Expand Up @@ -214,7 +214,7 @@ export class SurfacesUncertaintyLayer extends BaseLayer<SurfacesUncertaintyLayer
];
this.registerQueryKey(queryKey);

const promise = this._queryClient
const promise = queryClient
.fetchQuery({
queryKey,
queryFn: () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export type WellpicksLayerSettings = {
export type WellPicksLayerData = ReturnType<typeof transformFormationData>;

export class WellpicksLayer extends BaseLayer<WellpicksLayerSettings, WellPicksLayerData> {
constructor(name: string, queryClient: QueryClient) {
constructor(name: string) {
const defaultSettings = {
ensembleIdent: null,
wellboreUuid: null,
filterPicks: false,
selectedUnitPicks: [],
selectedNonUnitPicks: [],
};
super(name, defaultSettings, queryClient);
super(name, defaultSettings);
}

protected areSettingsValid(): boolean {
Expand Down Expand Up @@ -64,15 +64,15 @@ export class WellpicksLayer extends BaseLayer<WellpicksLayerSettings, WellPicksL
return data;
}

protected async fetchData(): Promise<WellPicksLayerData> {
protected async fetchData(queryClient: QueryClient): Promise<WellPicksLayerData> {
const queryKey = [
"getWellborePicksAndStratigraphicUnits",
this._settings.ensembleIdent?.getCaseUuid(),
this._settings.wellboreUuid,
];
this.registerQueryKey(queryKey);

return this._queryClient
return queryClient
.fetchQuery({
queryKey,
queryFn: () =>
Expand Down

0 comments on commit 4334c7c

Please sign in to comment.