Files
DiunaBI/WebAPI/dataProcessors/t3.SingleSource.processor.cs
2023-11-08 17:49:13 +01:00

163 lines
6.4 KiB
C#

using DiunaBIWebAPI.dataProcessors;
using Google.Apis.Sheets.v4;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Globalization;
using WebAPI.Controllers;
using WebAPI.Models;
namespace WebAPI.dataProcessors
{
public class T3SingleSourceProcessor
{
private AppDbContext db;
private SpreadsheetsResource.ValuesResource googleSheetValues;
private LayersController controller;
public T3SingleSourceProcessor(
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!);
int month = int.Parse(processWorker?.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
string? source = processWorker?.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
if (source == null)
{
throw new Exception("Source record not found");
}
Layer? sourceImportWorker = db.Layers.SingleOrDefault(x => x.Name == source);
if (sourceImportWorker == null)
{
throw new Exception("SourceImportWorkerL layer not found");
}
Layer? processedLayer = db.Layers
.Where(x => x.ParentId == processWorker!.Id)
.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}-{source}-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 = db.Layers
.Include(x => x.Records)
.Where(x => x.ParentId == sourceImportWorker.Id
&& !x.IsDeleted)
.OrderBy(x => x.CreatedAt)
.ToList();
if (dataSources.Count == 0)
{
throw new Exception($"DataSources are empty, {sourceImportWorker.Name}");
}
List<Record> allRecords = dataSources.SelectMany(x => x.Records!).ToList();
foreach (Record baseRecord in dataSources.Last()?.Records!)
{
List<Record> codeRecords = allRecords.Where(x => x.Code == baseRecord.Code).ToList();
Record processedRecord = new Record
{
Id = Guid.NewGuid(),
Code = baseRecord.Code,
CreatedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow
};
int lastDayInMonth = DateTime.DaysInMonth(year, month);
float previousValue = 0;
//day 1
float firstVal = codeRecords
.Where(x => x.CreatedAt.Date <= new DateTime(year, month, 1))
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault()?.Value1 ?? 0;
ProcessHelper.setValue(processedRecord, 1, firstVal);
previousValue = firstVal;
//days 2-29/30
for (int i=2; i<lastDayInMonth; i++)
{
float? dayVal = codeRecords
.Where(x => x.CreatedAt.Day == i && x.CreatedAt.Month == month)
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault()?.Value1;
if (dayVal == null)
{
//TODO: missing day value? Should I log it?
ProcessHelper.setValue(processedRecord, i, 0);
} else
{
float processedVal = (dayVal ?? 0) - previousValue;
ProcessHelper.setValue(processedRecord, i, processedVal);
previousValue = dayVal ?? 0;
}
}
//last day
float? lastVal = codeRecords
.Where(x => x.CreatedAt.Date >= new DateTime(year, month, lastDayInMonth))
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault()?.Value1;
if (lastVal == null)
{
ProcessHelper.setValue(processedRecord, lastDayInMonth, 0);
} else
{
ProcessHelper.setValue(processedRecord, lastDayInMonth, (lastVal ?? 0) - previousValue);
}
// copy last value
float? valueToCopy = codeRecords
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault()?.Value1;
ProcessHelper.setValue(processedRecord, 32, valueToCopy);
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();
}
}
}