Update GetEngineeringSpcReview to work without FS access.
IqsFileSegments and WaferCounterTwoFileSecondsWait
This commit is contained in:
		| @ -2,8 +2,7 @@ using System.Text.Json; | ||||
|  | ||||
| namespace OI.Metrology.Server.Models; | ||||
|  | ||||
| public record AppSettings(string ApiExportPath, | ||||
|                           string ApiFileShare, | ||||
| public record AppSettings(string ApiFileShare, | ||||
|                           string ApiLoggingContentTypes, | ||||
|                           string ApiLoggingPathPrefixes, | ||||
|                           string ApiLogPath, | ||||
| @ -17,7 +16,7 @@ public record AppSettings(string ApiExportPath, | ||||
|                           string GitCommitSeven, | ||||
|                           string InboundApiAllowedIPList, | ||||
|                           string IqsColumns, | ||||
|                           string IqsFile, | ||||
|                           string[] IqsFileSegments, | ||||
|                           string IqsKey, | ||||
|                           string IqsRed, | ||||
|                           string IqsYellow, | ||||
| @ -30,6 +29,7 @@ public record AppSettings(string ApiExportPath, | ||||
|                           Dictionary<string, string> TableToPath, | ||||
|                           string URLs, | ||||
|                           string WaferCounterDestinationDirectory, | ||||
|                           int WaferCounterTwoFileSecondsWait, | ||||
|                           string WorkingDirectoryName) | ||||
| { | ||||
|  | ||||
|  | ||||
| @ -6,7 +6,6 @@ namespace OI.Metrology.Server.Models.Binder; | ||||
| public class AppSettings | ||||
| { | ||||
|  | ||||
|     public string? ApiExportPath { get; set; } | ||||
|     public string? ApiFileShare { get; set; } | ||||
|     public string? ApiLoggingContentTypes { get; set; } | ||||
|     public string? ApiLoggingPathPrefixes { get; set; } | ||||
| @ -21,7 +20,7 @@ public class AppSettings | ||||
|     public string? GitCommitSeven { get; set; } | ||||
|     public string? InboundApiAllowedIPList { get; set; } | ||||
|     public string? IqsColumns { get; set; } | ||||
|     public string? IqsFile { get; set; } | ||||
|     public string[]? IqsFileSegments { get; set; } | ||||
|     public string? IqsKey { get; set; } | ||||
|     public string? IqsRed { get; set; } | ||||
|     public string? IqsYellow { get; set; } | ||||
| @ -34,6 +33,7 @@ public class AppSettings | ||||
|     public Dictionary<string, string>? TableToPath { get; set; } | ||||
|     public string? URLs { get; set; } | ||||
|     public string? WaferCounterDestinationDirectory { get; set; } | ||||
|     public int? WaferCounterTwoFileSecondsWait { get; set; } | ||||
|     public string? WorkingDirectoryName { get; set; } | ||||
|  | ||||
|     public override string ToString() | ||||
| @ -44,7 +44,7 @@ public class AppSettings | ||||
|  | ||||
|     private static void PreVerify(IConfigurationRoot configurationRoot, AppSettings? appSettings) | ||||
|     { | ||||
|         if (appSettings?.ApiExportPath is null) | ||||
|         if (appSettings?.ApiFileShare is null) | ||||
|         { | ||||
|             List<string> paths = new(); | ||||
|             foreach (IConfigurationProvider configurationProvider in configurationRoot.Providers) | ||||
| @ -63,7 +63,6 @@ public class AppSettings | ||||
|     { | ||||
|         Models.AppSettings result; | ||||
|         if (appSettings is null) throw new NullReferenceException(nameof(appSettings)); | ||||
|         if (appSettings.ApiExportPath is null) throw new NullReferenceException(nameof(ApiExportPath)); | ||||
|         if (appSettings.ApiFileShare is null) throw new NullReferenceException(nameof(ApiFileShare)); | ||||
|         if (appSettings.ApiLoggingContentTypes is null) throw new NullReferenceException(nameof(ApiLoggingContentTypes)); | ||||
|         if (appSettings.ApiLoggingPathPrefixes is null) throw new NullReferenceException(nameof(ApiLoggingPathPrefixes)); | ||||
| @ -78,7 +77,7 @@ public class AppSettings | ||||
|         if (appSettings.GitCommitSeven is null) throw new NullReferenceException(nameof(GitCommitSeven)); | ||||
|         if (appSettings.InboundApiAllowedIPList is null) throw new NullReferenceException(nameof(InboundApiAllowedIPList)); | ||||
|         if (appSettings.IqsColumns is null) throw new NullReferenceException(nameof(IqsColumns)); | ||||
|         if (appSettings.IqsFile is null) throw new NullReferenceException(nameof(IqsFile)); | ||||
|         if (appSettings.IqsFileSegments is null) throw new NullReferenceException(nameof(IqsFileSegments)); | ||||
|         if (appSettings.IqsKey is null) throw new NullReferenceException(nameof(IqsKey)); | ||||
|         if (appSettings.IqsRed is null) throw new NullReferenceException(nameof(IqsRed)); | ||||
|         if (appSettings.IqsYellow is null) throw new NullReferenceException(nameof(IqsYellow)); | ||||
| @ -91,9 +90,9 @@ public class AppSettings | ||||
|         if (appSettings.URLs is null) throw new NullReferenceException(nameof(URLs)); | ||||
|         if (appSettings.TableToPath is null) throw new NullReferenceException(nameof(TableToPath)); | ||||
|         if (appSettings.WaferCounterDestinationDirectory is null) throw new NullReferenceException(nameof(WaferCounterDestinationDirectory)); | ||||
|         if (appSettings.WaferCounterTwoFileSecondsWait is null) throw new NullReferenceException(nameof(WaferCounterTwoFileSecondsWait)); | ||||
|         if (appSettings.WorkingDirectoryName is null) throw new NullReferenceException(nameof(WorkingDirectoryName)); | ||||
|         result = new( | ||||
|             appSettings.ApiExportPath, | ||||
|             appSettings.ApiFileShare, | ||||
|             appSettings.ApiLoggingContentTypes, | ||||
|             appSettings.ApiLoggingPathPrefixes, | ||||
| @ -108,7 +107,7 @@ public class AppSettings | ||||
|             appSettings.GitCommitSeven, | ||||
|             appSettings.InboundApiAllowedIPList, | ||||
|             appSettings.IqsColumns, | ||||
|             appSettings.IqsFile, | ||||
|             appSettings.IqsFileSegments, | ||||
|             appSettings.IqsKey, | ||||
|             appSettings.IqsRed, | ||||
|             appSettings.IqsYellow, | ||||
| @ -121,6 +120,7 @@ public class AppSettings | ||||
|             appSettings.TableToPath, | ||||
|             appSettings.URLs, | ||||
|             appSettings.WaferCounterDestinationDirectory, | ||||
|             appSettings.WaferCounterTwoFileSecondsWait.Value, | ||||
|             appSettings.WorkingDirectoryName); | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
| @ -677,12 +677,11 @@ public class InfinityQSV4Repository : IInfinityQSV4Repository | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     private static ReadOnlyCollection<Record> GetRecords(AppSettings appSettings, string file) | ||||
|     private static ReadOnlyCollection<Record> GetRecords(AppSettings appSettings, string[] lines) | ||||
|     { | ||||
|         Record? record; | ||||
|         List<Record> results = new(); | ||||
|         List<string> checkColumns = new(); | ||||
|         string[] lines = File.ReadAllLines(file); | ||||
|         foreach (string line in lines) | ||||
|         { | ||||
|             if (line.StartsWith("<th")) | ||||
| @ -747,10 +746,23 @@ public class InfinityQSV4Repository : IInfinityQSV4Repository | ||||
|         return results; | ||||
|     } | ||||
|  | ||||
|     private void FileFindReplaceAndSave(string iqsFile, FileInfo fileInfo) | ||||
|     private void FileFindReplaceAndSave(FileInfo fileInfo, string value) | ||||
|     { | ||||
|         string lines = File.ReadAllText(iqsFile).Replace(_AppSettings.IqsRed, "red").Replace(_AppSettings.IqsYellow, "Yellow"); | ||||
|         _FileShareRepository.FileWrite(fileInfo.FullName, lines); | ||||
|         string lines = value.Replace(_AppSettings.IqsRed, "red").Replace(_AppSettings.IqsYellow, "Yellow"); | ||||
|         File.WriteAllText(fileInfo.FullName, lines); | ||||
|     } | ||||
|  | ||||
|     private static string GetWwwRootDirectory() | ||||
|     { | ||||
|         string result; | ||||
|         Assembly assembly = Assembly.GetExecutingAssembly(); | ||||
|         string? assemblyName = assembly.GetName()?.Name; | ||||
|         if (string.IsNullOrEmpty(assemblyName)) | ||||
|             throw new Exception(); | ||||
|         result = Path.Combine(AppContext.BaseDirectory, "wwwroot"); | ||||
|         if (!Directory.Exists(result)) | ||||
|             _ = Directory.CreateDirectory(result); | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     Dictionary<string, List<string>> IInfinityQSV4Repository.GetEngineeringSpcReview() | ||||
| @ -765,19 +777,27 @@ public class InfinityQSV4Repository : IInfinityQSV4Repository | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             FileInfo iqsFileInfo = new(_AppSettings.IqsFile); | ||||
|             Assembly assembly = Assembly.GetExecutingAssembly(); | ||||
|             string? assemblyName = assembly.GetName()?.Name; | ||||
|             if (string.IsNullOrEmpty(assemblyName)) | ||||
|                 throw new Exception(); | ||||
|             FileInfo localFileInfo = new(Path.Combine(AppContext.BaseDirectory, "wwwroot", iqsFileInfo.Name)); | ||||
|             if (!localFileInfo.Exists && (!iqsFileInfo.Exists || iqsFileInfo.Length == 0)) | ||||
|             string wwwRootDirectory = GetWwwRootDirectory(); | ||||
|             HttpClient httpClient = _HttpClientFactory.CreateClient(); | ||||
|             FileInfo localFileInfo = new(Path.Combine(wwwRootDirectory, _AppSettings.IqsFileSegments.Last())); | ||||
|             Uri uri = _FileShareRepository.Append(new Uri(_AppSettings.EcMesaFileShareCharacterizationSi), _AppSettings.IqsFileSegments[0..^1]); | ||||
|             List<NginxFileSystemSortable> nginxFileSystemSortableCollection = _FileShareRepository.GetNginxFileSystemSortableCollection(httpClient, uri, ".html"); | ||||
|             if (!localFileInfo.Exists && nginxFileSystemSortableCollection.Count == 0) | ||||
|                 results = new(); | ||||
|             else | ||||
|             { | ||||
|                 if (!localFileInfo.Exists || localFileInfo.LastWriteTime != iqsFileInfo.LastWriteTime) | ||||
|                     FileFindReplaceAndSave(iqsFileInfo.FullName, localFileInfo); | ||||
|                 ReadOnlyCollection<Record> records = !iqsFileInfo.Exists || iqsFileInfo.Length == 0 ? GetRecords(_AppSettings, localFileInfo.FullName) : GetRecords(_AppSettings, iqsFileInfo.FullName); | ||||
|                 string[] lines; | ||||
|                 HttpResponseMessage httpResponseMessage = _FileShareRepository.ReadFile(httpClient, nginxFileSystemSortableCollection[0].Uri); | ||||
|                 Task<string> value = httpResponseMessage.Content.ReadAsStringAsync(); | ||||
|                 value.Wait(); | ||||
|                 if (localFileInfo.Exists && localFileInfo.LastWriteTime == nginxFileSystemSortableCollection[0].DateTime) | ||||
|                     lines = File.ReadAllLines(localFileInfo.FullName); | ||||
|                 else | ||||
|                 { | ||||
|                     FileFindReplaceAndSave(localFileInfo, value.Result); | ||||
|                     lines = value.Result.Split("\r\n"); | ||||
|                 } | ||||
|                 ReadOnlyCollection<Record> records = GetRecords(_AppSettings, lines); | ||||
|                 List<Dictionary<string, Record>> collection = GetCollection(_AppSettings.IqsColumns, records); | ||||
|                 results = GetResults(_AppSettings, collection); | ||||
|             } | ||||
|  | ||||
| @ -4,7 +4,6 @@ using OI.Metrology.Shared.Models; | ||||
| using OI.Metrology.Shared.Models.Stateless; | ||||
| using OI.Metrology.Shared.Repositories; | ||||
| using System.Globalization; | ||||
| using System.Text.Json; | ||||
|  | ||||
| namespace OI.Metrology.Server.Repository; | ||||
|  | ||||
| @ -137,94 +136,75 @@ public class WaferCounterRepository : IWaferCounterRepository | ||||
|  | ||||
|     private List<NginxFileSystemSortable> GetNginxFileSystemSortableCollection(HttpClient httpClient, Uri waferSizeUri, string waferSizeDirectory) | ||||
|     { | ||||
|         List<NginxFileSystemSortable> results = _FileShareRepository.GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, ".wc"); | ||||
|         List<NginxFileSystemSortable> results = new(); | ||||
|         DateTime dateTime = DateTime.Now; | ||||
|         long ticks = dateTime.AddSeconds(_AppSettings.WaferCounterTwoFileSecondsWait).Ticks; | ||||
|         for (int i = 0; i < int.MaxValue; i++) | ||||
|         { | ||||
|             results = _FileShareRepository.GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, ".wc"); | ||||
|             if (results.Count > 1 || DateTime.Now.Ticks > ticks) | ||||
|                 break; | ||||
|             Thread.Sleep(250); | ||||
|         } | ||||
|         for (int i = 1; i < results.Count; i++) | ||||
|             MoveFile(waferSizeDirectory, results[i]); | ||||
|         return results; | ||||
|     } | ||||
|  | ||||
|     private static WaferCounter? GetLastQuantityAndSlotMapWithText(string waferSize, string text, HttpClient httpClient, NginxFileSystemSortable nginxFileSystemSortable) | ||||
|     private static WaferCounter GetLastQuantityAndSlotMapWithText(string waferSize, string text, HttpClient httpClient, NginxFileSystemSortable nginxFileSystemSortable) | ||||
|     { | ||||
|         WaferCounter? result; | ||||
|         WaferCounter result; | ||||
|         Task<string> value = httpClient.GetStringAsync(nginxFileSystemSortable.Uri); | ||||
|         value.Wait(); | ||||
|         string[] lines = value.Result.Split(Environment.NewLine); | ||||
|         if (lines.Length < 2) | ||||
|             result = null; | ||||
|         else | ||||
|         { | ||||
|             string[] segments = nginxFileSystemSortable.Name.Split('-'); | ||||
|             Record record = GetRecord(lines[0], lines[1]); | ||||
|             string equipmentId = segments.Length < 2 ? nginxFileSystemSortable.Name : segments[1].Split('.')[0]; | ||||
|             if (string.IsNullOrEmpty(record.SlotMap) || record.SlotMap.Length != 25) | ||||
|                 result = null; // Wrong length! | ||||
|             else if (record.Total != record.Check) | ||||
|                 result = null; // Invalid! | ||||
|             else | ||||
|                 result = new(nginxFileSystemSortable.DateTime, nginxFileSystemSortable.DateTime.ToString("yyyy-MM-dd hh:mm tt"), $"WC{waferSize}{equipmentId}", text, record.Total, record.SlotMap); | ||||
|         } | ||||
|         string[] lines = value.Result.Split("\r\n"); | ||||
|         if (lines.Length <= 1) | ||||
|             throw new Exception("Incomplete file length!"); | ||||
|         string[] segments = nginxFileSystemSortable.Name.Split('-'); | ||||
|         Record record = GetRecord(lines[0], lines[1]); | ||||
|         string equipmentId = segments.Length <= 1 ? nginxFileSystemSortable.Name : segments[1].Split('.')[0]; | ||||
|         if (string.IsNullOrEmpty(record.SlotMap) || record.SlotMap.Length != 25) | ||||
|             throw new Exception("Wrong length!"); | ||||
|         if (record.Total != record.Check) | ||||
|             throw new Exception("Invalid!"); | ||||
|         result = new(nginxFileSystemSortable.DateTime, nginxFileSystemSortable.DateTime.ToString("yyyy-MM-dd hh:mm tt"), $"WC{waferSize}{equipmentId}", text, record.Total, record.SlotMap); | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     WaferCounter? IWaferCounterRepository.GetLastQuantityAndSlotMap(string area, string waferSize) | ||||
|     WaferCounter IWaferCounterRepository.GetLastQuantityAndSlotMap(string area, string waferSize) | ||||
|     { | ||||
|         WaferCounter? result; | ||||
|         if (!string.IsNullOrEmpty(_MockRoot)) | ||||
|         { | ||||
|             string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IWaferCounterRepository.GetLastQuantityAndSlotMap)}.json")); | ||||
|             result = JsonSerializer.Deserialize<WaferCounter>(json); | ||||
|             if (result is null) | ||||
|                 throw new NullReferenceException(nameof(result)); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             Uri waferSizeUri = GetWaferSizeUri(area, waferSize); | ||||
|             HttpClient httpClient = _HttpClientFactory.CreateClient(); | ||||
|             string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false); | ||||
|             List<NginxFileSystemSortable> nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, waferSizeDirectory); | ||||
|             if (nginxFileSystemSortableCollection.Count == 0) | ||||
|                 result = null; | ||||
|             else | ||||
|             { | ||||
|                 string text = string.Empty; | ||||
|                 result = GetLastQuantityAndSlotMapWithText(waferSize, text, httpClient, nginxFileSystemSortableCollection[0]); | ||||
|             } | ||||
|         } | ||||
|         WaferCounter result; | ||||
|         Uri waferSizeUri = GetWaferSizeUri(area, waferSize); | ||||
|         HttpClient httpClient = _HttpClientFactory.CreateClient(); | ||||
|         string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false); | ||||
|         List<NginxFileSystemSortable> nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, waferSizeDirectory); | ||||
|         if (nginxFileSystemSortableCollection.Count < 1) | ||||
|             throw new Exception("No files!"); | ||||
|         string text = string.Empty; | ||||
|         result = GetLastQuantityAndSlotMapWithText(waferSize, text, httpClient, nginxFileSystemSortableCollection[0]); | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     private void Save(string waferSizeDestinationDirectory, string area, string waferSize, string text, NginxFileSystemSortable nginxFileSystemSortable, WaferCounter result) => | ||||
|         _FileShareRepository.FileWrite(Path.Combine(waferSizeDestinationDirectory, $"{nginxFileSystemSortable.Name}.csv"), $"100,{waferSize},{area},{nginxFileSystemSortable.DateTime},{text},{result.Total:00},{result.SlotMap} "); | ||||
|  | ||||
|     WaferCounter? IWaferCounterRepository.GetLastQuantityAndSlotMapWithText(string area, string waferSize, string text) | ||||
|     WaferCounter IWaferCounterRepository.GetLastQuantityAndSlotMapWithText(string area, string waferSize, string text) | ||||
|     { | ||||
|         WaferCounter? result; | ||||
|         if (!string.IsNullOrEmpty(_MockRoot)) | ||||
|         WaferCounter result; | ||||
|         Uri waferSizeUri = GetWaferSizeUri(area, waferSize); | ||||
|         HttpClient httpClient = _HttpClientFactory.CreateClient(); | ||||
|         string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false); | ||||
|         List<NginxFileSystemSortable> nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, waferSizeDirectory); | ||||
|         if (nginxFileSystemSortableCollection.Count < 1) | ||||
|             throw new Exception("No files!"); | ||||
|         result = GetLastQuantityAndSlotMapWithText(waferSize, text, httpClient, nginxFileSystemSortableCollection[0]); | ||||
|         if (result is not null) | ||||
|         { | ||||
|             string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IWaferCounterRepository.GetLastQuantityAndSlotMapWithText)}.json")); | ||||
|             result = JsonSerializer.Deserialize<WaferCounter>(json); | ||||
|             if (result is null) | ||||
|                 throw new NullReferenceException(nameof(result)); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             Uri waferSizeUri = GetWaferSizeUri(area, waferSize); | ||||
|             HttpClient httpClient = _HttpClientFactory.CreateClient(); | ||||
|             string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false); | ||||
|             List<NginxFileSystemSortable> nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, waferSizeDirectory); | ||||
|             if (nginxFileSystemSortableCollection.Count == 0) | ||||
|                 result = null; | ||||
|             else | ||||
|             { | ||||
|                 result = GetLastQuantityAndSlotMapWithText(waferSize, text, httpClient, nginxFileSystemSortableCollection[0]); | ||||
|                 if (result is not null) | ||||
|                 { | ||||
|                     string waferSizeDestinationDirectory = _AppSettings.WaferCounterDestinationDirectory; | ||||
|                     // string waferSizeDestinationDirectory = GetWaferSizeUri(area, waferSize, destination: true); | ||||
|                     Save(waferSizeDestinationDirectory, area, waferSize, text, nginxFileSystemSortableCollection[0], result); | ||||
|                 } | ||||
|             } | ||||
|             string waferSizeDestinationDirectory = _AppSettings.WaferCounterDestinationDirectory; | ||||
|             // string waferSizeDestinationDirectory = GetWaferSizeUri(area, waferSize, destination: true); | ||||
|             Save(waferSizeDestinationDirectory, area, waferSize, text, nginxFileSystemSortableCollection[0], result); | ||||
|         } | ||||
|         if (result is null) | ||||
|             throw new NullReferenceException(nameof(result)); | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user