Files
DiunaBI/WebAPI/dataProcessors/t3.SingleSource.processor.cs

167 lines
6.6 KiB
C#
Raw Normal View History

2023-12-01 15:19:19 +01:00
using DiunaBIWebAPI.dataProcessors;
using Google.Apis.Sheets.v4;
2023-10-30 16:39:13 +01:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
2023-12-01 15:19:19 +01:00
using System.Globalization;
2023-10-30 16:39:13 +01:00
using WebAPI.Controllers;
2023-12-01 15:19:19 +01:00
using WebAPI.Models;
namespace WebAPI.dataProcessors
{
public class T3SingleSourceProcessor
{
2023-11-09 15:17:22 +01:00
private readonly AppDbContext db;
2023-12-01 15:19:19 +01:00
private readonly SpreadsheetsResource.ValuesResource googleSheetValues;
private readonly LayersController controller;
public T3SingleSourceProcessor(
AppDbContext _db,
SpreadsheetsResource.ValuesResource _googleSheetValues,
LayersController _controller)
{
db = _db;
googleSheetValues = _googleSheetValues;
controller = _controller;
}
public void process(Layer processWorker)
2023-10-30 16:39:13 +01:00
{
int year = int.Parse(processWorker?.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
int month = int.Parse(processWorker?.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
2023-12-01 15:19:19 +01:00
string? sourceLayer = processWorker?.Records?.SingleOrDefault(x => x.Code == "SourceLayer")?.Desc1;
if (sourceLayer == null)
{
2023-12-01 15:19:19 +01:00
throw new Exception("SourceLayer record not found");
2023-10-30 16:39:13 +01:00
}
2023-12-01 15:19:19 +01:00
Layer? sourceImportWorker = db.Layers.SingleOrDefault(x => x.Name == sourceLayer);
if (sourceImportWorker == null)
{
throw new Exception("SourceImportWorkerL layer not found");
2023-10-30 16:39:13 +01:00
}
2023-12-01 15:19:19 +01:00
string? source= processWorker?.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
if (sourceLayer == null)
{
throw new Exception("Source record not found");
}
2023-10-30 16:39:13 +01:00
2023-12-01 15:19:19 +01:00
Layer? processedLayer = db.Layers
.Where(x => x.ParentId == processWorker!.Id)
.OrderByDescending(x => x.CreatedAt)
2023-10-30 16:39:13 +01:00
.FirstOrDefault();
bool isNew = false;
if (processedLayer == null)
{
isNew = true;
processedLayer = new Layer
{
Id = Guid.NewGuid(),
Source = "",
Type = LayerType.processed,
ParentId = processWorker!.Id,
2023-10-30 16:39:13 +01:00
Number = db.Layers.Count() + 1,
};
processedLayer.Name = $"L{processedLayer.Number}-P-{year}/{month}-{source}-T3";
2023-10-30 16:39:13 +01:00
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>();
2023-12-01 15:19:19 +01:00
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
processedLayer.ModifiedAt = DateTime.UtcNow;
List<Record> newRecords = new List<Record>();
2023-10-30 16:39:13 +01:00
List<Layer> dataSources = db.Layers
2023-12-01 15:19:19 +01:00
.Include(x => x.Records)
.Where(x => x.ParentId == sourceImportWorker.Id
&& !x.IsDeleted)
.OrderBy(x => x.CreatedAt)
.ToList();
2023-10-30 16:39:13 +01:00
if (dataSources.Count == 0)
2023-12-01 15:19:19 +01:00
{
2023-10-30 16:39:13 +01:00
throw new Exception($"DataSources are empty, {sourceImportWorker.Name}");
2023-12-01 15:19:19 +01:00
}
List<Record> allRecords = dataSources.SelectMany(x => x.Records!).ToList();
2023-11-06 17:19:20 +01:00
foreach (Record baseRecord in dataSources.Last()?.Records!)
2023-12-01 15:19:19 +01:00
{
List<Record> codeRecords = allRecords.Where(x => x.Code == baseRecord.Code).ToList();
2023-10-30 16:39:13 +01:00
Record processedRecord = new Record
2023-12-01 15:19:19 +01:00
{
2023-10-30 16:39:13 +01:00
Id = Guid.NewGuid(),
Code = baseRecord.Code,
CreatedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow
2023-12-01 15:19:19 +01:00
};
int lastDayInMonth = DateTime.DaysInMonth(year, month);
2024-03-09 08:53:47 +01:00
double previousValue = 0;
2023-12-01 15:19:19 +01:00
//day 1
2024-03-09 08:53:47 +01:00
double firstVal = codeRecords
2023-12-01 15:19:19 +01:00
.Where(x => x.CreatedAt.Date <= new DateTime(year, month, 1))
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault()?.Value1 ?? 0;
2024-06-18 19:40:16 +02:00
ProcessHelper.SetValue(processedRecord, 1, firstVal);
2023-12-01 15:19:19 +01:00
previousValue = firstVal;
//days 2-29/30
2023-10-30 16:39:13 +01:00
for (int i=2; i<lastDayInMonth; i++)
{
2024-03-09 08:53:47 +01:00
double? dayVal = codeRecords
.Where(x => x.CreatedAt.Day == i && x.CreatedAt.Month == month)
2023-10-30 16:39:13 +01:00
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault()?.Value1;
if (dayVal == null)
{
//TODO: missing day value? Should I log it?
2024-06-18 19:40:16 +02:00
ProcessHelper.SetValue(processedRecord, i, 0);
2023-10-30 16:39:13 +01:00
} else
{
2024-03-09 08:53:47 +01:00
double processedVal = (dayVal ?? 0) - previousValue;
2024-06-18 19:40:16 +02:00
ProcessHelper.SetValue(processedRecord, i, processedVal);
2023-10-30 16:39:13 +01:00
previousValue = dayVal ?? 0;
}
}
//last day
2024-03-09 08:53:47 +01:00
double? lastVal = codeRecords
.Where(x => x.CreatedAt.Date >= new DateTime(year, month, lastDayInMonth))
2023-10-30 16:39:13 +01:00
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault()?.Value1;
if (lastVal == null)
{
2024-06-18 19:40:16 +02:00
ProcessHelper.SetValue(processedRecord, lastDayInMonth, 0);
2023-10-30 16:39:13 +01:00
} else
{
2024-06-18 19:40:16 +02:00
ProcessHelper.SetValue(processedRecord, lastDayInMonth, (lastVal ?? 0) - previousValue);
2023-10-30 16:39:13 +01:00
}
// copy last value
2024-03-09 08:53:47 +01:00
double? valueToCopy = codeRecords
2023-10-30 16:39:13 +01:00
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault()?.Value1;
2024-06-18 19:40:16 +02:00
ProcessHelper.SetValue(processedRecord, 32, valueToCopy);
2023-10-30 16:39:13 +01:00
newRecords.Add(processedRecord);
2023-12-01 15:19:19 +01:00
}
2023-10-30 16:39:13 +01:00
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();
2023-12-01 15:19:19 +01:00
}
}
}