diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..610838f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+webui
\ No newline at end of file
diff --git a/appTime/AppTime-0.12/AppTime.sln b/appTime/AppTime-0.12/AppTime.sln
new file mode 100644
index 0000000..ed5ffb7
--- /dev/null
+++ b/appTime/AppTime-0.12/AppTime.sln
@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 16
+VisualStudioVersion = 16.0.30907.101
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppTime", "AppTime\AppTime.csproj", "{4892E56B-5942-444B-B41C-1E4F22A3752A}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {4892E56B-5942-444B-B41C-1E4F22A3752A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {4892E56B-5942-444B-B41C-1E4F22A3752A}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {4892E56B-5942-444B-B41C-1E4F22A3752A}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {4892E56B-5942-444B-B41C-1E4F22A3752A}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {2525871C-F224-48A9-9189-E784A51F63A8}
+ EndGlobalSection
+EndGlobal
diff --git a/appTime/AppTime-0.12/AppTime/.gitattributes b/appTime/AppTime-0.12/AppTime/.gitattributes
new file mode 100644
index 0000000..dfe0770
--- /dev/null
+++ b/appTime/AppTime-0.12/AppTime/.gitattributes
@@ -0,0 +1,2 @@
+# Auto detect text files and perform LF normalization
+* text=auto
diff --git a/appTime/AppTime-0.12/AppTime/AppTime.csproj b/appTime/AppTime-0.12/AppTime/AppTime.csproj
new file mode 100644
index 0000000..2d8e9ce
--- /dev/null
+++ b/appTime/AppTime-0.12/AppTime/AppTime.csproj
@@ -0,0 +1,2837 @@
+
+
+
+
+ preview
+ Debug
+ AnyCPU
+ {4892E56B-5942-444B-B41C-1E4F22A3752A}
+ WinExe
+ AppTime
+ AppTime
+ v4.8
+ 512
+ false
+
+
+
+ publish\
+ true
+ Disk
+ false
+ Foreground
+ 7
+ Days
+ false
+ false
+ true
+ 0
+ 0.1.0.%2a
+ false
+ true
+
+
+ x64
+ true
+ full
+ false
+ bin\Debug\
+ DEBUG;TRACE
+ prompt
+ 4
+ false
+ true
+
+
+ AnyCPU
+ pdbonly
+ true
+ bin\Release\
+ TRACE
+ prompt
+ 4
+ false
+ true
+
+
+ apptime.ico
+
+
+ LocalIntranet
+
+
+ false
+
+
+ app.manifest
+
+
+ false
+
+
+
+ ..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll
+
+
+ ..\packages\SQLitePCLRaw.core.1.1.14\lib\net45\SQLitePCLRaw.core.dll
+
+
+ ..\packages\SQLitePCLRaw.provider.e_sqlite3.net45.1.1.14\lib\net45\SQLitePCLRaw.provider.e_sqlite3.dll
+
+
+
+
+ ..\packages\System.Data.SQLite.Core.1.0.113.1\lib\net46\System.Data.SQLite.dll
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Form
+
+
+ FrmMain.cs
+
+
+
+
+
+
+ FrmMain.cs
+
+
+ ResXFileCodeGenerator
+ Resources.Designer.cs
+ Designer
+
+
+ True
+ Resources.resx
+ True
+
+
+
+
+
+
+ SettingsSingleFileGenerator
+ Settings.Designer.cs
+
+
+ True
+ Settings.settings
+ True
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+
+
+
+
+
+ False
+ .NET Framework 3.5 SP1
+ false
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。
+
+
+
+
+
\ No newline at end of file
diff --git a/appTime/AppTime-0.12/AppTime/Controller.cs b/appTime/AppTime-0.12/AppTime/Controller.cs
new file mode 100644
index 0000000..1e1cb6c
--- /dev/null
+++ b/appTime/AppTime-0.12/AppTime/Controller.cs
@@ -0,0 +1,623 @@
+
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.SQLite;
+using System.Diagnostics;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.Globalization;
+using System.IO;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using static AppTime.Recorder;
+
+namespace AppTime
+{
+ class Controller
+ {
+ public WebServer Server =>Program.server;
+ public Recorder Recorder=>Program.recorder;
+
+
+ #region basic
+
+ int getColor(string str)
+ {
+ return str.GetHashCode();
+ }
+
+ static Dictionary appColors = new Dictionary();
+
+ int getAppColor(long appId)
+ {
+ if(!appColors.TryGetValue(appId, out var color))
+ {
+ var text = db.ExecuteValue($"select text from app where id={appId}");
+ lock (appColors)
+ {
+ color = appColors[appId] = getColor(text);
+ }
+ }
+ return color;
+ }
+
+ static Dictionary tagColors = new Dictionary();
+ int getTagColor(long tagId)
+ {
+ if (!tagColors.TryGetValue(tagId, out var color))
+ {
+ if (tagId == -1)
+ {
+ color = tagColors[tagId] = BitConverter.ToInt32(new byte[] { 0xFF, 0x99, 0x99, 0x99 }, 0);
+ }
+ else
+ {
+ var text = db.ExecuteValue($"select text from tag where id={tagId}");
+ color = tagColors[tagId] = getColor(text);
+ }
+ }
+ return color;
+ }
+
+
+ public unsafe byte[] getPeriodBar(DateTime timefrom, DateTime timeto, string view, int width)
+ {
+ if (width <= 0 || width > 8000)
+ {
+ return null;
+ }
+
+
+ var totalsecs = (timeto - timefrom).TotalSeconds;
+ IEnumerable data;
+ if (view == "app")
+ {
+ data = db.ExecuteDynamic(@"
+select
+ app.id appId,
+ p.timeStart,
+ p.timeEnd
+from app
+join win on win.appid = app.id
+join period p on p.winid = win.id
+where
+ timeStart between @v0 and @v1
+ or timeEnd between @v0 and @v1
+ or @v0 between timeStart and timeEnd
+order by p.timeStart
+",
+ timefrom, timeto
+ );
+ }
+ else
+ {
+ data = db.ExecuteDynamic(@"
+select
+ ifnull(tag.id,-1) tagId,
+ p.timeStart,
+ p.timeEnd
+from app
+join win on win.appid = app.id
+join period p on p.winid = win.id
+left join tag on tag.id = win.tagId or (win.tagId = 0 and tag.id = app.tagId)
+where
+ timeStart between @v0 and @v1
+ or timeEnd between @v0 and @v1
+ or @v0 between timeStart and timeEnd
+order by p.timeStart
+",
+ timefrom, timeto
+ );
+ }
+
+ //绘制PeriodBar,直接写内存比gdi+快
+ var imgdata = new int[width];
+ foreach (var period in data)
+ {
+ var from = Math.Max(0, (int)Math.Round((period.timeStart - timefrom).TotalSeconds / totalsecs * width));
+ var to = Math.Min(width - 1, (int)Math.Round((period.timeEnd - timefrom).TotalSeconds / totalsecs * width));
+
+ for (var x = from; x <= Math.Min(width - 1, to); x++)
+ {
+ imgdata[x] = view == "app" ? (int)getAppColor(period.appId) : (int)getTagColor(period.tagId);
+ }
+ }
+
+ fixed (int* p = &imgdata[0])
+ {
+ var ptr = new IntPtr(p);
+ using var bmp = new Bitmap(width, 1, width * 4, PixelFormat.Format32bppArgb, ptr);
+ using var mem = new MemoryStream();
+ bmp.Save(mem, ImageFormat.Png);
+ return mem.ToArray();
+ }
+
+ }
+
+ public object getTree(DateTime timefrom, DateTime timeto, string view, long parentKey)
+ {
+ var result = new List