Skip to content

Commit ac775c3

Browse files
committed
convertion query to ORM
1 parent ed8ede0 commit ac775c3

File tree

5 files changed

+231
-12
lines changed

5 files changed

+231
-12
lines changed

app.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
from collections import defaultdict
44
from flasgger import Swagger
55
import re,os,traceback
6-
from query import PostgresQuery
6+
from query import PostgresQuery,PostgresORM
77
from utils import *
88
from flask_cors import CORS,cross_origin
99
from v2_app import v2
10+
from flask_sqlalchemy import SQLAlchemy
11+
from models import db
12+
1013

1114

1215
app = Flask(__name__)
1316
CORS(app,supports_credentials=True)
1417

1518

19+
app.config['SQLALCHEMY_DATABASE_URI'] = SupabaseInterface.get_postgres_uri()
20+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
21+
22+
db.init_app(app)
23+
1624
Swagger(app)
1725

1826
GITHUB_TOKEN =os.getenv('GITHUB_TOKEN')
@@ -128,9 +136,16 @@ def get_issues():
128136
type: string
129137
"""
130138
try:
131-
# Fetch all issues with their details
139+
# Fetch all issues with their details
140+
data = PostgresORM.get_issue_query()
141+
response = []
132142

133-
response = PostgresQuery.get_issue_query()
143+
for result in data:
144+
response.append({
145+
'org_id': result.org_id,
146+
'org_name': result.org_name,
147+
'issues': result.issues
148+
})
134149

135150
return jsonify({"issues": response})
136151

@@ -183,12 +198,13 @@ def get_issues_by_owner(owner):
183198
try:
184199

185200
# Fetch organization details from dmp_orgs table
186-
response = PostgresQuery.get_issue_owner(owner)
187-
201+
response = PostgresORM.get_issue_owner(owner)
188202
if not response:
189203
return jsonify({'error': "Organization not found"}), 404
190-
191-
return jsonify(response)
204+
205+
orgs_dict = [org.to_dict() for org in response]
206+
207+
return jsonify(orgs_dict)
192208

193209
except Exception as e:
194210
error_traceback = traceback.format_exc()

db.py

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ def get_postgres_connection():
5656
)
5757
return conn
5858

59+
def get_postgres_uri():
60+
DB_HOST = os.getenv('POSTGRES_DB_HOST')
61+
DB_NAME = os.getenv('POSTGRES_DB_NAME')
62+
DB_USER = os.getenv('POSTGRES_DB_USER')
63+
DB_PASS = os.getenv('POSTGRES_DB_PASS')
64+
65+
return f'postgresql://{DB_USER}:{DB_PASS}@{DB_HOST}/{DB_NAME}'
66+
5967
def postgres_query(query,params=None):
6068
conn = SupabaseInterface.get_postgres_connection()
6169

models.py

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
from flask import Flask
2+
from flask_sqlalchemy import SQLAlchemy
3+
from datetime import datetime
4+
5+
db = SQLAlchemy()
6+
7+
class DmpOrg(db.Model):
8+
__tablename__ = 'dmp_orgs'
9+
10+
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
11+
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
12+
name = db.Column(db.String, nullable=False)
13+
description = db.Column(db.Text, nullable=True)
14+
link = db.Column(db.String, nullable=False)
15+
repo_owner = db.Column(db.String, nullable=False)
16+
17+
# Relationship to DmpIssueUpdate
18+
issues = db.relationship('DmpIssueUpdate', backref='organization', lazy=True)
19+
20+
# Updated relationship name to avoid conflict
21+
dmp_issues = db.relationship('DmpIssue', backref='organization', lazy=True)
22+
23+
def __repr__(self):
24+
return f"<DmpOrg(id={self.id}, name={self.name})>"
25+
26+
def to_dict(self):
27+
return {
28+
'id': self.id,
29+
'created_at': self.created_at.isoformat(),
30+
'name': self.name,
31+
'description': self.description,
32+
'link': self.link,
33+
'repo_owner': self.repo_owner
34+
}
35+
36+
class DmpIssue(db.Model):
37+
__tablename__ = 'dmp_issues'
38+
39+
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
40+
issue_url = db.Column(db.String, nullable=False)
41+
issue_number = db.Column(db.Integer, nullable=False)
42+
mentor_username = db.Column(db.String, nullable=True)
43+
contributor_username = db.Column(db.String, nullable=True)
44+
title = db.Column(db.String, nullable=False)
45+
org_id = db.Column(db.Integer, db.ForeignKey('dmp_orgs.id'), nullable=False)
46+
description = db.Column(db.Text, nullable=True)
47+
repo = db.Column(db.String, nullable=True)
48+
49+
50+
# Relationship to Prupdates
51+
pr_updates = db.relationship('Prupdates', backref='pr_details', lazy=True)
52+
53+
def __repr__(self):
54+
return f"<DmpIssue(id={self.id}, title={self.title})>"
55+
56+
def to_dict(self):
57+
return {
58+
'id': self.id,
59+
'issue_url': self.issue_url,
60+
'issue_number': self.issue_number,
61+
'mentor_username': self.mentor_username,
62+
'contributor_username': self.contributor_username,
63+
'title': self.title,
64+
'org_id': self.org_id,
65+
'description': self.description,
66+
'repo': self.repo
67+
}
68+
69+
class DmpIssueUpdate(db.Model):
70+
__tablename__ = 'dmp_issue_updates'
71+
72+
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
73+
body_text = db.Column(db.Text, nullable=False)
74+
comment_link = db.Column(db.String, nullable=False)
75+
comment_id = db.Column(db.BigInteger, primary_key=True, nullable=False)
76+
comment_api = db.Column(db.String, nullable=False)
77+
comment_updated_at = db.Column(db.DateTime, nullable=False)
78+
dmp_id = db.Column(db.Integer, db.ForeignKey('dmp_orgs.id'), nullable=False)
79+
created_by = db.Column(db.String, nullable=False)
80+
81+
def __repr__(self):
82+
return f"<DmpIssueUpdate(comment_id={self.comment_id}, dmp_id={self.dmp_id})>"
83+
84+
def to_dict(self):
85+
return {
86+
'created_at': self.created_at.isoformat(),
87+
'body_text': self.body_text,
88+
'comment_link': self.comment_link,
89+
'comment_id': self.comment_id,
90+
'comment_api': self.comment_api,
91+
'comment_updated_at': self.comment_updated_at.isoformat(),
92+
'dmp_id': self.dmp_id,
93+
'created_by': self.created_by
94+
}
95+
96+
class Prupdates(db.Model):
97+
__tablename__ = 'dmp_pr_updates'
98+
99+
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
100+
pr_id = db.Column(db.Integer, nullable=False,primary_key=True)
101+
status = db.Column(db.String, nullable=False)
102+
title = db.Column(db.String, nullable=False)
103+
pr_updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
104+
merged_at = db.Column(db.DateTime)
105+
closed_at = db.Column(db.DateTime)
106+
dmp_id = db.Column(db.Integer, db.ForeignKey('dmp_issues.id'), nullable=False) # ForeignKey relationship
107+
link = db.Column(db.String, nullable=False)
108+
109+
def __repr__(self):
110+
return f'<PullRequest {self.pr_id} - {self.title}>'
111+
112+
def to_dict(self):
113+
return {
114+
'created_at': self.created_at.isoformat(),
115+
'pr_id': self.pr_id,
116+
'status': self.status,
117+
'title': self.title,
118+
'pr_updated_at': self.pr_updated_at.isoformat(),
119+
'merged_at': self.merged_at.isoformat() if self.merged_at else None,
120+
'closed_at': self.closed_at.isoformat() if self.closed_at else None,
121+
'dmp_id': self.dmp_id,
122+
'link': self.link
123+
}
124+
125+
class DmpWeekUpdate(db.Model):
126+
__tablename__ = 'dmp_week_updates'
127+
128+
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
129+
issue_url = db.Column(db.String, nullable=False)
130+
week = db.Column(db.Integer, nullable=False)
131+
total_task = db.Column(db.Integer, nullable=False)
132+
completed_task = db.Column(db.Integer, nullable=False)
133+
progress = db.Column(db.Integer, nullable=False)
134+
task_data = db.Column(db.Text, nullable=False)
135+
dmp_id = db.Column(db.Integer, nullable=False)
136+
137+
def __repr__(self):
138+
return f"<DmpWeekUpdate(id={self.id}, week={self.week}, dmp_id={self.dmp_id})>"
139+
140+
# if __name__ == '__main__':
141+
# db.create_all()

query.py

+54
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from db import SupabaseInterface
2+
from models import *
3+
from sqlalchemy import func
24

35
class PostgresQuery:
46

@@ -75,4 +77,56 @@ def get_pr_data(dmp_issue_id):
7577
"""
7678
data = SupabaseInterface.postgres_query(query,(dmp_issue_id,))
7779
return data
80+
81+
82+
83+
class PostgresORM:
84+
85+
def get_issue_query():
86+
results = (
87+
db.session.query(
88+
DmpOrg.id.label('org_id'),
89+
DmpOrg.name.label('org_name'),
90+
func.json_agg(
91+
func.json_build_object(
92+
'id', DmpIssue.id,
93+
'name', DmpIssue.title
94+
)
95+
).label('issues')
96+
)
97+
.outerjoin(DmpIssue, DmpOrg.id == DmpIssue.org_id)
98+
.group_by(DmpOrg.id)
99+
.order_by(DmpOrg.id)
100+
.all()
101+
)
102+
103+
return results
104+
105+
def get_issue_owner(name):
106+
response = DmpOrg.query.filter_by(name=name).all()
107+
return response
108+
109+
def get_actual_owner_query(owner):
110+
results = DmpOrg.query.filter(DmpOrg.name.like(f'%{owner}%')).all()
111+
results = [val.to_dict() for val in results]
112+
return results
113+
114+
115+
def get_dmp_issues(issue_id):
116+
results = DmpIssue.query.filter_by(id=issue_id).all()
117+
results = [val.to_dict() for val in results]
118+
return results
119+
120+
121+
def get_dmp_issue_updates(dmp_issue_id):
122+
results = DmpIssueUpdate.query.filter_by(dmp_id=dmp_issue_id).all()
123+
results = [val.to_dict() for val in results]
124+
return results
125+
126+
127+
def get_pr_data(dmp_issue_id):
128+
pr_updates = Prupdates.query.filter_by(dmp_id=dmp_issue_id).all()
129+
pr_updates_dict = [pr_update.to_dict() for pr_update in pr_updates]
130+
return pr_updates_dict
131+
78132

