From 5cb362b5e0b0ac279059aa7024cf8f315a9dd230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Sun, 17 Sep 2023 13:00:24 +0200 Subject: [PATCH] WIP: process layers --- WebAPI/Controllers/LayersController.cs | 116 ++++++++++++++++-- WebAPI/Controllers/LogsController.cs | 3 + WebAPI/DiunaBI-WebAPI.csproj | 6 + WebAPI/Models/Layer.cs | 2 + WebAPI/Models/LogEntry.cs | 3 +- WebAPI/dataProcessors/copy.processor.cs | 62 ++++++++++ .../dataProcessors/deaggregation.processor.cs | 22 ++++ 7 files changed, 200 insertions(+), 14 deletions(-) create mode 100644 WebAPI/dataProcessors/copy.processor.cs create mode 100644 WebAPI/dataProcessors/deaggregation.processor.cs diff --git a/WebAPI/Controllers/LayersController.cs b/WebAPI/Controllers/LayersController.cs index aed1717..4f99400 100644 --- a/WebAPI/Controllers/LayersController.cs +++ b/WebAPI/Controllers/LayersController.cs @@ -172,29 +172,119 @@ namespace WebAPI.Controllers return BadRequest(e.ToString()); } return Ok(); - } - - - + } + [HttpGet] - [Route("autoImportMorska/{apiKey}")] + [Route("AutoProcess/{apiKey}")] [AllowAnonymous] - public IActionResult autoImportMorska(string apiKey) + public IActionResult AutoProcess(string apiKey) { if (Request.Host.Value != configuration["apiLocalUrl"] || apiKey != configuration["apiKey"]) { return Unauthorized(); } - morskaK5Parser parser = new morskaK5Parser(googleSheetValues, db); - Layer layer = parser.parse(); + List processWorkerLayers; + List layersToProcess; + try + { + processWorkerLayers = db.Layers + .Include(x => x.Records) + .Where(x => x.Records!.Any(x => x.Code == "Type" && x.Desc1 == "ProcessWorker")) + .ToList(); + layersToProcess = new List(); + + foreach (Layer layer in processWorkerLayers) + { + string? sourceName = layer?.Records?.Single(x => x.Code == "Source")?.Desc1; + if (sourceName == null) + { + logsController.AddEntry(new LogEntry + { + Title = "Process error", + Type = LogEntryType.error, + LogType = LogType.import, + Message = layer?.Name + " Source record not found", + CreatedAt = DateTime.UtcNow + }); + return BadRequest(); + } else + { + Layer sourceLayer = db.Layers.Single(x => x.Name == sourceName); + if (sourceLayer == null) + { + logsController.AddEntry(new LogEntry + { + Title = "Process error", + Type = LogEntryType.error, + LogType = LogType.import, + Message = layer?.Name + " Source layer not found " + sourceLayer, + CreatedAt = DateTime.UtcNow + }); + return BadRequest(); + } else + { + string startDate = sourceLayer.Records!.Where(x => x.Code == "StartDate").First().Desc1!; + string endDate = sourceLayer.Records!.Where(x => x.Code == "EndDate").First().Desc1!; + var startDateParsed = DateTime.ParseExact(startDate, "yyyy.MM.dd", null); + var endDateParsed = DateTime.ParseExact(endDate, "yyyy.MM.dd", null); + if (startDateParsed.Date <= DateTime.UtcNow.Date && endDateParsed.Date >= DateTime.UtcNow.Date) + { + string? processType = layer?.Records?.Single(x => x.Code == "ProcessType")?.Desc1; + if (processType == null) + { + logsController.AddEntry(new LogEntry + { + Title = "Process error", + Type = LogEntryType.error, + LogType = LogType.import, + Message = layer?.Name + " ProcessType record not found", + CreatedAt = DateTime.UtcNow + }); + return BadRequest(); + } else + { + switch (processType) + { + case "Copy": + break; + case "Deaggregate": + break; + } + } + } + } + } + //Layer sourceLayer = db.Layers.Single(x => x.Name == layer.Records.Single(y => y.Code == "Source").Desc1); - AddLayer(layer, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D")); - - return Ok("OK"); - } - + // string startDate = layer.Records!.Where(x => x.Code == "StartDate").First().Desc1!; + /// string endDate = layer.Records!.Where(x => x.Code == "EndDate").First().Desc1!; + // var startDateParsed = DateTime.ParseExact(startDate, "yyyy.MM.dd", null); + // var endDateParsed = DateTime.ParseExact(endDate, "yyyy.MM.dd", null); + // if (startDateParsed.Date <= DateTime.UtcNow.Date && endDateParsed.Date >= DateTime.UtcNow.Date) + // { + // processImportWorker(layer); + // } + } + + } + catch (Exception e) + { + logsController.AddEntry(new LogEntry + { + Title = "Process error", + Type = LogEntryType.error, + LogType = LogType.import, + Message = e.ToString(), + CreatedAt = DateTime.UtcNow + }); + return BadRequest(e.ToString()); + } + return Ok(); + } + + [HttpGet] [Route("checkDates")] public IActionResult checkDates() diff --git a/WebAPI/Controllers/LogsController.cs b/WebAPI/Controllers/LogsController.cs index 46d64a9..4944928 100644 --- a/WebAPI/Controllers/LogsController.cs +++ b/WebAPI/Controllers/LogsController.cs @@ -32,6 +32,9 @@ namespace WebAPI.Controllers case LogType.backup: type = "Backup"; break; + case LogType.process: + type = "Process"; + break; default: type = "Other"; // should never happen break; diff --git a/WebAPI/DiunaBI-WebAPI.csproj b/WebAPI/DiunaBI-WebAPI.csproj index 98b4dec..ac22cba 100644 --- a/WebAPI/DiunaBI-WebAPI.csproj +++ b/WebAPI/DiunaBI-WebAPI.csproj @@ -31,4 +31,10 @@ + + + + + + diff --git a/WebAPI/Models/Layer.cs b/WebAPI/Models/Layer.cs index 1afaccd..bbd404f 100644 --- a/WebAPI/Models/Layer.cs +++ b/WebAPI/Models/Layer.cs @@ -36,6 +36,8 @@ namespace WebAPI.Models [Required] public Guid ModifiedById { get; set; } public User? ModifiedBy { get; set; } + public Guid? parentId { get; set; } + public Layer? parent { get; set; } #endregion } } diff --git a/WebAPI/Models/LogEntry.cs b/WebAPI/Models/LogEntry.cs index a4d9193..487d7e6 100644 --- a/WebAPI/Models/LogEntry.cs +++ b/WebAPI/Models/LogEntry.cs @@ -10,7 +10,8 @@ namespace WebAPI.Models } public enum LogType { import, - backup + backup, + process } public class LogEntry { diff --git a/WebAPI/dataProcessors/copy.processor.cs b/WebAPI/dataProcessors/copy.processor.cs new file mode 100644 index 0000000..2304ad1 --- /dev/null +++ b/WebAPI/dataProcessors/copy.processor.cs @@ -0,0 +1,62 @@ +using Google.Apis.Sheets.v4; +using System.Globalization; +using WebAPI.Models; + +namespace WebAPI.dataParsers +{ + public class morskaK5Parser + { + private SpreadsheetsResource.ValuesResource googleSheetValues; + private AppDbContext db; + + public morskaK5Parser( + SpreadsheetsResource.ValuesResource _googleSheetValues, + AppDbContext _db) + { + googleSheetValues = _googleSheetValues; + db = _db; + } + + public Layer parse() + { + Layer layer = new Layer(); + layer.Source = "GoogleSheet"; + + string sheetId = "1ZzndU8HjYqz5VKCcrVHBOFW8fqpYfwquclznX9q39Yk"; + + var range = "Sierpien_2023!B3:AR5"; + + var request = googleSheetValues.Get(sheetId, range); + var response = request.Execute(); + var data = response.Values; + + layer.Source = "GoogleSheet"; + layer.Number = db.Layers.Count() + 1; + layer.Name = $"L{layer.Number}-I-{data[0][1]}-{data[0][2]}/{data[0][3]}-{DateTime.Now.ToString("yyyyMMddHHmm")}"; + layer.Type = LayerType.import; + + List records = new List(); + + for (int i = 1; i < data[1].Count; i++) + { + float value; + + if ( + data[1][i].ToString()?.Length > 0 && + float.TryParse(data[2][i].ToString(), CultureInfo.GetCultureInfo("pl-PL"), out value)) + { + Record record = new Record(); + record.Id = Guid.NewGuid(); + record.Code = data[1][i].ToString(); + record.Value1 = value; + record.CreatedAt = DateTime.UtcNow; + record.ModifiedAt = DateTime.UtcNow; + records.Add(record); + }; + + } + layer.Records = records; + return layer; + } + } +} diff --git a/WebAPI/dataProcessors/deaggregation.processor.cs b/WebAPI/dataProcessors/deaggregation.processor.cs new file mode 100644 index 0000000..0632de0 --- /dev/null +++ b/WebAPI/dataProcessors/deaggregation.processor.cs @@ -0,0 +1,22 @@ +using Google.Apis.Sheets.v4; +using System.Globalization; +using WebAPI.Models; + +namespace WebAPI.dataProcessors +{ + public class copyProcessor + { + private AppDbContext db; + + public copyProcessor( + AppDbContext _db) + { + db = _db; + } + + public Layer process() + { + return null; + } + } +}