WIP: refactor
This commit is contained in:
@@ -62,7 +62,7 @@ public class BaseCalc
|
||||
|
||||
for (var i = 1; i <= 32; i++)
|
||||
{
|
||||
var formula = ingredients.Aggregate(Formula, (current, ingredient) => current.Replace($"[{ingredient.Code}]", ProcessHelper.getValue(ingredient, i)?.ToString(CultureInfo.InvariantCulture)));
|
||||
var formula = ingredients.Aggregate(Formula, (current, ingredient) => current.Replace($"[{ingredient.Code}]", ProcessHelper.GetValue(ingredient, i)?.ToString(CultureInfo.InvariantCulture)));
|
||||
if (formula.Contains('['))
|
||||
{
|
||||
throw new Exception($"Not all placeholders were replaced. Value{i} [{formula}]");
|
||||
|
||||
@@ -16,12 +16,12 @@ public class AdminController : Controller
|
||||
|
||||
public AdminController(
|
||||
GoogleDriveHelper googleDriveHelper,
|
||||
IConfiguration configuration,
|
||||
LogsController logsController)
|
||||
GoogleSheetsHelper googleSheetsHelper,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_googleDriveHelper = googleDriveHelper;
|
||||
_configuration = configuration;
|
||||
_logsController = logsController;
|
||||
_logsController = new LogsController(googleSheetsHelper, _configuration);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
|
||||
@@ -493,7 +493,7 @@ public class LayersController : Controller
|
||||
case "T1-R1":
|
||||
{
|
||||
var processor = new T1R1Processor(_db, _googleSheetValues, this, _logsController);
|
||||
processor.process(processWorker);
|
||||
processor.Process(processWorker);
|
||||
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
@@ -553,7 +553,7 @@ public class LayersController : Controller
|
||||
{
|
||||
var t3MultiSourceCopySelectedCode =
|
||||
new T3MultiSourceCopySelectedCodesProcessor(_db, _googleSheetValues, this);
|
||||
t3MultiSourceCopySelectedCode.process(processWorker);
|
||||
t3MultiSourceCopySelectedCode.Process(processWorker);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ public static class ProcessHelper
|
||||
break;
|
||||
}
|
||||
}
|
||||
public static double? getValue(Record record, int number)
|
||||
public static double? GetValue(Record record, int number)
|
||||
{
|
||||
return number switch
|
||||
{
|
||||
@@ -159,7 +159,7 @@ public static class ProcessHelper
|
||||
break;
|
||||
case 2:
|
||||
{
|
||||
for (int i = int.Parse(range[0]); i <= int.Parse(range[1]); i++)
|
||||
for (var i = int.Parse(range[0]); i <= int.Parse(range[1]); i++)
|
||||
{
|
||||
codesList.Add(i);
|
||||
}
|
||||
|
||||
@@ -6,270 +6,270 @@ using Microsoft.EntityFrameworkCore;
|
||||
using WebAPI.Controllers;
|
||||
using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.dataProcessors
|
||||
namespace WebAPI.dataProcessors;
|
||||
|
||||
public class T1R1Processor
|
||||
{
|
||||
public class T1R1Processor
|
||||
private readonly AppDbContext _db;
|
||||
private readonly SpreadsheetsResource.ValuesResource _googleSheetValues;
|
||||
private readonly LayersController _controller;
|
||||
private readonly LogsController _logsController;
|
||||
|
||||
public T1R1Processor(
|
||||
AppDbContext db,
|
||||
SpreadsheetsResource.ValuesResource googleSheetValues,
|
||||
LayersController controller,
|
||||
LogsController logsController)
|
||||
{
|
||||
private readonly AppDbContext db;
|
||||
private readonly SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||
private readonly LayersController controller;
|
||||
private readonly LogsController logsController;
|
||||
_db = db;
|
||||
_googleSheetValues = googleSheetValues;
|
||||
_controller = controller;
|
||||
_logsController = logsController;
|
||||
}
|
||||
|
||||
public T1R1Processor(
|
||||
AppDbContext _db,
|
||||
SpreadsheetsResource.ValuesResource _googleSheetValues,
|
||||
LayersController _controller,
|
||||
LogsController _logsController)
|
||||
public void Process(Layer processWorker)
|
||||
{
|
||||
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
var sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
|
||||
if (!sources!.Any())
|
||||
{
|
||||
db = _db;
|
||||
googleSheetValues = _googleSheetValues;
|
||||
controller = _controller;
|
||||
logsController = _logsController;
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
public void process(Layer processWorker)
|
||||
var codes = processWorker.Records?.SingleOrDefault(x => x.Code == "Codes")?.Desc1;
|
||||
if (codes == null)
|
||||
{
|
||||
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");
|
||||
}
|
||||
string? codes = processWorker.Records?.SingleOrDefault(x => x.Code == "Codes")?.Desc1;
|
||||
if (codes == null)
|
||||
{
|
||||
throw new Exception("Codes record not found");
|
||||
}
|
||||
List<int> codesList = ProcessHelper.ParseCodes(codes);
|
||||
throw new Exception("Codes record not found");
|
||||
}
|
||||
var codesList = ProcessHelper.ParseCodes(codes);
|
||||
|
||||
Layer? processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker!.Id
|
||||
&& !x.IsDeleted)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
var processedLayer = _db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id
|
||||
&& !x.IsDeleted)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
|
||||
bool isNew = false;
|
||||
if (processedLayer == null)
|
||||
var isNew = false;
|
||||
if (processedLayer == null)
|
||||
{
|
||||
isNew = true;
|
||||
processedLayer = new Layer
|
||||
{
|
||||
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}-R1-T1";
|
||||
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>();
|
||||
Id = Guid.NewGuid(),
|
||||
Source = "",
|
||||
Type = LayerType.processed,
|
||||
ParentId = processWorker.Id,
|
||||
Number = _db.Layers.Count() + 1
|
||||
};
|
||||
processedLayer.Name = $"L{processedLayer.Number}-P-{year}-R1-T1";
|
||||
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<Layer> dataSources = new List<Layer>();
|
||||
var dataSources = new List<Layer>();
|
||||
|
||||
foreach (Record source in sources!)
|
||||
foreach (var source in sources!)
|
||||
{
|
||||
for (var month = 1; month <= DateTime.UtcNow.Month; month++)
|
||||
{
|
||||
for (int month = 1; month <= DateTime.UtcNow.Month; month++)
|
||||
{
|
||||
Layer? dataSource = db.Layers.Where(x =>
|
||||
var monthCopy = month;
|
||||
var dataSource = _db.Layers.Where(x =>
|
||||
x.Type == LayerType.processed &&
|
||||
!x.IsDeleted &&
|
||||
x.Name != null && x.Name.Contains($"{year}/{month}-{source.Desc1}-T3")
|
||||
)
|
||||
.Include(x => x.Records)
|
||||
.FirstOrDefault();
|
||||
if (dataSource != null)
|
||||
{
|
||||
dataSources.Add(dataSource);
|
||||
}
|
||||
else
|
||||
{
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker!.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.warning,
|
||||
LogType = LogType.process,
|
||||
Message = $"Data source {year}/{month}-{source.Desc1}-T3 not found",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
}
|
||||
// year summary
|
||||
Layer? dataSourceSum = db.Layers.Where(x =>
|
||||
x.Type == LayerType.processed &&
|
||||
!x.IsDeleted &&
|
||||
x.Name != null && x.Name.Contains($"{year}/13-{source.Desc1}-T3")
|
||||
x.Name != null && x.Name.Contains($"{year}/{monthCopy}-{source.Desc1}-T3")
|
||||
)
|
||||
.Include(x => x.Records)
|
||||
.FirstOrDefault();
|
||||
if (dataSourceSum != null)
|
||||
if (dataSource != null)
|
||||
{
|
||||
dataSources.Add(dataSourceSum);
|
||||
dataSources.Add(dataSource);
|
||||
}
|
||||
else
|
||||
{
|
||||
logsController.AddEntry(new LogEntry
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker!.Name}, {processWorker.Id}",
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.warning,
|
||||
LogType = LogType.process,
|
||||
Message = $"Data source {year}/13-{source.Desc1}-T3 not found",
|
||||
Message = $"Data source {year}/{month}-{source.Desc1}-T3 not found",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (dataSources.Count == 0)
|
||||
// year summary
|
||||
var dataSourceSum = _db.Layers.Where(x =>
|
||||
x.Type == LayerType.processed &&
|
||||
!x.IsDeleted &&
|
||||
x.Name != null && x.Name.Contains($"{year}/13-{source.Desc1}-T3")
|
||||
)
|
||||
.Include(x => x.Records)
|
||||
.FirstOrDefault();
|
||||
if (dataSourceSum != null)
|
||||
{
|
||||
throw new Exception($"DataSources are empty");
|
||||
}
|
||||
|
||||
List<Record> newRecords = dataSources
|
||||
.SelectMany(x => x.Records!)
|
||||
.Where(x => codesList.Contains(int.Parse(x.Code!)))
|
||||
.Select(x =>
|
||||
{
|
||||
Layer? layer = dataSources.SingleOrDefault(y => y.Id == x.LayerId);
|
||||
string postFix = layer!.Name!.Split("/")[1].Split("-")[0];
|
||||
if (postFix.Length == 1)
|
||||
{
|
||||
postFix = "0" + postFix;
|
||||
}
|
||||
|
||||
Record newRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = x.Code + postFix,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow,
|
||||
Value1 = x.Value32
|
||||
};
|
||||
|
||||
return newRecord;
|
||||
})
|
||||
.ToList();
|
||||
if (isNew)
|
||||
{
|
||||
db.Layers.Add(processedLayer);
|
||||
dataSources.Add(dataSourceSum);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Layers.Update(processedLayer);
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.warning,
|
||||
LogType = LogType.process,
|
||||
Message = $"Data source {year}/13-{source.Desc1}-T3 not found",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
|
||||
updateReport();
|
||||
}
|
||||
|
||||
public void updateReport()
|
||||
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
string sheetId = "1pph-XowjlK5CIaCEV_A5buK4ceJ0Z0YoUlDI4VMkhhA";
|
||||
string sheetName = "Raport_R1_Eksport";
|
||||
SpreadsheetsResource.ValuesResource.GetRequest request = googleSheetValues.Get(sheetId, "C4:EX4");
|
||||
var response = request.Execute();
|
||||
throw new Exception("DataSources are empty");
|
||||
}
|
||||
|
||||
Layer? R1 = db.Layers
|
||||
.Where(x => x.Number == 1205)
|
||||
.Include(x => x.Records)
|
||||
.FirstOrDefault();
|
||||
|
||||
int startRow = 6;
|
||||
|
||||
var codesRow = response.Values[0];
|
||||
for (int i = 1; i <= DateTime.UtcNow.Month; i++)
|
||||
var newRecords = dataSources
|
||||
.SelectMany(x => x.Records!)
|
||||
.Where(x => codesList.Contains(int.Parse(x.Code!)))
|
||||
.Select(x =>
|
||||
{
|
||||
List<object> values = new List<object>();
|
||||
string month = i < 10 ? $"0{i}" : i.ToString();
|
||||
string row = (startRow + i).ToString();
|
||||
foreach (string code in codesRow)
|
||||
var layer = dataSources.SingleOrDefault(y => y.Id == x.LayerId);
|
||||
var postFix = layer!.Name!.Split("/")[1].Split("-")[0];
|
||||
if (postFix.Length == 1)
|
||||
{
|
||||
Record? record = R1!.Records?.SingleOrDefault(x => x.Code == $"{code}{month}");
|
||||
if (record != null)
|
||||
{
|
||||
values.Add(record.Value1!.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
values.Add("0");
|
||||
}
|
||||
postFix = "0" + postFix;
|
||||
}
|
||||
ValueRange ValueRange = new ValueRange
|
||||
|
||||
var newRecord = new Record
|
||||
{
|
||||
Values = new List<IList<object>> { values }
|
||||
Id = Guid.NewGuid(),
|
||||
Code = x.Code + postFix,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow,
|
||||
Value1 = x.Value32
|
||||
};
|
||||
SpreadsheetsResource.ValuesResource.UpdateRequest update = googleSheetValues.Update(ValueRange, sheetId, $"{sheetName}!C{row}:XZ{row}");
|
||||
update.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
update.Execute();
|
||||
}
|
||||
|
||||
// sum
|
||||
List<object> valuesSum = new List<object>();
|
||||
List<object> emptyRow = new List<object>();
|
||||
string rowEmpty = (startRow + DateTime.UtcNow.Month + 1).ToString();
|
||||
string rowSum = (startRow + DateTime.UtcNow.Month + 2).ToString();
|
||||
|
||||
return newRecord;
|
||||
})
|
||||
.ToList();
|
||||
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();
|
||||
|
||||
UpdateReport();
|
||||
}
|
||||
|
||||
private void UpdateReport()
|
||||
{
|
||||
const string sheetId = "1pph-XowjlK5CIaCEV_A5buK4ceJ0Z0YoUlDI4VMkhhA";
|
||||
const string sheetName = "Raport_R1_Eksport";
|
||||
var request = _googleSheetValues.Get(sheetId, "C4:EX4");
|
||||
var response = request.Execute();
|
||||
|
||||
var r1 = _db.Layers
|
||||
.Where(x => x.Number == 1205)
|
||||
.Include(x => x.Records)
|
||||
.FirstOrDefault();
|
||||
|
||||
const int startRow = 6;
|
||||
|
||||
var codesRow = response.Values[0];
|
||||
for (var i = 1; i <= DateTime.UtcNow.Month; i++)
|
||||
{
|
||||
var values = new List<object>();
|
||||
var month = i < 10 ? $"0{i}" : i.ToString();
|
||||
var row = (startRow + i).ToString();
|
||||
foreach (string code in codesRow)
|
||||
{
|
||||
Record? record = R1!.Records?.SingleOrDefault(x => x.Code == $"{code}13");
|
||||
emptyRow.Add("");
|
||||
var record = r1!.Records?.SingleOrDefault(x => x.Code == $"{code}{month}");
|
||||
if (record != null)
|
||||
{
|
||||
valuesSum.Add(record.Value1!.Value);
|
||||
values.Add(record.Value1!.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
valuesSum.Add("0");
|
||||
values.Add("0");
|
||||
}
|
||||
}
|
||||
// insert empty row before sum
|
||||
ValueRange valueRangeEmpty = new ValueRange
|
||||
var valueRange = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { emptyRow }
|
||||
Values = new List<IList<object>> { values }
|
||||
};
|
||||
SpreadsheetsResource.ValuesResource.UpdateRequest updateEmpty = googleSheetValues.Update(valueRangeEmpty, sheetId, $"{sheetName}!C{rowEmpty}:XZ{rowEmpty}");
|
||||
updateEmpty.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateEmpty.Execute();
|
||||
|
||||
ValueRange ValueRangeSum = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { valuesSum }
|
||||
};
|
||||
SpreadsheetsResource.ValuesResource.UpdateRequest updateSum = googleSheetValues.Update(ValueRangeSum, sheetId, $"{sheetName}!C{rowSum}:XZ{rowSum}");
|
||||
updateSum.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateSum.Execute();
|
||||
|
||||
// update time
|
||||
List<object> timeUtc = new List<object>
|
||||
{
|
||||
R1!.ModifiedAt.ToString(CultureInfo.InvariantCulture),
|
||||
};
|
||||
ValueRange valueRangeUtcTime = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { timeUtc }
|
||||
};
|
||||
SpreadsheetsResource.ValuesResource.UpdateRequest updateTimeUtc = googleSheetValues.Update(valueRangeUtcTime, sheetId, $"{sheetName}!G1");
|
||||
updateTimeUtc.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateTimeUtc.Execute();
|
||||
|
||||
var warsawTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
var warsawTime = TimeZoneInfo.ConvertTimeFromUtc(R1!.ModifiedAt.ToUniversalTime(), warsawTimeZone);
|
||||
List<object> timeWarsaw = new List<object>
|
||||
{
|
||||
warsawTime.ToString(CultureInfo.InvariantCulture),
|
||||
};
|
||||
ValueRange valueRangeWarsawTime = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { timeWarsaw }
|
||||
};
|
||||
SpreadsheetsResource.ValuesResource.UpdateRequest updateTimeWarsaw = googleSheetValues.Update(valueRangeWarsawTime, sheetId, $"{sheetName}!G2");
|
||||
updateTimeWarsaw.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateTimeWarsaw.Execute();
|
||||
var update = _googleSheetValues.Update(valueRange, sheetId, $"{sheetName}!C{row}:XZ{row}");
|
||||
update.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
update.Execute();
|
||||
}
|
||||
|
||||
// sum
|
||||
var valuesSum = new List<object>();
|
||||
var emptyRow = new List<object>();
|
||||
var rowEmpty = (startRow + DateTime.UtcNow.Month + 1).ToString();
|
||||
var rowSum = (startRow + DateTime.UtcNow.Month + 2).ToString();
|
||||
foreach (string code in codesRow)
|
||||
{
|
||||
var record = r1!.Records?.SingleOrDefault(x => x.Code == $"{code}13");
|
||||
emptyRow.Add("");
|
||||
if (record != null)
|
||||
{
|
||||
valuesSum.Add(record.Value1!.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
valuesSum.Add("0");
|
||||
}
|
||||
}
|
||||
// insert empty row before sum
|
||||
var valueRangeEmpty = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { emptyRow }
|
||||
};
|
||||
var updateEmpty = _googleSheetValues.Update(valueRangeEmpty, sheetId, $"{sheetName}!C{rowEmpty}:XZ{rowEmpty}");
|
||||
updateEmpty.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateEmpty.Execute();
|
||||
|
||||
var valueRangeSum = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { valuesSum }
|
||||
};
|
||||
var updateSum = _googleSheetValues.Update(valueRangeSum, sheetId, $"{sheetName}!C{rowSum}:XZ{rowSum}");
|
||||
updateSum.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateSum.Execute();
|
||||
|
||||
// update time
|
||||
var timeUtc = new List<object>
|
||||
{
|
||||
r1!.ModifiedAt.ToString(CultureInfo.InvariantCulture)
|
||||
};
|
||||
var valueRangeUtcTime = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { timeUtc }
|
||||
};
|
||||
var updateTimeUtc = _googleSheetValues.Update(valueRangeUtcTime, sheetId, $"{sheetName}!G1");
|
||||
updateTimeUtc.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateTimeUtc.Execute();
|
||||
|
||||
var warsawTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
var warsawTime = TimeZoneInfo.ConvertTimeFromUtc(r1.ModifiedAt.ToUniversalTime(), warsawTimeZone);
|
||||
var timeWarsaw = new List<object>
|
||||
{
|
||||
warsawTime.ToString(CultureInfo.InvariantCulture)
|
||||
};
|
||||
var valueRangeWarsawTime = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { timeWarsaw }
|
||||
};
|
||||
var updateTimeWarsaw = _googleSheetValues.Update(valueRangeWarsawTime, sheetId, $"{sheetName}!G2");
|
||||
updateTimeWarsaw.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateTimeWarsaw.Execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,30 +23,30 @@ namespace WebAPI.dataProcessors
|
||||
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 month = int.Parse(processWorker!.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
|
||||
List<Record>? sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
|
||||
if (sources!.Count() == 0)
|
||||
var year = int.Parse(processWorker!.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
var month = int.Parse(processWorker!.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
|
||||
var sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
|
||||
if (!sources!.Any())
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
string? codes = processWorker.Records?.SingleOrDefault(x => x.Code == "Codes")?.Desc1;
|
||||
var codes = processWorker.Records?.SingleOrDefault(x => x.Code == "Codes")?.Desc1;
|
||||
if (codes == null)
|
||||
{
|
||||
throw new Exception("Codes record not found");
|
||||
}
|
||||
// create array of integers from string codes where: '501-503;505-505;510-512' -> [501,502,503,505,510,511,512]
|
||||
List<int> codesList = ProcessHelper.ParseCodes(codes);
|
||||
|
||||
Layer? processedLayer = db.Layers
|
||||
var codesList = ProcessHelper.ParseCodes(codes);
|
||||
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker!.Id
|
||||
&& !x.IsDeleted)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
|
||||
bool isNew = false;
|
||||
var isNew = false;
|
||||
if (processedLayer == null)
|
||||
{
|
||||
isNew = true;
|
||||
@@ -56,7 +56,7 @@ namespace WebAPI.dataProcessors
|
||||
Source = "",
|
||||
Type = LayerType.processed,
|
||||
ParentId = processWorker!.Id,
|
||||
Number = db.Layers.Count() + 1,
|
||||
Number = db.Layers.Count() + 1
|
||||
};
|
||||
processedLayer.Name = $"L{processedLayer.Number}-P-{year}/{month}-AB-T3";
|
||||
processedLayer.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
@@ -68,33 +68,23 @@ namespace WebAPI.dataProcessors
|
||||
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
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 != null && x.Name.Contains($"{year}/{month}-{source.Desc1}-T3")
|
||||
)
|
||||
var dataSources = sources!.Select(source => db.Layers.Where(x => x.Type == LayerType.processed && !x.IsDeleted && x.Name != null && x.Name.Contains($"{year}/{month}-{source.Desc1}-T3"))
|
||||
.Include(x => x.Records)
|
||||
.FirstOrDefault();
|
||||
if (dataSource != null)
|
||||
{
|
||||
dataSources.Add(dataSource);
|
||||
}
|
||||
}
|
||||
.FirstOrDefault())
|
||||
.OfType<Layer>()
|
||||
.ToList();
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
throw new Exception($"DataSources are empty");
|
||||
}
|
||||
|
||||
|
||||
List<Record> newRecords = dataSources
|
||||
var newRecords = dataSources
|
||||
.SelectMany(x => x.Records!)
|
||||
.Where(x => codesList.Contains(int.Parse(x.Code!)))
|
||||
.Select(x =>
|
||||
{
|
||||
Record newRecord = new Record
|
||||
var newRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = x.Code,
|
||||
@@ -103,7 +93,7 @@ namespace WebAPI.dataProcessors
|
||||
};
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(newRecord, i, ProcessHelper.getValue(x, i));
|
||||
ProcessHelper.SetValue(newRecord, i, ProcessHelper.GetValue(x, i));
|
||||
}
|
||||
return newRecord;
|
||||
})
|
||||
|
||||
@@ -94,7 +94,7 @@ namespace WebAPI.dataProcessors
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i,
|
||||
codeRecords.Sum(x => ProcessHelper.getValue(x, i)));
|
||||
codeRecords.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
}
|
||||
|
||||
newRecords.Add(processedRecord);
|
||||
|
||||
@@ -107,7 +107,7 @@ namespace WebAPI.dataProcessors
|
||||
for (var i = 1; i<33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i,
|
||||
codeRecords.Sum(x => ProcessHelper.getValue(x, i)));
|
||||
codeRecords.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
}
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
@@ -129,18 +129,18 @@ namespace WebAPI.dataProcessors
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i,
|
||||
codeRecords.Sum(x => ProcessHelper.getValue(x, i)));
|
||||
codeRecords.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
|
||||
ProcessHelper.SetValue(validationRecord, i,
|
||||
codeRecordsValidation.Sum(x => ProcessHelper.getValue(x, i)));
|
||||
codeRecordsValidation.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
|
||||
if (
|
||||
ProcessHelper.getValue(processedRecord,i) !=
|
||||
ProcessHelper.getValue(validationRecord, i))
|
||||
ProcessHelper.GetValue(processedRecord,i) !=
|
||||
ProcessHelper.GetValue(validationRecord, i))
|
||||
{
|
||||
throw new Exception($"ValidationError: Code {baseCode!}, " +
|
||||
$"Value{i} ({ProcessHelper.getValue(processedRecord, i)} | " +
|
||||
$"{ProcessHelper.getValue(validationRecord, i)})");
|
||||
$"Value{i} ({ProcessHelper.GetValue(processedRecord, i)} | " +
|
||||
$"{ProcessHelper.GetValue(validationRecord, i)})");
|
||||
}
|
||||
}
|
||||
newRecords.Add(processedRecord);
|
||||
|
||||
@@ -96,7 +96,7 @@ namespace WebAPI.dataProcessors
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i,
|
||||
codeRecords.Sum(x => ProcessHelper.getValue(x, i)));
|
||||
codeRecords.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
}
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user