PnlActivity: Log user activity.

This commit is contained in:
2019-05-30 07:21:00 +02:00
parent 8847f3f9b7
commit 11afa74e58
3 changed files with 245 additions and 1 deletions

View File

@@ -0,0 +1,108 @@
namespace VAR.Toolbox.UI.Tools
{
partial class PnlActivity
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.grpActivity = new System.Windows.Forms.GroupBox();
this.lblActive = new System.Windows.Forms.Label();
this.lblActiveWindowTitle = new System.Windows.Forms.Label();
this.txtCurrentActivity = new System.Windows.Forms.TextBox();
this.timTicker = new System.Windows.Forms.Timer(this.components);
this.grpActivity.SuspendLayout();
this.SuspendLayout();
//
// grpActivity
//
this.grpActivity.Controls.Add(this.lblActive);
this.grpActivity.Controls.Add(this.lblActiveWindowTitle);
this.grpActivity.Controls.Add(this.txtCurrentActivity);
this.grpActivity.Dock = System.Windows.Forms.DockStyle.Fill;
this.grpActivity.Location = new System.Drawing.Point(0, 0);
this.grpActivity.Name = "grpActivity";
this.grpActivity.Size = new System.Drawing.Size(200, 125);
this.grpActivity.TabIndex = 0;
this.grpActivity.TabStop = false;
this.grpActivity.Text = "Activity";
//
// lblActive
//
this.lblActive.AutoSize = true;
this.lblActive.Location = new System.Drawing.Point(7, 102);
this.lblActive.Name = "lblActive";
this.lblActive.Size = new System.Drawing.Size(37, 13);
this.lblActive.TabIndex = 2;
this.lblActive.Text = "Active";
//
// lblActiveWindowTitle
//
this.lblActiveWindowTitle.AutoSize = true;
this.lblActiveWindowTitle.Location = new System.Drawing.Point(7, 85);
this.lblActiveWindowTitle.Name = "lblActiveWindowTitle";
this.lblActiveWindowTitle.Size = new System.Drawing.Size(96, 13);
this.lblActiveWindowTitle.TabIndex = 1;
this.lblActiveWindowTitle.Text = "ActiveWindowTitle";
//
// txtCurrentActivity
//
this.txtCurrentActivity.Location = new System.Drawing.Point(7, 20);
this.txtCurrentActivity.Multiline = true;
this.txtCurrentActivity.Name = "txtCurrentActivity";
this.txtCurrentActivity.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtCurrentActivity.Size = new System.Drawing.Size(187, 58);
this.txtCurrentActivity.TabIndex = 0;
//
// timTicker
//
this.timTicker.Enabled = true;
this.timTicker.Interval = 1000;
this.timTicker.Tick += new System.EventHandler(this.TimTicker_Tick);
//
// PnlActivity
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.grpActivity);
this.Margin = new System.Windows.Forms.Padding(0);
this.Name = "PnlActivity";
this.Size = new System.Drawing.Size(200, 125);
this.grpActivity.ResumeLayout(false);
this.grpActivity.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox grpActivity;
private System.Windows.Forms.Label lblActive;
private System.Windows.Forms.Label lblActiveWindowTitle;
private System.Windows.Forms.TextBox txtCurrentActivity;
private System.Windows.Forms.Timer timTicker;
}
}

View File

@@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using VAR.Json;
using VAR.Toolbox.Code.Windows;
namespace VAR.Toolbox.UI.Tools
{
public partial class PnlActivity : UserControl, IToolPanel
{
public PnlActivity()
{
InitializeComponent();
}
private void TimTicker_Tick(object sender, EventArgs e)
{
if (DesignMode) { return; }
timTicker.Stop();
string activeWindowTitle = User32.GetActiveWindowTitle();
bool active = Win32.GetLastInputTime() < 2;
DateTime date = DateTime.UtcNow;
lblActiveWindowTitle.Text = activeWindowTitle;
lblActive.Text = active ? "Active" : "Inactive";
Activity_Register(activeWindowTitle, active, date);
timTicker.Start();
}
private class ActivityPoint
{
public string ActiveWindowTitle { get; set; }
public bool Active { get; set; }
public DateTime Date { get; set; }
}
private DateTime _currentDate = DateTime.MinValue;
private readonly List<ActivityPoint> _currentActivityPoints = new List<ActivityPoint>();
private const int SecondsPerFrame = 30;
private void Activity_Register(string activeWindowTitle, bool active, DateTime date)
{
TimeSpan diffTime = date - _currentDate;
if (diffTime.TotalSeconds > SecondsPerFrame)
{
Activity_EndFrame();
_currentActivityPoints.Clear();
_currentDate = date;
}
_currentActivityPoints.Add(new ActivityPoint
{
ActiveWindowTitle = activeWindowTitle,
Active = active,
Date = date,
});
}
private class ActivityFrame
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string CurrentActivity { get; set; }
public List<string> ActiveWindowTitles { get; set; } = new List<string>();
public float ActivityFactor { get; set; }
}
private void Activity_EndFrame()
{
if (_currentActivityPoints.Count == 0)
{
return;
}
// Prepare frame
ActivityFrame frame = new ActivityFrame
{
StartDate = _currentActivityPoints.Min(ap => ap.Date),
EndDate = _currentActivityPoints.Max(ap => ap.Date),
CurrentActivity = txtCurrentActivity.Text,
ActiveWindowTitles = _currentActivityPoints.Select(ap => ap.ActiveWindowTitle).Distinct().ToList(),
ActivityFactor = _currentActivityPoints.Count(ap => ap.Active) / (float)_currentActivityPoints.Count,
};
// Write frame
JsonWriter jsonWriter = new Json.JsonWriter();
string line = jsonWriter.Write(frame);
try
{
StreamWriter outStream = GetOutputStreamWritter();
outStream.WriteLine(line);
CloseOutputStreamWritter(outStream);
}
catch (Exception) { /* Nom Nom Nom */}
}
private static StreamWriter GetOutputStreamWritter()
{
try
{
string location = System.Reflection.Assembly.GetEntryAssembly().Location;
string path = Path.GetDirectoryName(location);
string fileOut = string.Format("{0}/Activity.{1}.txt", path,
DateTime.UtcNow.ToString("yyyy-MM-dd"));
return File.AppendText(fileOut);
}
catch (Exception)
{
return null;
}
}
private static void CloseOutputStreamWritter(StreamWriter stream)
{
if (stream != null)
{
stream.Close();
}
}
}
}

View File

@@ -193,6 +193,12 @@
<DependentUpon>FrmWebcam.cs</DependentUpon>
</Compile>
<Compile Include="UI\IToolForm.cs" />
<Compile Include="UI\Tools\PnlActivity.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="UI\Tools\PnlActivity.Designer.cs">
<DependentUpon>PnlActivity.cs</DependentUpon>
</Compile>
<Compile Include="UI\Tools\PnlCover.cs">
<SubType>UserControl</SubType>
</Compile>
@@ -214,7 +220,6 @@
<Content Include="Images\toolbox.svg" />
<Content Include="Toolbox.ico" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.