oi-metrology/Server/Repositories/ExportRepository.cs
Mike Phares 811f45a7df NginxFileSystem
Remove Reactors and Working Directory
AppSettings
2024-03-18 12:59:15 -07:00

163 lines
7.2 KiB
C#

using OI.Metrology.Server.Models;
using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models;
using OI.Metrology.Shared.Models.Stateless;
using System.Globalization;
using System.Text.Json;
namespace OI.Metrology.Server.Repository;
public class ExportRepository : IExportRepository
{
private readonly string _RepositoryName;
private readonly AppSettings _AppSettings;
private readonly ILogger<ExportRepository> _Logger;
private readonly IHttpClientFactory _HttpClientFactory;
private readonly IFileShareRepository _FileShareRepository;
private readonly Dictionary<string, Dictionary<long, HeaderCommon>> _RdsToHeaderCommonCollection;
public ExportRepository(ILogger<ExportRepository> logger, AppSettings appSettings, IHttpClientFactory httpClientFactory, IFileShareRepository fileShareRepository)
{
_Logger = logger;
_AppSettings = appSettings;
_RdsToHeaderCommonCollection = new();
_HttpClientFactory = httpClientFactory;
_FileShareRepository = fileShareRepository;
_RepositoryName = nameof(ExportRepository)[..^10];
}
private static string[] Get()
{
DateTime dateTime = DateTime.Now;
DateTime lastWeekDateTime = dateTime.AddDays(-7);
Calendar calendar = new CultureInfo("en-US").Calendar;
string weekOfYear = $"{dateTime:yyyy}_Week_{calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
string lastWeekOfYear = $"{lastWeekDateTime:yyyy}_Week_{calendar.GetWeekOfYear(lastWeekDateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
return new string[] { weekOfYear, lastWeekOfYear };
}
private NginxFileSystemSortable[] GetNginxFileSystemSortableCollection(HeaderCommon headerCommon, HttpClient httpClient, string endsWith)
{
List<NginxFileSystemSortable> results = new();
Uri uri;
string[] weeks = Get();
List<NginxFileSystemSortable> nginxFileSystemSortableCollection;
foreach (string weekYear in weeks)
{
if (headerCommon.ID < 1)
uri = _FileShareRepository.Append(new Uri(_AppSettings.EcMesaFileShareMetrologySi), "Archive", "API", weekYear, $"-{headerCommon.PSN}", $"-{headerCommon.Reactor}", $"-{headerCommon.RDS}");
else
uri = _FileShareRepository.Append(new Uri(_AppSettings.EcMesaFileShareMetrologySi), "Archive", "API", weekYear, $"-{headerCommon.PSN}", $"-{headerCommon.Reactor}", $"-{headerCommon.RDS}", $"-{headerCommon.ID}");
nginxFileSystemSortableCollection = _FileShareRepository.GetNginxFileSystemSortableCollection(httpClient, uri, endsWith);
results.AddRange(nginxFileSystemSortableCollection);
}
return results.OrderByDescending(l => l.DateTime).ToArray();
}
private string GetLines(HttpClient httpClient, NginxFileSystemSortable[] nginxFileSystemSortableCollection)
{
string result;
if (nginxFileSystemSortableCollection.Length != 1)
result = string.Empty;
else
{
HttpResponseMessage httpResponseMessage = _FileShareRepository.ReadFile(httpClient, nginxFileSystemSortableCollection.First().Uri);
if (httpResponseMessage.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception("File not found!");
Task<string> lines = httpResponseMessage.Content.ReadAsStringAsync();
lines.Wait();
result = lines.Result;
}
return result;
}
string IExportRepository.GetExport(HeaderCommon headerCommon)
{
string result;
HttpClient httpClient = _HttpClientFactory.CreateClient();
NginxFileSystemSortable[] nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(headerCommon, httpClient, ".txt");
result = GetLines(httpClient, nginxFileSystemSortableCollection);
return result;
}
Result<HeaderCommon[]> IExportRepository.GetHeaders(HeaderCommon headerCommon)
{
Result<HeaderCommon[]>? result;
List<HeaderCommon> results = new();
string json;
HeaderCommon? hc;
string? directory;
string directoryName;
JsonElement? jsonElement;
const string ticks = "Ticks";
JsonProperty[] jsonProperties;
HttpClient httpClient = _HttpClientFactory.CreateClient();
NginxFileSystemSortable[] nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(headerCommon, httpClient, ".json");
foreach (NginxFileSystemSortable nginxFileSystemSortable in nginxFileSystemSortableCollection)
{
json = GetLines(httpClient, nginxFileSystemSortableCollection);
hc = JsonSerializer.Deserialize<HeaderCommon>(json);
if (hc is null)
continue;
if (hc.ID < 1)
{
directory = Path.GetDirectoryName(nginxFileSystemSortable.Uri.OriginalString);
if (directory is null)
continue;
directoryName = Path.GetFileName(directory);
if (directoryName.Length < 1 || directoryName[0] != '-' || !long.TryParse(directoryName[1..], out long id))
continue;
hc.ID = id;
}
jsonElement = JsonSerializer.Deserialize<JsonElement>(json);
if (jsonElement is not null && jsonElement.Value.ValueKind == JsonValueKind.Object)
{
jsonProperties = (from l in jsonElement.Value.EnumerateObject() where l.Name == ticks select l).ToArray();
if (jsonProperties.Length != 0 && long.TryParse(jsonProperties[0].Value.ToString(), out long ticksValue))
hc.Date = new(ticksValue);
}
results.Add(hc);
}
result = new()
{
Results = results.ToArray(),
TotalRows = results.Count,
};
return result;
}
Result<HeaderCommon[]> IExportRepository.GetLogistics(HeaderCommon headerCommon)
{
Result<HeaderCommon[]>? result;
List<HeaderCommon> results = new();
string json;
HeaderCommon? hc;
HttpClient httpClient = _HttpClientFactory.CreateClient();
NginxFileSystemSortable[] nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(headerCommon, httpClient, ".json");
foreach (NginxFileSystemSortable nginxFileSystemSortable in nginxFileSystemSortableCollection)
{
json = GetLines(httpClient, nginxFileSystemSortableCollection);
hc = JsonSerializer.Deserialize<HeaderCommon>(json);
if (hc is null)
continue;
results.Add(hc);
}
result = new()
{
Results = results.ToArray(),
TotalRows = results.Count,
};
return result;
}
string IExportRepository.GetProcessDataStandardFormat(HeaderCommon headerCommon)
{
string result;
HttpClient httpClient = _HttpClientFactory.CreateClient();
NginxFileSystemSortable[] nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(headerCommon, httpClient, ".pdsf");
result = GetLines(httpClient, nginxFileSystemSortableCollection);
return result;
}
}