Logging refactor
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using DiunaBI.Core.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
using DiunaBI.Core.Services.Calculations;
|
||||
@@ -9,15 +10,23 @@ namespace DiunaBI.Plugins.Morska.Processors;
|
||||
public class T3MultiSourceSummaryProcessor : MorskaBaseProcessor
|
||||
{
|
||||
public override string ProcessorType => "T3.MultiSourceSummary";
|
||||
|
||||
private readonly AppDbContext _db;
|
||||
private readonly ILogger<T3MultiSourceSummaryProcessor> _logger;
|
||||
|
||||
T3MultiSourceSummaryProcessor(
|
||||
AppDbContext db)
|
||||
public T3MultiSourceSummaryProcessor(
|
||||
AppDbContext db,
|
||||
ILogger<T3MultiSourceSummaryProcessor> logger)
|
||||
{
|
||||
_db = db;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override void Process(Layer processWorker)
|
||||
{
|
||||
_logger.LogInformation("T3MultiSourceSummary: Starting processing for {ProcessWorkerName} ({ProcessWorkerId})",
|
||||
processWorker.Name, processWorker.Id);
|
||||
|
||||
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
var month = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
|
||||
var sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
|
||||
@@ -72,7 +81,6 @@ public class T3MultiSourceSummaryProcessor : MorskaBaseProcessor
|
||||
|
||||
foreach (var baseCode in baseCodes)
|
||||
{
|
||||
|
||||
var codeRecords = allRecords.Where(x =>
|
||||
x.Code![1..] == baseCode)
|
||||
.ToList();
|
||||
@@ -95,6 +103,7 @@ public class T3MultiSourceSummaryProcessor : MorskaBaseProcessor
|
||||
var dynamicCodes = processWorker.Records?
|
||||
.Where(x => x.Code!.Contains("DynamicCode-"))
|
||||
.OrderBy(x => int.Parse(x.Code!.Split('-')[1])).ToList();
|
||||
|
||||
if (dynamicCodes != null && dynamicCodes.Count != 0)
|
||||
{
|
||||
foreach (var dynamicCode in dynamicCodes)
|
||||
@@ -103,33 +112,16 @@ public class T3MultiSourceSummaryProcessor : MorskaBaseProcessor
|
||||
{
|
||||
if (dynamicCode.Desc1 == null)
|
||||
{
|
||||
//TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Formula in Record {dynamicCode.Id} is missing.",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
_logger.LogWarning("T3MultiSourceSummary: Formula in Record {RecordId} is missing. Process: {ProcessName} ({ProcessId})",
|
||||
dynamicCode.Id, processWorker.Name, processWorker.Id);
|
||||
continue;
|
||||
}
|
||||
|
||||
var calc = new BaseCalc(dynamicCode.Desc1);
|
||||
if (!calc.IsFormulaCorrect())
|
||||
{
|
||||
//TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Formula {calc.Expression} in Record {dynamicCode.Id} is not correct",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
_logger.LogWarning("T3MultiSourceSummary: Formula {Expression} in Record {RecordId} is not correct. Process: {ProcessName} ({ProcessId})",
|
||||
calc.Expression, dynamicCode.Id, processWorker.Name, processWorker.Id);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -139,37 +131,18 @@ public class T3MultiSourceSummaryProcessor : MorskaBaseProcessor
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Formula {calc.Expression} in Record {dynamicCode.Id} error: {e.Message}",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
_logger.LogWarning(e, "T3MultiSourceSummary: Formula {Expression} in Record {RecordId} calculation error. Process: {ProcessName} ({ProcessId})",
|
||||
calc.Expression, dynamicCode.Id, processWorker.Name, processWorker.Id);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Calculation error {dynamicCode.Id}: {e.Message} ",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
_logger.LogWarning(e, "T3MultiSourceSummary: Calculation error for DynamicCode {RecordId}. Process: {ProcessName} ({ProcessId})",
|
||||
dynamicCode.Id, processWorker.Name, processWorker.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
_db.Layers.Add(processedLayer);
|
||||
@@ -178,8 +151,32 @@ public class T3MultiSourceSummaryProcessor : 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();
|
||||
|
||||
_logger.LogInformation("T3MultiSourceSummary: 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("T3MultiSourceSummary: Saved {RecordCount} records for layer {LayerId}", records.Count, layerId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user