Skip to content

Commit

Permalink
Refactor result handling: Update query filters, enhance export functi…
Browse files Browse the repository at this point in the history
…onality, and implement result deletion
  • Loading branch information
Erik172 committed Dec 8, 2024
1 parent 72e9585 commit 7428695
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 43 deletions.
5 changes: 3 additions & 2 deletions app/resources/desd/desd_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .desd_processing import DESDProcessing

class DESDResource(Resource):
#TODO: Dejar de manejar los modelos en un archivo de configuración a base de datos
@jwt_required()
def post(self):
required_fields = ['result_id', 'models']
Expand All @@ -26,7 +27,7 @@ def post(self):
result_id = request.form.get('result_id')

# Verificar y crear el resultado si es necesario
result = Result.query.filter_by(result_id=result_id).first()
result = Result.query.filter_by(collection_id=result_id).first()
if result:
if result.status in [ResultStatusEnum.PROCESSING, ResultStatusEnum.COMPLETED, ResultStatusEnum.ERROR]:
return {"message": f"The result has status {result.status}"}, 400
Expand All @@ -44,7 +45,7 @@ def post(self):
db.session.add(result_status)
db.session.commit()

models = {model: ModelAIService(model) for model in model_names if model in ['inclinacion', 'rotacion', 'corte_informacion']}
models = {model: ModelAIService(model) for model in model_names if model in ['inclinacion', 'rotacion', 'corte informacion']}

processed_files = 0
for file in files:
Expand Down
4 changes: 2 additions & 2 deletions app/resources/result/export_result_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
class ExportResultResource(Resource):
def get(self, collection_name: str):
result_service = ResultService()
filename = result_service.export_result(collection_name)
filename = result_service.export_collection(collection_name)
if not filename:
return {"message": "No data found"}, 404

file_path = f"exports/{filename}"
file_path = f"..\\exports\\{filename}"
return send_file(file_path, as_attachment=True, mimetype='csv')
35 changes: 34 additions & 1 deletion app/resources/result/result_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from flask_jwt_extended import jwt_required
from flask_jwt_extended import current_user
from flask import request
from app.models import Result
from app import db, mongo
from app.models import Result, ResultStatus
from app.schemas import ResultSchema
import os

class ResultResource(Resource):
@jwt_required()
Expand All @@ -21,12 +23,19 @@ def get(self, result_id: str = None):

page = request.args.get('page', 1, type=int)
per_page = request.args.get('per_page', 10, type=int)
order_by = request.args.get('order_by', 'created_at')
order_type = request.args.get('order_type', 'desc')

query = Result.query

if request.args.get('user_id'):
query = query.filter_by(user_id=request.args.get('user_id'))

if order_type == 'desc':
query = query.order_by(getattr(Result, order_by).desc())
else:
query = query.order_by(getattr(Result, order_by))

result_query = query.paginate(page=page, per_page=per_page, error_out=False)
result = result_query.items

Expand All @@ -35,8 +44,32 @@ def get(self, result_id: str = None):
return {
'total': result_query.total,
'page': result_query.page,
'pages': result_query.pages,
'per_page': result_query.per_page,
'results': schema.dump(result)
}, 200

@jwt_required()
def delete(self, result_id: str):
result = Result.query.filter_by(collection_id=result_id).first()
result_status = ResultStatus.query.filter_by(result_id=result.id).first()
if not result:
return {'message': 'Result not found'}, 404

if not current_user.is_admin and result.user_id != current_user.id:
return {'message': 'Unauthorized'}, 401

try:
os.remove(f'exports/{result_id}.csv')
except FileNotFoundError:
pass

db.session.delete(result)
db.session.delete(result_status)
db.session.commit()

mongo[result_id].drop()

return {'message': 'Result deleted'}, 200


2 changes: 1 addition & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def initialize_routes(api: Api) -> None:
api.add_resource(DESDResource, '/api/v1/desd', endpoint='desd')

api.add_resource(ResultResource, '/api/v1/results', '/api/v1/results/<string:result_id>', endpoint='results')
api.add_resource(ExportResultResource, '/api/v1/results/<string:resultado_id>/export', endpoint='export_result')
api.add_resource(ExportResultResource, '/api/v1/results/<string:collection_name>/export', endpoint='export_result')
9 changes: 8 additions & 1 deletion app/schemas/result_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from marshmallow import validates, ValidationError, fields
from app.models import Result, ResultStatus
from app.models import Result, ResultStatus, User
from app import ma

class ResultSchema(ma.SQLAlchemyAutoSchema):
Expand All @@ -13,10 +13,17 @@ class Meta:
collection_id = fields.String(required=True)
alias = fields.String()
user_id = fields.Integer(required=True)
user = fields.Method('get_user', dump_only=True)
created_at = fields.DateTime(dump_only=True)
updated_at = fields.DateTime(dump_only=True)
status = fields.Method('get_status', dump_only=True)

def get_user(self, obj):
from app.schemas import UserSchema
user = User.query.get(obj.user_id)
schema = UserSchema(only=('id', 'name', 'email'))
return schema.dump(user)

def get_status(self, obj):
from app.schemas import ResultStatusSchema
result_status = ResultStatus.query.filter_by(result_id=obj.id).first()
Expand Down
1 change: 1 addition & 0 deletions app/services/dude/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hashlib
import os

#TODO: Implemetar DuDe
class DuDeBase:
def __init__(self, data_dir: str):
self.duplicates = {}
Expand Down
1 change: 1 addition & 0 deletions app/services/model_ai_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, model_name: str, task: str = 'classify') -> None:
model (YOLO): La instancia del modelo YOLO cargado.
results (dict): Un diccionario para almacenar los resultados del modelo.
"""
#TODO: Implementar la carga de modelos desde la base de datos
config_file = Path('models.yml')
if not config_file.exists():
raise FileNotFoundError(f"Configuration file '{config_file}' not found.")
Expand Down
18 changes: 15 additions & 3 deletions app/services/result_service.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import csv
import os
from app import mongo

class ResultService:
def export_collection(self, collection_name: str) -> str:
cursor = mongo[collection_name].find()
if cursor.count() == 0:
"""
Exporta una colección de MongoDB a un archivo CSV.
Args:
collection_name (str): El nombre de la colección a exportar.
Returns:
str: El nombre del archivo CSV exportado, o None si la colección está vacía.
"""
collection = mongo[collection_name]
if collection.count_documents({}) == 0:
return None

cursor = collection.find()
file_name = f"{collection_name}.csv"
file_path = f"exports/{file_name}"
file_path = os.path.join('exports', file_name)

# Crear la carpeta 'exports' si no existe
os.makedirs(os.path.dirname(file_path), exist_ok=True)

with open(file_path, "w", newline='') as f:
writer = csv.DictWriter(f, fieldnames=cursor[0].keys())
Expand Down
4 changes: 3 additions & 1 deletion app/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .parse_results import parse_result_yolov8
from .parse_results import parse_result_yolov8

__all__ = ['parse_result_yolov8']
31 changes: 0 additions & 31 deletions app/utils/random_generator.py

This file was deleted.

2 changes: 1 addition & 1 deletion models.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rotacion: app/ai_models/rode_v2.pt
inclinacion: app/ai_models/tilde_v1.pt
corte_informacion: app/ai_models/cude_v1.5.pt
corte informacion: app/ai_models/cude_v1.5.pt

0 comments on commit 7428695

Please sign in to comment.