From 6b673699cc8e01130d1cc21ca5e04e52765efcc4 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Thu, 14 May 2020 13:36:10 +0000 Subject: [PATCH] Added failed query logging to datalistfetcher --- .../AyaNova/Controllers/DataListController.cs | 2 +- server/AyaNova/DataList/DataListFetcher.cs | 151 ++++++++++-------- 2 files changed, 84 insertions(+), 69 deletions(-) diff --git a/server/AyaNova/Controllers/DataListController.cs b/server/AyaNova/Controllers/DataListController.cs index 69cda5ba..acfcd396 100644 --- a/server/AyaNova/Controllers/DataListController.cs +++ b/server/AyaNova/Controllers/DataListController.cs @@ -73,7 +73,7 @@ namespace AyaNova.Api.Controllers 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); } catch (System.UnauthorizedAccessException) diff --git a/server/AyaNova/DataList/DataListFetcher.cs b/server/AyaNova/DataList/DataListFetcher.cs index 62f11763..8eb2b24a 100644 --- a/server/AyaNova/DataList/DataListFetcher.cs +++ b/server/AyaNova/DataList/DataListFetcher.cs @@ -3,7 +3,7 @@ using System.Linq; using AyaNova.Biz; using Newtonsoft.Json.Linq; using AyaNova.Api.ControllerHelpers; -using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using AyaNova.Models; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; @@ -13,7 +13,7 @@ namespace AyaNova.DataList { internal static class DataListFetcher { - internal static async Task GetResponseAsync(string DataListKey, AyContext ct, ListOptions listOptions, long UserId, AuthorizationRoles UserRoles) + internal static async Task GetResponseAsync(string DataListKey, AyContext ct, ListOptions listOptions, long UserId, AuthorizationRoles UserRoles, ILogger log) { var DataList = DataListFactory.GetAyaDataList(DataListKey); @@ -83,99 +83,114 @@ namespace AyaNova.DataList //GET DATA RETURN ROWS command.CommandText = qDataQuery; - using (var dr = await command.ExecuteReaderAsync()) + try { - while (dr.Read()) + using (var dr = await command.ExecuteReaderAsync()) { - List row = new List(returnRowColumnCount); - - - //INSERT REMAINING FIELDS FROM TEMPLATE INTO THE RETURN ROWS LIST - foreach (string TemplateField in ListViewFieldList) + while (dr.Read()) { + List row = new List(returnRowColumnCount); - //get the AyaObjectFieldDefinition - AyaDataListFieldDefinition f = DataList.FieldDefinitions.FirstOrDefault(x => x.FieldKey == TemplateField); - if (f.IsCustomField) + + //INSERT REMAINING FIELDS FROM TEMPLATE INTO THE RETURN ROWS LIST + foreach (string TemplateField in ListViewFieldList) { - AyaFieldData AyaField = new AyaFieldData(); - var cust = dr.GetString(SelectBuild.map[f.GetSqlValueColumnName()]); - if (!string.IsNullOrWhiteSpace(cust)) + //get the AyaObjectFieldDefinition + AyaDataListFieldDefinition f = DataList.FieldDefinitions.FirstOrDefault(x => x.FieldKey == TemplateField); + 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(); - JToken o = j[InternalCustomFieldName]; - if (o != null) - AyaField.v = o.Value(); - 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(); + JToken o = j[InternalCustomFieldName]; + if (o != null) + AyaField.v = o.Value(); + else + AyaField.v = null; - /* - 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. + row.Add(AyaField); + } - dr.GetOrdinal(f.SqlValueColumnName) -'dr.GetOrdinal(f.SqlValueColumnName)' threw an exception of type 'System.IndexOutOfRangeException' -f.SqlValueColumnName -"awidget.customfields" -dr.GetName(nCurrentColumnPointer) -"customfields" -dr.GetOrdinal("customfields"); -5 + /* + 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. - */ - } - else - { - AyaFieldData AyaField = new AyaFieldData(); - AyaField.v = dr.GetValue(SelectBuild.map[f.GetSqlValueColumnName()]); + dr.GetOrdinal(f.SqlValueColumnName) + 'dr.GetOrdinal(f.SqlValueColumnName)' threw an exception of type 'System.IndexOutOfRangeException' + f.SqlValueColumnName + "awidget.customfields" + dr.GetName(nCurrentColumnPointer) + "customfields" + dr.GetOrdinal("customfields"); + 5 - if (f.IsRowId) - { - AyaField.rid = true; + */ } else { - AyaField.rid = null; - } + AyaFieldData AyaField = new AyaFieldData(); + AyaField.v = dr.GetValue(SelectBuild.map[f.GetSqlValueColumnName()]); - if (f.SqlIdColumnName != null) - { - var ordinal = SelectBuild.map[f.SqlIdColumnName]; - if (!await dr.IsDBNullAsync(ordinal)) - AyaField.i = dr.GetInt64(ordinal); + if (f.IsRowId) + { + AyaField.rid = true; + } + 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); } } - - //GET TOTAL RECORD COUNT - command.CommandText = qTotalRecordsQuery; - using (var dr = await command.ExecuteReaderAsync()) + catch (Npgsql.PostgresException e) { - if (dr.Read()) - { - totalRecordCount = dr.GetInt64(0); - } + //log out the exception and the query + log.LogInformation("DataList query failed unexpectedly. Data Query was:"); + 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 Newtonsoft.Json.Linq.JArray ColumnsJSON = null;