This commit is contained in:
139
server/AyaNova/kpi/KPIFetcher.cs
Normal file
139
server/AyaNova/kpi/KPIFetcher.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
#define AYSHOWKPIQUERYINFO
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AyaNova.Biz;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using AyaNova.Models;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
|
||||
namespace AyaNova.KPI
|
||||
{
|
||||
internal static class KPIFetcher
|
||||
{
|
||||
////////////////////////////////////////////////
|
||||
// Get the kpi data requested
|
||||
//
|
||||
//
|
||||
internal static async Task<JObject> GetResponseAsync(AyContext ct, string kpiName, string criteria, DateTimeOffset clientTimeStamp, AuthorizationRoles userRoles, ILogger log, long userId)
|
||||
{
|
||||
|
||||
/*
|
||||
return a jobject like:
|
||||
{
|
||||
error: "if error this key present",
|
||||
meta: {username:"joe blow","date range:blah to blah"}
|
||||
data:[{r1},{r2},{r3}]
|
||||
}
|
||||
*/
|
||||
//instantiate the list
|
||||
var kpi = KPIFactory.GetAyaKPI(kpiName);
|
||||
kpi.BuildQuery(criteria, clientTimeStamp);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(kpi.ErrorMessage))
|
||||
{
|
||||
return JObject.FromObject(new
|
||||
{
|
||||
error = kpi.ErrorMessage
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
System.Text.StringBuilder sbData = new System.Text.StringBuilder();
|
||||
string sMeta = string.Empty;
|
||||
|
||||
#if (DEBUG && AYSHOWKPIQUERYINFO)
|
||||
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
|
||||
|
||||
#endif
|
||||
//QUERY THE DB
|
||||
using (var command = ct.Database.GetDbConnection().CreateCommand())
|
||||
{
|
||||
await ct.Database.OpenConnectionAsync();
|
||||
|
||||
//GET DATA RETURN ROWS
|
||||
command.CommandText = kpi.DataQuery;
|
||||
try
|
||||
{
|
||||
#if (DEBUG && AYSHOWKPIQUERYINFO)
|
||||
stopWatch.Start();
|
||||
#endif
|
||||
using (var dr = await command.ExecuteReaderAsync())
|
||||
{
|
||||
#if (DEBUG && AYSHOWKPIQUERYINFO)
|
||||
stopWatch.Stop();
|
||||
log.LogInformation($"(debug) KPIFetcher:GetResponse DATA query took {stopWatch.ElapsedMilliseconds}ms to execute: {kpi.DataQuery}");
|
||||
stopWatch.Reset();
|
||||
#endif
|
||||
|
||||
while (dr.Read())
|
||||
{
|
||||
//only one column and it's the zeroth json string column
|
||||
if (!dr.IsDBNull(0))
|
||||
sbData.Append(dr.GetString(0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(kpi.MetaQuery))
|
||||
{
|
||||
//GET META DATA
|
||||
command.CommandText = kpi.MetaQuery;
|
||||
#if (DEBUG && AYSHOWQUERYINFO)
|
||||
stopWatch.Start();
|
||||
#endif
|
||||
using (var dr = await command.ExecuteReaderAsync())
|
||||
{
|
||||
#if (DEBUG && AYSHOWQUERYINFO)
|
||||
stopWatch.Stop();
|
||||
log.LogInformation($"(debug) KPIFetcher:GetResponse META query took {stopWatch.ElapsedMilliseconds}ms to execute: {qTotalRecordsQuery}");
|
||||
#endif
|
||||
if (dr.Read())
|
||||
{
|
||||
sMeta = dr.GetString(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
catch (Npgsql.PostgresException e)
|
||||
{
|
||||
//log out the exception and the query
|
||||
log.LogError("KPIFetcher:GetResponseAsync query failed. Data Query was:");
|
||||
log.LogError(kpi.DataQuery);
|
||||
log.LogError("Meta Query was:");
|
||||
log.LogError(kpi.MetaQuery);
|
||||
log.LogError(e, "DB Exception");
|
||||
throw new System.Exception("KPIFetcher:GetResponseAsync - Query failed see log");
|
||||
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
//ensure any other type of exception gets surfaced properly
|
||||
//log out the exception and the query
|
||||
log.LogError("KPIFetcher:GetResponseAsync unexpected failure. Data Query was:");
|
||||
log.LogError(kpi.DataQuery);
|
||||
log.LogError("Meta Query was:");
|
||||
log.LogError(kpi.MetaQuery);
|
||||
log.LogError(e, "Exception");
|
||||
throw new System.Exception("KPIFetcher:GetResponseAsync - unexpected failure see log");
|
||||
}
|
||||
|
||||
return JObject.FromObject(new
|
||||
{
|
||||
meta = sMeta,
|
||||
data = sbData
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}//eoc
|
||||
}//eons
|
||||
Reference in New Issue
Block a user