aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLibravatar Waishnav <waishnavdeore@gmail.com>2022-09-17 05:09:46 +0530
committerLibravatar Waishnav <waishnavdeore@gmail.com>2022-09-17 05:09:46 +0530
commit2a606b947dc318a783cfd202a446c03586a95cf4 (patch)
tree188e5dcd818a5357dfadffd9d9eb452fb115feaa /src
parentcd43e6c2ae541c1d5cb96f04c94f92b3f56f5735 (diff)
downloadshopno-os-log-sync-2a606b947dc318a783cfd202a446c03586a95cf4.tar.gz
shopno-os-log-sync-2a606b947dc318a783cfd202a446c03586a95cf4.zip
updated algorithm | update csv at every second
Diffstat (limited to 'src')
-rwxr-xr-xsrc/Watcher/analysis.py70
-rwxr-xr-xsrc/Watcher/get_windows.py16
-rwxr-xr-xsrc/Watcher/watch_log.py87
3 files changed, 79 insertions, 94 deletions
diff --git a/src/Watcher/analysis.py b/src/Watcher/analysis.py
index 6e26d29..0e917f8 100755
--- a/src/Watcher/analysis.py
+++ b/src/Watcher/analysis.py
@@ -4,45 +4,26 @@ from watch_log import get_date
import datetime
import time_operations as to
-def extract_data(date):
- user = os.getlogin()
- path = "/home/" + user +"/.cache/Watcher/raw_data/"
+# creating dictionary to store time spend on each applicaitons on that particular day
+def final_report(date):
+ path = "/home/" + os.getlogin() +"/.cache/Watcher/daily_data/"
filename = path + date + ".csv"
- l = list() # l = list of (app_name, time spent on app on that session)
- d = dict()
-
- if os.path.isfile(filename):
- with open(filename, 'r') as file:
- reader = csv.reader(file)
- for row in reader:
- for column in row:
- l.append([column[18::],column[9:17]])
- d.update({column[18::]: "O"})
- else:
- None
-
- d = list(d) # list of app opened on that day
- return d, l
-
-# creating dictionary to store time spend on each applicaitons on that particular day
-def final_report(window_opened, time_spent):
report = dict()
-
- for i in window_opened: # i is applications name
- time = '00:00:00'
- for j in time_spent: # j is list of applicaions_name and time_spent in particular session
- if i == j[0]:
- time = to.time_addition(j[1], time)
- report.update({i:time})
+ if os.path.isfile(filename):
+ with open(filename, 'r') as f:
+ raw_data = f.readlines()
+ for x in raw_data:
+ x = x.split('\t')
+ a = {x[1][:-1]:x[0]}
+ report.update(a)
#print(report)
- if "User-logged-in" in report.keys():
- report.pop("User-logged-in")
- if "AFK" in report.keys():
- report.pop("AFK")
if "Unknown" in report.keys():
report.pop("Unknown")
+ return report
+
+def sort_data(report):
# sort report dictonary in decreasing order of Usages
sorted_values = []
for x,y in report.items():
@@ -55,9 +36,11 @@ def final_report(window_opened, time_spent):
for x, y in report.items():
if to.convert_into_sec(y) == i:
sorted_report.update({x:y})
-
+ del report
+ del sorted_values
return sorted_report
+
def get_sunday_of_week(week):
year = int(week[4:])
week = int(week[1:3])
@@ -84,28 +67,29 @@ def weekly_logs(week = str(os.popen('''date +"W%V-%Y"''').read()[0:-1])):
filename = "/home/"+user+"/.cache/Watcher/Analysis/"+week+".csv"
with open(filename, "w") as csvfile:
csvwriter = csv.writer(csvfile, delimiter='\t')
- #csvwriter.writerow(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"])
if os.popen('''date +"W%V-%Y"''').read()[0:-1] == week:
dates = week_dates()
else:
dates = week_dates(get_sunday_of_week(week))
- window_opened = list()
- time_spent = list()
for i in dates:
- window_opened, time_spent = extract_data(i)
Total_screen_time = "00:00:00"
- for x, y in final_report(window_opened, time_spent).items():
+ for x, y in final_report(i).items():
Total_screen_time = to.time_addition(y, Total_screen_time)
-
csvwriter.writerow([weekday_from_date(i), Total_screen_time])
+ all_data = dict()
for i in dates:
- x, y = extract_data(str(i))
- window_opened += x
- time_spent += y
- for x, y in final_report(window_opened, time_spent).items():
+ for x, y in final_report(i).items():
+ usage = all_data.get(x)
+ if usage == None:
+ usage = "00:00:00"
+ y = to.time_addition(usage, y)
+ all_data.update({x:y})
+
+ all_data = sort_data(all_data)
+ for x, y in all_data.items():
csvwriter.writerow([y, x])
#testing
diff --git a/src/Watcher/get_windows.py b/src/Watcher/get_windows.py
index 0769eb9..008defa 100755
--- a/src/Watcher/get_windows.py
+++ b/src/Watcher/get_windows.py
@@ -42,18 +42,10 @@ def active_window():
return active_window
# returns true if user has move to next app which is not the same as previous
-def is_window_changed(a, afk, timeout):
- result = False
- while not(result):
- time.sleep(0.5)
- b = active_window()
- if a != b :
- result = True
- elif get_afk_status(afk, timeout):
- result = True
- else:
- result = False
- return result
+def previous_window(array_of_window, active_window):
+ array_of_window.remove(active_window)
+ array_of_window.append(active_window)
+ return l[-2]
### what to do after window get change I've to append one line in csv data file in following format
diff --git a/src/Watcher/watch_log.py b/src/Watcher/watch_log.py
index cc85a23..a221194 100755
--- a/src/Watcher/watch_log.py
+++ b/src/Watcher/watch_log.py
@@ -3,7 +3,7 @@ import csv
import time
import get_windows as x
import afk as y
-from time_operations import time_difference
+from time_operations import time_difference, time_addition, convert
# get current time whenever the function is called
def get_time():
@@ -15,59 +15,68 @@ def get_date():
d = os.popen('''date +"%Y-%m-%d"''').read()
return d[0:-1]
-def append_line_in_csv(date, opened_time, closed_time, window_name):
+def update_csv(date, Data):
user = os.getlogin()
- time_spent = time_difference(opened_time, closed_time)
- filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+date+".csv"
- Data = [closed_time, time_spent, window_name]
- with open(filename, 'a') as csvfile:
+ filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv"
+ overwrite_Data = []
+ with open(filename, 'w') as csvfile:
+ for x,y in Data.items():
+ overwrite_Data.append([y,x])
+
csvwriter = csv.writer(csvfile, delimiter='\t')
- csvwriter.writerow(Data)
+ csvwriter.writerows(overwrite_Data)
# Expected Behaviour == if date got changed then append line in new csv file after initializing the csv file
# also if usr is AFK then append line
+def import_data(file):
+ with open(file, 'r') as f:
+ raw_data = f.readlines()
+ data = dict()
+ #l = []
+ for x in raw_data:
+ x = x.split('\t')
+ a = {x[1][:-1]:x[0]}
+ #l.append(x[1][:-1])
+ data.update(a)
+ return data
+
# TODO: AFK feature devlopement (it will be developed after completing alpha product (after whole project up end running)
def log_creation():
- filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+get_date()+".csv"
+ filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv"
if not(os.path.isfile(filename)):
- with open(filename, 'a') as csvfile:
- csvwriter = csv.writer(csvfile, delimiter='\t')
- csvwriter.writerow([get_time(), "00:00:00", ""])
- logout_time = os.popen("tail -n1 " + filename).read().split("\t")[0]
- append_line_in_csv(get_date(), logout_time, get_time(), "User-logged-in")
+ os.popen("touch " + filename)
afk = False
- afkTimeout = 3 # timeout in minutes
-
+ afkTimeout = 1 # timeout in minutes
+ data = import_data(filename)
while True:
- opened_at = get_time()
- previous_window = x.active_window()
+ date = get_date()
+ filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv"
+ afk = y.is_afk(afkTimeout)
- if (y.returned_from_afk(afk, afkTimeout)):
- previous_window = "AFK"
- if os.path.exists(filename):
- opened_at = os.popen(" tail -n1 "+ filename).read().split("\t")[0]
- else:
- previous_date = os.popen("""date -d "1 day ago" '+%Y-%m-%d'""").read()[:-1]
- opened_at = os.popen(" tail -n1 ~/.cache/Watcher/raw_data/"+previous_date+".csv").read().split("\t")[0]
- afk = False
+ if not(afk):
+ active_window = x.active_window()
+ usage = data.get(active_window)
+ if usage == None:
+ usage = "00:00:00"
- if (x.is_window_changed(previous_window, afk, afkTimeout) and not afk):
- if(y.is_afk(afkTimeout)):
- afk = True
- # minimizing error
- closed_at = time_difference("00:02:58", get_time())
- else:
- closed_at = get_time() # for next_window its the opening time
+ time.sleep(1)
+ if y.is_afk(afkTimeout):
+ afk_time = int(round(int(os.popen("xprintidle").read()[:-1])/1000, 0))
+ usage = time_difference(convert(afk_time), usage)
- date = get_date()
- filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+date+".csv"
- append_line_in_csv(date, opened_at, closed_at, previous_window)
+ usage = time_addition("00:00:01", usage)
+ data.update({active_window : usage})
+ if os.path.isfile("/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv"):
+ update_csv(get_date(), data)
+ elif not(os.path.isfile("/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv")):
+ os.popen("touch " + "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv")
+ data.clear()
if __name__ == "__main__":
- #log_creation()
- filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+get_date()+".csv"
- opened_at = os.popen(" tail -n1 "+ filename).read().split("\t")[0]
- print(opened_at)
+ log_creation()
+ #afk_time = int(round(int(os.popen("xprintidle").read()[:-1])/1000, 0))
+ #print(afk_time)
+