JD2022-TU1/main/tools/services/JD.Nas.Service/NasReportingService.cs

308 lines
No EOL
11 KiB
C#

namespace JD.Nas.Service
{
using JD.Nas.Service.Constants;
using JD.Nas.Service.Exception;
using JD.Nas.Service.NasReportingDto;
using Plume.Core.Logging;
using Plume.Interfaces.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
public class NasReportingService: INasReportingService, IService
{
private ILogger _logger;
private NasServerConfiguration _nasServerConfiguration;
private NasReport _nasReport;
private NasServerAnalyse _nasServerAnalyse;
#region Public
public void Initialize(IServiceContext serviceContext)
{
this._logger = serviceContext.Logger;
}
public void Dispose()
{
// Nothing to dispose
}
/// <summary>
/// Analyse a nas server to create a nas report
/// </summary>
/// <param name="nasReport"></param>
/// <param name="configuration"></param>
/// <returns></returns>
public NasServerAnalyse AnalyseNasServer(NasReport nasReport, NasServerConfiguration configuration)
{
try
{
this._nasReport = nasReport;
this._nasServerConfiguration = configuration;
this._nasServerAnalyse = new NasServerAnalyse(this._nasServerConfiguration);
this.WriteLogInfo(string.Concat("Starting nas server analyse of : ", configuration.Folder));
if (!Directory.Exists(configuration.Folder))
{
throw new NasReportingServiceException(
NasReportingServiceExitCode.NasServerConfigurationNotExist,
string.Concat("Directory not exist : ", configuration.Folder)
);
}
if (!AccessDirectory(configuration.Folder))
{
throw new NasReportingServiceException(
NasReportingServiceExitCode.NasServerConfigurationAccessDenied,
string.Concat("Access denied to path : ", configuration.Folder)
);
}
if (configuration.MaxThread == 0)
{
if (!this._nasReport.NasReportConfiguration.MaxThread.HasValue || this._nasReport.NasReportConfiguration.MaxThread.Value == 0)
{
throw new NasReportingServiceException(
NasReportingServiceExitCode.MaxThreadNeeded,
string.Concat("MaxThread number need to be > 0 ", configuration.Folder)
);
}
else
{
configuration.MaxThread = this._nasReport.NasReportConfiguration.MaxThread.Value;
}
}
this.WriteLogWarn(string.Concat(
"This program use threading system. Please to be sure your engine/cpu can start ",
configuration.MaxThread,
" threads in same times (else this can slow down this process)."));
DirectoryInfo dirInfo = new DirectoryInfo(configuration.Folder);
List<NasMapAnalyse> mapstoAnalyse = new List<NasMapAnalyse>();
foreach (DirectoryInfo directory in dirInfo.EnumerateDirectories())
{
mapstoAnalyse.Add(new NasMapAnalyse(directory.FullName));
}
Parallel.ForEach(mapstoAnalyse, new ParallelOptions() { MaxDegreeOfParallelism = configuration.MaxThread }, maptoAnalyse =>
{
AnalyseTaskAction(maptoAnalyse);
});
}
catch (System.Exception ex)
{
throw ex;
}
return this._nasServerAnalyse;
}
#endregion
#region Private
/// <summary>
/// Analyse map action
/// </summary>
/// <param name="nasMapAnalyse"></param>
private void AnalyseTaskAction(NasMapAnalyse nasMapAnalyse)
{
this.WriteLogInfo(string.Concat("Analyzing folder ", nasMapAnalyse.FolderPath));
NasMapAnalyse(nasMapAnalyse);
nasMapAnalyse.DateStop = DateTime.UtcNow;
this._nasServerAnalyse.AddMap(nasMapAnalyse);
}
/// <summary>
/// Analyse a map in a nas server
/// </summary>
/// <param name="nasMapAnalyse"></param>
private void NasMapAnalyse(NasMapAnalyse nasMapAnalyse)
{
nasMapAnalyse.FolderSize = ComputeFolderSize(nasMapAnalyse.FolderPath, nasMapAnalyse);
nasMapAnalyse.NumberCoachVersions = ComputeDirectoryCount(Path.Combine(nasMapAnalyse.FolderPath, this._nasServerConfiguration.CoachVersionsFolder), nasMapAnalyse);
nasMapAnalyse.NumberFxVersions = ComputeDirectoryCount(Path.Combine(nasMapAnalyse.FolderPath, this._nasServerConfiguration.FxVersionsFolder), nasMapAnalyse);
nasMapAnalyse.NumberVersions = ComputeDirectoryCount(Path.Combine(nasMapAnalyse.FolderPath, this._nasServerConfiguration.VersionsFolder), nasMapAnalyse);
nasMapAnalyse.NumberCoachOnly = ComputeDirectoryCount(Path.Combine(nasMapAnalyse.FolderPath, this._nasServerConfiguration.CoachOnlyFolder), nasMapAnalyse);
nasMapAnalyse.NumberBackgroundOnly = ComputeDirectoryCount(Path.Combine(nasMapAnalyse.FolderPath, this._nasServerConfiguration.BackgroundOnlyFolder), nasMapAnalyse);
}
/// <summary>
/// Count the number of direcory in a directory
/// </summary>
/// <param name="folderPath"></param>
/// <param name="nasMapAnalyse"></param>
/// <returns></returns>
private int? ComputeDirectoryCount(string folderPath, NasMapAnalyse nasMapAnalyse)
{
int? count = null;
try
{
if (!Directory.Exists(folderPath))
{
this.WriteLogWarn(
new NasReportingServiceException(
string.Concat("Directory not exist : ", folderPath)
),
nasMapAnalyse
);
}
else
{
count = new DirectoryInfo(folderPath).EnumerateDirectories().Count();
}
}
catch (UnauthorizedAccessException ex)
{
this.WriteLogError(
new NasReportingServiceException(
ex,
string.Concat("Access denied to directory : ", folderPath)
),
nasMapAnalyse
);
}
catch (System.Exception ex)
{
this.WriteLogError(
new NasReportingServiceException(
ex,
string.Concat("Unexpected error in directory : ", folderPath)
),
nasMapAnalyse
);
}
return count;
}
/// <summary>
/// Calculate total size of a directory
/// </summary>
/// <param name="folderPath"></param>
/// <param name="nasMapAnalyse"></param>
/// <returns></returns>
private long? ComputeFolderSize(string folderPath, NasMapAnalyse nasMapAnalyse = null)
{
long? totalSize = null;
try
{
if (!Directory.Exists(folderPath))
{
this.WriteLogWarn(
new NasReportingServiceException(
string.Concat("Directory not exist : ", folderPath)
),
nasMapAnalyse
);
}
else
{
totalSize = new DirectoryInfo(folderPath).EnumerateFiles("*.*", SearchOption.AllDirectories).Sum(x => x.Length);
}
}
catch (UnauthorizedAccessException ex)
{
this.WriteLogError(
new NasReportingServiceException(
ex,
string.Concat("Access denied to directory : ", folderPath)
),
nasMapAnalyse
);
}
catch (System.Exception ex)
{
this.WriteLogError(
new NasReportingServiceException(
ex,
string.Concat("Unexpected error in directory : ", folderPath)
),
nasMapAnalyse
);
}
return totalSize;
}
/// <summary>
/// Try to get GetAccessControl of directory to detect if we have access
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private bool AccessDirectory(string path)
{
try
{
System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(path);
return true;
}
catch
{
return false;
}
}
#region Logs
private void WriteLogError(NasReportingServiceException exception, NasMapAnalyse map = null)
{
this.WriteLogId(exception.GetId(), map);
this._logger.Error(exception.ToString());
}
private void WriteLogWarn(NasReportingServiceException exception, NasMapAnalyse map = null)
{
this.WriteLogId(exception.GetId(), map);
this._logger.Warn(exception.ToString());
}
private void WriteLogWarn(string message)
{
Guid Id = Guid.NewGuid();
this.WriteLogId(Id);
this._logger.Warn(string.Concat(message, "[ID]", Id.ToString()));
}
private void WriteLogInfo(string message, Guid? id = null)
{
if (!id.HasValue)
id = Guid.NewGuid();
this.WriteLogId(id);
this._logger.Info(string.Concat(message, "[ID]", id.ToString()));
}
private void WriteLogId(Guid? Id = null, NasMapAnalyse map = null)
{
if (!Id.HasValue)
Id = Guid.NewGuid();
this._nasReport.AddLog(Id.Value.ToString());
this._nasServerAnalyse.AddLog(Id.Value.ToString());
map?.AddLog(Id.Value.ToString());
}
#endregion
#endregion
}
}