Skip to content

Commit

Permalink
Merge pull request eesast#216 from DreamEnderKing/dev
Browse files Browse the repository at this point in the history
Log system updated and add download report.
  • Loading branch information
DreamEnderKing authored Apr 13, 2024
2 parents af1c4ff + a32aeeb commit 62747be
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 29 deletions.
68 changes: 68 additions & 0 deletions installer/Data/DownloadReport.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
29 changes: 20 additions & 9 deletions installer/Model/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 方法区
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions installer/Model/Local_Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DirectoryInfo> 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);
Expand All @@ -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) :
Expand Down
8 changes: 5 additions & 3 deletions installer/Model/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public string Color
public abstract class Logger : IDisposable
{
private int jobID = 0;
public Logger? Partner;
public List<Logger> Partner = new List<Logger>();
public string PartnerInfo = string.Empty;
public Dictionary<LogLevel, int> CountDict = new Dictionary<LogLevel, int>
{
Expand All @@ -76,12 +76,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)
Expand Down Expand Up @@ -291,6 +291,7 @@ protected override void Log(LogLevel logLevel, int eventId, string message)
break;
}
writer.Flush();
writer.Dispose();
mutex.ReleaseMutex();
base.Log(logLevel, eventId, message);
}
Expand Down Expand Up @@ -332,6 +333,7 @@ protected override void Log(LogLevel logLevel, string message)
break;
}
writer.Flush();
writer.Dispose();
mutex.ReleaseMutex();
base.Log(logLevel, message);
}
Expand Down
38 changes: 27 additions & 11 deletions installer/Model/Tencent_Cos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using COSXML.Common;
using COSXML.Transfer;
using System;
using installer.Data;

// 禁用对没有调用异步API的异步函数的警告
#pragma warning disable CS1998
Expand All @@ -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 请求
Expand Down Expand Up @@ -79,6 +82,9 @@ public async Task<int> 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));
Expand All @@ -87,28 +93,36 @@ public async Task<int> 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;
Expand All @@ -117,16 +131,17 @@ public async Task<int> DownloadFileAsync(string savePath, string? remotePath = n
public async Task<int> DownloadQueueAsync(string basePath, IEnumerable<string> 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;
Expand All @@ -142,7 +157,8 @@ public async Task<int> DownloadQueueAsync(string basePath, IEnumerable<string> q
}
finally
{
Interlocked.Increment(ref finished);
Interlocked.Increment(ref c);
Report.ComCount = c;
Log.LogInfo(thID, $"Child process: {subID} finished.");
}
}
Expand Down

0 comments on commit 62747be

Please sign in to comment.