From e9a36d9a3cc50105ade148e0f8a286f89ac0c4df Mon Sep 17 00:00:00 2001 From: DemonKingSwarn Date: Sat, 25 Jan 2025 18:29:18 +0530 Subject: chore: main logger done --- src/WatchLog.cs | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 src/WatchLog.cs (limited to 'src/WatchLog.cs') diff --git a/src/WatchLog.cs b/src/WatchLog.cs new file mode 100644 index 0000000..f24e5a6 --- /dev/null +++ b/src/WatchLog.cs @@ -0,0 +1,181 @@ +namespace hyprwatch.Logger +{ + using System; + using System.IO; + using System.Threading; + using System.Diagnostics; + using System.Collections.Generic; + using hyprwatch.Window; + using hyprwatch.Time; + + public class WatchLog + { + public static string GetTime() + { + string? t = null; + + try + { + Process process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "date", + Arguments = "+%T", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + + process.Start(); + + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + t = output.Substring(0, output.Length - 1); + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + } + + return t; + } + + public static string GetDate() + { + string? d = null; + + try + { + Process process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "date", + Arguments = "+%d-%m-%Y", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + + process.Start(); + + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + d = output.Substring(0, output.Length - 1); + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + } + + return d; + } + + static void UpdateCSV(string date, Dictionary data) + { + string homeDir = Environment.GetEnvironmentVariable("HOME"); + string dataDir = Path.Combine(homeDir, ".cache", "hyprwatch", "daily_data"); + string filePath = Path.Combine(homeDir, ".cache", "hyprwatch", "daily_data", $"{date}.csv"); + + Directory.CreateDirectory(Path.GetDirectoryName(dataDir)); + + var overwriteData = new List(); + foreach(var kvp in data) + { + overwriteData.Add(new[] { kvp.Value, kvp.Key }); + } + + using (var writer = new StreamWriter(filePath)) + { + foreach (var row in overwriteData) + { + writer.WriteLine(string.Join("\t", row)); + } + } + } + + static Dictionary ImportData(string file) + { + var data = new Dictionary(); + + var rawData = File.ReadAllLines(file); + + 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; + } + } + + return data; + } + + public static void LogCreation() + { + string homeDir = Environment.GetEnvironmentVariable("HOME"); + string filename = Path.Combine($"{homeDir}", ".cache", "hyprwatch", "daily_data", $"{GetDate()}.csv"); + if(!File.Exists(filename)) + { + string directoryPath = Path.Combine($"{homeDir}", ".cache", "Watcher", "daily_data"); + + Directory.CreateDirectory(directoryPath); + + using (var fp = File.Create(filename)) + { + // The using block ensures the file is created and closed properly + } + + bool isAfk = false; + int afkTimeout = 1; + + var data = ImportData(filename); + + while(true) + { + string date = GetDate(); + 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) + { + usage = "00:00:00"; + } + + Thread.Sleep(1); + + usage = TimeOperations.TimeAddition("00:00:01", usage); + data[$"{activeWindow}"] = usage; + + 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)) + { + // The using block ensures the file is created and closed properly + } + + data.Clear(); + } + } + } + } + } + } +} -- cgit v1.1