11from flask import Flask , jsonify ,request ,url_for
2- from db import SupabaseInterface
32from collections import defaultdict
43from flasgger import Swagger
54import re ,os ,traceback
5+ # from query import PostgresORM
66from utils import *
77from flask_cors import CORS ,cross_origin
88from v2_app import v2
9+ from flask_sqlalchemy import SQLAlchemy
10+ from models import db
11+ from shared_migrations .db import get_postgres_uri
12+ from shared_migrations .db .dmp_api import DmpAPIQueries
13+ from sqlalchemy .ext .asyncio import create_async_engine , AsyncSession
14+ from sqlalchemy .orm import sessionmaker
15+ from sqlalchemy .pool import NullPool
16+
917
1018
1119app = Flask (__name__ )
1220CORS (app ,supports_credentials = True )
1321
1422
23+ app .config ['SQLALCHEMY_DATABASE_URI' ] = get_postgres_uri ()
24+ app .config ['SQLALCHEMY_TRACK_MODIFICATIONS' ] = False
25+
26+ # Initialize Async SQLAlchemy
27+ engine = create_async_engine (app .config ['SQLALCHEMY_DATABASE_URI' ], echo = False ,poolclass = NullPool )
28+ async_session = sessionmaker (autocommit = False , autoflush = False , bind = engine , class_ = AsyncSession )
29+
30+
31+ db .init_app (app )
32+
1533Swagger (app )
1634
1735GITHUB_TOKEN = os .getenv ('GITHUB_TOKEN' )
@@ -45,67 +63,12 @@ def greeting():
4563
4664
4765
48-
49- @app .route ('/get-data' , methods = ['GET' ])
50- @cross_origin (supports_credentials = True )
51- @require_secret_key
52- def get_data ():
53- """
54- Fetch data from Supabase.
55- ---
56- responses:
57- 200:
58- description: Data fetched successfully
59- schema:
60- type: array
61- items:
62- type: object
63- 500:
64- description: Error fetching data
65- schema:
66- type: object
67- properties:
68- error:
69- type: string
70- """
71- try :
72- response = SupabaseInterface ().get_instance ().client .table ('dmp_pr_updates' ).select ('*' ).execute ()
73- data = response .data
74- return jsonify (data )
75- except Exception as e :
76- return jsonify ({'error' : str (e )}), 200
77-
78-
79-
80- @app .route ('/v1/issues' , methods = ['GET' ])
81- @require_secret_key
82- def v1get_issues ():
83- try :
84- response = SupabaseInterface ().get_instance ().client .table ('dmp_issue_updates' ).select ('*' ).execute ()
85- data = response .data
86-
87- #group data based on issues
88- grouped_data = defaultdict (list )
89- for record in data :
90- issue_url = record ['issue_url' ]
91- grouped_data [issue_url ].append ({
92- 'id' : record ['id' ],
93- 'name' : record ['body_text' ]
94- })
95-
96- result = [{'issue_url' : issue_url , 'issues' : issues } for issue_url , issues in grouped_data .items ()]
97- grouped_data = group_by_owner (result )
98- return jsonify (grouped_data )
99-
100- except Exception as e :
101- error_traceback = traceback .format_exc ()
102- return jsonify ({'error' : str (e ), 'traceback' : error_traceback }), 200
10366
10467
10568@app .route ('/issues' , methods = ['GET' ])
106- @cross_origin (supports_credentials = True )
107- @require_secret_key
108- def get_issues ():
69+ # @cross_origin(supports_credentials=True)
70+ # @require_secret_key
71+ async def get_issues ():
10972 """
11073 Fetch all issues and group by owner.
11174 ---
@@ -127,30 +90,28 @@ def get_issues():
12790 type: string
12891 """
12992 try :
130- # Fetch all issues with their details
131- response = SupabaseInterface ().get_instance ().client .table ('dmp_orgs' ).select ('*, dmp_issues(*)' ).execute ()
132- res = []
133-
134- for org in response .data :
135- obj = {}
136- issues = org ['dmp_issues' ]
137- obj ['org_id' ] = org ['id' ]
138- obj ['org_name' ] = org ['name' ]
139- renamed_issues = [{"id" : issue ["id" ], "name" : issue ["title" ]} for issue in issues ]
140- obj ['issues' ] = renamed_issues
141-
142- res .append (obj )
143-
144- return jsonify ({"issues" : res })
93+ # Fetch all issues with their details
94+ print ('inside get all issues' )
95+ data = await DmpAPIQueries .get_issue_query (async_session )
96+ response = []
97+
98+ for result in data :
99+ response .append ({
100+ 'org_id' : result .org_id ,
101+ 'org_name' : result .org_name ,
102+ 'issues' : result .issues
103+ })
104+
105+ return jsonify ({"issues" : response })
145106
146107 except Exception as e :
147108 error_traceback = traceback .format_exc ()
148109 return jsonify ({'error' : str (e ), 'traceback' : error_traceback }), 500
149110
150111@app .route ('/issues/<owner>' , methods = ['GET' ])
151- @cross_origin (supports_credentials = True )
152- @require_secret_key
153- def get_issues_by_owner (owner ):
112+ # @cross_origin(supports_credentials=True)
113+ # @require_secret_key
114+ async def get_issues_by_owner (owner ):
154115 """
155116 Fetch organization details by owner's GitHub URL.
156117 ---
@@ -190,16 +151,15 @@ def get_issues_by_owner(owner):
190151 description: Error message
191152 """
192153 try :
193- # Construct the GitHub URL based on the owner parameter
194- org_link = f"https://github.com/{ owner } "
195-
154+
196155 # Fetch organization details from dmp_orgs table
197- response = SupabaseInterface ().get_instance ().client .table ('dmp_orgs' ).select ('name' , 'description' ).eq ('name' , owner ).execute ()
198-
199- if not response .data :
156+ response = await DmpAPIQueries .get_issue_owner (async_session , owner )
157+ if not response :
200158 return jsonify ({'error' : "Organization not found" }), 404
201-
202- return jsonify (response .data )
159+
160+ orgs_dict = [org .to_dict () for org in response ]
161+
162+ return jsonify (orgs_dict )
203163
204164 except Exception as e :
205165 error_traceback = traceback .format_exc ()
@@ -243,7 +203,7 @@ def get_issues_by_owner_id(owner, issue):
243203 """
244204 try :
245205 print ('inside get issues' )
246- SUPABASE_DB = SupabaseInterface () .get_instance ()
206+ SUPABASE_DB = DmpAPIQueries .get_instance ()
247207 response = SUPABASE_DB .client .table ('dmp_issue_updates' ).select ('*' ).eq ('owner' , owner ).eq ('issue_number' , issue ).execute ()
248208 if not response .data :
249209 return jsonify ({'error' : "No data found" }), 200
0 commit comments