From d5b956c74239486b6268b29720fd4566b32bd816 Mon Sep 17 00:00:00 2001 From: DreamEnderKing Date: Sat, 13 Apr 2024 20:37:50 +0800 Subject: [PATCH] fix: :sparkles: Log system bugs fixed and add download report. --- installer/Data/DownloadReport.cs | 68 ++++++++++++++++++++++++++++++++ installer/Model/Downloader.cs | 29 +++++++++----- installer/Model/Local_Data.cs | 13 +++--- installer/Model/Logger.cs | 8 ++-- installer/Model/Tencent_Cos.cs | 38 ++++++++++++------ 5 files changed, 127 insertions(+), 29 deletions(-) create mode 100644 installer/Data/DownloadReport.cs diff --git a/installer/Data/DownloadReport.cs b/installer/Data/DownloadReport.cs new file mode 100644 index 00000000..15f2daa5 --- /dev/null +++ b/installer/Data/DownloadReport.cs @@ -0,0 +1,68 @@ +using installer.ViewModel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace installer.Data +{ + public class DownloadReport : NotificationObject + { + // 是否开启大文件跟踪 + public bool BigFileTraceEnabled + { + get => bFTE; set + { + if (bFTE != value) OnPropertyChanged(); + bFTE = value; + } + } + + // 下载文件数量 + public long Count + { + get => cot; set + { + if (cot != value) OnPropertyChanged(); + cot = value; + } + } + + // 下载完成的文件数量 + public long ComCount + { + get => com; set + { + if (com != value) OnPropertyChanged(); + com = value; + } + } + + // 文件大小 + public long Total + { + get => tot; set + { + if (tot != value) OnPropertyChanged(); + tot = value; + } + } + + // 已完成大小 + public long Completed + { + get => cop; set + { + if (cop != value) OnPropertyChanged(); + cop = value; + } + } + + private bool bFTE; + private long cot; + private long com; + private long tot; + private long cop; + } +} diff --git a/installer/Model/Downloader.cs b/installer/Model/Downloader.cs index 05dbeaa0..2c32375f 100755 --- a/installer/Model/Downloader.cs +++ b/installer/Model/Downloader.cs @@ -70,6 +70,7 @@ public class Updater public bool LoginFailed { get; set; } = false; public bool RememberMe { get => Data.RememberMe; set { Data.RememberMe = value; } } + public DownloadReport CloudReport { get => Cloud.Report; } #endregion #region 方法区 @@ -106,10 +107,10 @@ public Downloader() Web = new EEsast(LoggerProvider.FromFile(Path.Combine(Data.LogPath, "EESAST.log"))); Web.Token_Changed += SaveToken; - Data.Log.Partner = Log; - Cloud.Log.Partner = Log; - Web.Log.Partner = Log; - Log.Partner = LogList; + Data.Log.Partner.Add(Log); + Cloud.Log.Partner.Add(Log); + Web.Log.Partner.Add(Log); + Log.Partner.Add(LogList); if (Data.Config.Remembered) { @@ -172,6 +173,7 @@ public void Install(string? path = null) string zp = Path.Combine(Data.Config.InstallPath, "THUAI7.tar.gz"); Status = UpdateStatus.downloading; + (CloudReport.Count, CloudReport.ComCount) = (1, 1); Log.LogInfo($"正在下载安装包……"); Cloud.DownloadFileAsync(zp, "THUAI7.tar.gz").Wait(); Status = UpdateStatus.unarchieving; @@ -232,10 +234,20 @@ public void ResetInstallPath(string newPath) var installPath = Data.Config.InstallPath.EndsWith(Path.DirectorySeparatorChar) ? Data.Config.InstallPath[0..-1] : Data.Config.InstallPath; if (newPath != installPath) { + if (!Directory.Exists(newPath)) + { + Directory.CreateDirectory(newPath); + } + if (Directory.Exists(Path.Combine(newPath, "Logs"))) + { + Directory.Delete(Path.Combine(newPath, "Logs"), true); + } + Directory.Move(Path.Combine(installPath, "Logs"), Path.Combine(newPath, "Logs")); + if (Cloud.Log is FileLogger) ((FileLogger)Cloud.Log).Path = Path.Combine(newPath, "Logs", "TencentCos.log"); + if (Web.Log is FileLogger) ((FileLogger)Web.Log).Path = Path.Combine(newPath, "Logs", "EESAST.log"); + if (Data.Log is FileLogger) ((FileLogger)Data.Log).Path = Path.Combine(newPath, "Logs", "Local_Data.log"); + if (Log is FileLogger) ((FileLogger)Log).Path = Path.Combine(newPath, "Logs", "Main.log"); Data.ResetInstallPath(newPath); - if (Cloud.Log is FileLogger) ((FileLogger)Cloud.Log).Path = Path.Combine(Data.LogPath, "TencentCos.log"); - if (Web.Log is FileLogger) ((FileLogger)Web.Log).Path = Path.Combine(Data.LogPath, "EESAST.log"); - if (Log is FileLogger) ((FileLogger)Log).Path = Path.Combine(Data.LogPath, "Main.log"); } Update(); } @@ -391,8 +403,7 @@ public int Update() CurrentVersion = Data.FileHashData.Version; Status = UpdateStatus.hash_computing; Log.LogInfo("正在校验……"); - Data.ScanDir(); - if (Data.MD5Update.Count == 0) + if (!CheckUpdate()) { Log.LogInfo("更新成功!"); Status = UpdateStatus.success; diff --git a/installer/Model/Local_Data.cs b/installer/Model/Local_Data.cs index ae2564db..3541dc3a 100755 --- a/installer/Model/Local_Data.cs +++ b/installer/Model/Local_Data.cs @@ -104,22 +104,24 @@ public void ResetInstallPath(string newPath) { if (Config.InstallPath != newPath) { + var oldPath = Config.InstallPath; + Config.InstallPath = newPath; if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } - Log.LogInfo($"Move work started: {Config.InstallPath} -> {newPath}"); + Log.LogInfo($"Move work started: {oldPath} -> {newPath}"); Action action = (dir) => { }; var moveTask = (DirectoryInfo dir) => { foreach (var file in dir.EnumerateFiles()) { - var newName = Path.Combine(newPath, FileService.ConvertAbsToRel(Config.InstallPath, file.FullName)); + var newName = Path.Combine(newPath, FileService.ConvertAbsToRel(oldPath, file.FullName)); file.MoveTo(newName); } foreach (var sub in dir.EnumerateDirectories()) { - var newName = Path.Combine(newPath, FileService.ConvertAbsToRel(Config.InstallPath, sub.FullName)); + var newName = Path.Combine(newPath, FileService.ConvertAbsToRel(oldPath, sub.FullName)); if (!Directory.Exists(newName)) { Directory.CreateDirectory(newName); @@ -128,9 +130,8 @@ public void ResetInstallPath(string newPath) } }; action = moveTask; - moveTask(new DirectoryInfo(Config.InstallPath)); - Directory.Delete(Config.InstallPath, true); - Config.InstallPath = newPath; + moveTask(new DirectoryInfo(oldPath)); + Directory.Delete(oldPath, true); } MD5DataPath = Config.MD5DataPath.StartsWith('.') ? Path.Combine(Config.InstallPath, Config.MD5DataPath) : diff --git a/installer/Model/Logger.cs b/installer/Model/Logger.cs index 6aed625b..622e6681 100644 --- a/installer/Model/Logger.cs +++ b/installer/Model/Logger.cs @@ -36,7 +36,7 @@ public class LogRecord public abstract class Logger : IDisposable { private int jobID = 0; - public Logger? Partner; + public List Partner = new List(); public string PartnerInfo = string.Empty; public Dictionary CountDict = new Dictionary { @@ -60,12 +60,12 @@ protected virtual bool IsEnabled(LogLevel level) protected virtual void Log(LogLevel logLevel, int eventId, string message) { CountDict[logLevel] += 1; - Partner?.Log(logLevel, eventId, PartnerInfo + message); + Partner.ForEach(i => i.Log(logLevel, eventId, PartnerInfo + message)); } protected virtual void Log(LogLevel logLevel, string message) { CountDict[logLevel] += 1; - Partner?.Log(logLevel, PartnerInfo + message); + Partner.ForEach(i => i.Log(logLevel, PartnerInfo + message)); } public int StartNew() => (jobID++); public void LogDebug(int eventId, string message) @@ -275,6 +275,7 @@ protected override void Log(LogLevel logLevel, int eventId, string message) break; } writer.Flush(); + writer.Dispose(); mutex.ReleaseMutex(); base.Log(logLevel, eventId, message); } @@ -316,6 +317,7 @@ protected override void Log(LogLevel logLevel, string message) break; } writer.Flush(); + writer.Dispose(); mutex.ReleaseMutex(); base.Log(logLevel, message); } diff --git a/installer/Model/Tencent_Cos.cs b/installer/Model/Tencent_Cos.cs index 16e97c9c..e464e863 100755 --- a/installer/Model/Tencent_Cos.cs +++ b/installer/Model/Tencent_Cos.cs @@ -8,6 +8,7 @@ using COSXML.Common; using COSXML.Transfer; using System; +using installer.Data; // 禁用对没有调用异步API的异步函数的警告 #pragma warning disable CS1998 @@ -23,12 +24,14 @@ public class Tencent_Cos protected CosXmlConfig config; protected CosXmlServer cosXml; + public DownloadReport Report; public Tencent_Cos(string appid, string region, string bucketName, Logger? _log = null) { Appid = appid; Region = region; BucketName = bucketName; Log = _log ?? LoggerProvider.FromConsole(); Log.PartnerInfo = "[COS]"; + Report = new DownloadReport(); // 初始化CosXmlConfig(提供配置SDK接口) config = new CosXmlConfig.Builder() .IsHttps(true) // 设置默认 HTTPS 请求 @@ -79,6 +82,9 @@ public async Task DownloadFileAsync(string savePath, string? remotePath = n if (head.size > (100 << 20)) { // 文件大小大于100MB则设置回调函数 + Report.Total = head.size; + Report.Completed = 0; + Report.BigFileTraceEnabled = true; var size = (head.size > 1 << 30) ? string.Format("{0:##.#}GB", ((double)head.size) / (1 << 30)) : string.Format("{0:##.#}MB", ((double)head.size) / (1 << 20)); @@ -87,28 +93,36 @@ public async Task DownloadFileAsync(string savePath, string? remotePath = n { if (completed > 1 << 30 && completed - c > 100 << 20) { - Log.LogInfo(string.Format("downloaded = {0:##.#}GB, progress = {1:##.##}%", ((double)completed) / (1 << 30), completed * 100.0 / total)); + Log.LogDebug(string.Format("downloaded = {0:##.#}GB, progress = {1:##.##}%", ((double)completed) / (1 << 30), completed * 100.0 / total)); c = completed; } if (completed < 1 << 30 && completed - c > 10 << 20) { - Log.LogInfo(string.Format("downloaded = {0:##.#}MB, progress = {1:##.##}%", ((double)completed) / (1 << 20), completed * 100.0 / total)); + Log.LogDebug(string.Format("downloaded = {0:##.#}MB, progress = {1:##.##}%", ((double)completed) / (1 << 20), completed * 100.0 / total)); c = completed; } + (Report.Completed, Report.Total) = (completed, total); }); } + else + { + if (Report.Completed > 0 && Report.Total > 0 && Report.Completed == Report.Total) + Report.BigFileTraceEnabled = false; + } // 执行请求 GetObjectResult result = cosXml.GetObject(request); + if (Report.BigFileTraceEnabled) + Report.Completed = Report.Total; // 请求成功 if (result.httpCode != 200) throw new Exception($"Download task: {{\"{remotePath}\"->\"{savePath}\"}} failed, message: {result.httpCode} {result.httpMessage}"); - Log.LogInfo(thID, $"Download task: {{\"{remotePath}\"->\"{savePath}\"}} finished."); + Log.LogDebug(thID, $"Download task: {{\"{remotePath}\"->\"{savePath}\"}} finished."); } catch (Exception ex) { Log.LogError(thID, ex.Message); - Log.LogInfo(thID, $"Download task: {{\"{remotePath}\"->\"{savePath}\"}} ended unexpectedly."); + Log.LogDebug(thID, $"Download task: {{\"{remotePath}\"->\"{savePath}\"}} ended unexpectedly."); thID = -1; } return thID; @@ -117,16 +131,17 @@ public async Task DownloadFileAsync(string savePath, string? remotePath = n public async Task DownloadQueueAsync(string basePath, IEnumerable queue) { int thID = Log.StartNew(); - Log.LogInfo(thID, "Batch download task started."); + Log.LogDebug(thID, "Batch download task started."); var array = queue.ToArray(); - int count = array.Count(); - int finished = 0; - if (count == 0) + Report.Count = array.Count(); + Report.ComCount = 0; + if (Report.Count == 0) return 0; - var partitionar = Partitioner.Create(0, count); + var partitionar = Partitioner.Create(0, Report.Count); + var c = 0; Parallel.ForEach(partitionar, (range, loopState) => { - for (int i = range.Item1; i < range.Item2; i++) + for (long i = range.Item1; i < range.Item2; i++) { if (loopState.IsStopped) break; @@ -142,7 +157,8 @@ public async Task DownloadQueueAsync(string basePath, IEnumerable q } finally { - Interlocked.Increment(ref finished); + Interlocked.Increment(ref c); + Report.ComCount = c; Log.LogInfo(thID, $"Child process: {subID} finished."); } }