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.Threading.Tasks;
using Microsoft.AspNetCore.Http;
@@ -5,6 +6,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json.Linq;
using AyaNova.Models;
using AyaNova.Api.ControllerHelpers;
@@ -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>
/// Get events as text document for object specified
///
/// Required roles:
/// OpsAdminFull | OpsAdminLimited
/// </summary>
/// <returns>Snapshot of metrics</returns>
[HttpGet("TextSnapShot")]
public async Task<IActionResult> GetObjectLog()
/// <returns>Event log for object</returns>
[HttpGet("ObjectLog")]
public async Task<IActionResult> GetObjectLog([FromQuery] EventLogOptions opt)
{
//Open or opsOnly and user is opsadminfull or 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
/// Get events for a user as text document for object specified
///
/// Required roles:
/// OpsAdminFull | OpsAdminLimited
/// </summary>
/// <returns>Snapshot of metrics</returns>
[HttpGet("JsonSnapShot")]
public async Task<IActionResult> GetJsonMetrics()
/// <returns>Event log for user</returns>
[HttpGet("UserLog")]
public async Task<IActionResult> GetUserLog([FromQuery] EventLogOptions opt)
{
//Open or opsOnly and user is opsadminfull or 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());
}
string sResult = await GetTheMetrics("json");
JObject json = JObject.Parse(sResult);
string sResult = await GetTheMetrics("plain");
//Log
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
}//eons