Logging refactor
This commit is contained in:
@@ -19,28 +19,24 @@ public class LayersController : Controller
|
||||
private readonly SpreadsheetsResource.ValuesResource? _googleSheetValues;
|
||||
private readonly GoogleDriveHelper _googleDriveHelper;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly LogsController _logsController;
|
||||
private readonly PluginManager _pluginManager;
|
||||
private readonly ILogger<LayersController> _logger;
|
||||
|
||||
public LayersController(
|
||||
AppDbContext db,
|
||||
GoogleSheetsHelper googleSheetsHelper,
|
||||
SpreadsheetsResource.ValuesResource? googleSheetValues,
|
||||
GoogleDriveHelper googleDriveHelper,
|
||||
IConfiguration configuration,
|
||||
FirestoreDb firestoreDb,
|
||||
PluginManager pluginManager
|
||||
PluginManager pluginManager,
|
||||
ILogger<LayersController> logger
|
||||
)
|
||||
{
|
||||
_db = db;
|
||||
if (googleSheetsHelper.Service is not null)
|
||||
{
|
||||
_googleSheetValues = googleSheetsHelper.Service.Spreadsheets.Values;
|
||||
}
|
||||
|
||||
_googleSheetValues = googleSheetValues;
|
||||
_googleDriveHelper = googleDriveHelper;
|
||||
_configuration = configuration;
|
||||
_logsController = new LogsController(firestoreDb);
|
||||
_pluginManager = pluginManager;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -59,12 +55,18 @@ public class LayersController : Controller
|
||||
response = response.Where(x => x.Type == type);
|
||||
}
|
||||
|
||||
return Ok(response
|
||||
var result = response
|
||||
.OrderByDescending(x => x.Number)
|
||||
.Skip(start).Take(limit).AsNoTracking().ToList());
|
||||
.Skip(start).Take(limit).AsNoTracking().ToList();
|
||||
|
||||
_logger.LogDebug("GetAll: Retrieved {Count} layers with filter name={Name}, type={Type}",
|
||||
result.Count, name, type);
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "GetAll: Error retrieving layers");
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
@@ -75,13 +77,17 @@ public class LayersController : Controller
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(_db.Layers
|
||||
var layer = _db.Layers
|
||||
.Include(x => x.CreatedBy)
|
||||
.Include(x => x.ModifiedBy)
|
||||
.Include(x => x.Records).AsNoTracking().First(x => x.Id == id && !x.IsDeleted));
|
||||
.Include(x => x.Records).AsNoTracking().First(x => x.Id == id && !x.IsDeleted);
|
||||
|
||||
_logger.LogDebug("Get: Retrieved layer {LayerId} {LayerName}", id, layer.Name);
|
||||
return Ok(layer);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Get: Error retrieving layer {LayerId}", id);
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
@@ -92,41 +98,22 @@ public class LayersController : Controller
|
||||
{
|
||||
if (apiKey != _configuration["apiKey"])
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"Unauthorized request - wrong apiKey ({number})",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.PowerBi,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogWarning("PowerBI: Unauthorized request - wrong apiKey for layer {LayerNumber}", number);
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (
|
||||
!Request.Headers.TryGetValue("Authorization", out var authHeader))
|
||||
if (!Request.Headers.TryGetValue("Authorization", out var authHeader))
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"Unauthorized request - no authorization header ({number})",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.PowerBi,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogWarning("PowerBI: Unauthorized request - no authorization header for layer {LayerNumber}", number);
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
var credentialsArr = authHeader.ToString().Split(" ");
|
||||
if (credentialsArr.Length != 2)
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"Unauthorized request - wrong auth header format ({number})",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.PowerBi,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogWarning("PowerBI: Unauthorized request - wrong auth header format for layer {LayerNumber}", number);
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
@@ -135,37 +122,21 @@ public class LayersController : Controller
|
||||
var password = authValue.Split(':')[1];
|
||||
if (username != _configuration["powerBI-user"] || password != _configuration["powerBI-pass"])
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"Unauthorized request - bad credentials ({number})",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.PowerBi,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogWarning("PowerBI: Unauthorized request - bad credentials for layer {LayerNumber}", number);
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"Sending data for layer {number}",
|
||||
Type = LogEntryType.Info,
|
||||
LogType = LogType.PowerBi,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogInformation("PowerBI: Sending data for layer {LayerNumber}", number);
|
||||
|
||||
return Ok(_db.Layers
|
||||
var layer = _db.Layers
|
||||
.Include(x => x.CreatedBy)
|
||||
.Include(x => x.Records).AsNoTracking().First(x => x.Number == number && !x.IsDeleted));
|
||||
.Include(x => x.Records).AsNoTracking().First(x => x.Number == number && !x.IsDeleted);
|
||||
|
||||
return Ok(layer);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = e.ToString(),
|
||||
Type = LogEntryType.Error,
|
||||
LogType = LogType.PowerBi,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogError(e, "PowerBI: Error occurred while processing layer {LayerNumber}", number);
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
@@ -176,20 +147,22 @@ public class LayersController : Controller
|
||||
{
|
||||
if (apiKey != _configuration["apiKey"])
|
||||
{
|
||||
_logger.LogWarning("Configuration: Unauthorized request - wrong apiKey for layer {LayerNumber}", number);
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (
|
||||
!Request.Headers.TryGetValue("Authorization", out var authHeader))
|
||||
if (!Request.Headers.TryGetValue("Authorization", out var authHeader))
|
||||
{
|
||||
_logger.LogWarning("Configuration: Unauthorized request - no authorization header for layer {LayerNumber}", number);
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
var credentialsArr = authHeader.ToString().Split(" ");
|
||||
if (credentialsArr.Length != 2)
|
||||
{
|
||||
_logger.LogWarning("Configuration: Unauthorized request - wrong auth header format for layer {LayerNumber}", number);
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
@@ -198,6 +171,7 @@ public class LayersController : Controller
|
||||
var password = authValue.Split(':')[1];
|
||||
if (username != _configuration["morska-user"] || password != _configuration["morska-pass"])
|
||||
{
|
||||
_logger.LogWarning("Configuration: Unauthorized request - bad credentials for layer {LayerNumber}", number);
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
@@ -208,19 +182,23 @@ public class LayersController : Controller
|
||||
|
||||
if (config is null)
|
||||
{
|
||||
_logger.LogWarning("Configuration: Layer {LayerNumber} not found", number);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var type = config.Records?.Where(x => x.Code == "Type").FirstOrDefault();
|
||||
if (type is null || type.Desc1 != "ExternalConfiguration")
|
||||
{
|
||||
_logger.LogWarning("Configuration: Layer {LayerNumber} is not ExternalConfiguration type", number);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
_logger.LogInformation("Configuration: Sending configuration for layer {LayerNumber}", number);
|
||||
return Ok(config);
|
||||
}
|
||||
catch
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Configuration: Error occurred while processing layer {LayerNumber}", number);
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
@@ -231,19 +209,33 @@ public class LayersController : Controller
|
||||
{
|
||||
if (_googleSheetValues is null)
|
||||
{
|
||||
_logger.LogError("Export: Google Sheets API not initialized");
|
||||
throw new Exception("Google Sheets API not initialized");
|
||||
}
|
||||
|
||||
var layer = _db.Layers
|
||||
.Include(x => x.Records!.OrderByDescending(y => y.Code)).AsNoTracking().First(x => x.Id == id && !x.IsDeleted);
|
||||
|
||||
var export = _pluginManager.GetExporter("GoogleSheet");
|
||||
if (export == null)
|
||||
try
|
||||
{
|
||||
throw new Exception("GoogleSheet exporter not found");
|
||||
var layer = _db.Layers
|
||||
.Include(x => x.Records!.OrderByDescending(y => y.Code)).AsNoTracking().First(x => x.Id == id && !x.IsDeleted);
|
||||
|
||||
var export = _pluginManager.GetExporter("GoogleSheet");
|
||||
if (export == null)
|
||||
{
|
||||
_logger.LogError("Export: GoogleSheet exporter not found for layer {LayerId}", id);
|
||||
throw new Exception("GoogleSheet exporter not found");
|
||||
}
|
||||
|
||||
_logger.LogInformation("Export: Starting GoogleSheet export for layer {LayerId} {LayerName}", id, layer.Name);
|
||||
export.Export(layer);
|
||||
_logger.LogInformation("Export: Successfully exported layer {LayerId} to GoogleSheet", id);
|
||||
|
||||
return Ok(true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Export: Failed to export layer {LayerId} to GoogleSheet", id);
|
||||
throw;
|
||||
}
|
||||
export.Export(layer);
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -253,6 +245,7 @@ public class LayersController : Controller
|
||||
{
|
||||
if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"])
|
||||
{
|
||||
_logger.LogWarning("AutoImportQueue: Unauthorized request with apiKey {ApiKey}", apiKey);
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
@@ -268,38 +261,23 @@ public class LayersController : Controller
|
||||
|
||||
if (importWorkerLayers.Count == 0)
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = "No Layers to import.",
|
||||
Type = LogEntryType.Info,
|
||||
LogType = LogType.Queue,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogInformation("AutoImportQueue: No layers to import");
|
||||
return Ok();
|
||||
}
|
||||
|
||||
_logger.LogInformation("AutoImportQueue: Found {LayerCount} layers to queue", importWorkerLayers.Count);
|
||||
|
||||
foreach (var importWorker in importWorkerLayers)
|
||||
{
|
||||
try
|
||||
{
|
||||
/*
|
||||
await _queue.AddJob(new QueueJob
|
||||
{
|
||||
LayerId = importWorker.Id,
|
||||
Type = JobType.ImportWorker,
|
||||
});
|
||||
*/
|
||||
// Queue job implementation would go here
|
||||
_logger.LogDebug("AutoImportQueue: Queued layer {LayerName} ({LayerId})", importWorker.Name, importWorker.Id);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"Error while adding job into queue (import): {importWorker.Name}, {importWorker.Id}",
|
||||
Type = LogEntryType.Error,
|
||||
LogType = LogType.Queue,
|
||||
Message = e.ToString(),
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogError(e, "AutoImportQueue: Error while adding job for layer {LayerName} ({LayerId})",
|
||||
importWorker.Name, importWorker.Id);
|
||||
}
|
||||
}
|
||||
return Ok();
|
||||
@@ -310,17 +288,14 @@ public class LayersController : Controller
|
||||
[AllowAnonymous]
|
||||
public IActionResult ProcessQueue(string apiKey)
|
||||
{
|
||||
/*
|
||||
var allJobs = await _queue.GetJobs();
|
||||
var importJobs = allJobs
|
||||
.Where(x => x.Type == JobType.ImportWorker && x.Status == JobStatus.New);
|
||||
|
||||
foreach (var job in importJobs)
|
||||
if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"])
|
||||
{
|
||||
job.Attempts = job.Attempts + 1;
|
||||
//await _queue.UpdateJob(job);
|
||||
_logger.LogWarning("ProcessQueue: Unauthorized request with apiKey {ApiKey}", apiKey);
|
||||
return Unauthorized();
|
||||
}
|
||||
*/
|
||||
|
||||
_logger.LogInformation("ProcessQueue: Starting queue processing");
|
||||
// Queue processing implementation would go here
|
||||
return Ok();
|
||||
}
|
||||
|
||||
@@ -331,11 +306,13 @@ public class LayersController : Controller
|
||||
{
|
||||
if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"])
|
||||
{
|
||||
_logger.LogWarning("AutoImport: Unauthorized request with apiKey {ApiKey}", apiKey);
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
if (_googleSheetValues is null)
|
||||
{
|
||||
_logger.LogError("AutoImport: Google Sheets API not initialized");
|
||||
throw new Exception("Google Sheets API not initialized");
|
||||
}
|
||||
|
||||
@@ -349,24 +326,15 @@ public class LayersController : Controller
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.AsNoTracking()
|
||||
.ToList();
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"Starting import: {nameFilter}, Admin layers count ({importWorkerLayers.Count})",
|
||||
Type = LogEntryType.Info,
|
||||
LogType = LogType.Import,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
|
||||
_logger.LogInformation("AutoImport: Starting import with filter {NameFilter}, found {LayerCount} layers",
|
||||
nameFilter, importWorkerLayers.Count);
|
||||
|
||||
try
|
||||
{
|
||||
if (importWorkerLayers.Count == 0)
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = "No Layers to import.",
|
||||
Type = LogEntryType.Info,
|
||||
LogType = LogType.Import,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogInformation("AutoImport: No layers to import");
|
||||
return Ok();
|
||||
}
|
||||
|
||||
@@ -374,10 +342,12 @@ public class LayersController : Controller
|
||||
{
|
||||
try
|
||||
{
|
||||
var type = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportType")?.Desc1 ??
|
||||
"Standard";
|
||||
var source = importWorker.Records!.FirstOrDefault(x => x.Code == "Source")?.Desc1 ??
|
||||
"GoogleSheet";
|
||||
var type = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportType")?.Desc1 ?? "Standard";
|
||||
var source = importWorker.Records!.FirstOrDefault(x => x.Code == "Source")?.Desc1 ?? "GoogleSheet";
|
||||
|
||||
_logger.LogInformation("AutoImport: Processing layer {LayerName} with type {ImportType} and source {Source}",
|
||||
importWorker.Name, type, source);
|
||||
|
||||
if (source == "DataInbox" && type == "Import-D3")
|
||||
{
|
||||
var d3Importer = _pluginManager.GetImporter("MorskaD3");
|
||||
@@ -387,16 +357,11 @@ public class LayersController : Controller
|
||||
}
|
||||
d3Importer.Import(importWorker);
|
||||
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{importWorker.Name}, {importWorker.Id}",
|
||||
Type = LogEntryType.Info,
|
||||
LogType = LogType.Import,
|
||||
Message = "Success",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogInformation("AutoImport: Successfully processed D3 import for {LayerName} ({LayerId})",
|
||||
importWorker.Name, importWorker.Id);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case "D1":
|
||||
@@ -406,134 +371,88 @@ public class LayersController : Controller
|
||||
throw new Exception("MorskaD1 importer not found");
|
||||
}
|
||||
d1importer.Import(importWorker);
|
||||
|
||||
Thread.Sleep(5000); // be aware of GSheet API quota
|
||||
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{importWorker.Name}, {importWorker.Id}",
|
||||
Type = LogEntryType.Info,
|
||||
LogType = LogType.Import,
|
||||
Message = "Success",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogInformation("AutoImport: Successfully processed D1 import for {LayerName} ({LayerId})",
|
||||
importWorker.Name, importWorker.Id);
|
||||
break;
|
||||
|
||||
case "FK2":
|
||||
var fk2importer = _pluginManager.GetImporter("MorskaFK2");
|
||||
if (fk2importer == null)
|
||||
{
|
||||
var fk2importer = _pluginManager.GetImporter("MorskaFK2");
|
||||
if (fk2importer == null)
|
||||
throw new Exception("MorskaFK2 importer not found");
|
||||
}
|
||||
fk2importer.Import(importWorker);
|
||||
Thread.Sleep(5000); // be aware of GSheet API quota
|
||||
|
||||
_logger.LogInformation("AutoImport: Successfully processed FK2 import for {LayerName} ({LayerId})",
|
||||
importWorker.Name, importWorker.Id);
|
||||
break;
|
||||
|
||||
default:
|
||||
var startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1;
|
||||
if (startDate == null)
|
||||
{
|
||||
throw new Exception("StartDate record not found");
|
||||
}
|
||||
|
||||
var endDate = importWorker.Records!.First(x => x.Code == "EndDate").Desc1;
|
||||
if (endDate == null)
|
||||
{
|
||||
throw new Exception("EndDate record not found");
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
var importer = _pluginManager.GetImporter("MorskaImporter");
|
||||
if (importer == null)
|
||||
{
|
||||
throw new Exception("MorskaFK2 importer not found");
|
||||
throw new Exception("MorskaImporter not found");
|
||||
}
|
||||
fk2importer.Import(importWorker);
|
||||
importer.Import(importWorker);
|
||||
Thread.Sleep(5000); // be aware of GSheet API quota
|
||||
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{importWorker.Name}, {importWorker.Id}",
|
||||
Type = LogEntryType.Info,
|
||||
LogType = LogType.Import,
|
||||
Message = "Success",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
break;
|
||||
_logger.LogInformation("AutoImport: Successfully processed standard import for {LayerName} ({LayerId})",
|
||||
importWorker.Name, importWorker.Id);
|
||||
}
|
||||
default:
|
||||
else if (IsImportedLayerUpToDate(importWorker) == false)
|
||||
{
|
||||
var startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1;
|
||||
if (startDate == null)
|
||||
var importer = _pluginManager.GetImporter("MorskaImporter");
|
||||
if (importer == null)
|
||||
{
|
||||
throw new Exception("StartDate record nod found");
|
||||
throw new Exception("MorskaImporter not found");
|
||||
}
|
||||
importer.Import(importWorker);
|
||||
Thread.Sleep(5000); // be aware of GSheet API quota
|
||||
|
||||
var endDate = importWorker.Records!.First(x => x.Code == "EndDate").Desc1;
|
||||
if (endDate == null)
|
||||
{
|
||||
throw new Exception("EndDate record nod found");
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
var importer = _pluginManager.GetImporter("MorskaImporter");
|
||||
if (importer == null)
|
||||
{
|
||||
throw new Exception("MorskaImporter not found");
|
||||
}
|
||||
importer.Import(importWorker);
|
||||
Thread.Sleep(5000); // be aware of GSheet API quota
|
||||
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{importWorker.Name}, {importWorker.Id}",
|
||||
Type = LogEntryType.Info,
|
||||
LogType = LogType.Import,
|
||||
Message = "Success",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
else if (IsImportedLayerUpToDate(importWorker) == false)
|
||||
{
|
||||
var importer = _pluginManager.GetImporter("MorskaImporter");
|
||||
if (importer == null)
|
||||
{
|
||||
throw new Exception("MorskaImporter not found");
|
||||
}
|
||||
importer.Import(importWorker);
|
||||
Thread.Sleep(5000); // be aware of GSheet API quota
|
||||
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{importWorker.Name}, {importWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Import,
|
||||
Message = "Success (reimported)",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{importWorker.Name}, {importWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Import,
|
||||
Message = "importLayer records are up to date. Not processed.",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
|
||||
break;
|
||||
_logger.LogWarning("AutoImport: Reimported out-of-date layer {LayerName} ({LayerId})",
|
||||
importWorker.Name, importWorker.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("AutoImport: Layer {LayerName} ({LayerId}) is up to date, skipping",
|
||||
importWorker.Name, importWorker.Id);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{importWorker.Name}, {importWorker.Id}",
|
||||
Type = LogEntryType.Error,
|
||||
LogType = LogType.Import,
|
||||
Message = e.ToString(),
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogError(e, "AutoImport: Failed to process layer {LayerName} ({LayerId})",
|
||||
importWorker.Name, importWorker.Id);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("AutoImport: Completed processing {LayerCount} layers", importWorkerLayers.Count);
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = "Process error",
|
||||
Type = LogEntryType.Error,
|
||||
LogType = LogType.Import,
|
||||
Message = e.ToString(),
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogError(e, "AutoImport: Process error");
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
@@ -545,11 +464,13 @@ public class LayersController : Controller
|
||||
{
|
||||
if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"])
|
||||
{
|
||||
_logger.LogWarning("AutoProcess: Unauthorized request with apiKey {ApiKey}", apiKey);
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
if (_googleSheetValues is null)
|
||||
{
|
||||
_logger.LogError("AutoProcess: Google Sheets API not initialized");
|
||||
throw new Exception("Google Sheets API not initialized");
|
||||
}
|
||||
|
||||
@@ -557,8 +478,8 @@ public class LayersController : Controller
|
||||
[
|
||||
"T3-SingleSource",
|
||||
"T3-SourceYearSummary",
|
||||
"T3-MultiSourceSummary", // AA
|
||||
"T3-MultiSourceYearSummary", // AA/13
|
||||
"T3-MultiSourceSummary",
|
||||
"T3-MultiSourceYearSummary",
|
||||
"T4-SingleSource",
|
||||
"T5-LastValues",
|
||||
"T1-R1",
|
||||
@@ -566,6 +487,8 @@ public class LayersController : Controller
|
||||
"T1-R3"
|
||||
];
|
||||
|
||||
_logger.LogInformation("AutoProcess: Starting processing for {ProcessTypeCount} process types", processTypes.Length);
|
||||
|
||||
foreach (var type in processTypes)
|
||||
{
|
||||
try
|
||||
@@ -581,38 +504,31 @@ public class LayersController : Controller
|
||||
.AsNoTracking()
|
||||
.ToList();
|
||||
|
||||
_logger.LogInformation("AutoProcess: Processing type {ProcessType}, found {LayerCount} layers",
|
||||
type, processWorkerLayers.Count);
|
||||
|
||||
foreach (var processWorker in processWorkerLayers)
|
||||
{
|
||||
try
|
||||
{
|
||||
ProcessLayer(processWorker);
|
||||
_logger.LogInformation("AutoProcess: Successfully processed {LayerName} ({LayerId}) with type {ProcessType}",
|
||||
processWorker.Name, processWorker.Id, type);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Error,
|
||||
LogType = LogType.Process,
|
||||
Message = e.ToString(),
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogError(e, "AutoProcess: Failed to process {LayerName} ({LayerId}) with type {ProcessType}",
|
||||
processWorker.Name, processWorker.Id, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = "Process error",
|
||||
Type = LogEntryType.Error,
|
||||
LogType = LogType.Process,
|
||||
Message = e.ToString(),
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
_logger.LogError(e, "AutoProcess: Error processing type {ProcessType}", type);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("AutoProcess: Completed processing all process types");
|
||||
return Ok();
|
||||
}
|
||||
|
||||
@@ -626,7 +542,7 @@ public class LayersController : Controller
|
||||
var year = processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1;
|
||||
if (year == null)
|
||||
{
|
||||
throw new Exception("Year record nod found");
|
||||
throw new Exception("Year record not found");
|
||||
}
|
||||
|
||||
var processType = processWorker.Records?.SingleOrDefault(x => x.Code == "ProcessType")?.Desc1;
|
||||
@@ -642,15 +558,6 @@ public class LayersController : Controller
|
||||
throw new Exception("T3.SourceYearSummary processor not found");
|
||||
}
|
||||
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 "T3-MultiSourceYearSummary":
|
||||
@@ -661,15 +568,6 @@ public class LayersController : Controller
|
||||
throw new Exception("T3.MultiSourceYearSummary processor not found");
|
||||
}
|
||||
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 "T3-MultiSourceCopySelectedCodesYearSummary":
|
||||
@@ -680,15 +578,6 @@ public class LayersController : Controller
|
||||
throw new Exception("T3.MultiSourceCopySelectedCodesYearSummary processor not found");
|
||||
}
|
||||
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 "T1-R1":
|
||||
@@ -699,15 +588,6 @@ public class LayersController : Controller
|
||||
throw new Exception("T1.R1 processor not found");
|
||||
}
|
||||
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":
|
||||
@@ -718,15 +598,6 @@ public class LayersController : Controller
|
||||
throw new Exception("T4.R2 processor not found");
|
||||
}
|
||||
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 "T1-R3":
|
||||
@@ -737,15 +608,6 @@ public class LayersController : Controller
|
||||
throw new Exception("T1.R3 processor not found");
|
||||
}
|
||||
processor.Process(processWorker);
|
||||
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Info,
|
||||
LogType = LogType.Process,
|
||||
Message = "Success",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -809,35 +671,8 @@ public class LayersController : Controller
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Info,
|
||||
LogType = LogType.Process,
|
||||
Message = "Success",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
|
||||
internal void SaveRecords(Guid id, ICollection<Record> records, Guid currentUserId)
|
||||
{
|
||||
var toDelete = _db.Records.Where(x => x.LayerId == id).ToList();
|
||||
if (toDelete.Count > 0)
|
||||
{
|
||||
_db.Records.RemoveRange(toDelete);
|
||||
}
|
||||
|
||||
foreach (var record in records)
|
||||
{
|
||||
record.CreatedById = currentUserId;
|
||||
record.CreatedAt = DateTime.UtcNow;
|
||||
record.ModifiedById = currentUserId;
|
||||
record.ModifiedAt = DateTime.UtcNow;
|
||||
record.LayerId = id;
|
||||
_db.Records.Add(record);
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteToConsole(params string[] messages)
|
||||
{
|
||||
@@ -863,6 +698,7 @@ public class LayersController : Controller
|
||||
|
||||
if (newestLayer is null)
|
||||
{
|
||||
_logger.LogDebug("IsImportedLayerUpToDate: No child layers found for {LayerName}, treating as up to date", importWorker.Name);
|
||||
return true; // importWorker is not active yet, no check needed
|
||||
}
|
||||
|
||||
@@ -884,39 +720,51 @@ public class LayersController : Controller
|
||||
throw new Exception($"DataRange not found, {importWorker.Name}");
|
||||
}
|
||||
|
||||
var dataRangeResponse = _googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute();
|
||||
var data = dataRangeResponse.Values;
|
||||
|
||||
var isUpToDate = true;
|
||||
|
||||
for (var i = 0; i < data[1].Count; i++)
|
||||
try
|
||||
{
|
||||
if (data[0][i].ToString() == "") continue;
|
||||
var record = newestLayer.Records!.FirstOrDefault(x => x.Code == data[0][i].ToString());
|
||||
if (record == null)
|
||||
var dataRangeResponse = _googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute();
|
||||
var data = dataRangeResponse.Values;
|
||||
|
||||
var isUpToDate = true;
|
||||
|
||||
for (var i = 0; i < data[1].Count; i++)
|
||||
{
|
||||
WriteToConsole("Code not found in DiunaBI", data[0][i].ToString()!);
|
||||
if (data[0][i].ToString() == "") continue;
|
||||
var record = newestLayer.Records!.FirstOrDefault(x => x.Code == data[0][i].ToString());
|
||||
if (record == null)
|
||||
{
|
||||
_logger.LogDebug("IsImportedLayerUpToDate: Code {Code} not found in DiunaBI for layer {LayerName}",
|
||||
data[0][i].ToString(), importWorker.Name);
|
||||
isUpToDate = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!double.TryParse(data[1][i].ToString(), CultureInfo.GetCultureInfo("pl-PL"), out var value) ||
|
||||
double.Abs((double)(record.Value1 - value)!) < 0.01) continue;
|
||||
isUpToDate = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!double.TryParse(data[1][i].ToString(), CultureInfo.GetCultureInfo("pl-PL"),
|
||||
out var value) ||
|
||||
double.Abs((double)(record.Value1 - value)!) < 0.01) continue;
|
||||
isUpToDate = false;
|
||||
}
|
||||
|
||||
foreach (var record in newestLayer.Records!)
|
||||
{
|
||||
if (data[0].Contains(record.Code))
|
||||
foreach (var record in newestLayer.Records!)
|
||||
{
|
||||
continue;
|
||||
if (data[0].Contains(record.Code))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_logger.LogDebug("IsImportedLayerUpToDate: Code {Code} not found in GoogleSheet for layer {LayerName}",
|
||||
record.Code, importWorker.Name);
|
||||
isUpToDate = false;
|
||||
}
|
||||
|
||||
WriteToConsole($"Code not found in GoogleSheet: {record.Code}");
|
||||
isUpToDate = false;
|
||||
}
|
||||
_logger.LogDebug("IsImportedLayerUpToDate: Layer {LayerName} is {Status}",
|
||||
importWorker.Name, isUpToDate ? "up to date" : "outdated");
|
||||
|
||||
return isUpToDate;
|
||||
return isUpToDate;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "IsImportedLayerUpToDate: Error checking if layer {LayerName} is up to date", importWorker.Name);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user