diff options
author | 2022-07-09 00:11:48 +0530 | |
---|---|---|
committer | 2022-07-09 00:11:48 +0530 | |
commit | f0e484878af5b86b723de6af5f506f8b18e66ddc (patch) | |
tree | 0e688955119acf9598a85fdcba77ce21c104b327 /src/Watcher/watch_log.py | |
parent | 255487a34700d6790a2771a177c16991caaca72f (diff) | |
parent | ac79bbea7b113613bf160512064b8e2478181db9 (diff) | |
download | shopno-os-log-sync-f0e484878af5b86b723de6af5f506f8b18e66ddc.tar.gz shopno-os-log-sync-f0e484878af5b86b723de6af5f506f8b18e66ddc.zip |
Update 1.2 | AFK feature | New algorithm to update log file at each second
Diffstat (limited to 'src/Watcher/watch_log.py')
-rwxr-xr-x | src/Watcher/watch_log.py | 64 |
1 files changed, 29 insertions, 35 deletions
diff --git a/src/Watcher/watch_log.py b/src/Watcher/watch_log.py index 1359ecc..4956deb 100755 --- a/src/Watcher/watch_log.py +++ b/src/Watcher/watch_log.py @@ -2,7 +2,8 @@ import os import csv import time import get_windows as x -from time_operations import time_difference +import afk +from time_operations import time_difference, time_addition # get current time whenever the function is called def get_time(): @@ -14,58 +15,51 @@ def get_date(): d = os.popen('''date +"%Y-%m-%d"''').read() return d[0:-1] -def append_line_in_csv(date, closed_time, window_name): +def append_line_in_csv(date, opened_time, time_spent, window_name): user = os.getlogin() filename = "/home/"+user+"/.cache/Watcher/raw_data/"+date+".csv" - with open(filename, 'r') as file: - last_app_time = file.readlines()[-1][0:8] - - time_spent = time_difference(last_app_time, closed_time) - - Data = [closed_time, time_spent, window_name] + Data = [opened_time, time_spent, window_name] with open(filename, 'a') as csvfile: csvwriter = csv.writer(csvfile, delimiter='\t') csvwriter.writerow(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 -# TODO: AFK feature devlopement (it will be developed after completing alpha product (after whole project up end running) - -afk = False def log_creation(): - global afk - filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_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", ""]) - append_line_in_csv(get_date(), get_time(), "User-logged-in") + + # appending line at login + logged_out_time = os.popen("tail -n1 " + filename).read().split("\t")[0] + time_away_from_laptop = time_difference(logged_out_time, get_time()) + append_line_in_csv(get_date(), get_time(), time_away_from_laptop, "User-logged-in") while True: - previous_window = x.active_window() - if x.is_window_changed(previous_window) and not(afk): - next_window = x.active_window() - closed_at = get_time() # for next_window its the opening time + actv_window = x.active_window() + if x.is_window_changed(actv_window): + next_actv_window = x.active_window() date = get_date() - filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+date+".csv" - if not(os.path.isfile(filename)): - with open(filename, 'a') as csvfile: - csvwriter = csv.writer(csvfile, delimiter='\t') - prev_date = os.popen("""date -d "yesterday" '+%Y-%m-%d'""").read()[0:-1] - prev_file = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+prev_date+".csv" - with open(prev_file, 'r') as file: - last_app_time = file.readlines()[-1][0:8] - csvwriter.writerow([get_time(), time_difference(last_app_time, closed_at), previous_window]) + opened_at = get_time() + time_spent = "00:00:00" + append_line_in_csv(date, opened_at, time_spent, next_actv_window) - else: - # appends line when app gets closed - append_line_in_csv(date, closed_at, previous_window) + else: + date = get_date() + now = get_time() # for next_window its the opening time + last_line = os.popen("tail -n1 " + filename).read().split("\t") + opened_at = last_line[0] + os.popen("""sed -i -e '$ d' """ + filename) + time.sleep(0.2) + time_spnt = time_difference(opened_at, get_time()) - if afk: - afk_closed_time = get_time() - append_line_in_csv(date, afk_closed_time, "AFK") + if afk.IsAFK(): + append_line_in_csv(date, opened_time, time_spent, "AFK") + else: + append_line_in_csv(date, opened_at, time_spnt, actv_window) if __name__ == "__main__": - log_creation() + opened_at = os.popen("""tail -n1 ~/.cache/Watcher/raw_data/2022-07-07.csv""").read().split("\t")[1] + print(opened_at) |