123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using FISLib.AfterSales;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.FIS.Sonopost.Common;
- using Vinno.FIS.Sonopost.Helpers;
- using Vinno.IUS.Common.Log;
- namespace Vinno.FIS.Sonopost.Features.AfterSales
- {
- internal class GetLogAfterSalesTask : AfterSalesTask
- {
- private DateTime _startTime;
- private DateTime _endTime;
- private string _exportLogFolder;
- private string _logPath;
- private string _targetId;
- private const double _prepareLogSuccess = 0.2;
- private const double _compressSuccessProgress = 0.4;
- private const double _finishProcess = 1.0;
- private string _remoteLogZipPath;
- private Stopwatch _stopwatch;
- private FISMasterInfo _masterInfo;
- public GetLogAfterSalesTask(FISMasterInfo masterInfo)
- {
- Status = AfterSalesCommandStatus.Progressing;
- _masterInfo = masterInfo;
- }
- protected override void OnDownloadProgressChanged(object sender, double e)
- {
- }
- protected override void OnDownloadResultRaised(object sender, int e)
- {
- }
- public override void Execute()
- {
- try
- {
- if (IsExcuting)
- {
- return;
- }
- IsExcuting = true;
- if (FISAdditionParameters == null)
- {
- Logger.WriteLineError($"GetLogTask FISAdditionParameters is null");
- SendProcessResultToServer(new FISProcessResult(_masterInfo.UserId, FISTaskType.GetLog, FISTaskStatus.Failed, FISDetailType.GetLogDeny, 0.01, FISProcessSource.FromTerminal, string.Empty));
- return;
- }
- InitParameters();
- _exportLogFolder = Path.Combine(SonopostConstants.DataFolder, "ExportLogFolder");
- string[] logsType = { "Logs", "SmartLogs", "TRTCClientLogs", "TRTCLogs" };
- try
- {
- DirectoryHelper.DeleteDirectory(_exportLogFolder);
- DirectoryHelper.CreateDirectory(_exportLogFolder);
- foreach (var type in logsType)
- {
- GetLogsFile(type);
- }
- UploadLogFiles(_exportLogFolder);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"OnGetLogEvent error:{ex}");
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogAfterSalesTask Excute Error:{ex}");
- IsExcuting = false;
- }
- }
- private void InitParameters()
- {
- var parameterBeginTime = FISAdditionParameters.FirstOrDefault(x => x.Name == "BeginTime");
- if (parameterBeginTime != null) DateTime.TryParse(parameterBeginTime.Value, out _startTime);
- var parameterEndTime = FISAdditionParameters.FirstOrDefault(x => x.Name == "EndTime");
- if (parameterEndTime != null) DateTime.TryParse(parameterEndTime.Value, out _endTime);
- _targetId = _masterInfo.UserId;
- _remoteLogZipPath = Path.Combine(SonopostConstants.DataFolder, @"vinnologs\remotelogs.zip");
- }
- /// <summary>
- /// 获取Logs
- /// </summary>
- /// <param name="info"></param>
- private void GetLogsFile(string pathType)
- {
- string logsPath = Path.Combine(SonopostConstants.DataFolder, pathType);
- var exportLogFolder = Path.Combine(_exportLogFolder, pathType);
- if (!Directory.Exists(logsPath))
- {
- Logger.WriteLineInfo($"No Exist Path:{logsPath}");
- return;
- }
- DirectoryInfo di = new DirectoryInfo(logsPath);
- var fileList = di.GetFiles("*.*", SearchOption.AllDirectories);
- foreach (var logFile in fileList)
- {
- if (_startTime <= logFile.CreationTime.ToUniversalTime() && logFile.CreationTime.ToUniversalTime() <= _endTime)
- {
- string newFileName = logFile.FullName.Replace(logsPath, exportLogFolder);
- if (!newFileName.EndsWith(".log"))
- newFileName = newFileName + ".log";
- string fileDir = Path.GetDirectoryName(newFileName);
- DirectoryHelper.CreateDirectory(fileDir);
- logFile.CopyTo(newFileName, true);
- }
- }
- }
- /// <summary>
- /// Uplaod Log Files
- /// </summary>
- /// <param name="path">file path</param>
- private void UploadLogFiles(string path)
- {
- _logPath = path;
- var task = new Task(UploadLog, CancellationTokenSource.Token);
- task.Start();
- }
- private void UploadLog()
- {
- try
- {
- var remotelogs = Directory.GetFiles(_logPath, "*.*", SearchOption.AllDirectories);
- if (remotelogs.Any())
- {
- SendProcess(FISTaskStatus.Progressing, FISDetailType.PropareLogFile, _prepareLogSuccess);
- FileHelper.DeleteFile(_remoteLogZipPath);
- CompressHelper.CompressFolder(_logPath, _remoteLogZipPath);
- _stopwatch = new Stopwatch();
- _stopwatch.Start();
- OnUploadProgressChanged(this, 0.00);
- UploadFile(_remoteLogZipPath);
- }
- else
- {
- Logger.WriteLineError($"Upload Log Failed, because no log files found");
- SendProcess(FISTaskStatus.Failed, FISDetailType.GetLogError, 0);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Upload log error:{ex}");
- if (CancellationTokenSource.IsCancellationRequested)
- {
- return;
- }
- SendProcess(FISTaskStatus.Failed, FISDetailType.GetLogError, 0);
- IsExcuting = false;
- }
- }
- private void SendProcess(FISTaskStatus taskStatus, FISDetailType detailType, double percent)
- {
- try
- {
- if (percent >= _finishProcess)
- {
- return;
- }
- var processInfo = new FISProcessInfo(percent, FISProgressStatus.Progressing, detailType);
- UpdateProgressInfo(processInfo);
- SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.GetLog, taskStatus, detailType, percent, FISProcessSource.FromTerminal, string.Empty));
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Send process error:{ex}");
- }
- }
- protected override void OnUploadProgressChanged(object sender, double uploadProgress)
- {
- var percent = uploadProgress * (1 - _compressSuccessProgress) + _compressSuccessProgress;
- SendProcess(FISTaskStatus.Progressing, FISDetailType.UplaodLog, percent);
- Logger.WriteLineInfo($"Get Log Task:OnUploadProgressChanged,Send Process {percent}");
- }
- protected override void OnUploadResultRaised(object sender, string fileToken)
- {
- try
- {
- _stopwatch.Stop();
- if (!string.IsNullOrEmpty(fileToken))
- {
- Logger.WriteLineInfo($"Upload file to Vcloud storage finished, AVG speed:{0:F}KB/s," +
- $"{new FileInfo(_remoteLogZipPath).Length / 1024d / _stopwatch.Elapsed.TotalSeconds}");
- var processInfo = new FISProcessInfo(_finishProcess, FISProgressStatus.Progressing, FISDetailType.GetLogSuccess);
- UpdateProgressInfo(processInfo);
- SendProcessResultToServer(new FISProcessResult(_targetId, FISTaskType.GetLog, FISTaskStatus.Finished, FISDetailType.GetLogSuccess, _finishProcess, FISProcessSource.FromTerminal, fileToken));
- }
- else
- {
- SendProcess(FISTaskStatus.Failed, FISDetailType.GetLogError, 0);
- }
- FileHelper.DeleteFile(_remoteLogZipPath);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogAfterSalesTask OnUploadResultRaised Error:{ex}");
- }
- finally
- {
- CancelUpload();
- IsExcuting = false;
- }
- }
- protected override void OnCancel()
- {
- try
- {
- var process = new FISProcessResult(_targetId, FISTaskType.GetLog, FISTaskStatus
- .Failed, FISDetailType.GetLogCancel, 0, FISProcessSource.FromTerminal, string.Empty);
- SendProcessResultToServer(process);
- CancellationTokenSource?.Cancel();
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetLogTask OnCancel error: {ex}");
- }
- }
- }
- }
|