v2_app.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from db import SupabaseInterface
66
from utils import determine_week
77
from v2_utils import calculate_overall_progress, define_link_data, week_data_formatter
8-
from query import PostgresQuery
8+
from query import PostgresQuery,PostgresORM
99

1010

1111
v2 = Blueprint('v2', __name__)
@@ -20,19 +20,19 @@ def get_issues_by_owner_id_v2(owner, issue):
2020

2121
url = f"https://github.com/{owner}"
2222

23-
actual_owner = PostgresQuery.get_actual_owner_query(owner)
23+
actual_owner = PostgresORM.get_actual_owner_query(owner)
2424
repo_owner =actual_owner[0]['repo_owner'] if actual_owner else ""
2525
#create url with repo owner
2626
url = f"https://github.com/{repo_owner}" if repo_owner else None
2727

28-
dmp_issue_id = PostgresQuery.get_dmp_issues(issue)
28+
dmp_issue_id = PostgresORM.get_dmp_issues(issue)
2929
if not dmp_issue_id:
3030
print(f"url....{url}....{issue}")
3131
return jsonify({'error': "No data found in dmp_issue"}), 500
3232

3333
dmp_issue_id = dmp_issue_id[0]
3434

35-
response = PostgresQuery.get_dmp_issue_updates(dmp_issue_id['id'])
35+
response = PostgresORM.get_dmp_issue_updates(dmp_issue_id['id'])
3636
if not response:
3737
print(f"dmp_issue_id....{response}....{dmp_issue_id}")
3838
return jsonify({'error': "No data found in dmp_issue_updates"}), 500
@@ -85,7 +85,7 @@ def get_issues_by_owner_id_v2(owner, issue):
8585
}
8686

8787

88-
pr_Data = PostgresQuery.get_pr_data(dmp_issue_id['id'])
88+
pr_Data = PostgresORM.get_pr_data(dmp_issue_id['id'])
8989
transformed = {"pr_details": []}
9090
if pr_Data:
9191
for pr in pr_Data:

0 commit comments

Comments
 (0)