Added failed query logging to datalistfetcher

This commit is contained in:
2020-05-14 13:36:10 +00:00
parent f3eb085169
commit 6b673699cc
2 changed files with 84 additions and 69 deletions

View File

@@ -73,7 +73,7 @@ namespace AyaNova.Api.Controllers
try try
{ {
ApiDataListResponse r = await DataListFetcher.GetResponseAsync(listOptions.DataListKey, ct, listOptions, UserId, UserRoles); ApiDataListResponse r = await DataListFetcher.GetResponseAsync(listOptions.DataListKey, ct, listOptions, UserId, UserRoles, log);
return Ok(r); return Ok(r);
} }
catch (System.UnauthorizedAccessException) catch (System.UnauthorizedAccessException)

View File

@@ -3,7 +3,7 @@ using System.Linq;
using AyaNova.Biz; using AyaNova.Biz;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using AyaNova.Api.ControllerHelpers; using AyaNova.Api.ControllerHelpers;
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging;
using AyaNova.Models; using AyaNova.Models;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -13,7 +13,7 @@ namespace AyaNova.DataList
{ {
internal static class DataListFetcher internal static class DataListFetcher
{ {
internal static async Task<ApiDataListResponse> GetResponseAsync(string DataListKey, AyContext ct, ListOptions listOptions, long UserId, AuthorizationRoles UserRoles) internal static async Task<ApiDataListResponse> GetResponseAsync(string DataListKey, AyContext ct, ListOptions listOptions, long UserId, AuthorizationRoles UserRoles, ILogger log)
{ {
var DataList = DataListFactory.GetAyaDataList(DataListKey); var DataList = DataListFactory.GetAyaDataList(DataListKey);
@@ -83,99 +83,114 @@ namespace AyaNova.DataList
//GET DATA RETURN ROWS //GET DATA RETURN ROWS
command.CommandText = qDataQuery; command.CommandText = qDataQuery;
using (var dr = await command.ExecuteReaderAsync()) try
{ {
while (dr.Read()) using (var dr = await command.ExecuteReaderAsync())
{ {
List<AyaFieldData> row = new List<AyaFieldData>(returnRowColumnCount); while (dr.Read())
//INSERT REMAINING FIELDS FROM TEMPLATE INTO THE RETURN ROWS LIST
foreach (string TemplateField in ListViewFieldList)
{ {
List<AyaFieldData> row = new List<AyaFieldData>(returnRowColumnCount);
//get the AyaObjectFieldDefinition
AyaDataListFieldDefinition f = DataList.FieldDefinitions.FirstOrDefault(x => x.FieldKey == TemplateField); //INSERT REMAINING FIELDS FROM TEMPLATE INTO THE RETURN ROWS LIST
if (f.IsCustomField) foreach (string TemplateField in ListViewFieldList)
{ {
AyaFieldData AyaField = new AyaFieldData(); //get the AyaObjectFieldDefinition
var cust = dr.GetString(SelectBuild.map[f.GetSqlValueColumnName()]); AyaDataListFieldDefinition f = DataList.FieldDefinitions.FirstOrDefault(x => x.FieldKey == TemplateField);
if (!string.IsNullOrWhiteSpace(cust)) if (f.IsCustomField)
{ {
JObject j = JObject.Parse(cust);
//convert field name to cust name then get value
var InternalCustomFieldName = AyaFormFieldDefinitions.TranslateLTCustomFieldToInternalCustomFieldName(TemplateField);
//Sometimes a custom field is specified but doesn't exist in the collection so don't assume it's there
// AyaField.v = j[InternalCustomFieldName].Value<object>();
JToken o = j[InternalCustomFieldName];
if (o != null)
AyaField.v = o.Value<object>();
else
AyaField.v = null;
row.Add(AyaField); AyaFieldData AyaField = new AyaFieldData();
} var cust = dr.GetString(SelectBuild.map[f.GetSqlValueColumnName()]);
if (!string.IsNullOrWhiteSpace(cust))
{
JObject j = JObject.Parse(cust);
//convert field name to cust name then get value
var InternalCustomFieldName = AyaFormFieldDefinitions.TranslateLTCustomFieldToInternalCustomFieldName(TemplateField);
//Sometimes a custom field is specified but doesn't exist in the collection so don't assume it's there
// AyaField.v = j[InternalCustomFieldName].Value<object>();
JToken o = j[InternalCustomFieldName];
if (o != null)
AyaField.v = o.Value<object>();
else
AyaField.v = null;
/* row.Add(AyaField);
TODO: Custom field handling }
GetName works just not with multipart identifiers
I could force naming by making all fields and AS, or
I could map the ordinal when generating the Select fields so that I have a map to refer to here
mapping in advance actually makes a lot of sense, then no more of this fragility of going by pointer index and hoping for the best
it would just be premapped out.
dr.GetOrdinal(f.SqlValueColumnName) /*
'dr.GetOrdinal(f.SqlValueColumnName)' threw an exception of type 'System.IndexOutOfRangeException' TODO: Custom field handling
f.SqlValueColumnName GetName works just not with multipart identifiers
"awidget.customfields" I could force naming by making all fields and AS, or
dr.GetName(nCurrentColumnPointer) I could map the ordinal when generating the Select fields so that I have a map to refer to here
"customfields" mapping in advance actually makes a lot of sense, then no more of this fragility of going by pointer index and hoping for the best
dr.GetOrdinal("customfields"); it would just be premapped out.
5
*/ dr.GetOrdinal(f.SqlValueColumnName)
} 'dr.GetOrdinal(f.SqlValueColumnName)' threw an exception of type 'System.IndexOutOfRangeException'
else f.SqlValueColumnName
{ "awidget.customfields"
AyaFieldData AyaField = new AyaFieldData(); dr.GetName(nCurrentColumnPointer)
AyaField.v = dr.GetValue(SelectBuild.map[f.GetSqlValueColumnName()]); "customfields"
dr.GetOrdinal("customfields");
5
if (f.IsRowId) */
{
AyaField.rid = true;
} }
else else
{ {
AyaField.rid = null; AyaFieldData AyaField = new AyaFieldData();
} AyaField.v = dr.GetValue(SelectBuild.map[f.GetSqlValueColumnName()]);
if (f.SqlIdColumnName != null) if (f.IsRowId)
{ {
var ordinal = SelectBuild.map[f.SqlIdColumnName]; AyaField.rid = true;
if (!await dr.IsDBNullAsync(ordinal)) }
AyaField.i = dr.GetInt64(ordinal); else
{
AyaField.rid = null;
}
if (f.SqlIdColumnName != null)
{
var ordinal = SelectBuild.map[f.SqlIdColumnName];
if (!await dr.IsDBNullAsync(ordinal))
AyaField.i = dr.GetInt64(ordinal);
}
row.Add(AyaField);
} }
row.Add(AyaField);
} }
rows.Add(row);
}
}
//GET TOTAL RECORD COUNT
command.CommandText = qTotalRecordsQuery;
using (var dr = await command.ExecuteReaderAsync())
{
if (dr.Read())
{
totalRecordCount = dr.GetInt64(0);
} }
rows.Add(row);
} }
} }
catch (Npgsql.PostgresException e)
//GET TOTAL RECORD COUNT
command.CommandText = qTotalRecordsQuery;
using (var dr = await command.ExecuteReaderAsync())
{ {
if (dr.Read()) //log out the exception and the query
{ log.LogInformation("DataList query failed unexpectedly. Data Query was:");
totalRecordCount = dr.GetInt64(0); log.LogInformation(qDataQuery);
} log.LogInformation("Count Query was:");
log.LogInformation(qTotalRecordsQuery);
log.LogInformation(e, "DB Exception");
throw new System.Exception("DataListFetcher - Query failed see log");
} }
} }
//BUILD THE COLUMNS RETURN PROPERTY JSON FRAGMENT //BUILD THE COLUMNS RETURN PROPERTY JSON FRAGMENT
Newtonsoft.Json.Linq.JArray ColumnsJSON = null; Newtonsoft.Json.Linq.JArray ColumnsJSON = null;