Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
46 changes: 27 additions & 19 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
{
"configurations": [
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": { "host": "localhost", "port": 5678 },
"pathMappings": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": { "host": "localhost", "port": 5678 },
"pathMappings": [
{
"localRoot": "${workspaceFolder}/backend_py/primary",
"remoteRoot": "/home/appuser/backend_py/primary"
}
]
},
{
"name": "TS: Launch Chrome and Attach",
"request": "launch",
"type": "chrome",
"webRoot": "${workspaceFolder}/frontend",
"url": "http://localhost:8080"
"localRoot": "${workspaceFolder}/backend_py/primary",
"remoteRoot": "/home/appuser/backend_py/primary"
}
]
]
},
{
"name": "TS: Launch Chrome and Attach",
"request": "launch",
"type": "chrome",
"webRoot": "${workspaceFolder}/frontend",
"url": "http://localhost:8080"
},
{
"name": "Run script",
"type": "node",
"cwd": "${workspaceFolder}/frontend",
"request": "launch",
"program": "${workspaceFolder}/frontend/scripts/add-api-suffix.cjs",
"args": ["--suffix", "'__api'", "--dir", "'src/api/'"]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class EnforceLoggedInMiddleware(BaseHTTPMiddleware):
"""Middleware to enforces that the user is logged in
"""Middleware to enforce that the user is logged in

By default, all paths except `/login` and `/auth-callback` are protected.

Expand Down
8 changes: 6 additions & 2 deletions backend_py/primary/primary/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import os

from fastapi import FastAPI
from fastapi import Depends, FastAPI
from fastapi.responses import ORJSONResponse
from fastapi.routing import APIRoute
from starsessions import SessionMiddleware
Expand Down Expand Up @@ -34,6 +34,7 @@
from primary.utils.exception_handlers import configure_service_level_exception_handlers
from primary.utils.exception_handlers import override_default_fastapi_exception_handlers
from primary.utils.logging_setup import ensure_console_log_handler_is_configured, setup_normal_log_levels
from primary.middleware.add_warnings_middleware import AddWarningsMiddleware, allow_warnings_dep

from . import config

Expand All @@ -60,6 +61,7 @@ def custom_generate_unique_id(route: APIRoute) -> str:
generate_unique_id_function=custom_generate_unique_id,
root_path="/api",
default_response_class=ORJSONResponse,
dependencies=[Depends(allow_warnings_dep)],
)

if os.environ.get("APPLICATIONINSIGHTS_CONNECTION_STRING"):
Expand Down Expand Up @@ -90,12 +92,14 @@ def custom_generate_unique_id(route: APIRoute) -> str:
app.include_router(dev_router, prefix="/dev", tags=["dev"], include_in_schema=False)

auth_helper = AuthHelper()
app.include_router(auth_helper.router)
app.include_router(auth_helper.router, dependencies=[])
app.include_router(general_router)

configure_service_level_exception_handlers(app)
override_default_fastapi_exception_handlers(app)

# This middleware instance adds a Warnings header to the response containing a JSON array of warnings
app.add_middleware(AddWarningsMiddleware)

# This middleware instance approximately measures execution time of the route handler itself
app.add_middleware(AddProcessTimeToServerTimingMiddleware, metric_name="total-exec-route")
Expand Down
59 changes: 59 additions & 0 deletions backend_py/primary/primary/middleware/add_warnings_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from typing_extensions import Annotated
import json
from contextvars import ContextVar

from fastapi import Query, HTTPException
from starlette.datastructures import MutableHeaders

# Add warnings to ContextVar
warnings_context: ContextVar = ContextVar(
"warnings_context", default={"report_errors_as_warnings": False, "reported_warnings": []}
)


async def allow_warnings_dep(allow_warnings: Annotated[bool, Query(description="Allow warnings")] = None) -> None:
if allow_warnings:
context = warnings_context.get()
context["report_errors_as_warnings"] = True
warnings_context.set(context)


def add_warning(warning: str) -> None:
context = warnings_context.get()
if context["report_errors_as_warnings"]:
context["reported_warnings"].append(warning)


def soft_raise_exception(exception: HTTPException) -> None:
context = warnings_context.get()
if context["report_errors_as_warnings"]:
context["reported_warnings"].append(exception.detail)
else:
raise exception


class AddWarningsMiddleware:
"""
Adds a Warnings header to the response containing a JSON array of warnings
"""

def __init__(self, app):
self.app = app

async def __call__(self, scope, receive, send) -> None:
if scope["type"] != "http":
return await self.app(scope, receive, send)

warnings_context.set({"report_errors_as_warnings": False, "reported_warnings": []})

async def send_with_warning_header(message) -> None:
if message["type"] == "http.response.start":
context = warnings_context.get()
if len(context["reported_warnings"]) > 0:
headers = MutableHeaders(scope=message)
warnings = json.dumps(context["reported_warnings"])
headers.append("Webviz-Content-Warnings", warnings)

