aboutsummaryrefslogtreecommitdiff
path: root/src/WeeklyReport.cs
blob: 9f815101a39d6855f20d84d98e024b8f4ab0fab6 (plain)
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
115
116
117
118
119
120
using System;
using System.IO;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using hyprwatch.Time;

namespace hyprwatch.Report.Weekly
{
    public class WeeklyReport
    {
        private static string FormatTime(string time)
        {
            var parts = time.Split(':');
            if (parts.Length == 3)
            {
                int hr = int.Parse(parts[0]);
                int mn = int.Parse(parts[1]);
                int sec = int.Parse(parts[2]);
                return $"{hr:D2}:{mn:D2}:{sec:D2}";
            }
            return "00:00:00"; // Should not happen with valid data
        }

        private static int ConvertToSeconds(string time)
        {
            var parts = time.Split(':');
            if (parts.Length == 3)
            {
                int hr = int.Parse(parts[0]);
                int mn = int.Parse(parts[1]);
                int sec = int.Parse(parts[2]);
                return hr * 3600 + mn * 60 + sec;
            }
            return 0;
        }

        private static string ConvertFromSeconds(int totalSeconds)
        {
            int hr = totalSeconds / 3600;
            totalSeconds %= 3600;
            int mn = totalSeconds / 60;
            int sec = totalSeconds % 60;
            return $"{hr:D2}:{mn:D2}:{sec:D2}";
        }

        public static Dictionary<string, int> GetWeeklyReport()
        {
            var allData = new Dictionary<string, int>();
            string path = Environment.GetEnvironmentVariable("HOME") + "/.cache/hyprwatch/daily_data/";
            var files = Directory.GetFiles(path, "*.csv");

            DateTime today = DateTime.Today;
            int daysUntilMonday = (int)today.DayOfWeek - (int)DayOfWeek.Monday;
            if (daysUntilMonday < 0) daysUntilMonday += 7;
            DateTime startOfWeek = today.AddDays(-daysUntilMonday);

            foreach (var file in files)
            {
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
                if (DateTime.TryParse(fileNameWithoutExtension, out DateTime fileDate))
                {
                    if (fileDate >= startOfWeek && fileDate < startOfWeek.AddDays(7))
                    {
                        var report = new Dictionary<string, string>();
                        var rawData = File.ReadAllLines(file);
                        foreach (var line in rawData)
                        {
                            var splitData = line.Split('\t');
                            if (splitData.Length >= 2)
                            {
                                report[splitData[1].Trim()] = splitData[0];
                            }
                        }

                        foreach (var kvp in report)
                        {
                            if (!allData.ContainsKey(kvp.Key))
                            {
                                allData[kvp.Key] = 0;
                            }
                            allData[kvp.Key] += ConvertToSeconds(FormatTime(kvp.Value));
                        }
                    }
                }
            }

            return allData;
        }

        public static void DisplayWeeklyReport()
        {
            string green = "\x1b[0;32m";
            string yellow = "\x1b[1;33m";
            string red = "\x1b[0;31m";
            string blue = "\x1b[0;34m";
            string reset = "\x1b[0m";

            var weeklyData = GetWeeklyReport();

            var sortedData = from entry in weeklyData orderby entry.Value descending select entry;

            int totalSeconds = weeklyData.Values.Sum();
            string totalTime = ConvertFromSeconds(totalSeconds);

            Console.WriteLine(" ");
            Console.WriteLine($"This Week's Screen Usage\t{totalTime}");
            Console.WriteLine($"{red}{new string('-', 60)}{reset}");
            Console.WriteLine($"{yellow}{"App".PadRight(30)}{"Time".PadLeft(30)}{reset}");
            Console.WriteLine($"{red}{new string('-', 60)}{reset}");

            foreach (var entry in sortedData)
            {
                string key = entry.Key.Length > 30 ? entry.Key.Substring(0, 27) + "..." : entry.Key;
                string time = ConvertFromSeconds(entry.Value);
                Console.WriteLine($"{blue}{key.PadRight(30)}{reset}{green}{time.PadLeft(30)}{reset}");
            }
        }
    }
}