forked from Abhiroop-tales/ACCORD-WWW24Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivitylogs.py
More file actions
66 lines (51 loc) · 2.45 KB
/
activitylogs.py
File metadata and controls
66 lines (51 loc) · 2.45 KB
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
from sqlconnector import DatabaseQuery
from logextraction import extractDriveLog
from datetime import datetime, timedelta
class Logupdater():
'''Fetch logs and update database.
Retrieve all new logs since date last fetched ("last log date") from
Admin SDK Reports API service. Parse logs and insert into logs table
in database.
Attributes:
mysql: flask_mysqldb.MySQL
reportsAPI_service: googleapiclient.discovery.Resource, for
interacting with Admin SDK Reports API
'''
def __init__(self, mysql, reportsAPI_service):
self.mysql = mysql
self.reportsAPI_service = reportsAPI_service
def updateLogs_database(self):
'''Fetch recent logs, parse, and insert into activity_logs table.
Returns:
Number of added activity logs.
'''
try:
db = DatabaseQuery(self.mysql.connection, self.mysql.connection.cursor())
# Extract last log date from the database
last_log_date = db.extract_lastLog_date()
totalLogs = 0
if(last_log_date != None):
# Extract the activity logs from the Google cloud from lastlog Date
activity_logs = extractDriveLog(last_log_date, self.reportsAPI_service)
activity_logs.pop(0)
# Update the log Database table when the new activities are recorded
if(len(activity_logs) > 1):
new_log_date = activity_logs[0].split('\t*\t')[0]
# Parse the string into a datetime object
date_format = "%Y-%m-%dT%H:%M:%S.%fZ"
log_datetime = datetime.strptime(new_log_date, date_format)
# Subtract 4 hours to convert to Eastern Daylight Time
updated_datetime = log_datetime - timedelta(hours=4)
# Format it back to a string
updated_log_date = updated_datetime.strftime(date_format)
db.add_activity_logs(activity_logs)
db.update_log_date(updated_log_date)
totalLogs = len(activity_logs)-1
del db
return totalLogs
except LookupError as le:
return "Error in the key or index !!\n" + str(le)
except ValueError as ve:
return "Error in Value Entered !!\n" + str(ve)
except TypeError as te:
return "Error in Type matching !!\n" + str(te)