T3-SingleSource Layer name fix

This commit is contained in:
Michał Zieliński
2023-12-01 15:19:19 +01:00
parent e174a02b54
commit add95e3dfe

View File

@@ -1,48 +1,53 @@
using DiunaBIWebAPI.dataProcessors; using DiunaBIWebAPI.dataProcessors;
using Google.Apis.Sheets.v4; using Google.Apis.Sheets.v4;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Globalization; using System.Globalization;
using WebAPI.Controllers; using WebAPI.Controllers;
using WebAPI.Models; using WebAPI.Models;
namespace WebAPI.dataProcessors namespace WebAPI.dataProcessors
{ {
public class T3SingleSourceProcessor public class T3SingleSourceProcessor
{ {
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;
public T3SingleSourceProcessor( public T3SingleSourceProcessor(
AppDbContext _db, AppDbContext _db,
SpreadsheetsResource.ValuesResource _googleSheetValues, SpreadsheetsResource.ValuesResource _googleSheetValues,
LayersController _controller) LayersController _controller)
{ {
db = _db; db = _db;
googleSheetValues = _googleSheetValues; googleSheetValues = _googleSheetValues;
controller = _controller; controller = _controller;
} }
public void process(Layer processWorker) public void process(Layer processWorker)
{ {
int year = int.Parse(processWorker?.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!); int year = int.Parse(processWorker?.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
int month = int.Parse(processWorker?.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!); int month = int.Parse(processWorker?.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
string? source = processWorker?.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1; string? sourceLayer = processWorker?.Records?.SingleOrDefault(x => x.Code == "SourceLayer")?.Desc1;
if (source == null) if (sourceLayer == null)
{ {
throw new Exception("Source record not found"); throw new Exception("SourceLayer record not found");
} }
Layer? sourceImportWorker = db.Layers.SingleOrDefault(x => x.Name == source); Layer? sourceImportWorker = db.Layers.SingleOrDefault(x => x.Name == sourceLayer);
if (sourceImportWorker == null) if (sourceImportWorker == null)
{ {
throw new Exception("SourceImportWorkerL layer not found"); throw new Exception("SourceImportWorkerL layer not found");
} }
string? source= processWorker?.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
if (sourceLayer == null)
{
throw new Exception("Source record not found");
}
Layer? processedLayer = db.Layers Layer? processedLayer = db.Layers
.Where(x => x.ParentId == processWorker!.Id) .Where(x => x.ParentId == processWorker!.Id)
.OrderByDescending(x => x.CreatedAt) .OrderByDescending(x => x.CreatedAt)
.FirstOrDefault(); .FirstOrDefault();
bool isNew = false; bool isNew = false;
@@ -64,48 +69,48 @@ namespace WebAPI.dataProcessors
processedLayer.ModifiedAt = DateTime.UtcNow; processedLayer.ModifiedAt = DateTime.UtcNow;
} }
processedLayer.Sources = new List<ProcessSource>(); processedLayer.Sources = new List<ProcessSource>();
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;
List<Record> newRecords = new List<Record>(); List<Record> newRecords = new List<Record>();
List<Layer> dataSources = db.Layers List<Layer> dataSources = db.Layers
.Include(x => x.Records) .Include(x => x.Records)
.Where(x => x.ParentId == sourceImportWorker.Id .Where(x => x.ParentId == sourceImportWorker.Id
&& !x.IsDeleted) && !x.IsDeleted)
.OrderBy(x => x.CreatedAt) .OrderBy(x => x.CreatedAt)
.ToList(); .ToList();
if (dataSources.Count == 0) if (dataSources.Count == 0)
{ {
throw new Exception($"DataSources are empty, {sourceImportWorker.Name}"); throw new Exception($"DataSources are empty, {sourceImportWorker.Name}");
} }
List<Record> allRecords = dataSources.SelectMany(x => x.Records!).ToList(); List<Record> allRecords = dataSources.SelectMany(x => x.Records!).ToList();
foreach (Record baseRecord in dataSources.Last()?.Records!) foreach (Record baseRecord in dataSources.Last()?.Records!)
{ {
List<Record> codeRecords = allRecords.Where(x => x.Code == baseRecord.Code).ToList(); List<Record> codeRecords = allRecords.Where(x => x.Code == baseRecord.Code).ToList();
Record processedRecord = new Record Record processedRecord = new Record
{ {
Id = Guid.NewGuid(), Id = Guid.NewGuid(),
Code = baseRecord.Code, Code = baseRecord.Code,
CreatedAt = DateTime.UtcNow, CreatedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow ModifiedAt = DateTime.UtcNow
}; };
int lastDayInMonth = DateTime.DaysInMonth(year, month); int lastDayInMonth = DateTime.DaysInMonth(year, month);
float previousValue = 0; float previousValue = 0;
//day 1 //day 1
float firstVal = codeRecords float firstVal = codeRecords
.Where(x => x.CreatedAt.Date <= new DateTime(year, month, 1)) .Where(x => x.CreatedAt.Date <= new DateTime(year, month, 1))
.OrderByDescending(x => x.CreatedAt) .OrderByDescending(x => x.CreatedAt)
.FirstOrDefault()?.Value1 ?? 0; .FirstOrDefault()?.Value1 ?? 0;
ProcessHelper.setValue(processedRecord, 1, firstVal); ProcessHelper.setValue(processedRecord, 1, firstVal);
previousValue = firstVal; previousValue = firstVal;
//days 2-29/30 //days 2-29/30
for (int i=2; i<lastDayInMonth; i++) for (int i=2; i<lastDayInMonth; i++)
{ {
float? dayVal = codeRecords float? dayVal = codeRecords
@@ -144,7 +149,7 @@ namespace WebAPI.dataProcessors
ProcessHelper.setValue(processedRecord, 32, valueToCopy); ProcessHelper.setValue(processedRecord, 32, valueToCopy);
newRecords.Add(processedRecord); newRecords.Add(processedRecord);
} }
if (isNew) if (isNew)
{ {
@@ -155,7 +160,7 @@ namespace WebAPI.dataProcessors
} }
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();
} }
} }
} }