Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenthoms committed Jun 25, 2024
1 parent fdee465 commit a64f5e9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
3 changes: 0 additions & 3 deletions backend_py/primary/primary/routers/well_completions/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,4 @@ async def get_well_completions_data(
)
well_completions_data = access.get_well_completions_data(realization=realization)

if not well_completions_data:
raise HTTPException(status_code=404, detail="Well completions data not found")

return well_completions_data
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
WellCompletionsUnits,
)

from primary.services.service_exceptions import InvalidDataError, NoDataError, Service


class WellCompletionsAccess:
"""
Expand All @@ -35,7 +37,7 @@ async def from_case_uuid_async(
case: Case = await create_sumo_case_async(client=sumo_client, case_uuid=case_uuid, want_keepalive_pit=False)
return WellCompletionsAccess(case=case, iteration_name=iteration_name)

def get_well_completions_data(self, realization: Optional[int]) -> Optional[WellCompletionsData]:
def get_well_completions_data(self, realization: Optional[int]) -> WellCompletionsData:
"""Get well completions data for case and iteration"""

# With single realization, filter on realization
Expand All @@ -45,7 +47,9 @@ def get_well_completions_data(self, realization: Optional[int]) -> Optional[Well
)
well_completions_df = well_completions_tables[0].to_pandas() if len(well_completions_tables) > 0 else None
if well_completions_df is None:
return None
raise NoDataError(
f"No well completions data found for realization '{realization}'", service=Service.SUMO
)

return WellCompletionDataConverter(well_completions_df).create_data()

Expand All @@ -56,7 +60,9 @@ def get_well_completions_data(self, realization: Optional[int]) -> Optional[Well

# As of now, two tables are expected - one with OP/SH and one with KH
if len(well_completions_tables) < 2:
return None
raise InvalidDataError(
f"Expected 2 tables (OP/SH and KH) but got only {len(well_completions_tables)}", service=Service.SUMO
)

expected_common_columns = set(["WELL", "DATE", "ZONE", "REAL"])
first_df = well_completions_tables[0].to_pandas()
Expand Down Expand Up @@ -135,9 +141,9 @@ def __init__(self, well_completions_df: pd.DataFrame) -> None:

# NOTE:
# - How to handle well attributes? Should be provided by Sumo?
self._well_attributes: Dict[
str, Dict[str, WellCompletionsAttributeType]
] = {} # Each well has dict of attributes
self._well_attributes: Dict[str, Dict[str, WellCompletionsAttributeType]] = (
{}
) # Each well has dict of attributes

def create_data(self) -> WellCompletionsData:
"""Creates well completions dataset for front-end"""
Expand Down
82 changes: 82 additions & 0 deletions frontend/src/framework/utils/ApiErrorHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { ApiError } from "@api";
import { UseQueryResult } from "@tanstack/react-query";

export class ApiErrorHelper {
private _error: ApiError | null = null;
private _statusCode: number | null = null;
private _service: string | null = null;
private _type: string | null = null;
private _message: string | null = null;

constructor(readonly queryResult: UseQueryResult<any>) {
if (queryResult.error && queryResult.error instanceof ApiError) {
this._error = queryResult.error;
this._statusCode = queryResult.error.status;
this.extractInfoFromError(queryResult.error);
}
}

private extractInfoFromError(apiError: ApiError): void {
if (!("error" in apiError.body)) {
return;
}

if ("type" in apiError.body.error) {
this._type = apiError.body.error.type;
}

if ("message" in apiError.body.error) {
this._message = apiError.body.error.message;
}

if ("service" in apiError.body.error) {
this._service = apiError.body.error.service;
}

if (!("type" in apiError.body.error) || !("message" in apiError.body.error)) {
return;
}

this._type = apiError.body.error.type;
this._message = apiError.body.error.message;
}

isError(): boolean {
return this._error !== null;
}

getService(): string | null {
return this._service;
}

getType(): string | null {
return this._type;
}

getMessage(): string | null {
return this._message;
}

getStatusCode(): number | null {
return this._statusCode;
}
}

export function makeErrorMessage(apiErrorHelper: ApiErrorHelper): string {
let errorMessage = "";
if (apiErrorHelper.isError()) {
const additionalInformation: string[] = [];
if (apiErrorHelper.getStatusCode()) {
additionalInformation.push(`${apiErrorHelper.getStatusCode()}`);
}
if (apiErrorHelper.getService()) {
additionalInformation.push(`${apiErrorHelper.getService()?.toUpperCase()}`);
}
if (apiErrorHelper.getType()) {
additionalInformation.push(`${apiErrorHelper.getType()}`);
}
errorMessage = `${apiErrorHelper.getMessage()} (${additionalInformation.join(", ")})`;
}

return errorMessage;
}
11 changes: 11 additions & 0 deletions frontend/src/modules/WellCompletions/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useSettingsStatusWriter } from "@framework/StatusWriter";
import { SyncSettingKey, SyncSettingsHelper } from "@framework/SyncSettings";
import { useEnsembleSet } from "@framework/WorkbenchSession";
import { EnsembleDropdown } from "@framework/components/EnsembleDropdown";
import { ApiErrorHelper, makeErrorMessage } from "@framework/utils/ApiErrorHelper";
import { maybeAssignFirstSyncedEnsemble } from "@framework/utils/ensembleUiHelpers";
import { CircularProgress } from "@lib/components/CircularProgress";
import { CollapsibleGroup } from "@lib/components/CollapsibleGroup";
Expand Down Expand Up @@ -83,13 +84,23 @@ export const Settings = ({
realizationSelection === RealizationSelection.Single ? selectedRealizationNumber : undefined
);

const apiErrorHelper = new ApiErrorHelper(wellCompletionsQuery);

if (apiErrorHelper.isError()) {
statusWriter.addError(makeErrorMessage(apiErrorHelper));
}

/*
if (wellCompletionsQuery.isError) {
const err = wellCompletionsQuery.error as ApiError;
console.debug(err.body);
let message = "Error loading well completions data for ensemble";
if (realizationSelection === RealizationSelection.Single) {
message += ` and realization ${selectedRealizationNumber}`;
}
statusWriter.addError(message);
}
*/

// Use ref to prevent new every render
const wellCompletionsDataAccessor = React.useRef<WellCompletionsDataAccessor>(new WellCompletionsDataAccessor());
Expand Down

0 comments on commit a64f5e9

Please sign in to comment.