Move logs into FirebaseDB
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -58,4 +58,5 @@ _ReSharper*/
|
|||||||
*.DotSettings.user
|
*.DotSettings.user
|
||||||
|
|
||||||
.vs/
|
.vs/
|
||||||
Temp/
|
Temp/
|
||||||
|
WebAPI/diunabi-admin-firebase-Development.json
|
||||||
|
|||||||
BIN
WebAPI/.DS_Store
vendored
BIN
WebAPI/.DS_Store
vendored
Binary file not shown.
@@ -1,4 +1,5 @@
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
|
using Google.Cloud.Firestore;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Data.SqlClient;
|
using Microsoft.Data.SqlClient;
|
||||||
@@ -17,11 +18,12 @@ public class AdminController : Controller
|
|||||||
public AdminController(
|
public AdminController(
|
||||||
GoogleDriveHelper googleDriveHelper,
|
GoogleDriveHelper googleDriveHelper,
|
||||||
GoogleSheetsHelper googleSheetsHelper,
|
GoogleSheetsHelper googleSheetsHelper,
|
||||||
IConfiguration configuration)
|
IConfiguration configuration,
|
||||||
|
FirestoreDb firestoreDb)
|
||||||
{
|
{
|
||||||
_googleDriveHelper = googleDriveHelper;
|
_googleDriveHelper = googleDriveHelper;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
_logsController = new LogsController(googleSheetsHelper, _configuration);
|
_logsController = new LogsController(firestoreDb);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Google.Cloud.Firestore;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Data.SqlClient;
|
using Microsoft.Data.SqlClient;
|
||||||
@@ -19,11 +20,12 @@ public class DataInboxController : Controller
|
|||||||
public DataInboxController(
|
public DataInboxController(
|
||||||
AppDbContext db,
|
AppDbContext db,
|
||||||
GoogleSheetsHelper googleSheetsHelper,
|
GoogleSheetsHelper googleSheetsHelper,
|
||||||
IConfiguration configuration)
|
IConfiguration configuration,
|
||||||
|
FirestoreDb firestoreDb)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
_logsController = new LogsController(googleSheetsHelper, _configuration);
|
_logsController = new LogsController(firestoreDb);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut]
|
[HttpPut]
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ using System.Globalization;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using DiunaBIWebAPI.dataImporters;
|
using DiunaBIWebAPI.dataImporters;
|
||||||
using Google.Apis.Sheets.v4;
|
using Google.Apis.Sheets.v4;
|
||||||
|
using Google.Cloud.Firestore;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@@ -25,7 +26,8 @@ public class LayersController : Controller
|
|||||||
AppDbContext db,
|
AppDbContext db,
|
||||||
GoogleSheetsHelper googleSheetsHelper,
|
GoogleSheetsHelper googleSheetsHelper,
|
||||||
GoogleDriveHelper googleDriveHelper,
|
GoogleDriveHelper googleDriveHelper,
|
||||||
IConfiguration configuration
|
IConfiguration configuration,
|
||||||
|
FirestoreDb firestoreDb
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
@@ -36,7 +38,7 @@ public class LayersController : Controller
|
|||||||
|
|
||||||
_googleDriveHelper = googleDriveHelper;
|
_googleDriveHelper = googleDriveHelper;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
_logsController = new LogsController(googleSheetsHelper, _configuration);
|
_logsController = new LogsController(firestoreDb);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
@@ -330,42 +332,9 @@ public class LayersController : Controller
|
|||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "FK2":
|
case "FK2":
|
||||||
{
|
|
||||||
var fk2Importer = new MorskaFk2Importer(_db, _googleSheetValues, this);
|
|
||||||
fk2Importer.Import(importWorker);
|
|
||||||
Thread.Sleep(5000); // be aware of GSheet API quota
|
|
||||||
|
|
||||||
_logsController.AddEntry(new LogEntry
|
|
||||||
{
|
{
|
||||||
Title = $"{importWorker.Name}, {importWorker.Id}",
|
var fk2Importer = new MorskaFk2Importer(_db, _googleSheetValues, this);
|
||||||
Type = LogEntryType.Info,
|
fk2Importer.Import(importWorker);
|
||||||
LogType = LogType.Import,
|
|
||||||
Message = "Success",
|
|
||||||
CreatedAt = DateTime.UtcNow
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
var startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1;
|
|
||||||
if (startDate == null)
|
|
||||||
{
|
|
||||||
throw new Exception("StartDate record nod found");
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = new MorskaImporter(_db, _googleSheetValues, this);
|
|
||||||
importer.Import(importWorker);
|
|
||||||
Thread.Sleep(5000); // be aware of GSheet API quota
|
Thread.Sleep(5000); // be aware of GSheet API quota
|
||||||
|
|
||||||
_logsController.AddEntry(new LogEntry
|
_logsController.AddEntry(new LogEntry
|
||||||
@@ -376,36 +345,69 @@ public class LayersController : Controller
|
|||||||
Message = "Success",
|
Message = "Success",
|
||||||
CreatedAt = DateTime.UtcNow
|
CreatedAt = DateTime.UtcNow
|
||||||
});
|
});
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else if (IsImportedLayerUpToDate(importWorker) == false)
|
default:
|
||||||
{
|
{
|
||||||
var importer = new MorskaImporter(_db, _googleSheetValues, this);
|
var startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1;
|
||||||
importer.Import(importWorker);
|
if (startDate == null)
|
||||||
Thread.Sleep(5000); // be aware of GSheet API quota
|
|
||||||
|
|
||||||
_logsController.AddEntry(new LogEntry
|
|
||||||
{
|
{
|
||||||
Title = $"{importWorker.Name}, {importWorker.Id}",
|
throw new Exception("StartDate record nod found");
|
||||||
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;
|
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 = new MorskaImporter(_db, _googleSheetValues, this);
|
||||||
|
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 = new MorskaImporter(_db, _googleSheetValues, this);
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@@ -483,6 +485,7 @@ public class LayersController : Controller
|
|||||||
x.Records!.Any(y => y.Code == "Type" && y.Desc1 == "ProcessWorker") &&
|
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 == "IsEnabled" && y.Desc1 == "True") &&
|
||||||
x.Records!.Any(y => y.Code == "ProcessType" && y.Desc1 == type)
|
x.Records!.Any(y => y.Code == "ProcessType" && y.Desc1 == type)
|
||||||
|
&& x.Number == 5586
|
||||||
)
|
)
|
||||||
.OrderBy(x => x.CreatedAt)
|
.OrderBy(x => x.CreatedAt)
|
||||||
.AsNoTracking()
|
.AsNoTracking()
|
||||||
@@ -553,98 +556,98 @@ public class LayersController : Controller
|
|||||||
case null:
|
case null:
|
||||||
throw new Exception("ProcessType record not found");
|
throw new Exception("ProcessType record not found");
|
||||||
case "T3-SourceYearSummary":
|
case "T3-SourceYearSummary":
|
||||||
{
|
|
||||||
var processor =
|
|
||||||
new T3SourceYearSummaryProcessor(_db, this);
|
|
||||||
processor.Process(processWorker);
|
|
||||||
|
|
||||||
_logsController.AddEntry(new LogEntry
|
|
||||||
{
|
{
|
||||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
var processor =
|
||||||
Type = LogEntryType.Info,
|
new T3SourceYearSummaryProcessor(_db, this);
|
||||||
LogType = LogType.Process,
|
processor.Process(processWorker);
|
||||||
Message = "Success",
|
|
||||||
CreatedAt = DateTime.UtcNow
|
_logsController.AddEntry(new LogEntry
|
||||||
});
|
{
|
||||||
return;
|
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||||
}
|
Type = LogEntryType.Info,
|
||||||
|
LogType = LogType.Process,
|
||||||
|
Message = "Success",
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
case "T3-MultiSourceYearSummary":
|
case "T3-MultiSourceYearSummary":
|
||||||
{
|
|
||||||
var processor =
|
|
||||||
new T3MultiSourceYearSummaryProcessor(_db, this, _logsController);
|
|
||||||
processor.Process(processWorker);
|
|
||||||
|
|
||||||
_logsController.AddEntry(new LogEntry
|
|
||||||
{
|
{
|
||||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
var processor =
|
||||||
Type = LogEntryType.Info,
|
new T3MultiSourceYearSummaryProcessor(_db, this, _logsController);
|
||||||
LogType = LogType.Process,
|
processor.Process(processWorker);
|
||||||
Message = "Success",
|
|
||||||
CreatedAt = DateTime.UtcNow
|
_logsController.AddEntry(new LogEntry
|
||||||
});
|
{
|
||||||
return;
|
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||||
}
|
Type = LogEntryType.Info,
|
||||||
|
LogType = LogType.Process,
|
||||||
|
Message = "Success",
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
case "T3-MultiSourceCopySelectedCodesYearSummary":
|
case "T3-MultiSourceCopySelectedCodesYearSummary":
|
||||||
{
|
|
||||||
var processor =
|
|
||||||
new T3MultiSourceCopySelectedCodesYearSummaryProcessor(_db, this);
|
|
||||||
processor.Process(processWorker);
|
|
||||||
|
|
||||||
_logsController.AddEntry(new LogEntry
|
|
||||||
{
|
{
|
||||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
var processor =
|
||||||
Type = LogEntryType.Info,
|
new T3MultiSourceCopySelectedCodesYearSummaryProcessor(_db, this);
|
||||||
LogType = LogType.Process,
|
processor.Process(processWorker);
|
||||||
Message = "Success",
|
|
||||||
CreatedAt = DateTime.UtcNow
|
_logsController.AddEntry(new LogEntry
|
||||||
});
|
{
|
||||||
return;
|
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||||
}
|
Type = LogEntryType.Info,
|
||||||
|
LogType = LogType.Process,
|
||||||
|
Message = "Success",
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
case "T1-R1":
|
case "T1-R1":
|
||||||
{
|
|
||||||
var processor = new T1R1Processor(_db, _googleSheetValues, this, _logsController);
|
|
||||||
processor.Process(processWorker);
|
|
||||||
|
|
||||||
_logsController.AddEntry(new LogEntry
|
|
||||||
{
|
{
|
||||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
var processor = new T1R1Processor(_db, _googleSheetValues, this, _logsController);
|
||||||
Type = LogEntryType.Info,
|
processor.Process(processWorker);
|
||||||
LogType = LogType.Process,
|
|
||||||
Message = "Success",
|
_logsController.AddEntry(new LogEntry
|
||||||
CreatedAt = DateTime.UtcNow
|
{
|
||||||
});
|
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||||
return;
|
Type = LogEntryType.Info,
|
||||||
}
|
LogType = LogType.Process,
|
||||||
|
Message = "Success",
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
case "T4-R2":
|
case "T4-R2":
|
||||||
{
|
|
||||||
var processor = new T4R2Processor(_db, this, _logsController, _googleSheetValues);
|
|
||||||
processor.Process(processWorker);
|
|
||||||
|
|
||||||
_logsController.AddEntry(new LogEntry
|
|
||||||
{
|
{
|
||||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
var processor = new T4R2Processor(_db, this, _logsController, _googleSheetValues);
|
||||||
Type = LogEntryType.Info,
|
processor.Process(processWorker);
|
||||||
LogType = LogType.Process,
|
|
||||||
Message = "Success",
|
_logsController.AddEntry(new LogEntry
|
||||||
CreatedAt = DateTime.UtcNow
|
{
|
||||||
});
|
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||||
return;
|
Type = LogEntryType.Info,
|
||||||
}
|
LogType = LogType.Process,
|
||||||
|
Message = "Success",
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
case "T1-R3":
|
case "T1-R3":
|
||||||
{
|
|
||||||
var processor = new T1R3Processor(_db, this, _googleSheetValues);
|
|
||||||
processor.Process(processWorker);
|
|
||||||
|
|
||||||
_logsController.AddEntry(new LogEntry
|
|
||||||
{
|
{
|
||||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
var processor = new T1R3Processor(_db, this, _googleSheetValues);
|
||||||
Type = LogEntryType.Info,
|
processor.Process(processWorker);
|
||||||
LogType = LogType.Process,
|
|
||||||
Message = "Success",
|
_logsController.AddEntry(new LogEntry
|
||||||
CreatedAt = DateTime.UtcNow
|
{
|
||||||
});
|
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||||
return;
|
Type = LogEntryType.Info,
|
||||||
}
|
LogType = LogType.Process,
|
||||||
|
Message = "Success",
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var month = processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1;
|
var month = processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1;
|
||||||
@@ -656,37 +659,37 @@ public class LayersController : Controller
|
|||||||
switch (processType!)
|
switch (processType!)
|
||||||
{
|
{
|
||||||
case "T3-SingleSource":
|
case "T3-SingleSource":
|
||||||
{
|
{
|
||||||
var t3SingleSource = new T3SingleSourceProcessor(_db, this, _logsController);
|
var t3SingleSource = new T3SingleSourceProcessor(_db, this);
|
||||||
t3SingleSource.Process(processWorker);
|
t3SingleSource.Process(processWorker);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "T4-SingleSource":
|
case "T4-SingleSource":
|
||||||
{
|
{
|
||||||
var t4SingleSource = new T4SingleSourceProcessor(_db, this);
|
var t4SingleSource = new T4SingleSourceProcessor(_db, this);
|
||||||
t4SingleSource.Process(processWorker);
|
t4SingleSource.Process(processWorker);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "T5-LastValues":
|
case "T5-LastValues":
|
||||||
{
|
{
|
||||||
var t5LastValues = new T5LastValuesProcessor(_db, this);
|
var t5LastValues = new T5LastValuesProcessor(_db, this);
|
||||||
t5LastValues.Process(processWorker);
|
t5LastValues.Process(processWorker);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "T3-MultiSourceSummary":
|
case "T3-MultiSourceSummary":
|
||||||
{
|
{
|
||||||
var t3MultiSourceSummary =
|
var t3MultiSourceSummary =
|
||||||
new T3MultiSourceSummaryProcessor(_db, this, _logsController);
|
new T3MultiSourceSummaryProcessor(_db, this, _logsController);
|
||||||
t3MultiSourceSummary.Process(processWorker);
|
t3MultiSourceSummary.Process(processWorker);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "T3-MultiSourceCopySelectedCodes":
|
case "T3-MultiSourceCopySelectedCodes":
|
||||||
{
|
{
|
||||||
var t3MultiSourceCopySelectedCode =
|
var t3MultiSourceCopySelectedCode =
|
||||||
new T3MultiSourceCopySelectedCodesProcessor(_db, this);
|
new T3MultiSourceCopySelectedCodesProcessor(_db, this);
|
||||||
t3MultiSourceCopySelectedCode.Process(processWorker);
|
t3MultiSourceCopySelectedCode.Process(processWorker);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_logsController.AddEntry(new LogEntry
|
_logsController.AddEntry(new LogEntry
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using Google.Apis.Sheets.v4;
|
using Google.Apis.Sheets.v4;
|
||||||
using Google.Apis.Sheets.v4.Data;
|
using Google.Apis.Sheets.v4.Data;
|
||||||
|
using Google.Cloud.Firestore;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using WebAPI.Models;
|
using WebAPI.Models;
|
||||||
|
|
||||||
@@ -8,59 +9,35 @@ namespace WebAPI.Controllers;
|
|||||||
|
|
||||||
public class LogsController : Controller
|
public class LogsController : Controller
|
||||||
{
|
{
|
||||||
private readonly SpreadsheetsResource.ValuesResource? _googleSheetValues;
|
|
||||||
private readonly IConfiguration _configuration;
|
private readonly FirestoreDb _firestoreDb;
|
||||||
|
private readonly Guid _SessionId = Guid.NewGuid();
|
||||||
public LogsController(
|
public LogsController(
|
||||||
GoogleSheetsHelper googleSheetsHelper,
|
FirestoreDb firestoreDb
|
||||||
IConfiguration configuration)
|
)
|
||||||
{
|
{
|
||||||
if (googleSheetsHelper.Service is not null) {
|
_firestoreDb = firestoreDb;
|
||||||
_googleSheetValues = googleSheetsHelper.Service.Spreadsheets.Values;
|
|
||||||
}
|
|
||||||
_configuration = configuration;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddEntry(LogEntry entry)
|
public void AddEntry(LogEntry entry)
|
||||||
{
|
{
|
||||||
if (_googleSheetValues is null) {
|
entry.SessionId = _SessionId;
|
||||||
throw new Exception("Google Sheets API not initialized");
|
entry.Instance = LogInstance.Morska;
|
||||||
|
|
||||||
|
try {
|
||||||
|
var collection = _firestoreDb.Collection("ApiLogs");
|
||||||
|
var document = collection.Document();
|
||||||
|
document.SetAsync(new {
|
||||||
|
entry.Message,
|
||||||
|
entry.Title,
|
||||||
|
Type = Enum.GetName(typeof(LogEntryType), entry.Type),
|
||||||
|
LogType = Enum.GetName(typeof(LogType), entry.LogType),
|
||||||
|
Instance = Enum.GetName(typeof(LogInstance), entry.Instance),
|
||||||
|
entry.CreatedAt,
|
||||||
|
SessionId = entry.SessionId.ToString()
|
||||||
|
}).Wait();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Console.WriteLine(e.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// until we move logs into firebase disable save for info and warnings
|
|
||||||
if (entry.Type == LogEntryType.Info || entry.Type == LogEntryType.Warning) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var type = entry.LogType switch
|
|
||||||
{
|
|
||||||
LogType.Import => "Import",
|
|
||||||
LogType.Backup => "Backup",
|
|
||||||
LogType.Process => "Process",
|
|
||||||
LogType.PowerBi => "PowerBIAccess",
|
|
||||||
LogType.DataInbox => "DataInbox",
|
|
||||||
LogType.Queue => "Queue",
|
|
||||||
_ => "Other"
|
|
||||||
};
|
|
||||||
var response = _googleSheetValues.Get(_configuration["appLogsFile"], $"{type}!A:A").Execute();
|
|
||||||
var data = response.Values;
|
|
||||||
var row = 1;
|
|
||||||
if (data != null) {
|
|
||||||
row = data.Count + 1;
|
|
||||||
}
|
|
||||||
var range = $"{type}!A{row}:D{row}";
|
|
||||||
|
|
||||||
var logRow = new List<object>
|
|
||||||
{
|
|
||||||
entry.CreatedAt.ToString(new CultureInfo("pl-PL")),
|
|
||||||
entry.Type.ToString(),
|
|
||||||
entry.Title!,
|
|
||||||
entry.Message!
|
|
||||||
};
|
|
||||||
|
|
||||||
var valueRange = new ValueRange { Values = new IList<object>[] { logRow }};
|
|
||||||
|
|
||||||
var updateRequest = _googleSheetValues.Update(valueRange, _configuration["appLogsFile"], range);
|
|
||||||
updateRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
|
|
||||||
updateRequest.Execute();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,9 +10,11 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AngouriMath" Version="1.4.0-preview.3" />
|
<PackageReference Include="AngouriMath" Version="1.4.0-preview.3" />
|
||||||
<PackageReference Include="CsvHelper" Version="33.0.1" />
|
<PackageReference Include="CsvHelper" Version="33.0.1" />
|
||||||
|
<PackageReference Include="FirebaseAdmin" Version="3.1.0" />
|
||||||
<PackageReference Include="Google.Apis.Auth" Version="1.68.0" />
|
<PackageReference Include="Google.Apis.Auth" Version="1.68.0" />
|
||||||
<PackageReference Include="Google.Apis.Drive.v3" Version="1.68.0.3627" />
|
<PackageReference Include="Google.Apis.Drive.v3" Version="1.68.0.3627" />
|
||||||
<PackageReference Include="Google.Apis.Sheets.v4" Version="1.68.0.3624" />
|
<PackageReference Include="Google.Apis.Sheets.v4" Version="1.68.0.3624" />
|
||||||
|
<PackageReference Include="Google.Cloud.Firestore" Version="3.9.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ public class GoogleSheetsHelper
|
|||||||
}
|
}
|
||||||
private static GoogleCredential GetCredentialsFromFile()
|
private static GoogleCredential GetCredentialsFromFile()
|
||||||
{
|
{
|
||||||
// ReSharper disable once RedundantAssignment
|
|
||||||
var fileName = "client_secrets.json";
|
var fileName = "client_secrets.json";
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
fileName = "client_secrets.Development.json";
|
fileName = "client_secrets.Development.json";
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using Google.Cloud.Firestore;
|
||||||
|
|
||||||
namespace WebAPI.Models;
|
namespace WebAPI.Models;
|
||||||
|
|
||||||
public enum LogEntryType
|
public enum LogEntryType
|
||||||
@@ -6,7 +8,8 @@ public enum LogEntryType
|
|||||||
Warning,
|
Warning,
|
||||||
Error
|
Error
|
||||||
}
|
}
|
||||||
public enum LogType {
|
public enum LogType
|
||||||
|
{
|
||||||
Import,
|
Import,
|
||||||
Backup,
|
Backup,
|
||||||
Process,
|
Process,
|
||||||
@@ -14,11 +17,18 @@ public enum LogType {
|
|||||||
DataInbox,
|
DataInbox,
|
||||||
Queue
|
Queue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum LogInstance
|
||||||
|
{
|
||||||
|
Morska
|
||||||
|
}
|
||||||
public class LogEntry
|
public class LogEntry
|
||||||
{
|
{
|
||||||
public LogType LogType { get; init; }
|
public LogType LogType { get; init; }
|
||||||
public LogEntryType Type { get; init; }
|
public LogEntryType Type { get; init; }
|
||||||
public string? Message { get; init; }
|
public string? Message { get; init; }
|
||||||
public string? Title {get; init;}
|
public string? Title { get; init; }
|
||||||
public DateTime CreatedAt { get; init; }
|
public DateTime CreatedAt { get; init; }
|
||||||
|
public Guid SessionId { get; set; }
|
||||||
|
public LogInstance Instance { get; set; }
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
using FirebaseAdmin;
|
||||||
|
using Google.Apis.Auth.OAuth2;
|
||||||
|
using Google.Cloud.Firestore;
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
@@ -8,10 +11,11 @@ using WebAPI;
|
|||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
var connectionString = builder.Configuration.GetConnectionString("SQLDatabase");
|
var connectionString = builder.Configuration.GetConnectionString("SQLDatabase");
|
||||||
builder.Services.AddDbContext<AppDbContext>(x => {
|
builder.Services.AddDbContext<AppDbContext>(x =>
|
||||||
|
{
|
||||||
x.UseSqlServer(connectionString);
|
x.UseSqlServer(connectionString);
|
||||||
x.EnableSensitiveDataLogging();
|
x.EnableSensitiveDataLogging();
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.Services.AddCors(options =>
|
builder.Services.AddCors(options =>
|
||||||
{
|
{
|
||||||
@@ -54,15 +58,28 @@ builder.Services.AddAuthentication();
|
|||||||
builder.Services.AddSingleton(typeof(GoogleSheetsHelper));
|
builder.Services.AddSingleton(typeof(GoogleSheetsHelper));
|
||||||
builder.Services.AddSingleton(typeof(GoogleDriveHelper));
|
builder.Services.AddSingleton(typeof(GoogleDriveHelper));
|
||||||
|
|
||||||
|
var fileName = "diunabi-admin-firebase.json";
|
||||||
|
#if DEBUG
|
||||||
|
fileName = "diunabi-admin-firebase-Development.json";
|
||||||
|
#endif
|
||||||
|
var credentialPath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
|
||||||
|
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credentialPath);
|
||||||
|
FirebaseAdmin.FirebaseApp.Create(new AppOptions()
|
||||||
|
{
|
||||||
|
Credential = GoogleCredential.GetApplicationDefault()
|
||||||
|
});
|
||||||
|
builder.Services.AddSingleton(FirestoreDb.Create("diunabi-admin"));
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|
||||||
app.Use(async (context, next) =>
|
app.Use(async (context, next) =>
|
||||||
{
|
{
|
||||||
var token = context.Request.Headers.Authorization.ToString();
|
var token = context.Request.Headers.Authorization.ToString();
|
||||||
if (token.Length > 0
|
if (token.Length > 0
|
||||||
&& !context.Request.Path.ToString().Contains("getForPowerBI")
|
&& !context.Request.Path.ToString().Contains("getForPowerBI")
|
||||||
&& !context.Request.Path.ToString().Contains("DataInbox/Add")) {
|
&& !context.Request.Path.ToString().Contains("DataInbox/Add"))
|
||||||
|
{
|
||||||
var handler = new JwtSecurityTokenHandler();
|
var handler = new JwtSecurityTokenHandler();
|
||||||
var data = handler.ReadJwtToken(token.Split(' ')[1]);
|
var data = handler.ReadJwtToken(token.Split(' ')[1]);
|
||||||
context.Request.Headers.Append("UserId", new Microsoft.Extensions.Primitives.StringValues(data.Subject));
|
context.Request.Headers.Append("UserId", new Microsoft.Extensions.Primitives.StringValues(data.Subject));
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ namespace WebAPI.dataProcessors;
|
|||||||
|
|
||||||
public class T3SingleSourceProcessor(
|
public class T3SingleSourceProcessor(
|
||||||
AppDbContext db,
|
AppDbContext db,
|
||||||
LayersController controller,
|
LayersController controller)
|
||||||
LogsController logsController)
|
|
||||||
{
|
{
|
||||||
public void Process(Layer processWorker)
|
public void Process(Layer processWorker)
|
||||||
{
|
{
|
||||||
|
|||||||
13
WebAPI/diunabi-admin-firebase.json
Normal file
13
WebAPI/diunabi-admin-firebase.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"type": "service_account",
|
||||||
|
"project_id": "#{firebase-project-id}#",
|
||||||
|
"private_key_id": "#{firebase-private-key-id}#",
|
||||||
|
"private_key": "#{firebase-private-key}#",
|
||||||
|
"client_email": "#{firebase-client-email}#",
|
||||||
|
"client_id": "#{firebase-client-id}#",
|
||||||
|
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
||||||
|
"token_uri": "https://oauth2.googleapis.com/token",
|
||||||
|
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||||
|
"client_x509_cert_url": "#{firebase-client-cert-url}#",
|
||||||
|
"universe_domain": "googleapis.com"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user