169 lines
6.8 KiB
C#
169 lines
6.8 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace File_Folder_Helper.Day.Q32024;
|
|
|
|
internal static partial class Helper20240828
|
|
{
|
|
|
|
public record Record(string? CassetteId,
|
|
ReadOnlyCollection<string>? CassetteSegments,
|
|
DateTime? Date,
|
|
string? Employee,
|
|
ReadOnlyCollection<string>? EquipmentSegments,
|
|
int I,
|
|
string? LastDate,
|
|
ReadOnlyCollection<ReadOnlyCollection<string>>? Matches);
|
|
|
|
private static Record GetRecord(int i, string? lastDate, ReadOnlyCollection<ReadOnlyCollection<string>> matches)
|
|
{
|
|
Record result;
|
|
if (matches.Count != 4 || matches[0].Count != 3 || matches[3].Count != 3)
|
|
result = new Record(null, null, null, null, null, i, null, null);
|
|
else
|
|
{
|
|
string[] equipmentSegments = matches[1][2].Split('|');
|
|
if (equipmentSegments.Length != 2)
|
|
result = new Record(null, null, null, null, null, i, null, null);
|
|
else
|
|
{
|
|
string[] cassetteIdSegments = matches[3][2].Split('|');
|
|
if (cassetteIdSegments.Length <= 3)
|
|
result = new Record(null, null, null, null, null, i, null, null);
|
|
else
|
|
result = new Record(cassetteIdSegments[2],
|
|
new(cassetteIdSegments),
|
|
DateTime.Parse(matches[0][0]),
|
|
matches[0][1],
|
|
new(equipmentSegments),
|
|
i,
|
|
lastDate,
|
|
new(matches));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static Record GetRecord(string[] lines, int i)
|
|
{
|
|
Record result;
|
|
int ii = i;
|
|
string line;
|
|
string[] segments;
|
|
string? lastDate = null;
|
|
List<ReadOnlyCollection<string>> matches = [];
|
|
for (int j = i; j >= 0; j--)
|
|
{
|
|
ii = j;
|
|
line = lines[j];
|
|
segments = line.Split(',');
|
|
if (segments.Length < 2)
|
|
continue;
|
|
lastDate ??= segments[0];
|
|
if (segments[0] != lastDate)
|
|
{
|
|
lastDate = segments[0];
|
|
break;
|
|
}
|
|
matches.Add(new(segments));
|
|
}
|
|
result = GetRecord(ii + 1, lastDate, new(matches));
|
|
return result;
|
|
}
|
|
|
|
private static ReadOnlyCollection<Record> GetRecords(string logDirectory, string logSearchPattern)
|
|
{
|
|
List<Record> results = [];
|
|
Record record;
|
|
string[] lines;
|
|
string[] logFiles = Directory.GetFiles(logDirectory, logSearchPattern, SearchOption.TopDirectoryOnly);
|
|
foreach (string logFile in logFiles)
|
|
{
|
|
lines = File.ReadAllLines(logFile);
|
|
for (int i = lines.Length - 1; i >= 0; i--)
|
|
{
|
|
record = GetRecord(lines, i);
|
|
i = record.I;
|
|
if (record.CassetteId is null || record.CassetteSegments is null || record.Date is null || record.Employee is null || record.EquipmentSegments is null)
|
|
{
|
|
if (i < 4)
|
|
break;
|
|
continue;
|
|
}
|
|
results.Add(record);
|
|
}
|
|
}
|
|
return new(results);
|
|
}
|
|
|
|
private static Dictionary<int, ReadOnlyCollection<Record>> GetKeyValuePairs(Dictionary<int, List<Record>> keyValuePairs)
|
|
{
|
|
Dictionary<int, ReadOnlyCollection<Record>> results = [];
|
|
foreach (KeyValuePair<int, List<Record>> keyValuePair in keyValuePairs)
|
|
results.Add(keyValuePair.Key, new(keyValuePair.Value));
|
|
return new(results);
|
|
}
|
|
|
|
private static Dictionary<int, ReadOnlyCollection<Record>> GetKeyValuePairs(string logSearchPattern, string logDirectory)
|
|
{
|
|
Dictionary<int, ReadOnlyCollection<Record>> results;
|
|
int totalMinutes;
|
|
TimeSpan timeSpan;
|
|
List<Record>? collection;
|
|
Dictionary<int, List<Record>> keyValuePairs = [];
|
|
ReadOnlyCollection<Record> records = GetRecords(logDirectory, logSearchPattern);
|
|
foreach (Record record in records)
|
|
{
|
|
if (record.CassetteId is null || record.CassetteSegments is null || record.Date is null || record.Employee is null || record.EquipmentSegments is null)
|
|
continue;
|
|
timeSpan = TimeSpan.FromTicks(record.Date.Value.Ticks);
|
|
totalMinutes = (int)Math.Floor(timeSpan.TotalMinutes);
|
|
if (!keyValuePairs.TryGetValue(totalMinutes, out collection))
|
|
{
|
|
keyValuePairs.Add(totalMinutes, []);
|
|
if (!keyValuePairs.TryGetValue(totalMinutes, out collection))
|
|
throw new Exception();
|
|
}
|
|
collection.Add(record);
|
|
}
|
|
results = GetKeyValuePairs(keyValuePairs);
|
|
return results;
|
|
}
|
|
|
|
internal static void MoveWaferCounterToArchive(ILogger<Worker> logger, List<string> args)
|
|
{
|
|
int totalMinutes;
|
|
string checkFile;
|
|
TimeSpan timeSpan;
|
|
string? checkDirectory;
|
|
string logDateFormat = args[3];
|
|
string wcSearchPattern = args[5];
|
|
string logSearchPattern = args[2];
|
|
ReadOnlyCollection<Record>? records;
|
|
string logDirectory = Path.GetFullPath(args[0]);
|
|
string sourceDirectory = Path.GetFullPath(args[4]);
|
|
Dictionary<int, ReadOnlyCollection<Record>> keyValuePairs = GetKeyValuePairs(logSearchPattern, logDirectory);
|
|
logger.LogInformation("Mapped {keyValuePairs}(s)", keyValuePairs.Count);
|
|
FileInfo[] collection = Directory.GetFiles(sourceDirectory, wcSearchPattern, SearchOption.AllDirectories).Select(l => new FileInfo(l)).ToArray();
|
|
logger.LogInformation("Found {collection}(s)", collection.Length);
|
|
foreach (FileInfo fileInfo in collection)
|
|
{
|
|
timeSpan = TimeSpan.FromTicks(fileInfo.LastWriteTime.Ticks);
|
|
totalMinutes = (int)Math.Floor(timeSpan.TotalMinutes);
|
|
if (!keyValuePairs.TryGetValue(totalMinutes, out records))
|
|
continue;
|
|
if (records.Count != 1)
|
|
continue;
|
|
checkDirectory = Path.Combine(logDirectory, timeSpan.ToString(logDateFormat));
|
|
if (string.IsNullOrEmpty(checkDirectory))
|
|
continue;
|
|
if (!Directory.Exists(checkDirectory))
|
|
_ = Directory.CreateDirectory(checkDirectory);
|
|
checkFile = Path.Combine(checkDirectory, fileInfo.Name);
|
|
if (File.Exists(checkFile))
|
|
continue;
|
|
File.Move(fileInfo.FullName, checkFile);
|
|
}
|
|
}
|
|
|
|
} |