Check if layer records are up to date with source
This commit is contained in:
@@ -198,14 +198,26 @@ namespace WebAPI.Controllers
|
|||||||
CreatedAt = DateTime.UtcNow
|
CreatedAt = DateTime.UtcNow
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else if (IsImportedLayerUpToDate(importWorker) == false)
|
||||||
{
|
{
|
||||||
logsController.AddEntry(new LogEntry
|
logsController.AddEntry(new LogEntry
|
||||||
{
|
{
|
||||||
Title = $"{importWorker!.Name}, {importWorker.Id}",
|
Title = $"{importWorker!.Name}, {importWorker.Id}",
|
||||||
Type = LogEntryType.warning,
|
Type = LogEntryType.warning,
|
||||||
LogType = LogType.import,
|
LogType = LogType.import,
|
||||||
Message = "importLayer is out of date. Should be disabled. Not processed.",
|
Message = "importLayer records are out of date. Should be reimported.",
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: import again
|
||||||
|
|
||||||
|
} else {
|
||||||
|
logsController.AddEntry(new LogEntry
|
||||||
|
{
|
||||||
|
Title = $"{importWorker!.Name}, {importWorker.Id}",
|
||||||
|
Type = LogEntryType.warning,
|
||||||
|
LogType = LogType.import,
|
||||||
|
Message = "importLayer records are up of date. Not processed.",
|
||||||
CreatedAt = DateTime.UtcNow
|
CreatedAt = DateTime.UtcNow
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -424,7 +436,6 @@ namespace WebAPI.Controllers
|
|||||||
CreatedAt = DateTime.UtcNow
|
CreatedAt = DateTime.UtcNow
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SaveRecords(Guid id, ICollection<Record> records, Guid currentUserId)
|
internal void SaveRecords(Guid id, ICollection<Record> records, Guid currentUserId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -449,5 +460,79 @@ namespace WebAPI.Controllers
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
internal void WriteToConsole(params string[] messages)
|
||||||
|
{
|
||||||
|
foreach (string message in messages)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"DiunaLog: {message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
internal bool IsImportedLayerUpToDate(Layer importWorker)
|
||||||
|
{
|
||||||
|
if (googleSheetValues is null)
|
||||||
|
{
|
||||||
|
throw new Exception("Google Sheets API not initialized");
|
||||||
|
}
|
||||||
|
|
||||||
|
Layer? 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
|
||||||
|
}
|
||||||
|
|
||||||
|
string? sheetId = importWorker.Records!.Where(x => x.Code == "SheetId").FirstOrDefault()?.Desc1;
|
||||||
|
if (sheetId == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"SheetId not found, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
string? sheetTabName = importWorker.Records!.Where(x => x.Code == "SheetTabName").FirstOrDefault()?.Desc1;
|
||||||
|
if (sheetTabName == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"SheetTabName not found, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
string? dataRange = importWorker.Records!.Where(x => x.Code == "DataRange").FirstOrDefault()?.Desc1;
|
||||||
|
if (dataRange == null)
|
||||||
|
{
|
||||||
|
throw new Exception($"DataRange not found, {importWorker.Name}");
|
||||||
|
}
|
||||||
|
var dataRangeResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute();
|
||||||
|
var data = dataRangeResponse.Values;
|
||||||
|
|
||||||
|
bool isUpToDate = true;
|
||||||
|
|
||||||
|
for (int i = 0; i < data[1].Count; i++)
|
||||||
|
{
|
||||||
|
if (data[0][i].ToString() != "")
|
||||||
|
{
|
||||||
|
Record? record = newestLayer.Records!.Where(x => x.Code == data[0][i].ToString()).FirstOrDefault();
|
||||||
|
if (record == null)
|
||||||
|
{
|
||||||
|
WriteToConsole("Code not foundin DiunaBI", data[0][i].ToString()!);
|
||||||
|
isUpToDate = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (string.Format("{0:N2}", record.Value1) != data[1][i].ToString())
|
||||||
|
{
|
||||||
|
WriteToConsole($"Code: {data[0][i]}. DiunaBI: {string.Format("{0:N2}", record.Value1)}. GoogleSheet: {data[1][i]}");
|
||||||
|
isUpToDate = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach (Record record in newestLayer.Records!)
|
||||||
|
{
|
||||||
|
if (data[0].Contains(record.Code))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
WriteToConsole($"Code not found in GoogleSheet: {record.Code}");
|
||||||
|
isUpToDate = false;
|
||||||
|
}
|
||||||
|
return isUpToDate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user