Ditched app.metrics

This commit is contained in:
2020-05-24 21:51:03 +00:00
parent f0153eaa0f
commit c2f6fd94c1
12 changed files with 404 additions and 499 deletions

View File

@@ -8,7 +8,6 @@ using AyaNova.Api.ControllerHelpers;
using System.Linq;
using System;
using System.Threading.Tasks;
using App.Metrics;
using AyaNova.Biz;
//required to inject configuration in constructor
@@ -29,7 +28,6 @@ namespace AyaNova.Api.Controllers
private readonly ILogger<AuthController> log;
private readonly IConfiguration _configuration;
private readonly ApiServerState serverState;
private readonly IMetrics metrics;
private const int JWT_LIFETIME_DAYS = 7;
/// <summary>
@@ -38,15 +36,13 @@ namespace AyaNova.Api.Controllers
/// <param name="context"></param>
/// <param name="logger"></param>
/// <param name="configuration"></param>
/// <param name="apiServerState"></param>
/// <param name="Metrics"></param>
public AuthController(AyContext context, ILogger<AuthController> logger, IConfiguration configuration, ApiServerState apiServerState, IMetrics Metrics)//these two are injected, see startup.cs
/// <param name="apiServerState"></param>
public AuthController(AyContext context, ILogger<AuthController> logger, IConfiguration configuration, ApiServerState apiServerState)
{
ct = context;
log = logger;
_configuration = configuration;
serverState = apiServerState;
metrics = Metrics;
}
//AUTHENTICATE CREDS
@@ -149,7 +145,6 @@ namespace AyaNova.Api.Controllers
if (string.IsNullOrWhiteSpace(creds.Login) || string.IsNullOrWhiteSpace(creds.Password))
{
metrics.Measure.Meter.Mark(MetricsRegistry.FailedLoginMeter);
//Make a failed pw wait
await Task.Delay(nFailedAuthDelay);
return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED));
@@ -214,7 +209,7 @@ namespace AyaNova.Api.Controllers
await ct.SaveChangesAsync();
log.LogDebug($"User number \"{u.Id}\" logged in from \"{Util.StringUtil.MaskIPAddress(HttpContext.Connection.RemoteIpAddress.ToString())}\" ok");
metrics.Measure.Meter.Mark(MetricsRegistry.SuccessfulLoginMeter);
return Ok(ApiOkResponse.Response(new
{
@@ -228,8 +223,7 @@ namespace AyaNova.Api.Controllers
}
//No users matched, it's a failed login
//Make a failed pw wait
metrics.Measure.Meter.Mark(MetricsRegistry.FailedLoginMeter);
//Make a failed pw wait
await Task.Delay(nFailedAuthDelay);
return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED));
}
@@ -258,7 +252,6 @@ namespace AyaNova.Api.Controllers
if (string.IsNullOrWhiteSpace(changecreds.OldPassword) || string.IsNullOrWhiteSpace(changecreds.LoginName))
{
metrics.Measure.Meter.Mark(MetricsRegistry.FailedLoginMeter);
//Make a failed pw wait
await Task.Delay(nFailedAuthDelay);
return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED));

View File

