This commit is contained in:
2018-08-24 18:08:21 +00:00
parent 1cfa78472e
commit 06aeca8e16
2 changed files with 40 additions and 42 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@@ -5,6 +6,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using AyaNova.Models; using AyaNova.Models;
using AyaNova.Api.ControllerHelpers; using AyaNova.Api.ControllerHelpers;
@@ -25,7 +27,7 @@ namespace AyaNova.Api.Controllers
private readonly AyContext ct; private readonly AyContext ct;
private readonly ILogger<LogFilesController> log; private readonly ILogger<LogFilesController> log;
private readonly ApiServerState serverState; private readonly ApiServerState serverState;
/// <summary> /// <summary>
/// ctor /// ctor
@@ -41,15 +43,21 @@ namespace AyaNova.Api.Controllers
} }
//TODO: flesh out these routes, just text only for now
//Need to set roles properly
// User should be able to get own user log, but not someone else's without elevated rights, this is a bizadmin type thing, ops maybe shouldn't be able to see it? Or should? not sure
//Owner or with rights to type should be able to get object log
//Actual log processor and constructor should be in EventLogProcessor
/// <summary> /// <summary>
/// Get events as text document for object specified /// Get events as text document for object specified
/// ///
/// Required roles: /// Required roles:
/// OpsAdminFull | OpsAdminLimited /// OpsAdminFull | OpsAdminLimited
/// </summary> /// </summary>
/// <returns>Snapshot of metrics</returns> /// <returns>Event log for object</returns>
[HttpGet("TextSnapShot")] [HttpGet("ObjectLog")]
public async Task<IActionResult> GetObjectLog() public async Task<IActionResult> GetObjectLog([FromQuery] EventLogOptions opt)
{ {
//Open or opsOnly and user is opsadminfull or opsadminlimited //Open or opsOnly and user is opsadminfull or opsadminlimited
if (!serverState.IsOpenOrOpsOnly || (serverState.IsOpsOnly && !Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.OpsAdminFull | AuthorizationRoles.OpsAdminLimited))) if (!serverState.IsOpenOrOpsOnly || (serverState.IsOpsOnly && !Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.OpsAdminFull | AuthorizationRoles.OpsAdminLimited)))
@@ -71,15 +79,16 @@ namespace AyaNova.Api.Controllers
} }
/// <summary>
/// Get metrics as json object /// <summary>
/// Get events for a user as text document for object specified
/// ///
/// Required roles: /// Required roles:
/// OpsAdminFull | OpsAdminLimited /// OpsAdminFull | OpsAdminLimited
/// </summary> /// </summary>
/// <returns>Snapshot of metrics</returns> /// <returns>Event log for user</returns>
[HttpGet("JsonSnapShot")] [HttpGet("UserLog")]
public async Task<IActionResult> GetJsonMetrics() public async Task<IActionResult> GetUserLog([FromQuery] EventLogOptions opt)
{ {
//Open or opsOnly and user is opsadminfull or opsadminlimited //Open or opsOnly and user is opsadminfull or opsadminlimited
if (!serverState.IsOpenOrOpsOnly || (serverState.IsOpsOnly && !Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.OpsAdminFull | AuthorizationRoles.OpsAdminLimited))) if (!serverState.IsOpenOrOpsOnly || (serverState.IsOpsOnly && !Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.OpsAdminFull | AuthorizationRoles.OpsAdminLimited)))
@@ -92,45 +101,31 @@ namespace AyaNova.Api.Controllers
return StatusCode(401, new ApiNotAuthorizedResponse()); return StatusCode(401, new ApiNotAuthorizedResponse());
} }
string sResult = await GetTheMetrics("json"); string sResult = await GetTheMetrics("plain");
JObject json = JObject.Parse(sResult);
//Log //Log
EventLogProcessor.AddEntry(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.Metrics, AyaEvent.Retrieved), ct); EventLogProcessor.AddEntry(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.Metrics, AyaEvent.Retrieved), ct);
return Ok(new ApiOkResponse(json)); return Content(sResult);
} }
/// <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";
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;
}
//------------ //------------
} public sealed class EventLogOptions
} {
[FromQuery]
public AyaType AyType { get; set; }
[FromQuery]
public long AyId { get; set; }
[FromQuery]
public DateTime StartDate { get; set; }
[FromQuery]
public DateTime EndDate { get; set; }
}
}//eoc
}//eons

View File

@@ -46,6 +46,9 @@ namespace AyaNova.Biz
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
}//eoc }//eoc
}//eons }//eons