Skip to content

Commit

Permalink
Merge pull request #3 from Code4GovTech/dev
Browse files Browse the repository at this point in the history
dmp api changes and model changes
  • Loading branch information
jaanbaaz authored Dec 23, 2024
2 parents 77cb56d + 4cbeec2 commit 85b93ba
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 41 deletions.
124 changes: 85 additions & 39 deletions db/dmp_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


5 changes: 3 additions & 2 deletions db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<DmpIssues(id={self.id}, title={self.title})>"
Expand All @@ -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):
Expand All @@ -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')

Expand Down

0 comments on commit 85b93ba

Please sign in to comment.