Skip to content

Commit f460133

Browse files
committed
update delete endpoint
1 parent ba20277 commit f460133

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

src/webapp/routers/institutions.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pydantic import BaseModel
1010
from sqlalchemy.orm import Session
1111
from sqlalchemy.future import select
12-
from sqlalchemy import and_
12+
from sqlalchemy import and_, delete
1313
from google.cloud import storage
1414
from google.cloud.storage import Client
1515

@@ -25,6 +25,7 @@
2525
SchemaType,
2626
PDP_SCHEMA_GROUP,
2727
UsState,
28+
get_external_bucket_name,
2829
)
2930

3031
from ..gcsutil import StorageControl
@@ -254,12 +255,24 @@ def delete_inst(
254255
)
255256

256257
local_session.set(sql_session)
258+
query_result = (
259+
local_session.get()
260+
.execute(select(InstTable).where(InstTable.id == str_to_uuid(inst_id)))
261+
.all()
262+
)
263+
if len(query_result) != 1:
264+
raise HTTPException(
265+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
266+
detail="Unexpected number of institutions found. Expected 1 got "
267+
+ str(len(query_result)),
268+
)
269+
inst_name = query_result[0][0].name
257270
local_session.get().execute(
258271
delete(InstTable).where(InstTable.id == str_to_uuid(inst_id))
259272
)
260273
local_session.get().commit()
261274
# Delete GCS bucket
262-
bucket_name = get_external_bucket_name_from_uuid(query_result[0][0].id)
275+
bucket_name = get_external_bucket_name(inst_id)
263276
try:
264277
storage_control.delete_bucket(bucket_name)
265278
except ValueError as e:
@@ -269,7 +282,7 @@ def delete_inst(
269282
)
270283
# Delete all databricks managed pieces.
271284
try:
272-
databricks_control.delete_inst(query_result[0][0].name)
285+
databricks_control.delete_inst(inst_name)
273286
except Exception as e:
274287
raise HTTPException(
275288
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,

src/webapp/routers/models.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ def read_inst_model_outputs(
320320
"""Returns top-level info around all executions of a given model.
321321
322322
Only visible to users of that institution or Datakinder access types.
323+
Returns a list of runs in order of most recent to least recent based on triggering time.
323324
324325
Args:
325326
current_user: the user making the request.
@@ -351,22 +352,26 @@ def read_inst_model_outputs(
351352
)
352353
update_db_from_bucket(inst_id, local_session.get(), storage_control)
353354
local_session.get().commit()
354-
res = []
355-
for elem in query_result[0][0].jobs or []:
356-
# if not elem.output_filename:
355+
if not query_result[0][0].jobs:
356+
return []
357+
result = query_result[0][0].jobs
358+
result.sort(key=lambda x: x.triggered_at, reverse=True)
359+
ret_val = []
360+
for elem in result:
361+
# This will show incomplete runs as well.
357362
# TODO make a query to databricks to retrieve status.
358-
res.append(
363+
ret_val.append(
359364
{
360-
"inst_id": uuid_to_str(query_result[0][0].inst_id),
361-
"m_name": query_result[0][0].name,
365+
"inst_id": uuid_to_str(elem.inst_id),
366+
"m_name": elem.name,
362367
"run_id": elem.id,
363368
"created_by": uuid_to_str(elem.created_by),
364369
"triggered_at": elem.triggered_at,
365370
"batch_name": elem.batch_name,
366371
"output_filename": elem.output_filename,
367372
}
368373
)
369-
return res
374+
return ret_val
370375

371376

372377
@router.get(

src/webapp/routers/models_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def test_trigger_inference_run(client: TestClient):
323323
assert response.status_code == 400
324324
assert (
325325
response.text
326-
== '{"detail":"The files in this batch don\'t conform to the Schema configs allowed by this batch."}'
326+
== '{"detail":"The files in this batch don\'t conform to the schema configs allowed by this model."}'
327327
)
328328

329329
response = client.post(

src/webapp/utilities.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from datetime import datetime, timedelta, timezone
1717
from sqlalchemy.future import select
1818
from urllib.parse import unquote
19+
from sqlalchemy import and_
1920

2021
from .authn import (
2122
verify_password,
@@ -297,7 +298,7 @@ def authenticate_api_key(api_key_enduser_tuple: str, sess: Session) -> BaseUser:
297298
if inst:
298299
# If there's an institution set, ensure the user is part of the institution
299300
user_query = select(AccountTable).where(
300-
_and(
301+
and_(
301302
AccountTable.email == enduser,
302303
AccountTable.inst_id == uuid_to_str(inst),
303304
)

0 commit comments

Comments
 (0)