提交记录修改Core/Servicers/Instances/Data.cs+63 −116看文件
修改Core/Servicers/Instances/Main.cs+54 −35看文件
修改Core/Servicers/Interfaces/IData.cs+5 −4看文件
| @@ -13,6 +13,8 @@ using Npoi.Mapper; | ||
| 13 | 13 | using System.IO; |
| 14 | 14 | using CsvHelper; |
| 15 | 15 | using System.Globalization; |
| 16 | 新增:using System.Threading.Tasks; | |
| 17 | 新增:using NPOI.SS.Formula.Functions; | |
| 16 | 18 | |
| 17 | 19 | namespace Core.Servicers.Instances |
| 18 | 20 | { |
| @@ -26,156 +28,101 @@ namespace Core.Servicers.Instances | ||
| 26 | 28 | _appData = appData_; |
| 27 | 29 | _database = database_; |
| 28 | 30 | } |
| 29 | 删除: public void Set(string processName, int seconds, DateTime? time_ = null) | |
| 31 | 新增: | |
| 32 | 新增: public void SaveAppDuration(string processName_, int duration_, DateTime startDateTime_) | |
| 30 | 33 | { |
| 31 | 删除: lock (setLock) | |
| 32 | 删除: { | |
| 33 | 删除: DateTime time = !time_.HasValue ? DateTime.Now : time_.Value; | |
| 34 | 删除: var today = time.Date; | |
| 34 | 新增: // 过滤无效数据 | |
| 35 | 新增: if (string.IsNullOrEmpty(processName_) || duration_ <= 0) return; | |
| 35 | 36 | |
| 36 | 删除: if (string.IsNullOrEmpty(processName) || seconds <= 0) | |
| 37 | 新增: Task.Run(() => | |
| 38 | 新增: { | |
| 39 | 新增: try | |
| 37 | 40 | { |
| 38 | 删除: return; | |
| 39 | 删除: } | |
| 41 | 新增: if (startDateTime_.Minute == 59 && startDateTime_.Second >= 58) | |
| 42 | 新增: { | |
| 43 | 新增: // 当记录开始时间接近59分59秒时划到下一个小时时段 | |
| 44 | 新增: startDateTime_ = startDateTime_.AddSeconds(2); | |
| 45 | 新增: startDateTime_ = new DateTime(startDateTime_.Year, startDateTime_.Month, startDateTime_.Day, startDateTime_.Hour, 0, 0); | |
| 46 | 新增: } | |
| 40 | 47 | |
| 41 | 删除: AppModel app = _appData.GetApp(processName); | |
| 48 | 新增: using (var db = _database.GetWriterContext()) | |
| 49 | 新增: { | |
| 50 | 新增: var app = db.App.Where(m => m.Name == processName_).FirstOrDefault(); | |
| 51 | 新增: if (app == null) | |
| 52 | 新增: { | |
| 53 | 新增: _database.CloseWriter(); | |
| 54 | 新增: return; | |
| 55 | 新增: } | |
| 42 | 56 | |
| 43 | 删除: if (app == null) | |
| 44 | 删除: { | |
| 45 | 删除: return; | |
| 46 | 删除: } | |
| 47 | 删除: // 统计app累计使用时长 | |
| 48 | 删除: app.TotalTime += seconds; | |
| 49 | 删除: _appData.UpdateApp(app); | |
| 57 | 新增: // 当前时段最大可记录时长 | |
| 58 | 新增: int nowHoursMaxDuration = (60 - startDateTime_.Minute) * 60; | |
| 59 | 新增: duration_ = duration_ > nowHoursMaxDuration ? nowHoursMaxDuration : duration_; | |
| 50 | 60 | |
| 61 | 新增: // 更新app累计时长 | |
| 62 | 新增: app.TotalTime += duration_; | |
| 51 | 63 | |
| 52 | 删除: //using (var db = _database.GetReaderContext()) | |
| 53 | 删除: using (var db = _database.GetWriterContext()) | |
| 54 | 删除: { | |
| 55 | 删除: using (var transcation = db.Database.BeginTransaction()) | |
| 56 | 删除: { | |
| 57 | 删除: try | |
| 64 | 新增: // 更新每日数据 | |
| 65 | 新增: var dailyLog = db.DailyLog.SingleOrDefault(m => m.Date == startDateTime_.Date && m.AppModelID == app.ID); | |
| 66 | 新增: if (dailyLog == null) | |
| 58 | 67 | { |
| 59 | 删除: var res = db.DailyLog.SingleOrDefault(m => m.Date == today && m.AppModelID == app.ID); | |
| 60 | 删除: if (res == null) | |
| 68 | 新增: //数据库中没有时则创建 | |
| 69 | 新增: db.DailyLog.Add(new DailyLogModel() | |
| 61 | 70 | { |
| 62 | 删除: //数据库中没有时则创建 | |
| 63 | 删除: db.DailyLog.Add(new Models.DailyLogModel() | |
| 64 | 删除: { | |
| 65 | 删除: Date = today, | |
| 66 | 删除: AppModelID = app.ID, | |
| 67 | 删除: Time = seconds, | |
| 68 | 删除: }); | |
| 71 | 新增: Date = startDateTime_.Date, | |
| 72 | 新增: AppModelID = app.ID, | |
| 73 | 新增: Time = duration_ > 86400 ? 86400 : duration_, | |
| 74 | 新增: }); | |
| 75 | 新增: } | |
| 76 | 新增: else | |
| 77 | 新增: { | |
| 78 | 新增: if (dailyLog.Time + duration_ > 86400) | |
| 79 | 新增: { | |
| 80 | 新增: dailyLog.Time = 86400; | |
| 69 | 81 | } |
| 70 | 82 | else |
| 71 | 83 | { |
| 72 | 删除: res.Time += seconds; | |
| 84 | 新增: dailyLog.Time += duration_; | |
| 73 | 85 | } |
| 74 | 删除: | |
| 75 | 删除: db.SaveChanges(); | |
| 76 | 删除: transcation.Commit(); | |
| 77 | 删除: } | |
| 78 | 删除: catch (Exception e) | |
| 79 | 删除: { | |
| 80 | 删除: Logger.Error(e.ToString()); | |
| 81 | 删除: transcation.Rollback(); | |
| 82 | 86 | } |
| 83 | 删除: finally | |
| 84 | 删除: { | |
| 85 | 删除: _database.CloseWriter(); | |
| 86 | 删除: } | |
| 87 | 删除: } | |
| 88 | 删除: } | |
| 89 | 删除: | |
| 90 | 删除: SetHoursTime(app, seconds, time); | |
| 91 | 删除: } | |
| 92 | 删除: } | |
| 93 | 87 | |
| 94 | 删除: | |
| 95 | 删除: private void SetHoursTime(AppModel app, int seconds, DateTime time) | |
| 96 | 删除: { | |
| 97 | 删除: string processName = app.Name; | |
| 98 | 删除: // 当前时段 | |
| 99 | 删除: var nowtime = new DateTime(time.Year, time.Month, time.Day, time.Hour, 0, 0); | |
| 100 | 删除: | |
| 101 | 删除: if (time.Minute == 59 && time.Second == 59) | |
| 102 | 删除: { | |
| 103 | 删除: SetHoursTime(app, seconds, nowtime.AddHours(1)); | |
| 104 | 删除: return; | |
| 105 | 删除: } | |
| 106 | 删除: | |
| 107 | 删除: if (seconds <= 0 || time > DateTime.Now) | |
| 108 | 删除: { | |
| 109 | 删除: return; | |
| 110 | 删除: } | |
| 111 | 删除: | |
| 112 | 删除: | |
| 113 | 删除: //using (var db = _database.GetReaderContext()) | |
| 114 | 删除: using (var db = _database.GetWriterContext()) | |
| 115 | 删除: { | |
| 116 | 删除: using (var transcation = db.Database.BeginTransaction()) | |
| 117 | 删除: { | |
| 118 | 删除: try | |
| 119 | 删除: { | |
| 120 | 删除: var hourslog = db.HoursLog.SingleOrDefault( | |
| 121 | 删除: m => | |
| 122 | 删除: m.DataTime == nowtime | |
| 123 | 删除: && m.AppModelID == app.ID | |
| 124 | 删除: ); | |
| 125 | 删除: | |
| 126 | 删除: int overflowSeconds = 0; | |
| 127 | 删除: | |
| 128 | 删除: if (hourslog == null) | |
| 88 | 新增: // 更新时段数据 | |
| 89 | 新增: var time = new DateTime(startDateTime_.Year, startDateTime_.Month, startDateTime_.Day, startDateTime_.Hour, 0, 0); | |
| 90 | 新增: var hoursLog = db.HoursLog.SingleOrDefault(m => m.DataTime == time && m.AppModelID == app.ID); | |
| 91 | 新增: if (hoursLog == null) | |
| 129 | 92 | { |
| 130 | 删除: // 没有时创建 | |
| 131 | 删除: | |
| 132 | 删除: if (seconds > 3600) | |
| 93 | 新增: // 没有记录时创建 | |
| 94 | 新增: db.HoursLog.Add(new HoursLogModel() | |
| 133 | 95 | { |
| 134 | 删除: overflowSeconds = seconds - 3600; | |
| 135 | 删除: seconds = 3600; | |
| 136 | 删除: } | |
| 137 | 删除: | |
| 138 | 删除: db.HoursLog.Add(new Models.HoursLogModel() | |
| 139 | 删除: { | |
| 140 | 删除: DataTime = nowtime, | |
| 96 | 新增: DataTime = time, | |
| 141 | 97 | AppModelID = app.ID, |
| 142 | 删除: Time = seconds | |
| 98 | 新增: Time = duration_ | |
| 143 | 99 | }); |
| 144 | 100 | } |
| 145 | 101 | else |
| 146 | 102 | { |
| 147 | 删除: if (hourslog.Time + seconds > 3600) | |
| 103 | 新增: if (hoursLog.Time + duration_ > 3600) | |
| 148 | 104 | { |
| 149 | 删除: hourslog.Time = 3600; | |
| 150 | 删除: overflowSeconds = hourslog.Time + seconds - 3600; | |
| 105 | 新增: hoursLog.Time = 3600; | |
| 151 | 106 | } |
| 152 | 107 | else |
| 153 | 108 | { |
| 154 | 删除: hourslog.Time += seconds; | |
| 109 | 新增: hoursLog.Time += duration_; | |
| 155 | 110 | } |
| 156 | 111 | } |
| 157 | 112 | |
| 158 | 113 | db.SaveChanges(); |
| 159 | 删除: transcation.Commit(); | |
| 160 | 删除: | |
| 161 | 删除: if (overflowSeconds > 0) | |
| 162 | 删除: { | |
| 163 | 删除: SetHoursTime(app, overflowSeconds, nowtime.AddHours(1)); | |
| 164 | 删除: } | |
| 165 | 删除: } | |
| 166 | 删除: catch (Exception e) | |
| 167 | 删除: { | |
| 168 | 删除: Logger.Error(e.ToString()); | |
| 169 | 删除: transcation.Rollback(); | |
| 170 | 删除: } | |
| 171 | 删除: finally | |
| 172 | 删除: { | |
| 173 | 114 | _database.CloseWriter(); |
| 115 | 新增: Logger.Info($"Save app time done.Process:{processName_},Duration:{duration_},StartDateTime:{startDateTime_},AppID:{app.ID}"); | |
| 174 | 116 | } |
| 175 | 117 | } |
| 176 | 删除: //_database.Close(); | |
| 177 | 删除: } | |
| 118 | 新增: catch (Exception e) | |
| 119 | 新增: { | |
| 120 | 新增: Logger.Error($"Save app time error!Process:{processName_},Duration:{duration_},StartDateTime:{startDateTime_}.\r\nError:\r\n{e.Message}"); | |
| 121 | 新增: | |
| 122 | 新增: } | |
| 123 | 新增: }); | |
| 178 | 124 | } |
| 125 | 新增: | |
| 179 | 126 | public List<DailyLogModel> GetTodaylogList() |
| 180 | 127 | { |
| 181 | 128 | |
| @@ -9,6 +9,7 @@ using System; | ||
| 9 | 9 | using System.Collections.Generic; |
| 10 | 10 | using System.Data.Entity; |
| 11 | 11 | using System.Diagnostics; |
| 12 | 新增:using System.IO; | |
| 12 | 13 | using System.Linq; |
| 13 | 14 | using System.Text; |
| 14 | 15 | using System.Text.RegularExpressions; |
| @@ -148,13 +149,13 @@ namespace Core.Servicers.Instances | ||
| 148 | 149 | { |
| 149 | 150 | await Task.Run(() => |
| 150 | 151 | { |
| 151 | 删除: Debug.WriteLine("db self check start"); | |
| 152 | 新增: CreateDirectory(); | |
| 153 | 新增: | |
| 152 | 154 | // 数据库自检 |
| 153 | 155 | using (var db = new TaiDbContext()) |
| 154 | 156 | { |
| 155 | 157 | db.SelfCheck(); |
| 156 | 158 | } |
| 157 | 删除: Debug.WriteLine("db self check over!"); | |
| 158 | 159 | |
| 159 | 160 | // 加载app信息 |
| 160 | 161 | appData.Load(); |
| @@ -172,14 +173,14 @@ namespace Core.Servicers.Instances | ||
| 172 | 173 | config = appConfig.GetConfig(); |
| 173 | 174 | UpdateConfigIgnoreProcess(); |
| 174 | 175 | |
| 175 | 删除: | |
| 176 | 新增: | |
| 176 | 177 | |
| 177 | 178 | // 启动睡眠监测 |
| 178 | 179 | sleepdiscover.Start(); |
| 179 | 删除: | |
| 180 | 新增: | |
| 180 | 181 | // 初始化过滤器 |
| 181 | 182 | _webFilter.Init(); |
| 182 | 删除: | |
| 183 | 新增: | |
| 183 | 184 | // 启动主服务 |
| 184 | 185 | Start(); |
| 185 | 186 | |
| @@ -208,6 +209,14 @@ namespace Core.Servicers.Instances | ||
| 208 | 209 | appObserver?.Stop(); |
| 209 | 210 | } |
| 210 | 211 | |
| 212 | 新增: /// <summary> | |
| 213 | 新增: /// 创建程序目录 | |
| 214 | 新增: /// </summary> | |
| 215 | 新增: private void CreateDirectory() | |
| 216 | 新增: { | |
| 217 | 新增: string dir = Path.Combine(FileHelper.GetRootDirectory(), "Data"); | |
| 218 | 新增: Directory.CreateDirectory(dir); | |
| 219 | 新增: } | |
| 211 | 220 | |
| 212 | 221 | private void UpdateConfigIgnoreProcess() |
| 213 | 222 | { |
| @@ -335,8 +344,12 @@ namespace Core.Servicers.Instances | ||
| 335 | 344 | return; |
| 336 | 345 | } |
| 337 | 346 | |
| 347 | 新增: string lastActiveProcess = activeProcess != null ? activeProcess.ToString() : ""; | |
| 348 | 新增: | |
| 338 | 349 | bool isCheck = IsCheckApp(args.ProcessName, args.Description, args.File); |
| 339 | 350 | |
| 351 | 新增: Logger.Info($"Active[{isCheck}]:" + args.ProcessName + ",Last:" + lastActiveProcess + ",Time:" + activeStartTime.ToString()); | |
| 352 | 新增: | |
| 340 | 353 | if (activeProcess != args.ProcessName) |
| 341 | 354 | { |
| 342 | 355 | UpdateTime(); |
| @@ -354,41 +367,45 @@ namespace Core.Servicers.Instances | ||
| 354 | 367 | { |
| 355 | 368 | activeProcess = null; |
| 356 | 369 | } |
| 370 | 新增: | |
| 357 | 371 | } |
| 358 | 372 | |
| 359 | 删除: private void HandleLinks(string processName, int seconds, DateTime? time = null) | |
| 373 | 新增: private void HandleLinks(string processName, int seconds, DateTime time) | |
| 360 | 374 | { |
| 361 | 删除: try | |
| 375 | 新增: Task.Run(() => | |
| 362 | 376 | { |
| 363 | 删除: List<LinkModel> links = config.Links != null ? config.Links : new List<LinkModel>(); | |
| 364 | 删除: foreach (LinkModel link in links) | |
| 377 | 新增: try | |
| 365 | 378 | { |
| 366 | 删除: if (link.ProcessList != null | |
| 367 | 删除: && link.ProcessList.Count >= 2 | |
| 368 | 删除: && link.ProcessList.Contains(processName)) | |
| 379 | 新增: List<LinkModel> links = config.Links != null ? config.Links : new List<LinkModel>(); | |
| 380 | 新增: foreach (LinkModel link in links) | |
| 369 | 381 | { |
| 370 | 删除: // 属于关联进程 | |
| 371 | 删除: foreach (string linkProcess in link.ProcessList) | |
| 382 | 新增: if (link.ProcessList != null | |
| 383 | 新增: && link.ProcessList.Count >= 2 | |
| 384 | 新增: && link.ProcessList.Contains(processName)) | |
| 372 | 385 | { |
| 373 | 删除: if (linkProcess != processName) | |
| 386 | 新增: // 属于关联进程 | |
| 387 | 新增: foreach (string linkProcess in link.ProcessList) | |
| 374 | 388 | { |
| 375 | 删除: if (IsProcessRuning(linkProcess)) | |
| 389 | 新增: if (linkProcess != processName) | |
| 376 | 390 | { |
| 377 | 删除: // 同步更新 | |
| 378 | 删除: data.Set(linkProcess, seconds, time); | |
| 379 | 删除: } | |
| 391 | 新增: if (IsProcessRuning(linkProcess)) | |
| 392 | 新增: { | |
| 393 | 新增: // 同步更新 | |
| 394 | 新增: data.SaveAppDuration(linkProcess, seconds, time); | |
| 395 | 新增: } | |
| 380 | 396 | |
| 397 | 新增: } | |
| 381 | 398 | } |
| 399 | 新增: break; | |
| 382 | 400 | } |
| 383 | 删除: break; | |
| 384 | 401 | } |
| 385 | 删除: } | |
| 386 | 删除: } | |
| 387 | 删除: catch (Exception ex) | |
| 388 | 删除: { | |
| 389 | 删除: Logger.Error(ex.Message + ",关联进程更新错误,Process Name: " + processName + ",Time: " + seconds); | |
| 390 | 删除: } | |
| 391 | 402 | |
| 403 | 新增: } | |
| 404 | 新增: catch (Exception ex) | |
| 405 | 新增: { | |
| 406 | 新增: Logger.Error(ex.Message + ",关联进程更新错误,Process Name: " + processName + ",Time: " + seconds); | |
| 407 | 新增: } | |
| 408 | 新增: }); | |
| 392 | 409 | } |
| 393 | 410 | |
| 394 | 411 | #region 判断进程是否在运行中 |
| @@ -410,10 +427,10 @@ namespace Core.Servicers.Instances | ||
| 410 | 427 | |
| 411 | 428 | if (!string.IsNullOrEmpty(app)) |
| 412 | 429 | { |
| 413 | 删除: var time = activeStartTime; | |
| 430 | 新增: var time = new DateTime(activeStartTime.Ticks); | |
| 414 | 431 | |
| 415 | 432 | // 更新计时 |
| 416 | 删除: TimeSpan timeSpan = DateTime.Now - activeStartTime; | |
| 433 | 新增: TimeSpan timeSpan = DateTime.Now - time; | |
| 417 | 434 | |
| 418 | 435 | int seconds = (int)timeSpan.TotalSeconds; |
| 419 | 436 | |
| @@ -425,15 +442,12 @@ namespace Core.Servicers.Instances | ||
| 425 | 442 | |
| 426 | 443 | if (seconds > 0) |
| 427 | 444 | { |
| 428 | 删除: Task.Run(() => | |
| 429 | 删除: { | |
| 430 | 删除: data.Set(app, seconds, time); | |
| 445 | 新增: data.SaveAppDuration(app, seconds, time); | |
| 431 | 446 | |
| 432 | 删除: // 关联进程更新 | |
| 433 | 删除: HandleLinks(app, seconds, time); | |
| 447 | 新增: // 关联进程更新 | |
| 448 | 新增: HandleLinks(app, seconds, time); | |
| 434 | 449 | |
| 435 | 删除: Logger.Info("status:" + sleepStatus + ",process:" + app + ",seconds:" + seconds + ",start:" + activeStartTime.ToString() + ",end:" + DateTime.Now.ToString() + ",time:" + time.ToString()); | |
| 436 | 删除: }); | |
| 450 | 新增: //Logger.Info("status:" + sleepStatus + ",process:" + app + ",seconds:" + seconds + ",start:" + activeStartTime.ToString() + ",end:" + DateTime.Now.ToString() + ",time:" + time.ToString()); | |
| 437 | 451 | } |
| 438 | 452 | |
| 439 | 453 | activeStartTime = DateTime.Now; |
| @@ -447,6 +461,11 @@ namespace Core.Servicers.Instances | ||
| 447 | 461 | /// </summary> |
| 448 | 462 | private void HandleWebServiceConfig() |
| 449 | 463 | { |
| 464 | 新增: if (config == null) | |
| 465 | 新增: { | |
| 466 | 新增: return; | |
| 467 | 新增: } | |
| 468 | 新增: | |
| 450 | 469 | if (config.General.IsWebEnabled) |
| 451 | 470 | { |
| 452 | 471 | browserObserver.Start(); |
| @@ -12,11 +12,12 @@ namespace Core.Servicers.Interfaces | ||
| 12 | 12 | { |
| 13 | 13 | |
| 14 | 14 | /// <summary> |
| 15 | 删除: /// 设置进程数据 | |
| 15 | 新增: /// 保存APP使用时长数据 | |
| 16 | 16 | /// </summary> |
| 17 | 删除: /// <param name="processName">进程名称</param> | |
| 18 | 删除: /// <param name="seconds">时长(秒)</param> | |
| 19 | 删除: void Set(string processName, int seconds, DateTime? time = null); | |
| 17 | 新增: /// <param name="processName_">进程名称</param> | |
| 18 | 新增: /// <param name="duration_">时长(秒)</param> | |
| 19 | 新增: /// <param name="startDateTime_">记录开始时间</param> | |
| 20 | 新增: void SaveAppDuration(string processName_, int duration_, DateTime startDateTime_); | |
| 20 | 21 | |
| 21 | 22 | /// <summary> |
| 22 | 23 | /// 获取今天的数据 |

