1
1
from flask import Flask , jsonify ,request ,url_for
2
- from db import SupabaseInterface
3
2
from collections import defaultdict
4
3
from flasgger import Swagger
5
4
import re ,os ,traceback
5
+ # from query import PostgresORM
6
6
from utils import *
7
7
from flask_cors import CORS ,cross_origin
8
8
from 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
+
9
17
10
18
11
19
app = Flask (__name__ )
12
20
CORS (app ,supports_credentials = True )
13
21
14
22
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
+
15
33
Swagger (app )
16
34
17
35
GITHUB_TOKEN = os .getenv ('GITHUB_TOKEN' )
@@ -45,67 +63,12 @@ def greeting():
45
63
46
64
47
65
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
103
66
104
67
105
68
@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 ():
109
72
"""
110
73
Fetch all issues and group by owner.
111
74
---
@@ -127,30 +90,28 @@ def get_issues():
127
90
type: string
128
91
"""
129
92
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 })
145
106
146
107
except Exception as e :
147
108
error_traceback = traceback .format_exc ()
148
109
return jsonify ({'error' : str (e ), 'traceback' : error_traceback }), 500
149
110
150
111
@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 ):
154
115
"""
155
116
Fetch organization details by owner's GitHub URL.
156
117
---
@@ -190,16 +151,15 @@ def get_issues_by_owner(owner):
190
151
description: Error message
191
152
"""
192
153
try :
193
- # Construct the GitHub URL based on the owner parameter
194
- org_link = f"https://github.com/{ owner } "
195
-
154
+
196
155
# 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 :
200
158
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 )
203
163
204
164
except Exception as e :
205
165
error_traceback = traceback .format_exc ()
@@ -243,7 +203,7 @@ def get_issues_by_owner_id(owner, issue):
243
203
"""
244
204
try :
245
205
print ('inside get issues' )
246
- SUPABASE_DB = SupabaseInterface () .get_instance ()
206
+ SUPABASE_DB = DmpAPIQueries .get_instance ()
247
207
response = SUPABASE_DB .client .table ('dmp_issue_updates' ).select ('*' ).eq ('owner' , owner ).eq ('issue_number' , issue ).execute ()
248
208
if not response .data :
249
209
return jsonify ({'error' : "No data found" }), 200
0 commit comments