GetLogAfterSalesTask.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using FISLib.AfterSales;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Vinno.FIS.Sonopost.Common;
  9. using Vinno.FIS.Sonopost.Helpers;
  10. using Vinno.IUS.Common.Log;
  11. namespace Vinno.FIS.Sonopost.Features.AfterSales
  12. {
  13. internal class GetLogAfterSalesTask : AfterSalesTask
  14. {
  15. private DateTime _startTime;
  16. private DateTime _endTime;
  17. private string _exportLogFolder;
  18. private string _logPath;
  19. private string _targetId;
  20. private const double _prepareLogSuccess = 0.2;
  21. private const double _compressSuccessProgress = 0.4;
  22. private const double _finishProcess = 1.0;
  23. private string _remoteLogZipPath;
  24. private Stopwatch _stopwatch;
  25. private FISMasterInfo _masterInfo;
  26. public GetLogAfterSalesTask(FISMasterInfo masterInfo)
  27. {
  28. Status = AfterSalesCommandStatus.Progressing;
  29. _masterInfo = masterInfo;
  30. }
  31. protected override void OnDownloadProgressChanged(object sender, double e)
  32. {
  33. }
  34. protected override void OnDownloadResultRaised(object sender, int e)
  35. {
  36. }
  37. public override void Execute()
  38. {
  39. try
  40. {
  41. if (IsExcuting)
  42. {
  43. return;
  44. }
  45. IsExcuting = true;
  46. if (FISAdditionParameters == null)
  47. {
  48. Logger.WriteLineError($"GetLogTask FISAdditionParameters is null");
  49. SendProcessResultToServer(new FISProcessResult(_masterInfo.UserId, FISTaskType.GetLog, FISTaskStatus.Failed, FISDetailType.GetLogDeny, 0.01, FISProcessSource.FromTerminal, string.Empty));
  50. return;
  51. }
  52. InitParameters();
  53. _exportLogFolder = Path.Combine(SonopostConstants.DataFolder, "ExportLogFolder");
  54. string[] logsType = { "Logs", "SmartLogs", "TRTCClientLogs", "TRTCLogs" };
  55. try
  56. {
  57. DirectoryHelper.DeleteDirectory(_exportLogFolder);
  58. DirectoryHelper.CreateDirectory(_exportLogFolder);
  59. foreach (var type in logsType)
  60. {
  61. GetLogsFile(type);
  62. }
  63. UploadLogFiles(_exportLogFolder);
  64. }
  65. catch (Exception ex)
  66. {
  67. Logger.WriteLineError($"OnGetLogEvent error:{ex}");
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. Logger.WriteLineError($"GetLogAfterSalesTask Excute Error:{ex}");
  73. IsExcuting = false;
  74. }
  75. }
  76. private void InitParameters()
  77. {
  78. var parameterBeginTime = FISAdditionParameters.FirstOrDefault(x => x.Name == "BeginTime");
  79. if (parameterBeginTime != null) DateTime.TryParse(parameterBeginTime.Value, out _startTime);
  80. var parameterEndTime = FISAdditionParameters.FirstOrDefault(x => x.Name == "EndTime");
  81. if (parameterEndTime != null) DateTime.TryParse(parameterEndTime.Value, out _endTime);
  82. _targetId = _masterInfo.UserId;
  83. _remoteLogZipPath = Path.Combine(SonopostConstants.DataFolder, @"vinnologs\remotelogs.zip");
  84. }
  85. /// <summary>
  86. /// 获取Logs
  87. /// </summary>
  88. /// <param name="info"></param>
  89. private void GetLogsFile(string pathType)
  90. {
  91. string logsPath = Path.Combine(SonopostConstants.DataFolder, pathType);
  92. var exportLogFolder = Path.Combine(_exportLogFolder, pathType);
  93. if (!Directory.Exists(logsPath))
  94. {
  95. Logger.WriteLineInfo($"No Exist Path:{logsPath}");
  96. return;
  97. }
  98. DirectoryInfo di = new DirectoryInfo(logsPath);
  99. var fileList = di.GetFiles("*.*", SearchOption.AllDirectories);
  100. foreach (var logFile in fileList)
  101. {
  102. if (_startTime <= logFile.CreationTime.ToUniversalTime() && logFile.CreationTime.ToUniversalTime() <= _endTime)
  103. {
  104. string newFileName = logFile.FullName.Replace(logsPath, exportLogFolder);
  105. if (!newFileName.EndsWith(".log"))
  106. newFileName = newFileName + ".log";
  107. string fileDir = Path.GetDirectoryName(newFileName);
  108. DirectoryHelper.CreateDirectory(fileDir);
  109. logFile.CopyTo(newFileName, true);
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// Uplaod Log Files
  115. /// </summary>
  116. /// <param name="path">file path</param>
  117. private void UploadLogFiles(string path)
  118. {
  119. _logPath = path;
  120. var task = new Task(UploadLog, CancellationTokenSource.Token);
  121. task.Start();
  122. }
  123. private void UploadLog()
  124. {
  125. try
  126. {
  127. var remotelogs = Directory.GetFiles(_logPath, "*.*", SearchOption.AllDirectories);
  128. if (remotelogs.Any())
  129. {
  130. SendProcess(FISTaskStatus.Progressing, FISDetailType.PropareLogFile, _prepareLogSuccess);
  131. FileHelper.DeleteFile(_remoteLogZipPath);
  132. CompressHelper.CompressFolder(_logPath, _remoteLogZipPath);
  133. _stopwatch = new Stopwatch();
  134. _stopwatch.Start();
  135. OnUploadProgressChanged(this, 0.00);
  136. UploadFile(_remoteLogZipPath);
  137. }
  138. else
  139. {
  140. Logger.WriteLineError($"Upload Log Failed, because no log files found");
  141. SendProcess(FISTaskStatus.Failed, FISDetailType.GetLogError, 0);
  142. }
  143. }
  144. catch (Exception ex)
  145. {
  146. Logger.WriteLineError($"Upload log error:{ex}");
  147. if (CancellationTokenSource.IsCancellationRequested)
  148. {
  149. return;
  150. }
  151. SendProcess(FISTaskStatus.Failed, FISDetailType.GetLogError, 0);
  152. IsExcuting = false;
  153. }
  154. }
  155. private void SendProcess(FISTaskStatus taskStatus, FISDetailType detailType, double percent)
  156. {
  157. try
  158. {
  159. if (percent >= _finishProcess)
  160. {
  161. return;
  162. }
  163. var processInfo = new FISProcessInfo(percent, FISProgressStatus.Progressing, detailType);
  164. UpdateProgressInfo(processInfo);
  165. SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.GetLog, taskStatus, detailType, percent, FISProcessSource.FromTerminal, string.Empty));
  166. }
  167. catch (Exception ex)
  168. {
  169. Logger.WriteLineError($"Send process error:{ex}");
  170. }
  171. }
  172. protected override void OnUploadProgressChanged(object sender, double uploadProgress)
  173. {
  174. var percent = uploadProgress * (1 - _compressSuccessProgress) + _compressSuccessProgress;
  175. SendProcess(FISTaskStatus.Progressing, FISDetailType.UplaodLog, percent);
  176. Logger.WriteLineInfo($"Get Log Task:OnUploadProgressChanged,Send Process {percent}");
  177. }
  178. protected override void OnUploadResultRaised(object sender, string fileToken)
  179. {
  180. try
  181. {
  182. _stopwatch.Stop();
  183. if (!string.IsNullOrEmpty(fileToken))
  184. {
  185. Logger.WriteLineInfo($"Upload file to Vcloud storage finished, AVG speed:{0:F}KB/s," +
  186. $"{new FileInfo(_remoteLogZipPath).Length / 1024d / _stopwatch.Elapsed.TotalSeconds}");
  187. var processInfo = new FISProcessInfo(_finishProcess, FISProgressStatus.Progressing, FISDetailType.GetLogSuccess);
  188. UpdateProgressInfo(processInfo);
  189. SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.GetLog, FISTaskStatus.Finished, FISDetailType.GetLogSuccess, _finishProcess, FISProcessSource.FromTerminal, fileToken));
  190. }
  191. else
  192. {
  193. SendProcess(FISTaskStatus.Failed, FISDetailType.GetLogError, 0);
  194. }
  195. FileHelper.DeleteFile(_remoteLogZipPath);
  196. }
  197. catch (Exception ex)
  198. {
  199. Logger.WriteLineError($"GetLogAfterSalesTask OnUploadResultRaised Error:{ex}");
  200. }
  201. finally
  202. {
  203. CancelUpload();
  204. IsExcuting = false;
  205. }
  206. }
  207. protected override void OnCancel()
  208. {
  209. try
  210. {
  211. var process = new FISProcessResult(_targetId, FISTaskType.GetLog, FISTaskStatus
  212. .Failed, FISDetailType.GetLogCancel, 0, FISProcessSource.FromTerminal, string.Empty);
  213. SendProcessResultToServer(process);
  214. CancellationTokenSource?.Cancel();
  215. }
  216. catch (Exception ex)
  217. {
  218. Logger.WriteLineError($"GetLogTask OnCancel error: {ex}");
  219. }
  220. }
  221. }
  222. }