This commit is contained in:
2020-06-01 23:08:19 +00:00
parent 031c2432f3
commit c266848774
4 changed files with 67 additions and 4 deletions

View File

@@ -178,6 +178,49 @@ namespace AyaNova.Api.Controllers
/// <summary>
/// Get database related metrics for time period specified
/// </summary>
/// <param name="tsStart">Start timestamp UTC</param>
/// <param name="tsEnd">End timestamp UTC</param>
/// <param name="maxRecords">Optional maximum records to return (downsampled). There is a 400 record maximum fixed default</param>
/// <returns>Snapshot of metrics</returns>
[HttpGet("db")]
public async Task<IActionResult> GetDBMetrics([FromQuery, Required] DateTime? tsStart, [FromQuery, Required] DateTime? tsEnd, [FromQuery] int? maxRecords)
{
//Note: the date and times are nullable and required so that the regular modelstate code kicks in to ensure they are present
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 (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
maxRecords ??= DEFAULT_MAX_RECORDS;
List<MetricDD> DBMetrics = new List<MetricDD>();
//touniversal is because the parameters are converted to local time here
//but then sent to the query as local time as well and not universal time which is what it should be
DBMetrics = await ct.MetricDD.AsNoTracking().Where(z => z.t >= ((DateTime)tsStart).ToUniversalTime() && z.t <= ((DateTime)tsEnd).ToUniversalTime()).OrderBy(z => z.t).ToListAsync();
var dsDBTotalSize = DBMetrics.Select(z => new Tuple<double, double>(z.t.ToOADate(), z.DBTotalSize)).ToList();
dsDBTotalSize = Util.DataUtil.LargestTriangleThreeBuckets(dsDBTotalSize, (int)maxRecords) as List<Tuple<double, double>>;
//table distribution, top 10
var ret = new
{
totalSize = dsDBTotalSize.Select(z => new MetricLong(DateTime.FromOADate(z.Item1), z.Item2 / MB)).ToArray()
};
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.Metrics, AyaEvent.Retrieved), ct);
return Ok(ApiOkResponse.Response(ret));
}
//------------