-
Notifications
You must be signed in to change notification settings - Fork 1
/
activitylogs.py
57 lines (40 loc) · 2.02 KB
/
activitylogs.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
from sqlconnector import DatabaseQuery
from logextraction import extractDriveLog
from datetime import datetime, timedelta
# Method to update Activity Logs in the database
class Logupdater():
def __init__(self, mysql, reportsAPI_service):
self.mysql = mysql
self.reportsAPI_service = reportsAPI_service
def updateLogs_database(self):
try:
# Create DB connection
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
updated_datetime = log_datetime
# Format it back to a string if needed
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)