Logging refactor

This commit is contained in:
Michał Zieliński
2025-06-02 18:53:25 +02:00
parent b800890320
commit ab3310a0c2
25 changed files with 682 additions and 750 deletions

View File

@@ -2,6 +2,7 @@
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using DiunaBI.Core.Models;
using DiunaBI.Database.Context;
using DiunaBI.Core.Services;
@@ -15,15 +16,23 @@ public class T4R2Processor : MorskaBaseProcessor
private readonly AppDbContext _db;
private readonly SpreadsheetsResource.ValuesResource _googleSheetValues;
private readonly ILogger<T4R2Processor> _logger;
public T4R2Processor(
AppDbContext db,
SpreadsheetsResource.ValuesResource googleSheetValues)
AppDbContext db,
SpreadsheetsResource.ValuesResource googleSheetValues,
ILogger<T4R2Processor> logger)
{
_db = db;
_googleSheetValues = googleSheetValues;
_logger = logger;
}
public override void Process(Layer processWorker)
{
_logger.LogInformation("T4R2: Starting processing for {ProcessWorkerName} ({ProcessWorkerId})",
processWorker.Name, processWorker.Id);
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
var sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
if (sources!.Count == 0)
@@ -37,7 +46,6 @@ public class T4R2Processor : MorskaBaseProcessor
throw new Exception("LayerName record not found");
}
var processedLayer = _db.Layers
.Where(x => x.ParentId == processWorker.Id
&& !x.IsDeleted && !x.IsCancelled)
@@ -116,17 +124,8 @@ public class T4R2Processor : MorskaBaseProcessor
}
else
{
// TODO: log warning
/*
logsController.AddEntry(new LogEntry
{
Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.Warning,
LogType = LogType.Process,
Message = $"Data source {year}/{month:D2}-{source.Desc1}-T3 not found",
CreatedAt = DateTime.UtcNow
});
*/
_logger.LogWarning("T4R2: Data source {DataSource} not found. Process: {ProcessName} ({ProcessId})",
$"{year}/{month:D2}-{source.Desc1}-T3", processWorker.Name, processWorker.Id);
}
}
else
@@ -182,17 +181,8 @@ public class T4R2Processor : MorskaBaseProcessor
}
else
{
// TODO: log warning
/*
logsController.AddEntry(new LogEntry
{
Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.Warning,
LogType = LogType.Process,
Message = $"Data source {year}/13-{source.Desc1}-T3 not found",
CreatedAt = DateTime.UtcNow
});
*/
_logger.LogWarning("T4R2: Data source {DataSource} not found. Process: {ProcessName} ({ProcessId})",
$"{year}/13-{source.Desc1}-T3", processWorker.Name, processWorker.Id);
}
}
@@ -204,8 +194,8 @@ public class T4R2Processor : MorskaBaseProcessor
{
_db.Layers.Update(processedLayer);
}
// TODO: save records
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
SaveRecords(processedLayer.Id, newRecords);
_db.SaveChanges();
var reportSheetName = processWorker.Records?.SingleOrDefault(x => x.Code == "GoogleSheetName")?.Desc1;
@@ -219,7 +209,32 @@ public class T4R2Processor : MorskaBaseProcessor
{
throw new Exception("GoogleSheetName-Invoices record not found");
}
UpdateReport(processedLayer.Id, reportSheetName, invoicesSheetName);
_logger.LogInformation("T4R2: Successfully completed processing for {ProcessWorkerName} ({ProcessWorkerId})",
processWorker.Name, processWorker.Id);
}
private void SaveRecords(Guid layerId, ICollection<Record> records)
{
var toDelete = _db.Records.Where(x => x.LayerId == layerId).ToList();
if (toDelete.Count > 0)
{
_db.Records.RemoveRange(toDelete);
}
foreach (var record in records)
{
record.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
record.CreatedAt = DateTime.UtcNow;
record.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
record.ModifiedAt = DateTime.UtcNow;
record.LayerId = layerId;
_db.Records.Add(record);
}
_logger.LogDebug("T4R2: Saved {RecordCount} records for layer {LayerId}", records.Count, layerId);
}
private void UpdateReport(Guid sourceId, string reportSheetName, string invoicesSheetName)