Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

105 endpoint to retrieve content of a folder #111

Merged
merged 11 commits into from
Aug 1, 2024
91 changes: 74 additions & 17 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ async def before_serving():
if CONNECTION_STRING is None:
raise ServerError("Missing environment variable: NACHET_AZURE_STORAGE_CONNECTION_STRING")

if NACHET_DATA is None:
raise ServerError("Missing environment variable: NACHET_DATA")

if FERNET_KEY is None:
raise ServerError("Missing environment variable: FERNET_KEY")

Expand Down Expand Up @@ -176,11 +179,10 @@ async def before_serving():
"""
) #TODO Transform into logging

except (ServerError, inference.ModelAPIErrors) as e:
except (Exception, ServerError, inference.ModelAPIErrors) as e:
print(e)
raise


@app.post("/get-user-id")
async def get_user_id() :
"""
Expand Down Expand Up @@ -321,7 +323,7 @@ async def delete_with_archive():
print(error)
return jsonify([f"DeleteDirectoryRequestError: {str(error)}"]), 400


# Deprecated
@app.post("/dir")
async def list_directories():
"""
Expand All @@ -346,6 +348,73 @@ async def list_directories():
print(error)
return jsonify([f"ListDirectoriesRequestError: {str(error)}"]), 400

@app.post("/get-directories")
async def get_directories():
"""
get all directories in the user's container with pictures names and number of pictures
"""
try:
data = await request.get_json()
user_id = data["container_name"]
if user_id:
# Open db connection
connection = datastore.get_connection()
cursor = datastore.get_cursor(connection)

directories_list = await datastore.get_directories(cursor, str(user_id))

# Close connection
datastore.end_query(connection, cursor)

result = {"folders" : directories_list}
return jsonify(result)
else:
raise ListDirectoriesRequestError("Missing container name")

except (KeyError, TypeError, ListDirectoriesRequestError, azure_storage.MountContainerError, datastore.DatastoreError) as error:
print(error)
return jsonify([f"ListDirectoriesRequestError: {str(error)}"]), 400

@app.post("/get-picture")
async def get_picture():
"""
get all directories in the user's container with pictures names and number of pictures
"""
try:
data = await request.get_json()
container_name = data["container_name"]
user_id = container_name
picture_id = data["picture_id"]

if user_id:

container_client = await azure_storage.mount_container(
CONNECTION_STRING, container_name, create_container=True
)
# Open db connection
connection = datastore.get_connection()
cursor = datastore.get_cursor(connection)

picture = {}
picture["picture_id"] = picture_id

inference = await datastore.get_inference(cursor, str(user_id), str(picture_id))
picture["inference"] = inference

blob = await datastore.get_picture_blob(cursor, str(user_id), container_client, str(picture_id))
image_base64 = base64.b64encode(blob)
picture["image"] = "data:image/tiff;base64," + image_base64.decode("utf-8")

# Close connection
datastore.end_query(connection, cursor)
return jsonify(picture)
else:
raise ListDirectoriesRequestError("Missing container name")

except (KeyError, TypeError, ListDirectoriesRequestError, azure_storage.MountContainerError, datastore.DatastoreError) as error:
print(error)
return jsonify([f"ListDirectoriesRequestError: {str(error)}"]), 400


@app.post("/create-dir")
async def create_directory():
Expand All @@ -371,7 +440,7 @@ async def create_directory():
if response:
return jsonify([response]), 200
else:
raise CreateDirectoryRequestError("directory already exists")
raise CreateDirectoryRequestError("Error while creating directory")
else:
raise CreateDirectoryRequestError("missing container or directory name")

Expand Down Expand Up @@ -487,9 +556,8 @@ async def inference_request():
connection = datastore.get_connection()
cursor = datastore.get_cursor(connection)

image_hash_value = await azure_storage.generate_hash(image_bytes)
picture_id = await datastore.get_picture_id(
cursor, user_id, image_hash_value, container_client
cursor, user_id, image_bytes, container_client
)
# Close connection
datastore.end_query(connection, cursor)
Expand All @@ -508,17 +576,6 @@ async def inference_request():
cache_json_result[-1], imageDims, area_ratio, color_format
)

result_json_string = await record_model(pipeline, processed_result_json)

# upload the inference results to the user's container as async task
app.add_background_task(
azure_storage.upload_inference_result,
container_client,
folder_name,
result_json_string,
image_hash_value,
)

# Open db connection
connection = datastore.get_connection()
cursor = datastore.get_cursor(connection)
Expand Down
45 changes: 42 additions & 3 deletions docs/nachet-manage-folders.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,49 @@ note left of FE : "Are you sure ? Everything in this folder will be deleted and
The `create-dir` route need a folder_name and create the folder in database and
in Azure Blob storage.

### /dir
### /get-directories

The `get-directories` route retreives all user directories from the database
with their pictures as a json. There is 4 different cases for the pictures :

| **is_verified \\ inference_exist** | **false** | **true** |
|------------------------------------|----------------------|-----------------------|
| **false** | *should not happend* | inference not verified |
| **true** | batch import | inference verified |

```json
{
"folders" : [
{
"picture_set_id" : "xxxx-xxxx-xxxx-xxxx",
"folder_name" : "folder name",
"nb_pictures": 4,
"pictures" : [
{
"picture_id" : "xxxx-xxxx-xxxx-xxxx",
"inference_exist": false,
"is_validated": true
},
...
]
},
...
]
}
```

### /get-picture

The `dir` route retreives all user directories from the database (id, name and
nb_pictures).
The `get-picture` route retreives selected picture as a json :

```json
{
"picture_id" : "xxxx-xxxx-xxxx-xxxx",
"inference": {
}
"image": "data:image/...;base64,xxxxxxxx"
}
```

### /delete-request

Expand Down
9 changes: 8 additions & 1 deletion model/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ async def request_inference_from_test(model: namedtuple, previous_result: str):
},
],
}
]
],
"models" :
[
{
"name" : model.name,
"version" : 1
}
]
}
]

Expand Down
24 changes: 21 additions & 3 deletions storage/datastore_storage_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class UserNotFoundError(DatastoreError):
NACHET_DB_URL = os.getenv("NACHET_DB_URL")
NACHET_SCHEMA = os.getenv("NACHET_SCHEMA")

if NACHET_DB_URL is None:
raise DatastoreError("Missing environment variable: NACHET_DB_URL")

if NACHET_SCHEMA is None:
raise DatastoreError("Missing environment variable: NACHET_SCHEMA")

def get_connection() :
return db.connect_db(NACHET_DB_URL, NACHET_SCHEMA)

Expand Down Expand Up @@ -90,11 +96,11 @@ async def create_user(email: str, connection_string) -> datastore.User:
return user


async def get_picture_id(cursor, user_id, image_hash_value, container_client) :
async def get_picture_id(cursor, user_id, image, container_client) :
"""
Return the picture_id of the image
"""
picture_id = await nachet_datastore.upload_picture_unknown(cursor, str(user_id), image_hash_value, container_client)
picture_id = await nachet_datastore.upload_picture_unknown(cursor, str(user_id), image, container_client)
return picture_id

def upload_pictures(cursor, user_id, picture_set_id, container_client, pictures, seed_name: str, zoom_level: float = None, nb_seeds: int = None) :
Expand Down Expand Up @@ -151,6 +157,18 @@ async def delete_directory_with_archive(cursor, user_id, picture_set_id, contain

async def get_directories(cursor, user_id):
try :
return await datastore.get_picture_sets_info(cursor, user_id)
return await nachet_datastore.get_picture_sets_info(cursor, user_id)
except Exception as error:
raise DatastoreError(error)

async def get_inference(cursor, user_id, picture_id):
try :
return await nachet_datastore.get_picture_inference(cursor, user_id, picture_id)
except Exception as error:
raise DatastoreError(error)

async def get_picture_blob(cursor, user_id, container_client, picture_id):
try :
return await nachet_datastore.get_picture_blob(cursor, user_id, container_client, picture_id)
except Exception as error:
raise DatastoreError(error)
Loading
Loading