11# app.py
22from quart import Quart
3- import httpx
4- import os ,markdown2
5- from db import SupabaseInterface
3+ import os , markdown2 , httpx
64from apscheduler .schedulers .asyncio import AsyncIOScheduler
75from dotenv import load_dotenv
8- from datetime import datetime
9-
6+ from datetime import datetime , timezone
7+ # from query import PostgresORM
8+ from shared_migrations .db import PostgresORM , get_postgres_uri
9+ from shared_migrations .db .dmp_cron import DmpCronQueries
1010from utils import handle_week_data , parse_issue_description
11+ from sqlalchemy .ext .asyncio import create_async_engine , AsyncSession
12+ from sqlalchemy .orm import sessionmaker
13+ from datetime import datetime
14+ from shared_migrations .db .models import *
15+ from sqlalchemy .pool import NullPool
1116
1217# Load environment variables from .env file
1318load_dotenv ()
1419delay_mins : str = os .getenv ("SCHEDULER_DELAY_IN_MINS" )
1520
1621app = Quart (__name__ )
1722
23+ # Initialize Quart app
24+ app .config ['SQLALCHEMY_DATABASE_URI' ] = get_postgres_uri ()
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+
1830scheduler = AsyncIOScheduler ()
1931
2032
@@ -88,10 +100,9 @@ async def dmp_updates():
88100 GITHUB_TOKEN = os .getenv ('GITHUB_TOKEN' )
89101 try :
90102 TARGET_DATE = os .getenv ('TARGET_DATE' )
91- db = SupabaseInterface ().get_instance ()
92103
93104 # Loop through all dmp issues
94- dmp_tickets = db . get_dmp_issues ( )
105+ dmp_tickets = await DmpCronQueries . get_all_dmp_issues ( async_session )
95106
96107 for dmp in dmp_tickets :
97108 dmp_id = dmp ['id' ]
@@ -100,7 +111,7 @@ async def dmp_updates():
100111 repo = dmp ['repo' ]
101112 owner = dmp ['repo_owner' ]
102113
103- app .logger .info ("DMP_ID: " + str (dmp_id ))
114+ app .logger .info ("DMP_ID: " + str (dmp_id ))
104115
105116 # # Make the HTTP request to GitHub API
106117 headers = {
@@ -120,18 +131,22 @@ async def dmp_updates():
120131 # Parse issue discription
121132 print ('processing description ' )
122133 issue_update = define_issue_description_update (issue_response .json ())
123-
124- issue_update ['mentor_username' ] = dmp ['mentor_username' ] #get from db
125- issue_update ['contributor_username' ] = dmp ['contributor_username' ] # get from db
126-
134+
135+ issue_update ['mentor_username' ] = dmp ['mentor_username' ] # get from db
136+ issue_update ['contributor_username' ] = dmp ['contributor_username' ] # get from db
137+
127138 app .logger .info ('Decription from remote: ' , issue_update )
128- update_data = db .update_data (
129- issue_update , 'dmp_issues' , 'id' , dmp_id )
139+
140+ update_data = await DmpCronQueries .update_dmp_issue (async_session , issue_id = dmp_id ,
141+ update_data = issue_update )
142+
143+ print (f"dmp_issue update works - dmp_id { dmp_id } " ) if update_data else print (
144+ f"dmp_issue update failed - dmp_id { dmp_id } " )
130145 app .logger .info (update_data )
131146 else :
132147 print ('issue response ' , issue_response )
133148 app .logger .error ("Description API failed: " +
134- str (issue_response .status_code ) + " for dmp_id: " + str (dmp_id ))
149+ str (issue_response .status_code ) + " for dmp_id: " + str (dmp_id ))
135150
136151 # 2. Read & Update comments of the ticket
137152 page = 1
@@ -148,29 +163,43 @@ async def dmp_updates():
148163 week_learning_status = False
149164 # Loop through comments
150165 comments_array = comments_response .json ()
151- if comments_array == [] or len (comments_array )== 0 :
166+ if comments_array == [] or len (comments_array ) == 0 :
152167 break
153168 for val in comments_response .json ():
154- # Handle if any of the comments are week data
169+ # Handle if any of the comments are week data
155170 plain_text_body = markdown2 .markdown (val ['body' ])
156171 if "Weekly Goals" in plain_text_body and not week_update_status :
157- week_update_status = handle_week_data (val , dmp ['issue_url' ], dmp_id , issue_update ['mentor_username' ])
158-
172+ week_update_status = await handle_week_data (val , dmp ['issue_url' ], dmp_id ,
173+ issue_update ['mentor_username' ],
174+ async_session )
175+
159176 if "Weekly Learnings" in plain_text_body and not week_learning_status :
160- week_learning_status = handle_week_data (val , dmp ['issue_url' ], dmp_id , issue_update ['mentor_username' ])
161-
177+ week_learning_status = await handle_week_data (val , dmp ['issue_url' ], dmp_id ,
178+ issue_update ['mentor_username' ],
179+ async_session )
180+
162181 # Parse comments
163- comment_update = define_issue_update (
164- val , dmp_id = dmp_id )
165- app .logger .info (
166- 'Comment from remote: ' , comment_update )
167- upsert_comments = db .upsert_data (
168- comment_update , 'dmp_issue_updates' )
182+ comment_update = define_issue_update (val , dmp_id = dmp_id )
183+ app .logger .info ('Comment from remote: ' , comment_update )
184+
185+ # get created_at
186+ created_timestamp = await DmpCronQueries .get_timestamp (async_session , DmpIssueUpdates ,
187+ 'created_at' , 'comment_id' ,
188+ comment_update ['comment_id' ])
189+ comment_update [
190+ 'created_at' ] = datetime .utcnow () if not created_timestamp else created_timestamp
191+ comment_update ['comment_updated_at' ] = datetime .utcnow ().replace (tzinfo = None )
192+ comment_update ['created_at' ] = comment_update ['created_at' ].replace (tzinfo = None )
193+
194+ upsert_comments = await DmpCronQueries .upsert_data_orm (async_session , comment_update )
195+
196+ print (f"dmp_issue_updates works dmp_id - { dmp_id } " ) if upsert_comments else print (
197+ f"comment failed dmp_id - { dmp_id } " )
169198 app .logger .info (upsert_comments )
170199 else :
171200 print ('issue response ' , issue_response )
172201 app .logger .error ("Comments API failed: " +
173- str (issue_response .status_code ) + " for dmp_id: " + str (dmp_id ))
202+ str (issue_response .status_code ) + " for dmp_id: " + str (dmp_id ))
174203 break
175204 page = page + 1
176205
@@ -188,25 +217,37 @@ async def dmp_updates():
188217 pr_created_at = pr_val ['created_at' ]
189218 if (pr_created_at >= TARGET_DATE ):
190219 pr_data = define_pr_update (pr_val , dmp_id )
191- upsert_pr = db .upsert_data (
192- pr_data , 'dmp_pr_updates' )
220+
221+ created_timestamp = await DmpCronQueries .get_timestamp (async_session , DmpPrUpdates ,
222+ 'created_at' , 'pr_id' ,
223+ pr_data ['pr_id' ])
224+ pr_data ['created_at' ] = datetime .utcnow () if not created_timestamp else created_timestamp
225+ pr_data ['created_at' ] = pr_data ['created_at' ].replace (tzinfo = None )
226+
227+ upsert_pr = await DmpCronQueries .upsert_pr_update (async_session , pr_data )
228+
229+ print (f"dmp_pr_updates works - dmp_id is { dmp_id } " ) if upsert_pr else print (
230+ f"dmp_pr_updates failed - dmp_id is { dmp_id } " )
193231 app .logger .info (upsert_pr )
194232 else :
195233 print ('issue response ' , issue_response )
196234 app .logger .error ("PR API failed: " +
197- str (issue_response .status_code ) + " for dmp_id: " + str (dmp_id ))
235+ str (issue_response .status_code ) + " for dmp_id: " + str (dmp_id ))
236+ print (f"last run at - { datetime .utcnow ()} " )
198237 return "success"
199238 except Exception as e :
200239 print (e )
240+ print (f"last run with error - { datetime .utcnow ()} " )
201241 return "Server Error"
202242
203243
204244@app .before_serving
205245async def start_scheduler ():
206246 app .logger .info (
207- "Scheduling dmp_updates_job to run every " + delay_mins + " mins" )
247+ "Scheduling dmp_updates_job to run every " + delay_mins + " mins" )
208248 scheduler .add_job (dmp_updates , 'interval' , minutes = int (delay_mins ))
209249 scheduler .start ()
210250
251+
211252if __name__ == '__main__' :
212- app .run (host = '0.0.0.0' )
253+ app .run (host = '0.0.0.0' )
0 commit comments