-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
361 lines (341 loc) · 14.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# importing required libraries
import creds
import sys
import requests
import urllib3
import psycopg2
import pandas as pd
import time
import redis
import pickle
from sqlalchemy import create_engine
from logging_config import get_logger
# Initialize logger
logger = get_logger(__name__, '/var/lib/postgresql/data/mylog.log')
# Function to perform Github OAuth Device Flow to obtain access token
def perform_github_device_flow_oauth(client_id):
device_code_url = f'https://github.com/login/device/code?client_id={client_id}&scope=repo'
try:
response = requests.post(device_code_url, headers={'Accept': 'application/json'})
except requests.exceptions.RequestException as e:
logger.error("Request failed with error: %s", str(e))
raise Exception(f"Error while posting request: {e}")
else:
if response.status_code != 200:
logger.error("Request failed with error: %s", str(e))
raise Exception(f"Failed to obtain device code: {response.text}")
response_json = response.json()
device_code = response_json.get('device_code')
user_code = response_json.get('user_code')
verification_uri = response_json.get('verification_uri')
logger.info("Authentication flow started.")
print(f"Please visit this URL on any device and enter the following code to authorize the application:\n{verification_uri}\nCode: {user_code}")
token_url = 'https://github.com/login/oauth/access_token'
headers = {'Accept': 'application/json'}
data = {
'client_id': client_id,
'device_code': device_code,
'grant_type': 'urn:ietf:params:oauth:grant-type:device_code'
}
# Polling for access token
while True:
response = requests.post(token_url, headers=headers, data=data)
error = response.json().get('error')
if error == 'authorization_pending':
logger.info("Authorization pending. Polling again in 5 seconds...")
print("Authorization pending. Polling again in 5 seconds...")
time.sleep(5)
elif error == 'slow_down':
logger.info("Polling too frequently. Waiting 10 seconds before polling again...")
print("Polling too frequently. Waiting 10 seconds before polling again...")
time.sleep(10)
elif error is None:
access_token = response.json().get('access_token')
print("Authentication flow completed. Token has been obtained from Github.")
logger.info("Authentication flow completed. Token has been obtained from Github.")
return access_token
else:
logger.error("Request failed with error: %s", str(e))
raise Exception(f"Failed to obtain access token: {response.text}")
# Function to make requests with retry
def make_request_with_retry(url, headers, retries=5):
retry = urllib3.util.Retry(
total=retries,
backoff_factor=1,
status_forcelist=[500, 502, 503, 504],
)
adapter = requests.adapters.HTTPAdapter(max_retries=retry)
session = requests.Session()
session.mount("https://", adapter)
try:
response = session.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
return data
elif response.status_code >= 400:
raise requests.exceptions.HTTPError(response.text)
except requests.exceptions.RequestException as e:
logger.error("Request failed with error: %s", str(e))
raise
except ValueError as e:
logger.error("Invalid JSON response: %s", str(e))
raise
# Function to fetch required json data from Github
def getResponse(token):
headers = {"Authorization": "Bearer "+token}
url = "https://api.github.com/user/repos"
try:
data = make_request_with_retry(url, headers, retries=5)
logger.info("Data fetched successfully from Github.")
return data
except requests.exceptions.HTTPError as e:
logger.error("Request failed with error: %s", str(e))
raise
except requests.exceptions.RequestException as e:
logger.error("Request failed with error: %s", str(e))
raise
except ValueError as e:
logger.error("Invalid JSON response: %s", str(e))
raise
# Function to deduplicate data
def removeDuplicates(df):
return df.drop_duplicates(subset=['id'], keep='first')
# Function to normalize data
def normalize(jsonData):
ownerData=[]
repoData=[]
try:
for repo in jsonData:
ownerData+=[repo['owner']]
repo['owner_id']=repo['owner']['id']
repo.pop('owner', None)
repoData+=[repo]
except KeyError:
logger.error("Key not found in JSON data")
raise Exception("Key not found in JSON data")
else:
repoDataDf = removeDuplicates(pd.json_normalize(repoData))
ownerDataDf = removeDuplicates(pd.json_normalize(ownerData))
logger.info("Data normalized, deduplicated successfully.")
return repoDataDf, ownerDataDf
# Function to load data to postgres DB
def loadToDB(df,name):
try:
engine = create_engine(f'postgresql://{creds.dbUser}:{creds.dbPassword}@{creds.dbHost}:{creds.dbPort}/{creds.dbName}')
except (Exception) as error :
logger.error("Error while connecting to PostgreSQL", error)
raise Exception(f"Error while connecting to PostgreSQL: {error}")
else:
logger.info("Connected to postgres DB successfully.")
df.to_sql(name, con=engine, if_exists='replace')
engine.dispose()
logger.info("Data loaded to postgres DB successfully.")
print("Data loaded to DB successfully.")
# Function to make the required postgres query and write the output to a csv file
def postgresToCSV(csvFileName):
try:
conn = psycopg2.connect(f"dbname={creds.dbName} user={creds.dbUser} password={creds.dbPassword} host={creds.dbHost} port={creds.dbPort}")
cur = conn.cursor()
except (Exception) as error :
logger.error("Error while connecting to PostgreSQL", error)
raise Exception(f"Error while connecting to PostgreSQL: {error}")
else:
logger.info("PostgreSQL connection to query data successful.")
try:
sql = "COPY (select r.id as RepoID,r.name as RepoName,r.visibility as Status,r.stargazers_count as starsCount,o.id as ownerId,o.login as ownerName,o.gravatar_id as ownerEmail from repos r join owners o on r.owner_id = o.id) TO STDOUT WITH CSV DELIMITER ',' header"
with open(csvFileName, "w") as file:
cur.copy_expert(sql, file)
except (Exception) as error :
logger.error("Error in Postgres SQL Query", error)
raise Exception(f"Error in Postgres SQL Query: {error}")
else:
logger.info("Data loaded from DB to CSV successfully.")
conn.commit()
cur.close()
print(f"Data loaded to CSV successfully in {csvFileName}.")
return pd.read_csv(csvFileName)
# Function to establish connection with Redis
def redis_connection():
try:
pool = redis.ConnectionPool(host=creds.redisHost, port=creds.redisPort, db=creds.redisDB)
r = redis.Redis(connection_pool=pool)
except (Exception) as e:
logger.error("Redis connection is not ready. Error: " + str(e))
raise Exception(f"Redis connection is not ready. Error: {str(e)}")
else:
logger.info("Redis connection successful.")
return r
# Function to store data in Redis
def storeDataframeInRedis(r, key, df):
try:
df_serialized = pickle.dumps(df)
r.set(key, df_serialized)
except Exception as e:
logger.error("Error while storing data in Redis. Error: " + str(e))
raise Exception(f"Error while storing data in Redis. Error: {str(e)}")
else:
logger.info("Data stored in Redis successfully.")
# Function to get data from Redis
def getDataframeFromRedis(r, key):
try:
df_serialized = r.get(key)
df = pickle.loads(df_serialized)
except Exception as e:
logger.error("Error while getting data from Redis. Error: " + str(e))
raise Exception(f"Error while getting data from Redis. Error: {str(e)}")
else:
logger.info("Data loaded from Redis successfully.")
return df
# Driver code
def main():
logger.info("Starting the program.")
# Get Github OAuth token
try:
token = perform_github_device_flow_oauth(creds.githubClientID)
except Exception as e:
print("Error while getting token from Github API."+str(e))
print("Would you like to try again? (Y/N)")
choice = input()
while True:
if choice == 'Y' or choice == 'y':
try:
logger.info("Trying to get Github OAuth token again.")
token = perform_github_device_flow_oauth(creds.githubClientID)
except Exception as e:
print("Error while getting token from Github API."+str(e))
print("Would you like to try again? (Y/N)")
choice = input()
else:
logger.info("Github OAuth token fetched successfully.")
break
elif choice == 'N' or choice == 'n':
logger.info("User chose not to get Github OAuth token.")
logger.info("Exiting the program.")
logger.info("\n")
print("Exiting the program.")
sys.exit()
else:
print("Invalid choice. Please enter Y/N.")
logger.info("Invalid choice entered by user while trying to get Github OAuth token.")
choice = input()
# Get data from Github API
try:
jsonData = getResponse(token)
except Exception as e:
print("Error while getting data from Github API despite retrying. No JSON Data recieved.")
print("Exiting the program.")
logger.error("Error while getting data from Github API despite retrying. No JSON Data recieved. Error: "+str(e))
logger.info("Exiting the program.")
logger.info("\n")
sys.exit()
# Normalize the data and store it in postgres DB
try:
repoDataDf, ownerDataDf = normalize(jsonData)
loadToDB(repoDataDf, 'repos')
loadToDB(ownerDataDf, 'owners')
except Exception as e:
print("Error while loading data to postgres DB."+str(e))
print("Exiting the program.")
logger.error("Error while loading data to postgres DB."+str(e))
logger.info("Exiting the program.")
logger.info("\n")
sys.exit()
logger.info("Data loaded to postgres DB successfully.")
# Cache the data in Redis
try:
r = redis_connection()
storeDataframeInRedis(r, 'repos', repoDataDf)
storeDataframeInRedis(r, 'owners', ownerDataDf)
r.close()
except Exception as e:
print("Error while caching data in Redis."+str(e))
print("Exiting the program.")
logger.error("Error while caching data in Redis."+str(e))
logger.info("Exiting the program.")
logger.info("\n")
sys.exit()
logger.info("Data cached in Redis successfully.")
# Get data from postgres DB and store it in CSV
try:
resultDf = postgresToCSV("/var/lib/postgresql/data/result.csv")
except Exception as e:
print("Error while loading data to CSV."+str(e))
print("Exiting the program.")
logger.error("Error while loading data to CSV."+str(e))
logger.info("Exiting the program.")
logger.info("\n")
sys.exit()
# Cache the result data in Redis
try:
r = redis_connection()
storeDataframeInRedis(r, 'result', resultDf)
r.close()
except Exception as e:
print("Error while caching result data in Redis."+str(e))
print("Exiting the program.")
logger.error("Error while caching result data in Redis."+str(e))
logger.info("Exiting the program.")
logger.info("\n")
sys.exit()
# Get cached repos and owners data from Redis and print it if required by the user
print("Would you like to view the cached repos.csv and owners.csv from Redis? (Y/N)")
choice = input()
while True:
if choice == 'Y' or choice == 'y':
logger.info("User chose to view the repos.csv and owners.csv from Redis.")
try:
r = redis_connection()
reposRedisDf = getDataframeFromRedis(r, 'repos')
ownersRedisDf = getDataframeFromRedis(r, 'owners')
print(reposRedisDf)
print(ownersRedisDf)
logger.info("Repos and Owners data loaded from Redis successfully.")
r.close()
except Exception as e:
logger.error("Error while getting data from Redis."+str(e))
print("Error while getting data from Redis."+str(e))
finally:
print("Thank you for using the application.")
logger.info("Natural end of program.")
logger.info("\n")
break
elif choice == 'N' or choice == 'n':
print("Thank you for using the application.")
logger.info("User chose not to view the cached repos.csv and owners.csv from Redis.")
logger.info("Natural end of program.")
logger.info("\n")
break
else:
print("Invalid choice. Please enter Y/N.")
logger.info("Invalid choice entered by user while trying to view the cached repos.csv and owners.csv from Redis.")
choice = input()
# Get cached result data from Redis and print it if required by the user
print("Would you like to view the cached result.csv from Redis? (Y/N)")
choice = input()
while True:
if choice == 'Y' or choice == 'y':
logger.info("User chose to view the result.csv from Redis.")
try:
r = redis_connection()
resultRedisDf = getDataframeFromRedis(r, 'result')
print(resultRedisDf)
logger.info("Result data loaded from Redis successfully.")
r.close()
except Exception as e:
logger.error("Error while getting data from Redis."+str(e))
print("Error while getting data from Redis."+str(e))
finally:
print("Thank you for using the application.")
logger.info("Natural end of program.")
logger.info("\n")
break
elif choice == 'N' or choice == 'n':
print("Thank you for using the application.")
logger.info("User chose not to view the result.csv from Redis.")
logger.info("Natural end of program.")
logger.info("\n")
break
else:
print("Please enter a valid choice.")
logger.info("Invalid choice entered by user while trying to view the result.csv from Redis.")
choice = input()