diff options
| author | DemonKingSwarn <rockingswarn@gmail.com> | 2025-01-25 18:29:18 +0530 |
|---|---|---|
| committer | DemonKingSwarn <rockingswarn@gmail.com> | 2025-01-25 18:29:18 +0530 |
| commit | e9a36d9a3cc50105ade148e0f8a286f89ac0c4df (patch) | |
| tree | e546efe1daf970f467d3040e3818e528600bd05c /src/WatchLog.cs | |
| download | hypr-wellbeing-e9a36d9a3cc50105ade148e0f8a286f89ac0c4df.zip hypr-wellbeing-e9a36d9a3cc50105ade148e0f8a286f89ac0c4df.tar.gz | |
chore: main logger done
Diffstat (limited to 'src/WatchLog.cs')
| -rw-r--r-- | src/WatchLog.cs | 181 |
1 files changed, 181 insertions, 0 deletions
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<string, string> 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<string[]>(); + 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<string, string> ImportData(string file) + { + var data = new Dictionary<string, string>(); + + 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(); + } + } + } + } + } + } +} |
