This commit is contained in:
2022-02-25 00:34:25 +00:00
parent e0bf2cb733
commit 0424e78fb7
5 changed files with 144 additions and 7 deletions

View File

@@ -0,0 +1,85 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using AyaNova.Models;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
using AyaNova.KPI;
using System.Threading.Tasks;
using System.Linq;
namespace AyaNova.Api.Controllers
{
[ApiController]
[ApiVersion("8.0")]
[Route("api/v{version:apiVersion}/kpi")]
[Produces("application/json")]
[Authorize]
public class KPIController : ControllerBase
{
private readonly AyContext ct;
private readonly ILogger<KPIController> log;
private readonly ApiServerState serverState;
/// <summary>
/// ctor
/// </summary>
/// <param name="dbcontext"></param>
/// <param name="logger"></param>
/// <param name="apiServerState"></param>
public KPIController(AyContext dbcontext, ILogger<KPIController> logger, ApiServerState apiServerState)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
}
/// <summary>
/// Get KPI data
/// </summary>
/// <param name="options">Parameters for pick list see api docs for details </param>
/// <returns>Filtered list</returns>
[HttpPost]
public async Task<IActionResult> PostList([FromBody] KPIRequestOptions options)
{
if (serverState.IsClosed)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
//UserTypeFromContext.Type(HttpContext.Items)
return Ok(await KPIFetcher.GetResponseAsync(ct, options, UserRolesFromContext.Roles(HttpContext.Items), log, UserIdFromContext.Id(HttpContext.Items)));
}
//SAVE FOR LATER IF WANT TO RETURN LIST OF ALL KPI'S (likely something to do with report editing??)
// /// <summary>
// /// List of all DataList keys available
// /// </summary>
// /// <returns>List of strings</returns>
// [HttpGet("listkeys")]
// public ActionResult GetDataListKeys()
// {
// //NOTE: not used by AyaNova Client, convenience method for developers api usage
// if (!serverState.IsOpen)
// return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
// return Ok(ApiOkResponse.Response(DataListFactory.GetListOfAllDataListKeyNames()));
// }
}//eoc
}//ens

View File

@@ -8,7 +8,7 @@ namespace AyaNova.KPI
AuthorizationRoles AllowedRoles { get; }
//build the data and meta queries based on the criteria and this kpi's standard query
void BuildQuery(string criteria, DateTimeOffset clientTimeStamp);
void BuildQuery(KPIRequestOptions options);
string MetaQuery{get;}//Query to fetch json meta data for report purposes mainly (lookup stuff like names etc where applicable)
string DataQuery{get;}//Query to fetch json format data for result set
string ErrorMessage{get;}//if there was a problem then this is set with the error message

View File

@@ -18,7 +18,7 @@ namespace AyaNova.KPI
// 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)
internal static async Task<JObject> GetResponseAsync(AyContext ct, KPIRequestOptions options, AuthorizationRoles userRoles, ILogger log, long userId)
{
/*
@@ -30,8 +30,17 @@ namespace AyaNova.KPI
}
*/
//instantiate the list
var kpi = KPIFactory.GetAyaKPI(kpiName);
kpi.BuildQuery(criteria, clientTimeStamp);
var kpi = KPIFactory.GetAyaKPI(options.KPIName);
if (!AyaNova.Api.ControllerHelpers.Authorized.HasAnyRole(userRoles, kpi.AllowedRoles))
{
return JObject.FromObject(new
{
error = "NOT AUTHORIZED"
});
}
kpi.BuildQuery(options);
if (!string.IsNullOrWhiteSpace(kpi.ErrorMessage))
{

View File

@@ -0,0 +1,29 @@
using Microsoft.AspNetCore.Mvc;
using System;
namespace AyaNova.KPI
{
//string kpiName, string criteria, DateTimeOffset clientTimeStamp
public sealed class KPIRequestOptions
{
[FromBody]
public string KPIName { get; set; }
[FromBody]
public string Criteria { get; set; }
[FromBody]
public DateTimeOffset Template { get; set; }
public KPIRequestOptions()
{
KPIName=string.Empty;
Criteria=string.Empty;
}
}
}

View File

@@ -10,17 +10,31 @@ namespace AyaNova.KPI
{
private string _metaQuery = null;
private string _dataQuery = null;
private string _errorMessage=null;
private string _errorMessage = null;
public AuthorizationRoles AllowedRoles { get => BizRoles.GetRoleSet(AyaType.WorkOrderItemLabor).ReadFullRecord; }
public string MetaQuery => _metaQuery;
public string DataQuery => _dataQuery;
public string ErrorMessage => _errorMessage;
public void BuildQuery(string criteria, DateTimeOffset clientTimeStamp)
public void BuildQuery(KPIRequestOptions options)
{
//build data and meta queries
_dataQuery = @"SELECT row_to_json(t) as res from (
select SUM(AWORKORDERITEMLABOR.serviceratequantity) SERVICERATESUM, date_trunc('month',AWORKORDERITEMLABOR.servicestartdate) timeframe
FROM AWORKORDER
LEFT JOIN AWORKORDERITEM ON AWORKORDER.ID = AWORKORDERITEM.WORKORDERID
LEFT JOIN AWORKORDERITEMLABOR ON AWORKORDERITEM.ID = AWORKORDERITEMLABOR.WORKORDERITEMID
WHERE AWORKORDERITEMLABOR.userid = 10
GROUP BY timeframe
ORDER BY timeframe ASC
) t";
_metaQuery = @"SELECT row_to_json(t) as res from (
select name from auser where id = 10
) t";
}
// public string GetVariantCriteria(string variant)