aboutsummaryrefslogtreecommitdiff
path: root/src/WatchLog.cs
blob: 9191aa0a8db553fa3c627fa85564febc785ae454 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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 ?? string.Empty;
    }

    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 ?? string.Empty;
    }

    static void UpdateCSV(string date, Dictionary<string, string> data)
    {
      string homeDir = Environment.GetEnvironmentVariable("HOME") ?? throw new InvalidOperationException("HOME environment variable is not set.");
      string filePath = Path.Combine(homeDir, ".cache", "hyprwatch", "daily_data", $"{date}.csv");


      string? dirPath = Path.GetDirectoryName(filePath);
      if(dirPath is not null)
      {
        Directory.CreateDirectory(dirPath);
      }
      else
      {
        throw new InvalidOperationException("Invalid file path.");
      }

      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 currentDate = GetDate();
      string filename = Path.Combine($"{homeDir}", ".cache", "hyprwatch", "daily_data", $"{currentDate}.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);
        data ??= new Dictionary<string, string>();
        
        while(true)
        {
          string newDate = GetDate();
          if(newDate != currentDate)
          {
            currentDate = newDate;
            filename = Path.Combine($"{homeDir}", ".cache", "hyprwatch", "daily_data", $"{currentDate}.csv");
            data.Clear();

            using (var fp = File.Create(filename))
            {
              // The using block ensures the file is created and closed properly
            }
          }

          Console.WriteLine(data);

          string activeWindow = GetWindows.ActiveWindow();
          string usage = data.TryGetValue(activeWindow, out string? value) ? value : "00:00:00";

          Thread.Sleep(1000);

          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();
          

          }*/
          
          UpdateCSV(currentDate, data);
      }
    }
  }
}