Files
DiunaBI/WebAPI/Controllers/LayersController.cs

671 lines
26 KiB
C#
Raw Normal View History

2024-04-09 09:42:03 +02:00
using System.Globalization;
2024-06-06 20:25:20 +02:00
using System.Text;
2023-11-09 15:17:22 +01:00
using DiunaBIWebAPI.dataImporters;
2023-11-29 22:14:37 +01:00
using Google.Apis.Sheets.v4;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebAPI.dataProcessors;
using WebAPI.Exports;
using WebAPI.Models;
namespace WebAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class LayersController : Controller
{
2024-06-18 18:39:02 +02:00
private readonly AppDbContext _db;
private readonly SpreadsheetsResource.ValuesResource? _googleSheetValues;
private readonly GoogleDriveHelper _googleDriveHelper;
// private GoogleSheetsHelper _googleSheetsHelper;
private readonly IConfiguration _configuration;
private readonly LogsController _logsController;
2024-06-06 20:59:30 +02:00
2023-11-29 22:14:37 +01:00
public LayersController(
2024-06-18 18:39:02 +02:00
AppDbContext db,
GoogleSheetsHelper googleSheetsHelper,
GoogleDriveHelper googleDriveHelper,
IConfiguration configuration)
2023-11-29 22:14:37 +01:00
{
2024-06-18 18:39:02 +02:00
_db = db;
if (googleSheetsHelper.Service is not null)
2024-03-06 16:48:11 +01:00
{
2024-06-18 18:39:02 +02:00
_googleSheetValues = googleSheetsHelper.Service.Spreadsheets.Values;
2023-11-29 22:14:37 +01:00
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
//_googleSheetsHelper = googleSheetsHelper;
_googleDriveHelper = googleDriveHelper;
_configuration = configuration;
_logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration);
2023-11-29 22:14:37 +01:00
}
[HttpGet]
public IActionResult GetAll(int start, int limit, string? name, LayerType? type)
{
try
{
2024-06-18 18:39:02 +02:00
var response = _db.Layers.Where(x => !x.IsDeleted);
2023-11-29 22:14:37 +01:00
if (name != null)
{
2024-03-06 16:48:11 +01:00
response = response.Where(x => x.Name != null && x.Name.Contains(name));
2023-11-29 22:14:37 +01:00
}
2024-06-06 20:59:30 +02:00
2023-11-29 22:14:37 +01:00
if (type != null)
{
response = response.Where(x => x.Type == type);
}
return Ok(response
.OrderByDescending(x => x.Number)
.Skip(start).Take(limit).ToList());
}
catch (Exception e)
{
return BadRequest(e.ToString());
}
}
2024-06-06 20:59:30 +02:00
2023-11-29 22:14:37 +01:00
[HttpGet]
2024-03-06 16:48:11 +01:00
[Route("{id}")]
public IActionResult Get(Guid id)
2023-11-29 22:14:37 +01:00
{
try
{
2024-06-18 18:39:02 +02:00
return Ok(_db.Layers
2023-11-29 22:14:37 +01:00
.Include(x => x.CreatedBy)
2024-06-18 18:39:02 +02:00
.Include(x => x.Records).First(x => x.Id == id && !x.IsDeleted));
2023-11-29 22:14:37 +01:00
}
catch (Exception e)
{
return BadRequest(e.ToString());
}
}
2024-06-06 20:59:30 +02:00
2023-11-29 22:14:37 +01:00
[HttpGet]
2024-06-06 20:25:20 +02:00
[Route("getForPowerBI/{apiKey}/{number}")]
2023-12-26 11:42:51 +01:00
public IActionResult GetByNumber(string apiKey, int number)
{
2024-06-18 18:39:02 +02:00
if (apiKey != _configuration["apiKey"])
2023-12-26 11:42:51 +01:00
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2024-06-06 20:59:30 +02:00
{
Title = $"Unauthorized request - wrong apiKey ({number})",
Type = LogEntryType.warning,
LogType = LogType.powerBI,
CreatedAt = DateTime.UtcNow,
});
2023-12-26 11:42:51 +01:00
return Unauthorized();
}
2024-06-06 15:28:12 +02:00
2023-12-26 11:42:51 +01:00
try
{
2024-06-06 20:59:30 +02:00
if (
!Request.Headers.TryGetValue("Authorization", out var authHeader))
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2024-06-06 20:59:30 +02:00
{
Title = $"Unauthorized request - no authorization header ({number})",
Type = LogEntryType.warning,
LogType = LogType.powerBI,
CreatedAt = DateTime.UtcNow,
});
return Unauthorized();
}
2024-06-18 18:39:02 +02:00
var credentialsArr = authHeader.ToString().Split(" ");
2024-06-06 20:59:30 +02:00
if (credentialsArr.Length != 2)
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2024-06-06 20:59:30 +02:00
{
Title = $"Unauthorized request - wrong auth header format ({number})",
Type = LogEntryType.warning,
LogType = LogType.powerBI,
CreatedAt = DateTime.UtcNow,
});
return Unauthorized();
}
var authValue = Encoding.UTF8.GetString(Convert.FromBase64String(credentialsArr[1]));
var username = authValue.Split(':')[0];
var password = authValue.Split(':')[1];
2024-06-18 18:39:02 +02:00
if (username != _configuration["powerBI-user"] || password != _configuration["powerBI-pass"])
2024-06-06 20:59:30 +02:00
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2024-06-06 20:59:30 +02:00
{
Title = $"Unauthorized request - bad credentials ({number})",
Type = LogEntryType.warning,
LogType = LogType.powerBI,
CreatedAt = DateTime.UtcNow,
});
return Unauthorized();
}
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2024-06-06 20:59:30 +02:00
{
Title = $"Sending data for layer {number}",
Type = LogEntryType.info,
LogType = LogType.powerBI,
CreatedAt = DateTime.UtcNow,
});
2024-06-18 18:39:02 +02:00
return Ok(_db.Layers
2023-12-26 11:42:51 +01:00
.Include(x => x.CreatedBy)
2024-06-18 18:39:02 +02:00
.Include(x => x.Records).First(x => x.Number == number && !x.IsDeleted));
2023-12-26 11:42:51 +01:00
}
catch (Exception e)
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2024-06-06 20:59:30 +02:00
{
Title = e.ToString(),
Type = LogEntryType.error,
LogType = LogType.powerBI,
CreatedAt = DateTime.UtcNow,
});
2023-12-26 11:42:51 +01:00
return BadRequest(e.ToString());
}
}
2024-06-06 20:59:30 +02:00
2023-12-26 11:42:51 +01:00
[HttpGet]
2023-11-29 22:14:37 +01:00
[Route("exportToGoogleSheet/{id}")]
public IActionResult ExportToGoogleSheet(Guid id)
{
2024-06-18 18:39:02 +02:00
if (_googleSheetValues is null)
2024-03-06 16:48:11 +01:00
{
2023-11-29 22:14:37 +01:00
throw new Exception("Google Sheets API not initialized");
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
var layer = _db.Layers
.Include(x => x.Records!.OrderByDescending(y => y.Code)).First(x => x.Id == id && !x.IsDeleted);
2023-11-29 22:14:37 +01:00
2024-06-18 18:39:02 +02:00
var export = new googleSheetExport(_googleDriveHelper, _googleSheetValues, _configuration);
2023-11-29 22:14:37 +01:00
export.export(layer);
return Ok(true);
}
[HttpGet]
[Route("AutoImport/{apiKey}")]
[AllowAnonymous]
public IActionResult AutoImport(string apiKey)
{
2024-06-18 18:39:02 +02:00
if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"])
2023-11-29 22:14:37 +01:00
{
return Unauthorized();
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
if (_googleSheetValues is null)
2024-03-06 16:48:11 +01:00
{
2023-11-29 22:14:37 +01:00
throw new Exception("Google Sheets API not initialized");
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
var importWorkerLayers = _db.Layers
.Include(x => x.Records)
.Where(x =>
x.Records!.Any(y => y.Code == "Type" && y.Desc1 == "ImportWorker") &&
x.Records!.Any(y => y.Code == "IsEnabled" && y.Desc1 == "True")
//&& x.Records!.Any(x => x.Code == "ImportType" && x.Desc1 == "FK2")
)
.OrderBy(x => x.CreatedAt)
.ToList();
2023-11-09 15:17:22 +01:00
try
{
2024-06-18 18:39:02 +02:00
if (!importWorkerLayers.Any())
2023-11-09 15:17:22 +01:00
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2023-11-09 15:17:22 +01:00
{
Title = "No Layers to import.",
Type = LogEntryType.info,
LogType = LogType.import,
CreatedAt = DateTime.UtcNow,
2023-11-29 22:14:37 +01:00
});
2023-11-09 15:17:22 +01:00
return Ok();
}
2024-06-18 18:39:02 +02:00
foreach (var importWorker in importWorkerLayers)
2023-11-09 15:17:22 +01:00
{
try
{
2024-06-18 18:39:02 +02:00
var type = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportType")?.Desc1 ??
"Standard";
2024-06-06 14:33:08 +02:00
if (type == "FK2")
{
2024-06-18 18:39:02 +02:00
var importer = new MorskaFk2Importer(_db, _googleSheetValues, this);
2024-06-06 14:33:08 +02:00
importer.import(importWorker);
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2024-06-06 14:33:08 +02:00
{
2024-06-18 18:39:02 +02:00
Title = $"{importWorker.Name}, {importWorker.Id}",
2024-06-06 14:33:08 +02:00
Type = LogEntryType.info,
LogType = LogType.import,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
}
2024-06-06 16:05:19 +02:00
else
2023-11-09 15:17:22 +01:00
{
2024-06-18 18:39:02 +02:00
var startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1;
2024-06-06 16:05:19 +02:00
if (startDate == null)
2023-11-09 15:17:22 +01:00
{
2024-06-06 16:05:19 +02:00
throw new Exception("StartDate record nod found");
}
2024-06-18 18:39:02 +02:00
var endDate = importWorker.Records!.First(x => x.Code == "EndDate").Desc1;
2024-06-06 16:05:19 +02:00
if (endDate == null)
2023-11-09 15:17:22 +01:00
{
2024-06-06 16:05:19 +02:00
throw new Exception("EndDate record nod found");
}
2024-06-18 18:39:02 +02:00
var startDateParsed = DateTime.ParseExact(startDate, "yyyy.MM.dd", null);
var endDateParsed = DateTime.ParseExact(endDate, "yyyy.MM.dd", null);
2024-06-06 16:05:19 +02:00
if (startDateParsed.Date <= DateTime.UtcNow.Date &&
endDateParsed.Date >= DateTime.UtcNow.Date)
{
2024-06-18 18:39:02 +02:00
var importer = new MorskaImporter(_db, _googleSheetValues, this);
2024-06-06 16:05:19 +02:00
importer.import(importWorker);
Thread.Sleep(5000); // be aware of GSheet API quota
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2024-06-06 16:05:19 +02:00
{
2024-06-18 18:39:02 +02:00
Title = $"{importWorker.Name}, {importWorker.Id}",
2024-06-06 16:05:19 +02:00
Type = LogEntryType.info,
LogType = LogType.import,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
}
else if (IsImportedLayerUpToDate(importWorker) == false)
{
2024-06-18 18:39:02 +02:00
MorskaImporter importer = new MorskaImporter(_db, _googleSheetValues, this);
2024-06-06 16:05:19 +02:00
importer.import(importWorker);
Thread.Sleep(5000); // be aware of GSheet API quota
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2024-06-06 16:05:19 +02:00
{
2024-06-18 18:39:02 +02:00
Title = $"{importWorker.Name}, {importWorker.Id}",
2024-06-06 16:05:19 +02:00
Type = LogEntryType.warning,
LogType = LogType.import,
Message = "Success (reimported)",
CreatedAt = DateTime.UtcNow
});
}
else
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2024-06-06 16:05:19 +02:00
{
2024-06-18 18:39:02 +02:00
Title = $"{importWorker.Name}, {importWorker.Id}",
2024-06-06 16:05:19 +02:00
Type = LogEntryType.warning,
LogType = LogType.import,
Message = "importLayer records are up of date. Not processed.",
CreatedAt = DateTime.UtcNow
});
}
2023-11-09 15:17:22 +01:00
}
2024-03-06 16:48:11 +01:00
}
catch (Exception e)
2023-11-09 15:17:22 +01:00
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2023-11-09 15:17:22 +01:00
{
2024-06-18 18:39:02 +02:00
Title = $"{importWorker.Name}, {importWorker.Id}",
2023-11-09 15:17:22 +01:00
Type = LogEntryType.error,
LogType = LogType.import,
Message = e.ToString(),
CreatedAt = DateTime.UtcNow
});
}
}
2024-06-06 20:59:30 +02:00
2023-11-09 15:17:22 +01:00
return Ok();
2024-03-06 16:48:11 +01:00
}
catch (Exception e)
2023-11-09 15:17:22 +01:00
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2023-11-29 22:14:37 +01:00
{
Title = "Process error",
Type = LogEntryType.error,
LogType = LogType.import,
Message = e.ToString(),
CreatedAt = DateTime.UtcNow
});
2023-11-09 15:17:22 +01:00
return BadRequest(e.ToString());
2023-11-29 22:14:37 +01:00
}
}
2024-06-06 20:59:30 +02:00
2023-11-29 22:14:37 +01:00
[HttpGet]
[Route("AutoProcess/{apiKey}")]
2023-11-29 22:14:37 +01:00
[AllowAnonymous]
public IActionResult AutoProcess(string apiKey)
2023-11-29 22:14:37 +01:00
{
2024-06-18 18:39:02 +02:00
if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"])
2023-11-29 22:14:37 +01:00
{
return Unauthorized();
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
if (_googleSheetValues is null)
2024-03-06 16:48:11 +01:00
{
2023-11-29 22:14:37 +01:00
throw new Exception("Google Sheets API not initialized");
}
2024-06-18 18:39:02 +02:00
string[] processTypes =
2024-06-06 20:59:30 +02:00
{
"T3-SingleSource",
2024-04-04 22:26:34 +02:00
"T3-SourceYearSummary",
2024-05-22 18:40:10 +02:00
"T3-MultiSourceSummary", // AA
"T3-MultiSourceYearSummary", // AA/13
2024-06-11 20:50:55 +02:00
"T4-SingleSource",
2024-06-17 22:28:40 +02:00
"T1-R1",
"T4-R2"
2024-06-06 20:59:30 +02:00
};
2023-11-29 22:14:37 +01:00
foreach (string type in processTypes)
{
try
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
{
Title = $"Processing: {type}",
Type = LogEntryType.info,
LogType = LogType.process,
CreatedAt = DateTime.UtcNow,
2023-11-29 22:14:37 +01:00
});
2024-06-18 18:39:02 +02:00
List<Layer> processWorkerLayers = _db.Layers
.Include(x => x.Records)
.Where(x =>
2024-06-18 18:39:02 +02:00
x.Records!.Any(y => y.Code == "Type" && y.Desc1 == "ProcessWorker") &&
x.Records!.Any(y => y.Code == "IsEnabled" && y.Desc1 == "True") &&
x.Records!.Any(y => y.Code == "ProcessType" && y.Desc1 == type)
2024-06-06 20:59:30 +02:00
)
.OrderBy(x => x.CreatedAt)
.ToList();
2024-06-18 18:39:02 +02:00
if (!processWorkerLayers.Any())
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
{
Title = "No Layers to process.",
Type = LogEntryType.info,
LogType = LogType.process,
CreatedAt = DateTime.UtcNow,
});
2024-03-06 16:48:11 +01:00
}
foreach (Layer processWorker in processWorkerLayers)
{
try
{
ProcessLayer(processWorker);
}
catch (Exception e)
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
{
2024-06-18 18:39:02 +02:00
Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.error,
LogType = LogType.process,
Message = e.ToString(),
CreatedAt = DateTime.UtcNow
});
}
}
2024-03-06 16:48:11 +01:00
}
catch (Exception e)
2024-03-06 16:48:11 +01:00
{
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
{
Title = "Process error",
Type = LogEntryType.error,
LogType = LogType.process,
Message = e.ToString(),
CreatedAt = DateTime.UtcNow
});
}
2024-03-06 16:48:11 +01:00
}
2024-06-06 20:59:30 +02:00
return Ok();
2024-03-06 16:48:11 +01:00
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
private void ProcessLayer(Layer processWorker)
2024-03-06 16:48:11 +01:00
{
2024-06-18 18:39:02 +02:00
if (_googleSheetValues == null)
2024-03-10 10:47:11 +01:00
{
throw new Exception("Google Sheets API not initialized");
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
var year = processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1;
2024-03-06 16:48:11 +01:00
if (year == null)
{
throw new Exception("Year record nod found");
}
2024-06-18 18:39:02 +02:00
var processType = processWorker.Records?.SingleOrDefault(x => x.Code == "ProcessType")?.Desc1;
switch (processType)
2024-03-06 16:48:11 +01:00
{
2024-06-18 18:39:02 +02:00
case null:
throw new Exception("ProcessType record not found");
case "T3-SourceYearSummary":
2024-03-06 16:48:11 +01:00
{
2024-06-18 18:39:02 +02:00
T3SourceYearSummaryProcessor processor =
new T3SourceYearSummaryProcessor(_db, _googleSheetValues, this);
processor.process(processWorker);
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
{
Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.info,
LogType = LogType.process,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
return;
}
case "T3-MultiSourceYearSummary":
2024-03-06 16:48:11 +01:00
{
2024-06-18 18:39:02 +02:00
T3MultiSourceYearSummaryProcessor processor =
new T3MultiSourceYearSummaryProcessor(_db, _googleSheetValues, this, _logsController);
processor.process(processWorker);
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
{
Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.info,
LogType = LogType.process,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
return;
}
case "T3-MultiSourceCopySelectedCodesYearSummary":
{
2024-06-18 18:39:02 +02:00
T3MultiSourceCopySelectedCodesYearSummaryProcessor processor =
new T3MultiSourceCopySelectedCodesYearSummaryProcessor(_db, _googleSheetValues, this);
processor.process(processWorker);
2024-05-06 20:07:21 +02:00
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
{
Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.info,
LogType = LogType.process,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
return;
}
case "T1-R1":
2024-05-06 20:07:21 +02:00
{
2024-06-18 18:39:02 +02:00
T1R1Processor processor = new T1R1Processor(_db, _googleSheetValues, this, _logsController);
processor.process(processWorker);
_logsController.AddEntry(new LogEntry
{
Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.info,
LogType = LogType.process,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
return;
}
case "T4-R2":
2024-06-17 22:28:40 +02:00
{
2024-06-18 18:39:02 +02:00
var processor = new T4R2Processor(_db, _googleSheetValues, this, _logsController);
processor.process(processWorker);
_logsController.AddEntry(new LogEntry
{
Title = $"{processWorker.Name}, {processWorker.Id}",
Type = LogEntryType.info,
LogType = LogType.process,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
return;
}
2024-05-06 20:07:21 +02:00
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
var month = processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1;
2024-03-06 16:48:11 +01:00
if (month == null)
{
throw new Exception("Month record not found");
}
2024-06-06 20:59:30 +02:00
2024-03-06 16:48:11 +01:00
switch (processType!)
{
case "T3-SingleSource":
2024-06-06 20:59:30 +02:00
{
2024-06-18 18:39:02 +02:00
T3SingleSourceProcessor processor = new T3SingleSourceProcessor(_db, _googleSheetValues, this);
processor.process(processWorker);
2024-06-11 20:50:55 +02:00
break;
}
case "T4-SingleSource":
{
2024-06-18 18:39:02 +02:00
T4SingleSourceProcessor processor = new T4SingleSourceProcessor(_db, _googleSheetValues, this);
processor.process(processWorker);
2024-06-06 20:59:30 +02:00
break;
}
2024-03-06 16:48:11 +01:00
case "T3-MultiSourceSummary":
2024-06-06 20:59:30 +02:00
{
T3MultiSourceSummaryProcessor processor =
2024-06-18 18:39:02 +02:00
new T3MultiSourceSummaryProcessor(_db, _googleSheetValues, this, _logsController);
processor.process(processWorker);
2024-06-06 20:59:30 +02:00
break;
}
2024-03-06 16:48:11 +01:00
case "T3-MultiSourceCopySelectedCodes":
2024-06-06 20:59:30 +02:00
{
T3MultiSourceCopySelectedCodesProcessor processor =
2024-06-18 18:39:02 +02:00
new T3MultiSourceCopySelectedCodesProcessor(_db, _googleSheetValues, this);
processor.process(processWorker);
2024-06-06 20:59:30 +02:00
break;
}
2024-03-06 16:48:11 +01:00
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
_logsController.AddEntry(new LogEntry
2024-03-06 16:48:11 +01:00
{
2024-06-18 18:39:02 +02:00
Title = $"{processWorker.Name}, {processWorker.Id}",
2024-03-06 16:48:11 +01:00
Type = LogEntryType.info,
LogType = LogType.process,
Message = "Success",
CreatedAt = DateTime.UtcNow
});
}
2024-06-06 20:59:30 +02:00
2023-11-29 22:14:37 +01:00
internal void SaveRecords(Guid id, ICollection<Record> records, Guid currentUserId)
{
2024-06-18 18:39:02 +02:00
List<Record> toDelete = _db.Records.Where(x => x.LayerId == id).ToList();
if (toDelete.Count > 0)
2023-11-29 22:14:37 +01:00
{
2024-06-18 18:39:02 +02:00
_db.Records.RemoveRange(toDelete);
2023-11-29 22:14:37 +01:00
}
2024-06-18 18:39:02 +02:00
foreach (Record record in records)
2023-11-29 22:14:37 +01:00
{
2024-06-18 18:39:02 +02:00
record.CreatedById = currentUserId;
record.CreatedAt = DateTime.UtcNow;
record.ModifiedById = currentUserId;
record.ModifiedAt = DateTime.UtcNow;
record.LayerId = id;
_db.Records.Add(record);
2023-11-29 22:14:37 +01:00
}
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
private void WriteToConsole(params string[] messages)
{
2024-06-18 18:39:02 +02:00
foreach (var message in messages)
{
Console.WriteLine($"DiunaLog: {message}");
}
}
2024-06-06 20:59:30 +02:00
2024-06-06 14:33:08 +02:00
private bool IsImportedLayerUpToDate(Layer importWorker)
{
2024-06-18 18:39:02 +02:00
if (_googleSheetValues is null)
{
throw new Exception("Google Sheets API not initialized");
}
2024-06-18 18:39:02 +02:00
var newestLayer = _db.Layers
.Include(x => x.Records)
.Where(x => x.ParentId == importWorker.Id)
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault();
if (newestLayer is null)
{
return true; // importWorker is not active yet, no check needed
}
2024-06-18 18:39:02 +02:00
var sheetId = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetId")?.Desc1;
if (sheetId == null)
{
throw new Exception($"SheetId not found, {importWorker.Name}");
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
var sheetTabName = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetTabName")?.Desc1;
if (sheetTabName == null)
{
throw new Exception($"SheetTabName not found, {importWorker.Name}");
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
var dataRange = importWorker.Records!.FirstOrDefault(x => x.Code == "DataRange")?.Desc1;
if (dataRange == null)
{
throw new Exception($"DataRange not found, {importWorker.Name}");
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
var dataRangeResponse = _googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute();
var data = dataRangeResponse.Values;
2024-06-18 18:39:02 +02:00
var isUpToDate = true;
2024-06-18 18:39:02 +02:00
for (var i = 0; i < data[1].Count; i++)
{
2024-06-18 18:39:02 +02:00
if (data[0][i].ToString() == "") continue;
var record = newestLayer.Records!.FirstOrDefault(x => x.Code == data[0][i].ToString());
if (record == null)
{
2024-06-18 18:39:02 +02:00
WriteToConsole("Code not found in DiunaBI", data[0][i].ToString()!);
isUpToDate = false;
continue;
}
2024-06-06 20:59:30 +02:00
2024-06-18 18:39:02 +02:00
if ((!double.TryParse(data[1][i].ToString(), CultureInfo.GetCultureInfo("pl-PL"),
out var value) ||
double.Abs((double)(record.Value1-value)!) > 0.01)) continue;
WriteToConsole(
$"Code: {data[0][i]}. DiunaBI: {record.Value1:N2}. GoogleSheet: {data[1][i]}");
isUpToDate = false;
}
foreach (var record in newestLayer.Records!)
{
if (data[0].Contains(record.Code))
{
continue;
}
2024-06-06 20:59:30 +02:00
WriteToConsole($"Code not found in GoogleSheet: {record.Code}");
isUpToDate = false;
}
return isUpToDate;
}
2023-11-29 22:14:37 +01:00
}
2022-12-11 23:40:16 +01:00
}