MultiSourceYearSummary
This commit is contained in:
@@ -193,7 +193,7 @@ namespace WebAPI.Controllers
|
|||||||
.Where(x =>
|
.Where(x =>
|
||||||
x.Records!.Any(x => x.Code == "Type" && x.Desc1 == "ProcessWorker") &&
|
x.Records!.Any(x => x.Code == "Type" && x.Desc1 == "ProcessWorker") &&
|
||||||
x.Records!.Any(x => x.Code == "IsEnabled" && x.Desc1 == "True")
|
x.Records!.Any(x => x.Code == "IsEnabled" && x.Desc1 == "True")
|
||||||
//&& x.Number == 262
|
&& x.Number == 268
|
||||||
)
|
)
|
||||||
.OrderBy(x => x.CreatedAt)
|
.OrderBy(x => x.CreatedAt)
|
||||||
.ToList();
|
.ToList();
|
||||||
@@ -253,7 +253,34 @@ namespace WebAPI.Controllers
|
|||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
T3SourceYearSummaryProcessor processor = new T3SourceYearSummaryProcessor(db, googleSheetValues, this, logsController);
|
T3SourceYearSummaryProcessor processor = new T3SourceYearSummaryProcessor(db, googleSheetValues, this);
|
||||||
|
processor.process(processWorker!);
|
||||||
|
|
||||||
|
logsController.AddEntry(new LogEntry
|
||||||
|
{
|
||||||
|
Title = $"{processWorker!.Name}, {processWorker.Id}",
|
||||||
|
Type = LogEntryType.info,
|
||||||
|
LogType = LogType.process,
|
||||||
|
Message = "Success",
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (processType == "T3-MultiSourceYearSummary")
|
||||||
|
{
|
||||||
|
if (int.Parse(year!) < DateTime.UtcNow.Year)
|
||||||
|
{
|
||||||
|
logsController.AddEntry(new LogEntry
|
||||||
|
{
|
||||||
|
Title = $"{processWorker!.Name}, {processWorker.Id}",
|
||||||
|
Type = LogEntryType.warning,
|
||||||
|
LogType = LogType.process,
|
||||||
|
Message = "processLayer is out of date. Should be disabled. Not processed",
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
T3MultiSourceYearSummaryProcessor processor = new T3MultiSourceYearSummaryProcessor(db, googleSheetValues, this);
|
||||||
processor.process(processWorker!);
|
processor.process(processWorker!);
|
||||||
|
|
||||||
logsController.AddEntry(new LogEntry
|
logsController.AddEntry(new LogEntry
|
||||||
|
|||||||
115
WebAPI/dataProcessors/t3.MultiSourceYearSummary.processor.cs
Normal file
115
WebAPI/dataProcessors/t3.MultiSourceYearSummary.processor.cs
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
using Google.Apis.Sheets.v4;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WebAPI.Controllers;
|
||||||
|
using WebAPI.Models;
|
||||||
|
|
||||||
|
namespace WebAPI.dataProcessors
|
||||||
|
{
|
||||||
|
public class T3MultiSourceYearSummaryProcessor
|
||||||
|
{
|
||||||
|
private AppDbContext db;
|
||||||
|
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||||
|
private LayersController controller;
|
||||||
|
|
||||||
|
public T3MultiSourceYearSummaryProcessor(
|
||||||
|
AppDbContext _db,
|
||||||
|
SpreadsheetsResource.ValuesResource _googleSheetValues,
|
||||||
|
LayersController _controller)
|
||||||
|
{
|
||||||
|
db = _db;
|
||||||
|
googleSheetValues = _googleSheetValues;
|
||||||
|
controller = _controller;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void process(Layer processWorker)
|
||||||
|
{
|
||||||
|
int year = int.Parse(processWorker!.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||||
|
List<Record>? sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
|
||||||
|
if (sources!.Count() == 0)
|
||||||
|
{
|
||||||
|
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}/13-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>();
|
||||||
|
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||||
|
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
|
List<Record> newRecords = new List<Record>();
|
||||||
|
|
||||||
|
List<Layer> dataSources = new List<Layer>();
|
||||||
|
|
||||||
|
foreach (Record source in sources!)
|
||||||
|
{
|
||||||
|
Layer? dataSource = db.Layers.Where(x =>
|
||||||
|
x.Type == LayerType.processed &&
|
||||||
|
!x.IsDeleted &&
|
||||||
|
x.Name.Contains($"{year}/13-{source.Desc1}-T3")
|
||||||
|
)
|
||||||
|
.Include(x => x.Records)
|
||||||
|
.FirstOrDefault();
|
||||||
|
if (dataSource != null)
|
||||||
|
{
|
||||||
|
dataSources.Add(dataSource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dataSources.Count == 0)
|
||||||
|
{
|
||||||
|
throw new Exception($"DataSources are empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Record> allRecords = dataSources.SelectMany(x => x.Records!).ToList();
|
||||||
|
foreach (Record baseRecord in dataSources.Last()?.Records!)
|
||||||
|
{
|
||||||
|
|
||||||
|
List<Record> codeRecords = allRecords.Where(x =>
|
||||||
|
x.Code!.Substring(1) == baseRecord.Code!.Substring(1))
|
||||||
|
.ToList();
|
||||||
|
Record processedRecord = new Record
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
|
Code = $"1{baseRecord.Code}",
|
||||||
|
CreatedAt = DateTime.UtcNow,
|
||||||
|
ModifiedAt = DateTime.UtcNow,
|
||||||
|
Value1 = codeRecords.Sum(x => x.Value1)
|
||||||
|
};
|
||||||
|
newRecords.Add(processedRecord);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,19 +10,16 @@ namespace WebAPI.dataProcessors
|
|||||||
private readonly AppDbContext db;
|
private readonly AppDbContext db;
|
||||||
private readonly SpreadsheetsResource.ValuesResource googleSheetValues;
|
private readonly SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||||
private readonly LayersController controller;
|
private readonly LayersController controller;
|
||||||
private readonly LogsController logsController;
|
|
||||||
|
|
||||||
public T3SourceYearSummaryProcessor(
|
public T3SourceYearSummaryProcessor(
|
||||||
AppDbContext _db,
|
AppDbContext _db,
|
||||||
SpreadsheetsResource.ValuesResource _googleSheetValues,
|
SpreadsheetsResource.ValuesResource _googleSheetValues,
|
||||||
LayersController _controller,
|
LayersController _controller
|
||||||
LogsController _logsController
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
db = _db;
|
db = _db;
|
||||||
googleSheetValues = _googleSheetValues;
|
googleSheetValues = _googleSheetValues;
|
||||||
controller = _controller;
|
controller = _controller;
|
||||||
logsController = _logsController;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void process(Layer processWorker)
|
public void process(Layer processWorker)
|
||||||
|
|||||||
Reference in New Issue
Block a user