Code refactor finished
This commit is contained in:
@@ -1,192 +1,175 @@
|
||||
using DiunaBIWebAPI.dataProcessors;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WebAPI.Calculator;
|
||||
using WebAPI.Controllers;
|
||||
using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.dataProcessors
|
||||
{
|
||||
public class T3MultiSourceSummaryProcessor
|
||||
{
|
||||
private readonly AppDbContext db;
|
||||
private readonly SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||
private readonly LayersController controller;
|
||||
private readonly LogsController logsController;
|
||||
namespace WebAPI.dataProcessors;
|
||||
|
||||
public T3MultiSourceSummaryProcessor(
|
||||
AppDbContext _db,
|
||||
SpreadsheetsResource.ValuesResource _googleSheetValues,
|
||||
LayersController _controller,
|
||||
LogsController _logsController)
|
||||
public class T3MultiSourceSummaryProcessor
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
private readonly LayersController _controller;
|
||||
private readonly LogsController _logsController;
|
||||
|
||||
public T3MultiSourceSummaryProcessor(
|
||||
AppDbContext db,
|
||||
LayersController controller,
|
||||
LogsController logsController)
|
||||
{
|
||||
_db = db;
|
||||
_controller = controller;
|
||||
_logsController = logsController;
|
||||
}
|
||||
|
||||
public void Process(Layer processWorker)
|
||||
{
|
||||
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();
|
||||
if (!sources!.Any())
|
||||
{
|
||||
db = _db;
|
||||
googleSheetValues = _googleSheetValues;
|
||||
controller = _controller;
|
||||
logsController = _logsController;
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
public void process(Layer processWorker)
|
||||
var processedLayer = _db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id
|
||||
&& !x.IsDeleted)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
|
||||
var isNew = false;
|
||||
if (processedLayer == null)
|
||||
{
|
||||
int year = int.Parse(processWorker!.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
int month = int.Parse(processWorker!.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
|
||||
List<Record>? sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
|
||||
if (sources!.Count() == 0)
|
||||
isNew = true;
|
||||
processedLayer = new Layer
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
Layer? processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker!.Id
|
||||
&& !x.IsDeleted)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
|
||||
bool isNew = false;
|
||||
if (processedLayer == null)
|
||||
{
|
||||
isNew = true;
|
||||
processedLayer = new Layer
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Source = "",
|
||||
Type = LayerType.processed,
|
||||
ParentId = processWorker!.Id,
|
||||
Number = db.Layers.Count() + 1,
|
||||
};
|
||||
processedLayer.Name = $"L{processedLayer.Number}-P-{year}/{month}-AA-T3";
|
||||
processedLayer.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.CreatedAt = DateTime.UtcNow;
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
}
|
||||
processedLayer.Sources = new List<ProcessSource>();
|
||||
Id = Guid.NewGuid(),
|
||||
Type = LayerType.Processed,
|
||||
ParentId = processWorker.Id,
|
||||
Number = _db.Layers.Count() + 1
|
||||
};
|
||||
processedLayer.Name = $"L{processedLayer.Number}-P-{year}/{month}-AA-T3";
|
||||
processedLayer.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.CreatedAt = DateTime.UtcNow;
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
List<Record> newRecords = new List<Record>();
|
||||
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
List<Layer> dataSources = new List<Layer>();
|
||||
var newRecords = new List<Record>();
|
||||
|
||||
foreach (Record source in sources!)
|
||||
{
|
||||
Layer? dataSource = db.Layers.Where(x =>
|
||||
x.Type == LayerType.processed &&
|
||||
!x.IsDeleted &&
|
||||
x.Name!=null && x.Name.Contains($"{year}/{month}-{source.Desc1}-T3")
|
||||
)
|
||||
.Include(x => x.Records)
|
||||
.FirstOrDefault();
|
||||
if (dataSource != null)
|
||||
{
|
||||
dataSources.Add(dataSource);
|
||||
}
|
||||
}
|
||||
var dataSources = sources!.Select(source => _db.Layers.Where(x => x.Type == LayerType.Processed && !x.IsDeleted && x.Name != null && x.Name.Contains($"{year}/{month}-{source.Desc1}-T3"))
|
||||
.Include(x => x.Records)
|
||||
.FirstOrDefault())
|
||||
.OfType<Layer>()
|
||||
.ToList();
|
||||
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
throw new Exception($"DataSources are empty");
|
||||
}
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
throw new Exception("DataSources are empty");
|
||||
}
|
||||
|
||||
List<Record> allRecords = dataSources.SelectMany(x => x.Records!).ToList();
|
||||
List<string> baseCodess = allRecords.Select(x => x.Code!.Remove(0, 1)).Distinct().ToList();
|
||||
var allRecords = dataSources.SelectMany(x => x.Records!).ToList();
|
||||
var baseCodes = allRecords.Select(x => x.Code!.Remove(0, 1)).Distinct().ToList();
|
||||
|
||||
foreach (string baseCode in baseCodess)
|
||||
{
|
||||
foreach (var baseCode in baseCodes)
|
||||
{
|
||||
|
||||
List<Record> codeRecords = allRecords.Where(x =>
|
||||
x.Code!.Substring(1) == baseCode)
|
||||
.ToList();
|
||||
Record processedRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = $"9{baseCode}",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
for (var i = 1; i<33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i,
|
||||
codeRecords.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
}
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
// Dynamic Codes
|
||||
List<Record>? dynamicCodes = processWorker.Records?.Where(x => x.Code == "DynamicCode").ToList();
|
||||
if (dynamicCodes != null && dynamicCodes.Any())
|
||||
var codeRecords = allRecords.Where(x =>
|
||||
x.Code![1..] == baseCode)
|
||||
.ToList();
|
||||
var processedRecord = new Record
|
||||
{
|
||||
foreach (Record dynamicCode in dynamicCodes)
|
||||
Id = Guid.NewGuid(),
|
||||
Code = $"9{baseCode}",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
for (var i = 1; i<33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i,
|
||||
codeRecords.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
}
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
// Dynamic Codes
|
||||
var dynamicCodes = processWorker.Records?.Where(x => x.Code == "DynamicCode").ToList();
|
||||
if (dynamicCodes != null && dynamicCodes.Any())
|
||||
{
|
||||
foreach (var dynamicCode in dynamicCodes)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (dynamicCode.Desc1 == null)
|
||||
{
|
||||
_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
|
||||
});
|
||||
continue;
|
||||
}
|
||||
var calc = new BaseCalc(dynamicCode.Desc1);
|
||||
if (!calc.IsFormulaCorrect())
|
||||
{
|
||||
_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
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (dynamicCode.Desc1 == null)
|
||||
{
|
||||
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
|
||||
});
|
||||
continue;
|
||||
}
|
||||
BaseCalc calc = new BaseCalc(dynamicCode.Desc1);
|
||||
if (!calc.IsFormulaCorrect())
|
||||
{
|
||||
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
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
newRecords.Add(calc.CalculateT3(newRecords));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
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
|
||||
});
|
||||
}
|
||||
newRecords.Add(calc.CalculateT3(newRecords));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logsController.AddEntry(new LogEntry
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker!.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.warning,
|
||||
LogType = LogType.process,
|
||||
Message = $"Calculation error {dynamicCode.Id}: {e.Message} ",
|
||||
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
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_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
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Layers.Update(processedLayer);
|
||||
}
|
||||
controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
_db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
_db.Layers.Update(processedLayer);
|
||||
}
|
||||
_controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
_db.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user