public void UpdateChart(ActivitySummary summary) { Graphics g = Graphics.FromImage(bitmap); DrawSummary(g, summary); g.Dispose(); }
public ActionListViewItem(ActivityEntry entry, int imgIndex, ActivitySummary summary, ListViewItem header) { this.summary = summary; this.header = header; this.entry = entry; ImageIndex = imgIndex; ForeColor = entry.Share >= 20.0 ? Color.Black : entry.Share >= 10.0 ? Color.FromArgb(64, 64, 64) : Color.FromArgb(128, 128, 128); // Remove uneccesary extension var processedName = entry.App.Name.ToLower().Replace(".exe", ""); Text = processedName; /* process: */ AddColumn(entry.ApplicationTitle); /* title: */ AddColumn(entry.Subtitle); /* subtitle: */ AddColumn(entry.Share); /* share: */ AddColumn(entry.KeyboardIntensity); /* keyboard: */ AddColumn(entry.MouseIntensity); /* mouse: */ AddColumn(entry.Category); /* category: */ UseItemStyleForSubItems = false; for (int i = 0; i < SubItems.Count - 1; ++i) { SubItems[i].ForeColor = ForeColor; } SubItems[SubItems.Count - 1].ForeColor = Chart.GetColor(entry.CategoryIndex + 1); }
private void DrawSummary(Graphics g, ActivitySummary s) { DateTime time = s.TimePoint; int index = (int)(time - s.TimePoint.Date).TotalSeconds / Parameters.LogTimeUnit; double[] share = new double[RuleManager.Categories.Count + 1]; foreach (ActivityEntry e in s.Entries) { int k = e.CategoryIndex + 1; share[k] += e.Share; } float y1 = 0; double total = 0; for (int i = 0; i < share.Length; ++i) { Brush brush = GetBrush(i); int x = index * BAR_WIDTH; total += share[i]; float y2 = BAR_HEIGHT * (float)total / 100; g.FillRectangle(brush, index * BAR_WIDTH, TOP_MARGIN + (BAR_HEIGHT - y2), BAR_WIDTH - 1, y2 - y1); y1 = y2; } }
private static void OnCurrentLogExtended(ActivitySummary summary) { if (CurrentLogExtended != null) { CurrentLogExtended(summary); } }
private void CurrentLogExtended(ActivitySummary summary) { AddToActivitiesList(summary); MaybeScrollActivitiesList(); RefreshCategories(); RefreshSummary(); chart.UpdateChart(summary); }
private void AddToActivitiesList(ActivitySummary summary) { // Nothing to show if (summary.Entries.All(e => e.Share < Parameters.MinimumShare)) { return; } // Header string[] contentHeader = new string[] { /* time: */ summary.TimePoint.ToString(), /* title: */ "", /* subtitle: */ "", /* share: */ summary.TotalShare.ToString("F1"), /* keyboard:*/ summary.TotalKeyboardIntensity.ToString("F1"), /* mouse:*/ summary.TotalMouseIntensity.ToString("F1"), /* category:*/ "" }; ListViewItem header = new ListViewItem(contentHeader); header.BackColor = SystemColors.ActiveCaption; // ? header.Font = boldFont; activitiesListView.Items.Add(header); // Entries foreach (var entry in summary.Entries) { if (entry.Share >= Parameters.MinimumShare) { int iconIndex = GetIconIndex(entry.App); ListViewItem item = new ActionListViewItem(entry, iconIndex, summary, header); activitiesListView.Items.Add(item); } } }
private static void MaybeCommitSummary(DateTime currTimePoint) { if (currTimePoint > activeTimePoint) { // Separate snapshots belonging to the current and the next interval List <ActivitySnapshot> currSnapshots = new List <ActivitySnapshot>(); List <ActivitySnapshot> nextSnapshots = new List <ActivitySnapshot>(); SplitSnapshots(activeSnapshots, activeTimePoint, currSnapshots, nextSnapshots); // Summarize the previous interval ActivitySummary summary = GetActivitySummary(currSnapshots, activeTimePoint); if (summary.Entries.Count >= 1) { Persistence.Store(summary); currentLog.Add(summary); if (currentLog == selectedLog) { OnCurrentLogExtended(summary); } } // Consider the end of the day bool dateChanged = (currTimePoint.Date > activeTimePoint.Date); if (dateChanged) { Persistence.Close(); currentLog.Clear(); if (currentLog == selectedLog) { OnCurrentLogChanged(currTimePoint.Date); } } // Start a new interval activeSnapshots = nextSnapshots; activeTimePoint = currTimePoint; } }
public static void Store(ActivitySummary data) { if (writer == null) { string path = ConstructFileName(DateTime.Now, true); writer = new StreamWriter(path); writer.WriteLine("time;span;process;title;subtitle;document;share;keyboard-intensity;mouse-intensity;"); } writer.Write(data.TimePoint.ToString() + ";"); writer.Write(data.Span.ToString() + ";"); writer.Write(";"); writer.Write(";"); writer.Write(";"); writer.Write(";"); writer.Write(data.TotalShare.ToString("F2") + ";"); writer.Write(data.TotalKeyboardIntensity.ToString("F2") + ";"); writer.Write(data.TotalMouseIntensity.ToString("F2") + ";"); writer.WriteLine(); foreach (ActivityEntry entry in data.Entries) { writer.Write(";"); writer.Write(";"); writer.Write(entry.App.Path + ";"); writer.Write(entry.ApplicationTitle.Replace(';', ',').Replace('\r', ' ').Replace("\n", " ") + ";"); writer.Write(entry.WindowTitle.Replace(';', ',').Replace('\r', ' ').Replace("\n", " ") + ";"); writer.Write(entry.DocumentName.Replace(';', ',').Replace('\r', ' ').Replace("\n", " ") + ";"); writer.Write(entry.Share.ToString("F2") + ";"); writer.Write(entry.KeyboardIntensity.ToString("F2") + ";"); writer.Write(entry.MouseIntensity.ToString("F2") + ";"); writer.WriteLine(); } writer.Flush(); }
public void AddSummary(ActivitySummary summary) { Persistence.Store(summary); Activities.Add(summary); }
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 hasDocument; if (header == "time;span;process;title;subtitle;document;share;keyboard-intensity;mouse-intensity;") { hasSubtitle = true; hasDocument = true; } else if (header == "time;span;process;title;subtitle;share;keyboard-intensity;mouse-intensity;") { hasSubtitle = true; hasDocument = false; } else if (header == "time;span;process;title;share;keyboard-intensity;mouse-intensity;") { hasSubtitle = false; hasDocument = 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 (hasDocument == 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], DocumentName = parts[5] }; System.Diagnostics.Debug.Assert(entry.DocumentName != 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(); }
private static ActivitySummary GetActivitySummary(List <ActivitySnapshot> snapshots, DateTime timePoint) { ActivitySummary summary; if (snapshots.Count >= 1) { double keyboardIntensity = snapshots.Count >= 1 ? (from x in snapshots select x.KeyboardIntensity).Average() : 0.0; double mouseIntensity = snapshots.Count >= 1 ? (from x in snapshots select x.MouseIntensity).Average() : 0.0; summary = new ActivitySummary { TimePoint = timePoint, Span = new TimeSpan(0, 0, Parameters.LogTimeUnit), TotalShare = 100.0 * snapshots.Count / Parameters.LogSamplingRate, TotalKeyboardIntensity = keyboardIntensity, TotalMouseIntensity = mouseIntensity, Entries = new List <ActivityEntry>() }; } else { summary = new ActivitySummary { TimePoint = timePoint, Span = new TimeSpan(0, 0, Parameters.LogTimeUnit), TotalShare = 0, TotalKeyboardIntensity = 0, TotalMouseIntensity = 0, Entries = new List <ActivityEntry>() }; } Dictionary <string, double> processShare = new Dictionary <string, double>(); bool[] done = new bool[snapshots.Count]; for (int i = 0; i < snapshots.Count; ++i) { string thisApp = snapshots[i].App.Name; string thisTitle = snapshots[i].ApplicationTitle; string thisSubtitle = snapshots[i].WindowTitle; string thisDocument = snapshots[i].ValidDocumentName; if (done[i] == false) { int count = 1; double sumKeyboard = snapshots[i].KeyboardIntensity; double sumMouse = snapshots[i].MouseIntensity; for (int j = i + 1; j < snapshots.Count; ++j) { string commonTitle; if (snapshots[j].App.Name == thisApp && snapshots[j].WindowTitle == thisSubtitle && snapshots[j].ValidDocumentName == thisDocument && AreTitlesNearlyEqual(snapshots[j].ApplicationTitle, thisTitle, out commonTitle)) { count++; sumKeyboard += snapshots[j].KeyboardIntensity; sumMouse += snapshots[j].MouseIntensity; done[j] = true; thisTitle = commonTitle; } } Debug.Assert(count >= 1); ActivityEntry newEntry = new ActivityEntry { Share = 100 * count / Parameters.LogSamplingRate, App = snapshots[i].App, ApplicationTitle = thisTitle, WindowTitle = thisSubtitle, DocumentName = thisDocument, KeyboardIntensity = sumKeyboard / count, MouseIntensity = sumMouse / count }; newEntry.SetCategory(); summary.Entries.Add(newEntry); // Counting the total share per process path if (processShare.Keys.Contains(newEntry.App.Path)) { processShare[newEntry.App.Path] += newEntry.Share; } else { processShare.Add(newEntry.App.Path, newEntry.Share); } } } summary.Entries.Sort ( (a, b) => processShare[a.App.Path] != processShare[b.App.Path] ? (int)(1000 * (processShare[b.App.Path] - processShare[a.App.Path])) : (int)(1000 * (b.Share - a.Share)) ); return(summary); }
public void UpdateChart(ActivitySummary summary) { chart.UpdateChart(summary); }