@@ -1,128 +1,127 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using App.Metrics;
using AyaNova.Models;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
// using System.IO;
// using System.Threading.Tasks;
// using Microsoft.AspNetCore.Http;
// using Microsoft.AspNetCore.Mvc;
// using Microsoft.AspNetCore.Routing;
// using Microsoft.AspNetCore.Authorization;
// using Microsoft.Extensions.Logging;
// using Newtonsoft.Json.Linq;
// using AyaNova.Models;
// using AyaNova.Api.ControllerHelpers;
// using AyaNova.Biz;
namespace AyaNova.Api.Controllers
{
// namespace AyaNova.Api.Controllers
// {
/// <summary>
/// Log files controller
/// </summary>
[ApiController]
[ApiVersion("8.0")]
[Route("api/v{version:apiVersion}/metric")]
[Authorize]
public class MetricsController : ControllerBase
{
private readonly AyContext ct;
private readonly ILogger<LogFilesController> log;
private readonly ApiServerState serverState;
private readonly IMetrics metrics;
// /// <summary>
// /// Log files controller
// /// </summary>
// [ApiController]
// [ApiVersion("8.0")]
// [Route("api/v{version:apiVersion}/metric")]
// [Authorize]
// public class MetricsController : ControllerBase
// {
// private readonly AyContext ct;
// private readonly ILogger<LogFilesController> log;
// private readonly ApiServerState serverState;
/// <summary>
/// ctor
/// </summary>
/// <param name="dbcontext"></param>
/// <param name="logger"></param>
/// <param name="apiServerState"></param>
/// <param name="Metrics"></param>
public MetricsController(AyContext dbcontext, ILogger<LogFilesController> logger, ApiServerState apiServerState, IMetrics Metrics)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
metrics = Metrics;
}
// /// <summary>
// /// ctor
// /// </summary>
// /// <param name="dbcontext"></param>
// /// <param name="logger"></param>
// /// <param name="apiServerState"></param>
// public MetricsController(AyContext dbcontext, ILogger<LogFilesController> logger, ApiServerState apiServerState)
// {
// ct = dbcontext;
// log = logger;
// serverState = apiServerState;
// }
/// <summary>
/// Get metrics as text document
/// </summary>
/// <returns>Snapshot of metrics</returns>
[HttpGet("textsnapshot")]
public async Task<IActionResult> GetMetrics()
{
if (serverState.IsClosed)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
// /// <summary>
// /// Get metrics as text document
// /// </summary>
// /// <returns>Snapshot of metrics</returns>
// [HttpGet("textsnapshot")]
// public async Task<IActionResult> GetMetrics()
// {
// if (serverState.IsClosed)
// return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.Metrics))
{
return StatusCode(403, new ApiNotAuthorizedResponse());
}
// if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.Metrics))
// {
// return StatusCode(403, new ApiNotAuthorizedResponse());
// }
string sResult = await GetTheMetrics("plain");
// string sResult = await GetTheMetrics("plain");
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.Metrics, AyaEvent.Retrieved), ct);
// //Log
// await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.Metrics, AyaEvent.Retrieved), ct);
return Content(sResult);
}
// return Content(sResult);
// }
/// <summary>
/// Get metrics as json object
/// </summary>
/// <returns>Snapshot of metrics</returns>
[HttpGet("jsonsnapshot")]
public async Task<IActionResult> GetJsonMetrics()
{
if (serverState.IsClosed)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
// /// <summary>
// /// Get metrics as json object
// /// </summary>
// /// <returns>Snapshot of metrics</returns>
// [HttpGet("jsonsnapshot")]
// public async Task<IActionResult> GetJsonMetrics()
// {
// if (serverState.IsClosed)
// return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.Metrics))
{
return StatusCode(403, new ApiNotAuthorizedResponse());
}
// if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.Metrics))
// {
// return StatusCode(403, new ApiNotAuthorizedResponse());
// }
string sResult = await GetTheMetrics("json");
JObject json = JObject.Parse(sResult);
// string sResult = await GetTheMetrics("json");
// JObject json = JObject.Parse(sResult);
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.Metrics, AyaEvent.Retrieved), ct);
// //Log
// await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.Metrics, AyaEvent.Retrieved), ct);
return Ok(ApiOkResponse.Response(json));
}
// return Ok(ApiOkResponse.Response(json));
// }
/// <summary>
/// Get the metrics snapshot
/// </summary>
/// <param name="format">Either "json" for json format or "plain" for plaintext format</param>
/// <returns></returns>
private async Task<string> GetTheMetrics(string format)
{
var snapshot = metrics.Snapshot.Get();
// /// <summary>
// /// Get the metrics snapshot
// /// </summary>
// /// <param name="format">Either "json" for json format or "plain" for plaintext format</param>
// /// <returns></returns>
// private async Task<string> GetTheMetrics(string format)
// {
// var snapshot = metrics.Snapshot.Get();
var formatters = ((IMetricsRoot)metrics).OutputMetricsFormatters;
string sResult = $"ERROR GETTING METRICS IN {format} FORMAT";
// var formatters = ((IMetricsRoot)metrics).OutputMetricsFormatters;
// string sResult = $"ERROR GETTING METRICS IN {format} FORMAT";
foreach (var formatter in formatters)
{
if (formatter.MediaType.Format == format)
{
using (var stream = new MemoryStream())
{
await formatter.WriteAsync(stream, snapshot);
sResult = System.Text.Encoding.UTF8.GetString(stream.ToArray());
}
}
}
// foreach (var formatter in formatters)
// {
// if (formatter.MediaType.Format == format)
// {
// using (var stream = new MemoryStream())
// {
// await formatter.WriteAsync(stream, snapshot);
// sResult = System.Text.Encoding.UTF8.GetString(stream.ToArray());
// }
// }
// }
return sResult;
}
// return sResult;
// }
//------------
// //------------
}
}
// }
// }