LogsController
This commit is contained in:
35
.vscode/launch.json
vendored
Normal file
35
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
// Use IntelliSense to find out which attributes exist for C# debugging
|
||||||
|
// Use hover for the description of the existing attributes
|
||||||
|
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md.
|
||||||
|
"name": ".NET Core Launch (web)",
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "launch",
|
||||||
|
"preLaunchTask": "build",
|
||||||
|
// If you have changed target frameworks, make sure to update the program path.
|
||||||
|
"program": "${workspaceFolder}/WebAPI/bin/Debug/net7.0/DiunaBI-WebAPI.dll",
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}/WebAPI",
|
||||||
|
"stopAtEntry": false,
|
||||||
|
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
|
||||||
|
"serverReadyAction": {
|
||||||
|
"action": "openExternally",
|
||||||
|
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
|
||||||
|
},
|
||||||
|
"env": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
},
|
||||||
|
"sourceFileMap": {
|
||||||
|
"/Views": "${workspaceFolder}/Views"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": ".NET Core Attach",
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "attach"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
41
.vscode/tasks.json
vendored
Normal file
41
.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "build",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"${workspaceFolder}/DiunaBI.sln",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "publish",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"publish",
|
||||||
|
"${workspaceFolder}/DiunaBI.sln",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "watch",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"watch",
|
||||||
|
"run",
|
||||||
|
"--project",
|
||||||
|
"${workspaceFolder}/DiunaBI.sln"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
using System.Linq;
|
|
||||||
using Google.Apis.Sheets.v4;
|
using Google.Apis.Sheets.v4;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@@ -17,6 +16,7 @@ namespace WebAPI.Controllers
|
|||||||
private readonly AppDbContext db;
|
private readonly AppDbContext db;
|
||||||
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||||
private GoogleDriveHelper googleDriveHelper;
|
private GoogleDriveHelper googleDriveHelper;
|
||||||
|
private GoogleSheetsHelper googleSheetsHelper;
|
||||||
private readonly IConfiguration configuration;
|
private readonly IConfiguration configuration;
|
||||||
public LayersController(
|
public LayersController(
|
||||||
AppDbContext _db,
|
AppDbContext _db,
|
||||||
@@ -26,6 +26,7 @@ namespace WebAPI.Controllers
|
|||||||
{
|
{
|
||||||
db = _db;
|
db = _db;
|
||||||
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
|
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
|
||||||
|
googleSheetsHelper = _googleSheetsHelper;
|
||||||
googleDriveHelper = _googleDriveHelper;
|
googleDriveHelper = _googleDriveHelper;
|
||||||
configuration = _configuration;
|
configuration = _configuration;
|
||||||
}
|
}
|
||||||
@@ -34,18 +35,18 @@ namespace WebAPI.Controllers
|
|||||||
public IActionResult GetAll(int start, int limit, [FromQuery] string[] codes)
|
public IActionResult GetAll(int start, int limit, [FromQuery] string[] codes)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (codes != null && codes.Length > 0)
|
if (codes != null && codes.Length > 0)
|
||||||
{
|
{
|
||||||
return Ok(db.Layers.Where(x => !x.IsDeleted)
|
return Ok(db.Layers.Where(x => !x.IsDeleted)
|
||||||
.Where(x => codes.Select(Int32.Parse).ToList().Contains(x.Number))
|
.Where(x => codes.Select(Int32.Parse).ToList().Contains(x.Number))
|
||||||
.OrderByDescending(x => x.Number)
|
.OrderByDescending(x => x.Number)
|
||||||
.Skip(start).Take(limit).ToList());
|
.Skip(start).Take(limit).ToList());
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
return Ok(db.Layers.Where(x => !x.IsDeleted)
|
return Ok(db.Layers.Where(x => !x.IsDeleted)
|
||||||
.OrderByDescending(x => x.Number)
|
.OrderByDescending(x => x.Number)
|
||||||
.Skip(start).Take(limit).ToList());
|
.Skip(start).Take(limit).ToList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@@ -121,29 +122,39 @@ namespace WebAPI.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("autoImport/{apiKey}")]
|
[Route("AutoImport/{apiKey}")]
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
public IActionResult autoImport(string apiKey)
|
public IActionResult AutoImport(string apiKey)
|
||||||
{
|
{
|
||||||
if (Request.Host.Value != "localhost:5400" || apiKey != configuration["apiKey"])
|
if (Request.Host.Value != "localhost:5400" || apiKey != configuration["apiKey"])
|
||||||
{
|
{
|
||||||
return Unauthorized();
|
return Unauthorized();
|
||||||
}
|
}
|
||||||
string sheetId = "1G_Hu8DTP-PSPNXTaVYhc_ppnTQi6HWoA4oXSSdUmM9E";
|
|
||||||
string sheetName = "KOSZTY";
|
|
||||||
|
|
||||||
Layer layer = new Layer();
|
List<Layer> layersToImport;
|
||||||
layer.Source = "GoogleSheet";
|
try {
|
||||||
layer.Number = db.Layers.Count() + 1;
|
layersToImport = db.Layers
|
||||||
var parser = new googleSheetParser(googleSheetValues);
|
.Where(x => x.Records!.Any(x => x.Code == "StartDate" && DateTime.Parse(x.Desc1!) > DateTime.UtcNow))
|
||||||
dynamic parsedSheet = parser.parse(sheetId);
|
.Where(x => x.Records!.Any(x => x.Code == "EndDate" && DateTime.Parse(x.Desc1!) < DateTime.UtcNow))
|
||||||
layer.Records = parsedSheet.records;
|
.ToList();
|
||||||
layer.Name = $"W{layer.Number}-I-{sheetName}-{parsedSheet.date}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
} catch (Exception e) {
|
||||||
AddLayer(layer, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
LogEntry entry = new LogEntry
|
||||||
|
{
|
||||||
|
Title = "Import error",
|
||||||
|
Type = LogEntryType.error,
|
||||||
|
Message = e.ToString(),
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
LogsController logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration);
|
||||||
|
logsController.AddEntry(entry);
|
||||||
|
return BadRequest(e.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(layersToImport);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return Ok("OK");
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("autoImportMorska/{apiKey}")]
|
[Route("autoImportMorska/{apiKey}")]
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
@@ -153,23 +164,23 @@ namespace WebAPI.Controllers
|
|||||||
{
|
{
|
||||||
return Unauthorized();
|
return Unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
morskaK5Parser parser = new morskaK5Parser(googleSheetValues, db);
|
morskaK5Parser parser = new morskaK5Parser(googleSheetValues, db);
|
||||||
Layer layer = parser.parse();
|
Layer layer = parser.parse();
|
||||||
|
|
||||||
|
|
||||||
AddLayer(layer, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
AddLayer(layer, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||||
|
|
||||||
return Ok("OK");
|
return Ok("OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("checkDates")]
|
[Route("checkDates")]
|
||||||
public IActionResult checkDates()
|
public IActionResult checkDates()
|
||||||
{
|
{
|
||||||
var warsawTZ = TimeZoneInfo.FindSystemTimeZoneById("Singapore Standard Time");
|
var warsawTZ = TimeZoneInfo.FindSystemTimeZoneById("Singapore Standard Time");
|
||||||
DateTime date = DateTime.UtcNow;
|
DateTime date = DateTime.UtcNow;
|
||||||
return Ok(date);
|
return Ok(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
56
WebAPI/Controllers/LogsController.cs
Normal file
56
WebAPI/Controllers/LogsController.cs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
using Google.Apis.Sheets.v4;
|
||||||
|
using Google.Apis.Sheets.v4.Data;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WebAPI.Models;
|
||||||
|
|
||||||
|
namespace WebAPI.Controllers
|
||||||
|
{
|
||||||
|
public class LogsController : Controller
|
||||||
|
{
|
||||||
|
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||||
|
private GoogleDriveHelper googleDriveHelper;
|
||||||
|
private readonly IConfiguration configuration;
|
||||||
|
public LogsController(
|
||||||
|
GoogleSheetsHelper _googleSheetsHelper,
|
||||||
|
GoogleDriveHelper _googleDriveHelper,
|
||||||
|
IConfiguration _configuration)
|
||||||
|
{
|
||||||
|
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
|
||||||
|
googleDriveHelper = _googleDriveHelper;
|
||||||
|
configuration = _configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddEntry(LogEntry entry)
|
||||||
|
{
|
||||||
|
String type;
|
||||||
|
switch (entry.LogType) {
|
||||||
|
case LogType.import:
|
||||||
|
type = "Import";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
type = "Other"; // should never happen
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var response = googleSheetValues.Get(configuration["appLogsFile"], $"{type}!A:A").Execute();
|
||||||
|
var data = response.Values;
|
||||||
|
int row = data.Count + 1;
|
||||||
|
var range = $"{type}!A{row}:D{row}";
|
||||||
|
|
||||||
|
List<object> logRow = new List<object>
|
||||||
|
{
|
||||||
|
entry.CreatedAt.ToString(new CultureInfo("pl-PL")),
|
||||||
|
entry.Type.ToString(),
|
||||||
|
entry.Title!,
|
||||||
|
entry.Message!
|
||||||
|
};
|
||||||
|
|
||||||
|
ValueRange 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
22
WebAPI/Models/LogEntry.cs
Normal file
22
WebAPI/Models/LogEntry.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace WebAPI.Models
|
||||||
|
{
|
||||||
|
public enum LogEntryType
|
||||||
|
{
|
||||||
|
info,
|
||||||
|
warning,
|
||||||
|
error,
|
||||||
|
}
|
||||||
|
public enum LogType {
|
||||||
|
import
|
||||||
|
}
|
||||||
|
public class LogEntry
|
||||||
|
{
|
||||||
|
public LogType LogType { get; set; }
|
||||||
|
public LogEntryType Type { get; set; }
|
||||||
|
public string? Message { get; set; }
|
||||||
|
public string? Title {get; set;}
|
||||||
|
public DateTime CreatedAt { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
"Secret": "8393AF8EAEF8478CB738D44858690F9C7E2D19F65896DD9FBAA3EB2A6F493E80",
|
"Secret": "8393AF8EAEF8478CB738D44858690F9C7E2D19F65896DD9FBAA3EB2A6F493E80",
|
||||||
"apiKey": "10763478CB738D4ecb2h76g803478CB738D4e",
|
"apiKey": "10763478CB738D4ecb2h76g803478CB738D4e",
|
||||||
"exportDirectory": "1eTyCUzYbzVQB8f8sbNmvnebFXyW2-axt",
|
"exportDirectory": "1eTyCUzYbzVQB8f8sbNmvnebFXyW2-axt",
|
||||||
|
"appLogsFile": "1RL-qtl0THgBUWcEWWqc0nCaVY7c8RRfXk6uhsqczuVA",
|
||||||
"Kestrel": {
|
"Kestrel": {
|
||||||
"Endpoints": {
|
"Endpoints": {
|
||||||
"Http": {
|
"Http": {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
"Secret": "#{google-backend-login-secret}#",
|
"Secret": "#{google-backend-login-secret}#",
|
||||||
"apiKey": "#{api-key}#",
|
"apiKey": "#{api-key}#",
|
||||||
"exportDirectory": "#{export-directory}#",
|
"exportDirectory": "#{export-directory}#",
|
||||||
|
"appLogsFile": "#{app-logs-file}#",
|
||||||
"Kestrel": {
|
"Kestrel": {
|
||||||
"Endpoints": {
|
"Endpoints": {
|
||||||
"Http": {
|
"Http": {
|
||||||
|
|||||||
Reference in New Issue
Block a user