t2.SingleSource - check if need to update
This commit is contained in:
@@ -7,7 +7,8 @@ namespace WebAPI.dataProcessors;
|
|||||||
|
|
||||||
public class T3SingleSourceProcessor(
|
public class T3SingleSourceProcessor(
|
||||||
AppDbContext db,
|
AppDbContext db,
|
||||||
LayersController controller)
|
LayersController controller,
|
||||||
|
LogsController logsController)
|
||||||
{
|
{
|
||||||
public void Process(Layer processWorker)
|
public void Process(Layer processWorker)
|
||||||
{
|
{
|
||||||
@@ -23,7 +24,7 @@ public class T3SingleSourceProcessor(
|
|||||||
{
|
{
|
||||||
throw new Exception("SourceImportWorkerL layer not found");
|
throw new Exception("SourceImportWorkerL layer not found");
|
||||||
}
|
}
|
||||||
var source= processWorker.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
|
var source = processWorker.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
|
||||||
if (sourceLayer == null)
|
if (sourceLayer == null)
|
||||||
{
|
{
|
||||||
throw new Exception("Source record not found");
|
throw new Exception("Source record not found");
|
||||||
@@ -55,7 +56,7 @@ public class T3SingleSourceProcessor(
|
|||||||
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
|
|
||||||
var newRecords = new List<Record>();
|
var newRecords = new List<Record>();
|
||||||
|
|
||||||
var dataSources = db.Layers
|
var dataSources = db.Layers
|
||||||
@@ -70,6 +71,18 @@ public class T3SingleSourceProcessor(
|
|||||||
throw new Exception($"DataSources are empty, {sourceImportWorker.Name}");
|
throw new Exception($"DataSources are empty, {sourceImportWorker.Name}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!dataSources.Any(x => x.CreatedAt > processedLayer.ModifiedAt))
|
||||||
|
{
|
||||||
|
logsController.AddEntry(new LogEntry
|
||||||
|
{
|
||||||
|
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||||
|
Type = LogEntryType.Info,
|
||||||
|
LogType = LogType.Process,
|
||||||
|
Message = $"Layer is up to date",
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var allRecords = dataSources.SelectMany(x => x.Records!).ToList();
|
var allRecords = dataSources.SelectMany(x => x.Records!).ToList();
|
||||||
|
|
||||||
@@ -92,14 +105,15 @@ public class T3SingleSourceProcessor(
|
|||||||
ProcessHelper.SetValue(processedRecord, 1, firstVal);
|
ProcessHelper.SetValue(processedRecord, 1, firstVal);
|
||||||
var previousValue = firstVal;
|
var previousValue = firstVal;
|
||||||
//days 2-29/30
|
//days 2-29/30
|
||||||
for (var i=2; i<lastDayInMonth; i++)
|
for (var i = 2; i < lastDayInMonth; i++)
|
||||||
{
|
{
|
||||||
var dayVal = codeRecords
|
var dayVal = codeRecords
|
||||||
.Where(x => x.CreatedAt.Day == i && x.CreatedAt.Month == month).MaxBy(x => x.CreatedAt)?.Value1;
|
.Where(x => x.CreatedAt.Day == i && x.CreatedAt.Month == month).MaxBy(x => x.CreatedAt)?.Value1;
|
||||||
if (dayVal == null)
|
if (dayVal == null)
|
||||||
{
|
{
|
||||||
ProcessHelper.SetValue(processedRecord, i, 0);
|
ProcessHelper.SetValue(processedRecord, i, 0);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
var processedVal = dayVal - previousValue;
|
var processedVal = dayVal - previousValue;
|
||||||
ProcessHelper.SetValue(processedRecord, i, processedVal);
|
ProcessHelper.SetValue(processedRecord, i, processedVal);
|
||||||
@@ -113,7 +127,8 @@ public class T3SingleSourceProcessor(
|
|||||||
if (lastVal == null)
|
if (lastVal == null)
|
||||||
{
|
{
|
||||||
ProcessHelper.SetValue(processedRecord, lastDayInMonth, 0);
|
ProcessHelper.SetValue(processedRecord, lastDayInMonth, 0);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
ProcessHelper.SetValue(processedRecord, lastDayInMonth, (double)lastVal - previousValue);
|
ProcessHelper.SetValue(processedRecord, lastDayInMonth, (double)lastVal - previousValue);
|
||||||
}
|
}
|
||||||
@@ -124,16 +139,17 @@ public class T3SingleSourceProcessor(
|
|||||||
|
|
||||||
newRecords.Add(processedRecord);
|
newRecords.Add(processedRecord);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNew)
|
if (isNew)
|
||||||
{
|
{
|
||||||
db.Layers.Add(processedLayer);
|
db.Layers.Add(processedLayer);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
db.Layers.Update(processedLayer);
|
db.Layers.Update(processedLayer);
|
||||||
}
|
}
|
||||||
controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user