From 2f3f1b7947636b5860a896789e89663e3b12bba3 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Wed, 19 Apr 2023 08:33:17 -0700 Subject: [PATCH] Changed path and Changed seconds to Alpha --- Server/ApiControllers/ReactorsController.cs | 33 +---------- Server/Repositories/ExportRepository.cs | 4 +- Server/Repositories/ReactorsRepository.cs | 56 +++++++++++++++++++ Server/appsettings.Development.json | 2 +- Server/appsettings.json | 2 +- .../Models/Stateless/IReactorsRepository.cs | 1 + Tests/UnitTestReactorController.cs | 13 +++++ 7 files changed, 76 insertions(+), 35 deletions(-) diff --git a/Server/ApiControllers/ReactorsController.cs b/Server/ApiControllers/ReactorsController.cs index ada6c98..2fd00e1 100644 --- a/Server/ApiControllers/ReactorsController.cs +++ b/Server/ApiControllers/ReactorsController.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Mvc; using OI.Metrology.Shared.Models.Stateless; -using System.Globalization; using System.Text.Json; namespace OI.Metrology.Server.ApiControllers; @@ -19,35 +18,7 @@ public class ReactorsController : Controller, IReactorsController Json(even ? _ReactorsRepository.EvenReactors() : _ReactorsRepository.OddReactors(), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); [HttpPost()] - public IActionResult Post(Shared.DataModels.WorkMaterialOut workMaterialOut) - { - string? result = null; - if (workMaterialOut is null) - throw new Exception(); - if (workMaterialOut.Username is null || workMaterialOut.Username.Length < 2) - throw new ArgumentException(nameof(workMaterialOut.Username)); - if (workMaterialOut.RunDataSheet is null || workMaterialOut.RunDataSheet.Length < 2) - throw new ArgumentException(nameof(workMaterialOut.RunDataSheet)); - string? fileName = null; - DateTime dateTime = DateTime.Now; - Calendar calendar = new CultureInfo("en-US").Calendar; - string json = JsonSerializer.Serialize(workMaterialOut, new JsonSerializerOptions { WriteIndented = true }); - string weekOfYear = $"{dateTime:yyyy}_Week_{calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}"; - string directory = Path.Combine("D:/Tmp/Metrology", weekOfYear, dateTime.ToString("yyyy-MM-dd")); - if (!Directory.Exists(directory)) - _ = Directory.CreateDirectory(directory); - for (int i = 1; i < int.MaxValue; i++) - { - result = $"{workMaterialOut.Username.ToUpper()[0]}{dateTime:mmss}"; - fileName = Path.Combine(directory, $"WMO-{result}.json"); - if (!System.IO.File.Exists(fileName)) - break; - dateTime = dateTime.AddSeconds(i); - } - if (fileName is null) - throw new Exception(); - System.IO.File.WriteAllText(fileName, json); - return Ok(result); - } + public IActionResult Post(Shared.DataModels.WorkMaterialOut workMaterialOut) => + Ok(_ReactorsRepository.GetKey(workMaterialOut, save: true)); } \ No newline at end of file diff --git a/Server/Repositories/ExportRepository.cs b/Server/Repositories/ExportRepository.cs index 352501a..57b5188 100644 --- a/Server/Repositories/ExportRepository.cs +++ b/Server/Repositories/ExportRepository.cs @@ -40,9 +40,9 @@ public class ExportRepository : IExportRepository foreach (string weekYear in weeks) { if (headerCommon.ID < 1) - directory = Path.Combine(_AppSettings.ApiExportPath, weekYear, $"-{headerCommon.PSN}", $"-{headerCommon.Reactor}", $"-{headerCommon.RDS}"); + directory = Path.Combine(_AppSettings.ApiExportPath, "Archive", "API", weekYear, $"-{headerCommon.PSN}", $"-{headerCommon.Reactor}", $"-{headerCommon.RDS}"); else - directory = Path.Combine(_AppSettings.ApiExportPath, weekYear, $"-{headerCommon.PSN}", $"-{headerCommon.Reactor}", $"-{headerCommon.RDS}", $"-{headerCommon.ID}"); + directory = Path.Combine(_AppSettings.ApiExportPath, "Archive", "API", weekYear, $"-{headerCommon.PSN}", $"-{headerCommon.Reactor}", $"-{headerCommon.RDS}", $"-{headerCommon.ID}"); if (!Directory.Exists(directory)) continue; results.AddRange(Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories)); diff --git a/Server/Repositories/ReactorsRepository.cs b/Server/Repositories/ReactorsRepository.cs index 94888a8..3e927e9 100644 --- a/Server/Repositories/ReactorsRepository.cs +++ b/Server/Repositories/ReactorsRepository.cs @@ -1,11 +1,30 @@ +using OI.Metrology.Server.Models; using OI.Metrology.Shared.DataModels; using OI.Metrology.Shared.Models.Stateless; +using System.Globalization; +using System.Text.Json; namespace OI.Metrology.Server.Repository; public class ReactorsRepository : IReactorsRepository { + private readonly AppSettings _AppSettings; + private readonly Dictionary _SecondsToAlpha; + + public ReactorsRepository(AppSettings appSettings) + { + _AppSettings = appSettings; + _SecondsToAlpha = new(); + for (int i = 65; i < 91; i++) + { + if (i is 73 or 79) + continue; + _SecondsToAlpha.Add(i - 65, (char)i); + } + + } + Result IReactorsRepository.EvenReactors() { Result results; @@ -87,4 +106,41 @@ public class ReactorsRepository : IReactorsRepository return results; } + string? IReactorsRepository.GetKey(WorkMaterialOut workMaterialOut, bool save) + { + if (workMaterialOut is null) + throw new Exception(); + if (workMaterialOut.Username is null || workMaterialOut.Username.Length < 2) + throw new ArgumentException(nameof(workMaterialOut.Username)); + if (workMaterialOut.RunDataSheet is null || workMaterialOut.RunDataSheet.Length < 2) + throw new ArgumentException(nameof(workMaterialOut.RunDataSheet)); + string? result = null; + char c; + string? fileName = null; + DateTime dateTime = DateTime.Now; + Calendar calendar = new CultureInfo("en-US").Calendar; + string json = JsonSerializer.Serialize(workMaterialOut, new JsonSerializerOptions { WriteIndented = true }); + string weekOfYear = $"{dateTime:yyyy}_Week_{calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}"; + string directory = Path.Combine(_AppSettings.ApiExportPath, "WorkMaterialOut", "API", weekOfYear, dateTime.ToString("yyyy-MM-dd_HH")); + if (!Directory.Exists(directory)) + _ = Directory.CreateDirectory(directory); + for (int i = 0; i < int.MaxValue; i++) + { + dateTime = dateTime.AddSeconds(i); + if (!_SecondsToAlpha.TryGetValue(dateTime.Second, out c)) + continue; + fileName = Path.Combine(directory, $"WMO-{dateTime:mm}{c}.json"); + if (!File.Exists(fileName)) + { + result = $"{c}{dateTime:mm}"; + break; + } + } + if (fileName is null) + throw new Exception(); + if (save) + File.WriteAllText(fileName, json); + return result; + } + } \ No newline at end of file diff --git a/Server/appsettings.Development.json b/Server/appsettings.Development.json index 0de2768..67028c2 100644 --- a/Server/appsettings.Development.json +++ b/Server/appsettings.Development.json @@ -1,5 +1,5 @@ { - "ApiExportPath": "\\\\messdv002.na.infineon.com\\Candela\\Archive\\API", + "ApiExportPath": "\\\\messdv002.na.infineon.com\\Candela", "ApiUrl": "~/api", "ConnectionString": "Data Source=MESSAD1001\\TEST1,59583;Integrated Security=True;Initial Catalog=Metrology;", "IsDevelopment": true, diff --git a/Server/appsettings.json b/Server/appsettings.json index 1131189..7d2ecc8 100644 --- a/Server/appsettings.json +++ b/Server/appsettings.json @@ -1,6 +1,6 @@ { "AllowedHosts": "*", - "ApiExportPath": "\\\\messv02ecc1.ec.local\\EC_Metrology_Si\\Archive\\API", + "ApiExportPath": "\\\\messv02ecc1.ec.local\\EC_Metrology_Si", "ApiLoggingContentTypes": "application/json", "ApiLoggingPathPrefixes": "/api/inbound", "ApiUrl": "~/api", diff --git a/Shared/Models/Stateless/IReactorsRepository.cs b/Shared/Models/Stateless/IReactorsRepository.cs index 42a9812..faccabc 100644 --- a/Shared/Models/Stateless/IReactorsRepository.cs +++ b/Shared/Models/Stateless/IReactorsRepository.cs @@ -8,5 +8,6 @@ public interface IReactorsRepository Result EvenReactors(); Result OddReactors(); + string? GetKey(WorkMaterialOut workMaterialOut, bool save); } \ No newline at end of file diff --git a/Tests/UnitTestReactorController.cs b/Tests/UnitTestReactorController.cs index ee2265c..917b716 100644 --- a/Tests/UnitTestReactorController.cs +++ b/Tests/UnitTestReactorController.cs @@ -69,4 +69,17 @@ public class UnitTestReactorController NonThrowTryCatch(); } + [TestMethod] + public void GetKey() + { + _Logger.Information("Starting Web Application"); + IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; + IReactorsRepository reactorsRepository = serviceProvider.GetRequiredService(); + WorkMaterialOut workMaterialOut = new() { RunDataSheet = "123456", Username = "phares" }; + string? result = reactorsRepository.GetKey(workMaterialOut, save: false); + Assert.IsNotNull(result); + _Logger.Information($"{_TestContext?.TestName} completed"); + NonThrowTryCatch(); + } + } \ No newline at end of file