This commit is contained in:
85
server/AyaNova/Controllers/KPIController.cs
Normal file
85
server/AyaNova/Controllers/KPIController.cs
Normal 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
|
||||||
@@ -8,7 +8,7 @@ namespace AyaNova.KPI
|
|||||||
AuthorizationRoles AllowedRoles { get; }
|
AuthorizationRoles AllowedRoles { get; }
|
||||||
|
|
||||||
//build the data and meta queries based on the criteria and this kpi's standard query
|
//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 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 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
|
string ErrorMessage{get;}//if there was a problem then this is set with the error message
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace AyaNova.KPI
|
|||||||
// Get the kpi data requested
|
// 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
|
//instantiate the list
|
||||||
var kpi = KPIFactory.GetAyaKPI(kpiName);
|
var kpi = KPIFactory.GetAyaKPI(options.KPIName);
|
||||||
kpi.BuildQuery(criteria, clientTimeStamp);
|
|
||||||
|
if (!AyaNova.Api.ControllerHelpers.Authorized.HasAnyRole(userRoles, kpi.AllowedRoles))
|
||||||
|
{
|
||||||
|
return JObject.FromObject(new
|
||||||
|
{
|
||||||
|
error = "NOT AUTHORIZED"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
kpi.BuildQuery(options);
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(kpi.ErrorMessage))
|
if (!string.IsNullOrWhiteSpace(kpi.ErrorMessage))
|
||||||
{
|
{
|
||||||
|
|||||||
29
server/AyaNova/kpi/KPIRequestOptions.cs
Normal file
29
server/AyaNova/kpi/KPIRequestOptions.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ namespace AyaNova.KPI
|
|||||||
{
|
{
|
||||||
private string _metaQuery = null;
|
private string _metaQuery = null;
|
||||||
private string _dataQuery = null;
|
private string _dataQuery = null;
|
||||||
private string _errorMessage=null;
|
private string _errorMessage = null;
|
||||||
|
|
||||||
public AuthorizationRoles AllowedRoles { get => BizRoles.GetRoleSet(AyaType.WorkOrderItemLabor).ReadFullRecord; }
|
public AuthorizationRoles AllowedRoles { get => BizRoles.GetRoleSet(AyaType.WorkOrderItemLabor).ReadFullRecord; }
|
||||||
|
|
||||||
@@ -18,9 +18,23 @@ namespace AyaNova.KPI
|
|||||||
public string DataQuery => _dataQuery;
|
public string DataQuery => _dataQuery;
|
||||||
public string ErrorMessage => _errorMessage;
|
public string ErrorMessage => _errorMessage;
|
||||||
|
|
||||||
public void BuildQuery(string criteria, DateTimeOffset clientTimeStamp)
|
public void BuildQuery(KPIRequestOptions options)
|
||||||
{
|
{
|
||||||
//build data and meta queries
|
//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)
|
// public string GetVariantCriteria(string variant)
|
||||||
|
|||||||
Reference in New Issue
Block a user