-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLog.cs
324 lines (275 loc) · 10.6 KB
/
Log.cs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Herring
{
/// <summary>
/// Class contains all data asociated with daily activity.
/// </summary>
public class Log
{
public enum EventType
{
None,
AwayFromComputer,
WorkignHours
}
public class Event
{
public DateTime Start;
[XmlIgnore]
public TimeSpan Span;
public string SpanTime
{
get
{
return XmlConvert.ToString(Span);
}
set
{
Span = string.IsNullOrEmpty(value) ?
TimeSpan.Zero : XmlConvert.ToTimeSpan(value);
}
}
public string Description;
public EventType Type;
public Event(DateTime Start, TimeSpan Span, string Description, EventType Type)
{
this.Start = Start;
this.Span = Span;
this.Description = Description;
this.Type = Type;
}
public Event()
{
this.Start = DateTime.Now;
this.Span = new TimeSpan();
this.Description = null;
this.Type = EventType.None;
}
public bool ContainsTimePoint(DateTime time)
{
var period = time - Start;
return period > TimeSpan.Zero && period <= Span;
}
}
[XmlIgnore]
public List<ActivitySummary> Activities = new List<ActivitySummary>();
public List<Event> Events = new List<Event>();
public Log()
{
}
public static Log Load(GetAppDelegate getApp, DateTime date, out List<string> errors)
{
Log data = new Log();
string path = Persistence.GetLocalDataDir();
string filePattern = String.Format("herring{0:D4}{1:D2}{2:D2}_*.*", date.Year, date.Month, date.Day);
string[] files = Directory.GetFiles(path, filePattern);
List<string> errorLines = new List<string>();
foreach (string f in files)
{
LoadPart(getApp, f, data.Activities, errorLines);
}
errors = errorLines;
data.LoadEvents(date);
return data;
}
private static void LoadPart(GetAppDelegate getApp, string path, List<ActivitySummary> data, List<string> errorLines)
{
TextReader reader = new StreamReader(path);
string header = reader.ReadLine(); // csv header
bool hasSubtitle;
bool hasDocumentUrl;
if (header == "time;span;process;title;subtitle;document;share;keyboard-intensity;mouse-intensity;")
{
hasSubtitle = true;
hasDocumentUrl = true;
}
else
if (header == "time;span;process;title;subtitle;share;keyboard-intensity;mouse-intensity;")
{
hasSubtitle = true;
hasDocumentUrl = false;
}
else
if (header == "time;span;process;title;share;keyboard-intensity;mouse-intensity;")
{
hasSubtitle = false;
hasDocumentUrl = false;
}
else
{
throw new ApplicationException("Incorrect log file format.");
}
int lineNumber = 0;
ActivitySummary lastSummary = null;
while (true)
{
string line = reader.ReadLine();
lineNumber++;
if (line == null)
break;
try
{
List<string> parts = new List<string>(line.Split(new char[] { ';' }));
while (parts.Count < 9) parts.Add("");
if (hasSubtitle == false)
{
parts[7] = parts[6];
parts[6] = parts[5];
parts[5] = parts[4];
parts[4] = ""; // subtitle
}
if (hasDocumentUrl == false)
{
parts[8] = parts[7];
parts[7] = parts[6];
parts[6] = parts[5];
parts[5] = parts[4];
parts[4] = ""; // document
}
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTimeStyles styles = DateTimeStyles.None;
double keyboardIntensity;
double mouseIntensity;
if (double.TryParse(parts[7], out keyboardIntensity) == false)
{
// Try English
if (double.TryParse(parts[7], NumberStyles.Float, culture, out keyboardIntensity) == false)
{
throw new ApplicationException("Cannot parse a real number.");
}
}
if (double.TryParse(parts[8], out mouseIntensity) == false)
{
// Try English
if (double.TryParse(parts[8], NumberStyles.Float, culture, out mouseIntensity) == false)
{
throw new ApplicationException("Cannot parse a real number.");
}
}
double share;
if (double.TryParse(parts[6], out share) == false)
{
// Try English
if (double.TryParse(parts[6], NumberStyles.Float, culture, out share) == false)
{
throw new ApplicationException("Cannot parse a real number.");
}
}
if (parts[0] != "")
{
DateTime date;
if (DateTime.TryParse(parts[0], out date) == false)
{
// Try English
if (DateTime.TryParse(parts[0], culture, styles, out date) == false)
{
throw new ApplicationException("Cannot parse a date string.");
}
}
ActivitySummary summary = new ActivitySummary();
summary.TimePoint = date;
summary.Span = TimeSpan.Parse(parts[1]);
summary.TotalShare = share;
summary.TotalKeyboardIntensity = keyboardIntensity;
summary.TotalMouseIntensity = mouseIntensity;
summary.Entries = new List<ActivityEntry>();
data.Add(summary);
lastSummary = summary;
}
else
{
if (lastSummary == null)
{
throw new ApplicationException("First entry in the log file is not a summary.");
}
ActivityEntry entry =
new ActivityEntry()
{
App = getApp(parts[2]),
ApplicationTitle = parts[3],
WindowTitle = parts[4],
DocumentUrl = parts[5]
};
System.Diagnostics.Debug.Assert(entry.DocumentUrl != null);
entry.Share = share;
entry.KeyboardIntensity = keyboardIntensity;
entry.MouseIntensity = mouseIntensity;
entry.SetCategory();
lastSummary.Entries.Add(entry);
}
}
catch (Exception)
{
errorLines.Add(lineNumber.ToString("D3") + ":" + (line ?? "UNKNOWN"));
}
}
reader.Close();
}
public void AddSummary(ActivitySummary summary)
{
Persistence.Store(summary);
Activities.Add(summary);
}
public void Clear()
{
Persistence.Close();
Activities.Clear();
}
private void LoadEvents(DateTime date)
{
string path = Persistence.GetLocalDataDir();
var name = String.Format("herring_events{0:D4}{1:D2}{2:D2}.xml", date.Year, date.Month, date.Day);
var serializer = new XmlSerializer(typeof(Log));
var eventsFile = Path.Combine(path, name);
if (!File.Exists(eventsFile))
{
return;
}
using (var reader = new StreamReader(eventsFile))
{
using (var xml = XmlReader.Create(reader))
{
var data = (Log)serializer.Deserialize(xml);
Events.Clear();
Events.AddRange(data.Events);
}
}
}
private void StoreEvents(DateTime date)
{
string path = Persistence.GetLocalDataDir();
var name = String.Format("herring_events{0:D4}{1:D2}{2:D2}.xml", date.Year, date.Month, date.Day);
var serializer = new XmlSerializer(typeof(Log));
using (var writer = new StreamWriter(Path.Combine(path, name)))
{
using (var xml = XmlWriter.Create(writer))
{
serializer.Serialize(writer, this);
}
}
}
public void MarkWorkingHours(DateTime time, TimeSpan span)
{
var e = new Event(time, span, "Working hours", EventType.WorkignHours);
Events.Add(e);
StoreEvents(time);
}
public void AddAwayFromComputer(DateTime time, TimeSpan span, string description)
{
var e = new Event(time, span, description, EventType.AwayFromComputer);
Events.Add(e);
StoreEvents(time);
}
public void RemoveEvent(Event e)
{
Events.Remove(e);
StoreEvents(e.Start);
}
}
}