aboutsummaryrefslogtreecommitdiff
path: root/src/Watcher/analysis.py
blob: d37010c83b283f2967779d5c8ad8861d2a2888f8 (plain) (blame)
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
import os
import csv
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/"
    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})

    #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")
    # sort report dictonary in decreasing order of Usages
    sorted_values = []
    for x,y in report.items():
        sorted_values.append(to.convert_into_sec(y))

    sorted_values.sort(reverse=True)
    sorted_report = dict()

    for i in sorted_values:
        for x, y in report.items():
            if to.convert_into_sec(y) == i:
                sorted_report.update({x:y})

    return sorted_report

def get_sunday_of_week(week):
    year = int(week[4:])
    week = int(week[1:3])
    first = datetime.date(year, 1, 1)
    base = 1 if first.isocalendar()[1] == 1 else 8
    return first + datetime.timedelta(days=base - first.isocalendar()[2] + 7 * (week - 1)) + datetime.timedelta(days=6.9)

# getting dates of particular week for week summary
def get_dates(theday=datetime.date.today()):
    weekday = theday.isoweekday() - 1
    # The start of the week (Monday)
    start = theday - datetime.timedelta(days=weekday)
    # build a simple range
    dates = [start + datetime.timedelta(days=d) for d in range(weekday + 1)]
    dates = [str(d) for d in dates]
    return dates

def weekday_from_date(date):
    day = os.popen('''date -d "'''+ date + '''" +%a''').read()
    return day[0:-1]

def weekly_logs(week = str(os.popen('''date +"w%v-%y"''').read()[0:-1])):
    user = os.getlogin()
    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 = get_date()
        else:
            dates = get_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():
                Total_screen_time = to.time_addition(y, Total_screen_time)

            csvwriter.writerow([weekday_from_date(i), Total_screen_time])

        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():
            csvwriter.writerow([y, x])

#testing
if __name__ == "__main__":
    print(get_dates(get_sunday_of_week("W27-2022")))
    weekly_logs("W27-2022")