await send(message)

await self.app(scope, receive, send_with_warning_header)
18 changes: 9 additions & 9 deletions backend_py/primary/primary/routers/dev/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


@router.get("/usersession/{user_component}/call")
async def usersession_call(
async def get_usersession_call(
authenticated_user: Annotated[AuthenticatedUser, Depends(AuthHelper.get_authenticated_user)],
user_component: Annotated[UserComponent, Path(description="User session component")],
instance_str: Annotated[str, Query(description="Instance string")] = "myInst",
Expand Down Expand Up @@ -59,7 +59,7 @@ async def usersession_call(


@router.get("/usersession/{user_component}/radixlist")
async def usersession_radixlist(user_component: UserComponent) -> list:
async def get_usersession_radixlist(user_component: UserComponent) -> list:
LOGGER.debug(f"usersession_radixlist() {user_component=}")

session_def = _USER_SESSION_DEFS[user_component]
Expand All @@ -75,7 +75,7 @@ async def usersession_radixlist(user_component: UserComponent) -> list:


@router.get("/usersession/{user_component}/radixcreate")
async def usersession_radixcreate(user_component: UserComponent) -> str:
async def get_usersession_radixcreate(user_component: UserComponent) -> str:
LOGGER.debug(f"usersession_radixcreate() {user_component=}")

session_def = _USER_SESSION_DEFS[user_component]
Expand All @@ -100,7 +100,7 @@ async def usersession_radixcreate(user_component: UserComponent) -> str:


@router.get("/usersession/{user_component}/radixdelete")
async def usersession_radixdelete(user_component: UserComponent) -> str:
async def get_usersession_radixdelete(user_component: UserComponent) -> str:
LOGGER.debug(f"usersession_radixdelete() {user_component=}")

session_def = _USER_SESSION_DEFS[user_component]
Expand All @@ -112,7 +112,7 @@ async def usersession_radixdelete(user_component: UserComponent) -> str:


@router.get("/usersession/dirlist")
async def usersession_dirlist(
async def get_usersession_dirlist(
authenticated_user: Annotated[AuthenticatedUser, Depends(AuthHelper.get_authenticated_user)],
user_component: UserComponent | None = None,
) -> list:
Expand All @@ -134,7 +134,7 @@ async def usersession_dirlist(


@router.get("/usersession/dirdel")
async def usersession_dirdel(
async def get_usersession_dirdel(
authenticated_user: Annotated[AuthenticatedUser, Depends(AuthHelper.get_authenticated_user)],
user_component: UserComponent | None = None,
) -> str:
Expand All @@ -157,7 +157,7 @@ async def usersession_dirdel(


@router.get("/bgtask")
async def bgtask() -> str:
async def get_bgtask() -> str:
LOGGER.debug(f"bgtask() - start")

async def funcThatThrows() -> None:
Expand All @@ -175,7 +175,7 @@ async def funcThatLogs(msg: str) -> None:


@router.get("/ri_surf")
async def ri_surf(
async def get_ri_surf(
authenticated_user: Annotated[AuthenticatedUser, Depends(AuthHelper.get_authenticated_user)],
) -> str:
LOGGER.debug(f"ri_surf() - start")
Expand All @@ -200,7 +200,7 @@ async def ri_surf(


@router.get("/ri_isect")
async def ri_isect(
async def get_ri_isect(
authenticated_user: Annotated[AuthenticatedUser, Depends(AuthHelper.get_authenticated_user)],
) -> str:
LOGGER.debug(f"ri_isect() - start")
Expand Down
6 changes: 3 additions & 3 deletions backend_py/primary/primary/routers/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ class UserInfo(BaseModel):


@router.get("/alive")
def alive() -> str:
def get_alive() -> str:
print("entering alive route")
return f"ALIVE: Backend is alive at this time: {datetime.datetime.now()}"


@router.get("/alive_protected")
def alive_protected() -> str:
def get_alive_protected() -> str:
print("entering alive_protected route")
return f"ALIVE_PROTECTED: Backend is alive at this time: {datetime.datetime.now()}"


@router.get("/logged_in_user", response_model=UserInfo)
async def logged_in_user(
async def get_logged_in_user(
request: Request,
includeGraphApiInfo: bool = Query( # pylint: disable=invalid-name
False, description="Set to true to include user avatar and display name from Microsoft Graph API"
Expand Down
2 changes: 1 addition & 1 deletion backend_py/primary/primary/routers/graph/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


@router.get("/user_photo/")
async def user_info(
async def get_user_photo(
# fmt:off
authenticated_user: AuthenticatedUser = Depends(AuthHelper.get_authenticated_user),
user_id: str = Query(description="User id"),
Expand Down
6 changes: 3 additions & 3 deletions backend_py/primary/primary/routers/grid3d/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async def get_grid_models_info(


@router.get("/is_grid_geometry_shared/")
async def is_grid_geometry_shared(
async def get_is_grid_geometry_shared(
authenticated_user: Annotated[AuthenticatedUser, Depends(AuthHelper.get_authenticated_user)],
case_uuid: Annotated[str, Query(description="Sumo case uuid")],
ensemble_name: Annotated[str, Query(description="Ensemble name")],
Expand All @@ -75,7 +75,7 @@ async def is_grid_geometry_shared(
# Primary backend
@router.get("/grid_surface")
# pylint: disable=too-many-arguments
async def grid_surface(
async def get_grid_surface(
authenticated_user: Annotated[AuthenticatedUser, Depends(AuthHelper.get_authenticated_user)],
case_uuid: Annotated[str, Query(description="Sumo case uuid")],
ensemble_name: Annotated[str, Query(description="Ensemble name")],
Expand Down Expand Up @@ -126,7 +126,7 @@ async def grid_surface(

@router.get("/grid_parameter")
# pylint: disable=too-many-arguments
async def grid_parameter(
async def get_grid_parameter(
authenticated_user: Annotated[AuthenticatedUser, Depends(AuthHelper.get_authenticated_user)],
case_uuid: Annotated[str, Query(description="Sumo case uuid")],
ensemble_name: Annotated[str, Query(description="Ensemble name")],
Expand Down
2 changes: 1 addition & 1 deletion backend_py/primary/primary/routers/parameters/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async def get_parameters(


@router.get("/is_sensitivity_run/")
async def is_sensitivity_run(
async def get_is_sensitivity_run(
authenticated_user: AuthenticatedUser = Depends(AuthHelper.get_authenticated_user),
case_uuid: str = Query(description="Sumo case uuid"),
ensemble_name: str = Query(description="Ensemble name"),
Expand Down
7 changes: 5 additions & 2 deletions backend_py/primary/primary/routers/pvt/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from primary.auth.auth_helper import AuthHelper
from primary.services.sumo_access.table_access import TableAccess
from primary.services.utils.authenticated_user import AuthenticatedUser
from primary.middleware.add_warnings_middleware import soft_raise_exception

from .converters import pvt_dataframe_to_api_data
from .schemas import PvtData
Expand All @@ -16,7 +17,7 @@


@router.get("/table_data/")
async def table_data(
async def get_table_data(
# fmt:off
authenticated_user: AuthenticatedUser = Depends(AuthHelper.get_authenticated_user),
case_uuid: str = Query(description="Sumo case uuid"),
Expand All @@ -33,6 +34,8 @@ async def table_data(
# Get all table schemas for a given realization and find the pvt table
table_schemas = await access.get_table_schemas_single_realization_async(realization=realization)

soft_raise_exception(HTTPException(status_code=404, detail="PVT table not complete"))

table_schema = None
for schema in table_schemas:
if schema.tagname == "pvt":
Expand All @@ -50,7 +53,7 @@ async def table_data(

# DOES NOT CURRENTLY WORK
@router.get("/realizations_tables_are_equal/")
async def realizations_tables_are_equal(
async def get_realizations_tables_are_equal(
# fmt:off
authenticated_user: AuthenticatedUser = Depends(AuthHelper.get_authenticated_user),
case_uuid: str = Query(description="Sumo case uuid"),
Expand Down
4 changes: 2 additions & 2 deletions backend_py/primary/primary/routers/surface/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ async def post_get_surface_intersection(
return surface_intersection_response


@router.post("/sample_surface_in_points")
async def post_sample_surface_in_points(
@router.post("/get_sample_surface_in_points")
async def post_get_sample_surface_in_points(
case_uuid: str = Query(description="Sumo case uuid"),
ensemble_name: str = Query(description="Ensemble name"),
surface_name: str = Query(description="Surface name"),
Expand Down
4 changes: 3 additions & 1 deletion backend_py/primary/primary/routers/timeseries/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from primary.services.utils.authenticated_user import AuthenticatedUser
from primary.services.summary_delta_vectors import create_delta_vector_table, create_realization_delta_vector_list
from primary.utils.query_string_utils import decode_uint_list_str
from primary.middleware.add_warnings_middleware import add_warning

from . import converters, schemas
import asyncio
Expand All @@ -22,7 +23,6 @@

router = APIRouter()


@router.get("/vector_list/")
async def get_vector_list(
response: Response,
Expand All @@ -48,6 +48,8 @@ async def get_vector_list(

LOGGER.info(f"Got vector list in: {perf_metrics.to_string()}")

add_warning("The vectors shown might not be valid!")

return ret_arr


Expand Down
Loading