Move logs into FirebaseDB
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Globalization;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using Google.Apis.Sheets.v4.Data;
|
||||
using Google.Cloud.Firestore;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebAPI.Models;
|
||||
|
||||
@@ -8,59 +9,35 @@ namespace WebAPI.Controllers;
|
||||
|
||||
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(
|
||||
GoogleSheetsHelper googleSheetsHelper,
|
||||
IConfiguration configuration)
|
||||
FirestoreDb firestoreDb
|
||||
)
|
||||
{
|
||||
if (googleSheetsHelper.Service is not null) {
|
||||
_googleSheetValues = googleSheetsHelper.Service.Spreadsheets.Values;
|
||||
}
|
||||
_configuration = configuration;
|
||||
_firestoreDb = firestoreDb;
|
||||
}
|
||||
|
||||
public void AddEntry(LogEntry entry)
|
||||
{
|
||||
if (_googleSheetValues is null) {
|
||||
throw new Exception("Google Sheets API not initialized");
|
||||
entry.SessionId = _SessionId;
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user