Changed path and Changed seconds to Alpha

This commit is contained in:
Mike Phares 2023-04-19 08:33:17 -07:00
parent c655ed5c6b
commit 2f3f1b7947
7 changed files with 76 additions and 35 deletions

View File

@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using OI.Metrology.Shared.Models.Stateless; using OI.Metrology.Shared.Models.Stateless;
using System.Globalization;
using System.Text.Json; using System.Text.Json;
namespace OI.Metrology.Server.ApiControllers; namespace OI.Metrology.Server.ApiControllers;
@ -19,35 +18,7 @@ public class ReactorsController : Controller, IReactorsController<IActionResult>
Json(even ? _ReactorsRepository.EvenReactors() : _ReactorsRepository.OddReactors(), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); Json(even ? _ReactorsRepository.EvenReactors() : _ReactorsRepository.OddReactors(), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true });
[HttpPost()] [HttpPost()]
public IActionResult Post(Shared.DataModels.WorkMaterialOut workMaterialOut) public IActionResult Post(Shared.DataModels.WorkMaterialOut workMaterialOut) =>
{ Ok(_ReactorsRepository.GetKey(workMaterialOut, save: true));
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);
}
} }

View File

@ -40,9 +40,9 @@ public class ExportRepository : IExportRepository
foreach (string weekYear in weeks) foreach (string weekYear in weeks)
{ {
if (headerCommon.ID < 1) 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 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)) if (!Directory.Exists(directory))
continue; continue;
results.AddRange(Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories)); results.AddRange(Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories));

View File

@ -1,11 +1,30 @@
using OI.Metrology.Server.Models;
using OI.Metrology.Shared.DataModels; using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models.Stateless; using OI.Metrology.Shared.Models.Stateless;
using System.Globalization;
using System.Text.Json;
namespace OI.Metrology.Server.Repository; namespace OI.Metrology.Server.Repository;
public class ReactorsRepository : IReactorsRepository public class ReactorsRepository : IReactorsRepository
{ {
private readonly AppSettings _AppSettings;
private readonly Dictionary<int, char> _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<int[]> IReactorsRepository.EvenReactors() Result<int[]> IReactorsRepository.EvenReactors()
{ {
Result<int[]> results; Result<int[]> results;
@ -87,4 +106,41 @@ public class ReactorsRepository : IReactorsRepository
return results; 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;
}
} }

View File

@ -1,5 +1,5 @@
{ {
"ApiExportPath": "\\\\messdv002.na.infineon.com\\Candela\\Archive\\API", "ApiExportPath": "\\\\messdv002.na.infineon.com\\Candela",
"ApiUrl": "~/api", "ApiUrl": "~/api",
"ConnectionString": "Data Source=MESSAD1001\\TEST1,59583;Integrated Security=True;Initial Catalog=Metrology;", "ConnectionString": "Data Source=MESSAD1001\\TEST1,59583;Integrated Security=True;Initial Catalog=Metrology;",
"IsDevelopment": true, "IsDevelopment": true,

View File

@ -1,6 +1,6 @@
{ {
"AllowedHosts": "*", "AllowedHosts": "*",
"ApiExportPath": "\\\\messv02ecc1.ec.local\\EC_Metrology_Si\\Archive\\API", "ApiExportPath": "\\\\messv02ecc1.ec.local\\EC_Metrology_Si",
"ApiLoggingContentTypes": "application/json", "ApiLoggingContentTypes": "application/json",
"ApiLoggingPathPrefixes": "/api/inbound", "ApiLoggingPathPrefixes": "/api/inbound",
"ApiUrl": "~/api", "ApiUrl": "~/api",

View File

@ -8,5 +8,6 @@ public interface IReactorsRepository
Result<int[]> EvenReactors(); Result<int[]> EvenReactors();
Result<int[]> OddReactors(); Result<int[]> OddReactors();
string? GetKey(WorkMaterialOut workMaterialOut, bool save);
} }

View File

@ -69,4 +69,17 @@ public class UnitTestReactorController
NonThrowTryCatch(); NonThrowTryCatch();
} }
[TestMethod]
public void GetKey()
{
_Logger.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
IReactorsRepository reactorsRepository = serviceProvider.GetRequiredService<IReactorsRepository>();
WorkMaterialOut workMaterialOut = new() { RunDataSheet = "123456", Username = "phares" };
string? result = reactorsRepository.GetKey(workMaterialOut, save: false);
Assert.IsNotNull(result);
_Logger.Information($"{_TestContext?.TestName} completed");
NonThrowTryCatch();
}
} }