diff --git a/db/dmp_api.py b/db/dmp_api.py index 49c5e50..1093eb1 100644 --- a/db/dmp_api.py +++ b/db/dmp_api.py @@ -4,58 +4,104 @@ from dotenv import load_dotenv # from flask_sqlalchemy import SQLAlchemy import sqlalchemy +from sqlalchemy.future import select -# load_dotenv() -db = sqlalchemy() - class DmpAPIQueries: - def get_issue_query(): - results = ( - db.session.query( - DmpOrgs.id.label('org_id'), - DmpOrgs.name.label('org_name'), - func.json_agg( - func.json_build_object( - 'id', DmpIssues.id, - 'name', DmpIssues.title + async def get_issue_query(async_session): + try: + async with async_session() as session: + results = await session.execute( + select( + DmpOrgs.id.label('org_id'), + DmpOrgs.name.label('org_name'), + func.json_agg( + func.json_build_object( + 'id', DmpIssues.id, + 'name', DmpIssues.title + ) + ).label('issues') ) - ).label('issues') - ) - .outerjoin(DmpIssues, DmpOrgs.id == DmpIssues.org_id) - .group_by(DmpOrgs.id) - .order_by(DmpOrgs.id) - .all() - ) + .outerjoin(DmpIssues, DmpOrgs.id == DmpIssues.org_id) + .group_by(DmpOrgs.id) + .order_by(DmpOrgs.id) + ) + + # Extract results as a list of dictionaries if needed + data = results.all() + + return data + except Exception as e: + print(f"An error occurred: get_column_value {e}") + return None - return results - def get_issue_owner(name): - response = DmpOrgs.query.filter_by(name=name).all() - return response + async def get_issue_owner(async_session, name): + try: + async with async_session() as session: + response = await session.execute( + select(DmpOrgs).filter_by(name=name) + ) + results = response.scalars().all() + return results + except Exception as e: + print(f"An error occurred: get_column_value {e}") + return None - def get_actual_owner_query(owner): - results = DmpIssues.query.filter(DmpIssues.repo_owner.like(f'%{owner}%')).all() - results = [val.to_dict() for val in results] - return results + async def get_actual_owner_query(async_session, owner): + try: + async with async_session() as session: + response = await session.execute( + select(DmpIssues).filter(DmpIssues.repo_owner.like(f'%{owner}%')) + ) + results = response.scalars().all() # Fetch all matching rows as objects + results = [val.to_dict() for val in results] # Convert objects to dicts + return results + except Exception as e: + print(f"An error occurred: get_column_value {e}") + return None - def get_dmp_issues(issue_id): - results = DmpIssues.query.filter_by(id=issue_id).all() - results = [val.to_dict() for val in results] - return results + async def get_dmp_issues(async_session, issue_id): + try: + async with async_session() as session: + response = await session.execute( + select(DmpIssues).filter_by(id=issue_id) + ) + results = response.scalars().all() # Fetch all matching rows as objects + results = [val.to_dict() for val in results] # Convert objects to dicts + return results + except Exception as e: + print(f"An error occurred: get_column_value {e}") + return None - def get_dmp_issue_updates(dmp_issue_id): - results = DmpIssueUpdates.query.filter_by(dmp_id=dmp_issue_id).all() - results = [val.to_dict() for val in results] - return results + async def get_dmp_issue_updates(async_session, dmp_issue_id): + try: + async with async_session() as session: + response = await session.execute( + select(DmpIssueUpdates).filter_by(dmp_id=dmp_issue_id) + ) + results = response.scalars().all() # Fetch all matching rows as objects + results = [val.to_dict() for val in results] # Convert objects to dicts + return results + except Exception as e: + print(f"An error occurred: get_column_value {e}") + return None - def get_pr_data(dmp_issue_id): - pr_updates = DmpPrUpdates.query.filter_by(dmp_id=dmp_issue_id).all() - pr_updates_dict = [pr_update.to_dict() for pr_update in pr_updates] - return pr_updates_dict + async def get_pr_data(async_session, dmp_issue_id): + try: + async with async_session() as session: + response = await session.execute( + select(DmpPrUpdates).filter_by(dmp_id=dmp_issue_id) + ) + pr_updates = response.scalars().all() # Fetch all matching rows as objects + pr_updates_dict = [pr_update.to_dict() for pr_update in pr_updates] # Convert objects to dicts + return pr_updates_dict + except Exception as e: + print(f"An error occurred: get_column_value {e}") + return None \ No newline at end of file diff --git a/db/models.py b/db/models.py index 2a82f37..bd05abe 100644 --- a/db/models.py +++ b/db/models.py @@ -378,6 +378,7 @@ class DmpIssues(Base): org_id = Column(BigInteger, ForeignKey('dmp_orgs.id'), nullable=False) description = Column(Text, nullable=False) repo = Column(Text, nullable=False) + repo_owner = Column(Text, nullable=False) def __repr__(self): return f"" @@ -392,7 +393,8 @@ def to_dict(self): 'title': self.title, 'org_id': self.org_id, 'description': self.description, - 'repo': self.repo + 'repo': self.repo, + 'repo_owner': self.repo_owner } class DmpOrgs(Base): @@ -404,7 +406,6 @@ class DmpOrgs(Base): description = Column(Text, nullable=False) link = Column(Text, nullable=False) repo_owner = Column(Text, nullable=False) - version = Column(Text, nullable=True) # issues = relationship('Issues', backref='organization', lazy='joined')