diff options
| -rw-r--r-- | Program.cs | 49 | ||||
| -rw-r--r-- | src/Analysis.cs | 136 | ||||
| -rw-r--r-- | src/GetWindows.cs | 1 | ||||
| -rw-r--r-- | src/WatchLog.cs | 42 |
4 files changed, 201 insertions, 27 deletions
@@ -1,9 +1,52 @@ -using hyprwatch.Logger; +using System.IO; +using System.Runtime.InteropServices; +using hyprwatch.Logger; class Program { - static void Main(string[] args) + static void Main(string[] args) { - WatchLog.LogCreation(); + if (args.Length == 0 || args[0] != "-d" && args[0] != "--show") + { + Console.WriteLine("Usage: -d || --show"); + return; + } + + if(args[0] == "-d") + { + while(true) + { + WatchLog.LogCreation(); + System.Threading.Thread.Sleep(5000); + } + } + + if(args[0] == "--show") + { + Dictionary<string, string> data = new Dictionary<string, string>(); + + var date = WatchLog.GetDate(); + string filePath = Environment.GetEnvironmentVariable("HOME") + $"/.cache/hyprwatch/daily_data/{date}.csv"; + + var rawData = File.ReadAllLines(filePath); + + foreach(var line in rawData) + { + var parts = line.Split('\t'); + if(parts.Length >= 2) + { + string key = parts[1].TrimEnd(); + string value = parts[0]; + data[key] = value; + } + } + Console.WriteLine("{0,-30} {1,-30}", "App", "Time"); + Console.WriteLine(new string('-', 60)); + foreach (var entry in data) + { + Console.WriteLine("{0,-30} {1,-30}", entry.Key, entry.Value); + } + } } + } diff --git a/src/Analysis.cs b/src/Analysis.cs new file mode 100644 index 0000000..95a8236 --- /dev/null +++ b/src/Analysis.cs @@ -0,0 +1,136 @@ +namespace hyprwatch.Report +{ + using System; + using System.IO; + using System.Linq; + using System.Globalization; + using System.Collections.Generic; + using hyprwatch.Time; + + public class Analysis + { + public static Dictionary<string, string> FinalReport(string date) + { + string path = Environment.GetEnvironmentVariable("HOME") + "/.cache/hyprwatch/daily_data/"; + string filename = Path.Combine(path, date + ".csv"); + + var report = new Dictionary<string, string>(); + + if (File.Exists(filename)) + { + var rawData = File.ReadAllLines(filename); + foreach (var line in rawData) + { + var splitData = line.Split('\t'); + if (splitData.Length >= 2) + { + report[splitData[1].Trim()] = splitData[0]; + } + } + } + + report.Remove("Unknown"); + return report; + } + + public static Dictionary<string, string> SortData(Dictionary<string, string> report) + { + var sortedValues = report.Select(kvp => TimeOperations.ConvertIntoSec(kvp.Value)).OrderByDescending(sec => sec).ToList(); + var sortedReport = new Dictionary<string, string>(); + + foreach (var seconds in sortedValues) + { + foreach (var kvp in report) + { + if (TimeOperations.ConvertIntoSec(kvp.Value) == seconds && !sortedReport.ContainsKey(kvp.Key)) + { + sortedReport[kvp.Key] = kvp.Value; + break; + } + } + } + + return sortedReport; + } + + public static DateTime GetSundayOfWeek(string week) + { + int year = int.Parse(week.Substring(4)); + int weekNumber = int.Parse(week.Substring(1, 2)); + var firstDayOfYear = new DateTime(year, 1, 1); + var baseDay = firstDayOfYear.DayOfWeek == DayOfWeek.Monday ? firstDayOfYear : firstDayOfYear.AddDays(8 - (int)firstDayOfYear.DayOfWeek); + return baseDay.AddDays((weekNumber - 1) * 7 + 6); + } + + public static List<string> WeekDates(DateTime theDay) + { + int weekday = (int)theDay.DayOfWeek - 1; + var start = theDay.AddDays(-weekday); + return Enumerable.Range(0, weekday + 1).Select(offset => start.AddDays(offset).ToString("yyyy-MM-dd")).ToList(); + } + + public static string WeekdayFromDate(string date) + { + if (DateTime.TryParseExact(date, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var parsedDate)) + { + return parsedDate.ToString("ddd"); + } + + return string.Empty; + } + + public static void WeeklyLogs(string week) + { + string user = Environment.GetEnvironmentVariable("HOME"); + string filename = Path.Combine(user, ".cache/Watcher/Analysis/", week + ".csv"); + + using (var writer = new StreamWriter(filename)) + { + List<string> dates; + if (GetCurrentWeek() == week) + { + dates = WeekDates(DateTime.Today); + } + else + { + dates = WeekDates(GetSundayOfWeek(week)); + } + + foreach (var date in dates) + { + string totalScreenTime = "00:00:00"; + foreach (var kvp in FinalReport(date)) + { + totalScreenTime = TimeOperations.TimeAddition(totalScreenTime, kvp.Value); + } + writer.WriteLine($"{WeekdayFromDate(date)}\t{totalScreenTime}"); + } + + var allData = new Dictionary<string, string>(); + foreach (var date in dates) + { + foreach (var kvp in FinalReport(date)) + { + if (!allData.ContainsKey(kvp.Key)) + { + allData[kvp.Key] = "00:00:00"; + } + allData[kvp.Key] = TimeOperations.TimeAddition(allData[kvp.Key], kvp.Value); + } + } + + allData = SortData(allData); + foreach (var kvp in allData) + { + writer.WriteLine($"{kvp.Value}\t{kvp.Key}"); + } + } + } + + public static string GetCurrentWeek() + { + var currentWeek = DateTime.Now.ToString("\'W\'\'\'W" + CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday)); + return currentWeek; + } + } +} diff --git a/src/GetWindows.cs b/src/GetWindows.cs index 90f28c6..eafb117 100644 --- a/src/GetWindows.cs +++ b/src/GetWindows.cs @@ -43,5 +43,4 @@ namespace hyprwatch.Window return activeWindow ?? string.Empty; } } - } diff --git a/src/WatchLog.cs b/src/WatchLog.cs index b153804..83001c1 100644 --- a/src/WatchLog.cs +++ b/src/WatchLog.cs @@ -143,7 +143,7 @@ namespace hyprwatch.Logger } } - bool isAfk = false; + //bool isAfk = false; //int afkTimeout = 1; var data = ImportData(filename); @@ -155,36 +155,32 @@ namespace hyprwatch.Logger filename = Path.Combine($"{homeDir}", ".cache", "hyprwatch", "daily_data", $"{date}.csv"); Console.WriteLine(data); - if(!isAfk) + string activeWindow = GetWindows.ActiveWindow(); + string usage = data.TryGetValue(activeWindow, out string? value) ? value : null; + if(usage == null) { - string activeWindow = GetWindows.ActiveWindow(); - string usage = data.TryGetValue(activeWindow, out string? value) ? value : null; - if(usage == null) - { - usage = "00:00:00"; - } + usage = "00:00:00"; + } - Thread.Sleep(1000); + Thread.Sleep(1000); - usage = TimeOperations.TimeAddition("00:00:01", usage); - data[$"{activeWindow}"] = usage; + usage = TimeOperations.TimeAddition("00:00:01", usage); + data[$"{activeWindow}"] = usage; - if(File.Exists(filename)) - { - UpdateCSV(GetDate(), data); - } - else if(!File.Exists(filename)) + if(File.Exists(filename)) + { + UpdateCSV(GetDate(), data); + } + else if(!File.Exists(filename)) + { + string newFilename = Path.Combine($"{homeDir}", ".cache", "hyprwatch", "daily_data", $"{GetDate()}.csv"); + using (var fp = File.Create(newFilename)) { - string newFilename = Path.Combine($"{homeDir}", ".cache", "hyprwatch", "daily_data", $"{GetDate()}.csv"); - using (var fp = File.Create(newFilename)) - { // The using block ensures the file is created and closed properly - } - - data.Clear(); } + + data.Clear(); } - } } } |
