From ed2fa442028497685637a0f3960c3ce493bc78ab Mon Sep 17 00:00:00 2001 From: Alex-At-Home Date: Sat, 28 Sep 2019 17:38:44 -0400 Subject: [PATCH] [#15] As an interim measure, allow a skip param to be used as a blank column --- src/server/models/TableModel.gs | 2 +- .../utils/ElasticsearchResponseUtils.gs | 45 +++++++++++++------ .../utils/TestElasticsearchResponseUtils.gs | 15 ++++--- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/server/models/TableModel.gs b/src/server/models/TableModel.gs index f7e8b74..ae11229 100644 --- a/src/server/models/TableModel.gs +++ b/src/server/models/TableModel.gs @@ -42,7 +42,7 @@ var defaultTableConfig_ = { "position": "top", //(or "bottom", "top_bottom", "none") .. NOT_SUPPORTED: "bottom", "top_bottom" "field_filters": [ "# eg -x.*.y / +x.y (start with -s where possible)", - "# pre-built groups: $$", + "# pre-built groups: $$ ($$skipN for blank cols)", "#(note fields are laid out in match order)" ], //(# to ignore an entry, [+-] to be +ve/-ve selection, // for regex else * for full wildcard) "exclude_filtered_fields_from_autocomplete": true, diff --git a/src/server/utils/ElasticsearchResponseUtils.gs b/src/server/utils/ElasticsearchResponseUtils.gs index f5dfdaa..cbd6153 100644 --- a/src/server/utils/ElasticsearchResponseUtils.gs +++ b/src/server/utils/ElasticsearchResponseUtils.gs @@ -366,7 +366,8 @@ var ElasticsearchResponseUtils_ = (function() { var cell = range.getCell(specialRows.headers, i + 1) if (i < numDataCols) { var nameOrAliasObj = fullCols[filteredCols[i]] - var nameOrAlias = nameOrAliasObj.alias || nameOrAliasObj.name + var nameOrAlias = + (null != nameOrAliasObj.alias) ? nameOrAliasObj.alias : nameOrAliasObj.name cell.setValue(nameOrAlias) } else { cell.clearContent() @@ -402,13 +403,17 @@ var ElasticsearchResponseUtils_ = (function() { if (i < numDataCols) { if (rowIsArray) { var index = fullCols[filteredCols[i]].index - colArray.push(row[index]) + if (index < row.size) { + colArray.push(row[index]) + } else { + colArray.push("") //(probably a skip col) + } } else { var colName = fullCols[filteredCols[i]].name if (row.hasOwnProperty(colName)) { colArray.push(row[colName]) } else { - colArray.push("") + colArray.push("") //(probably a skip col) } } } else { @@ -553,12 +558,21 @@ var ElasticsearchResponseUtils_ = (function() { isFieldWanted_(mutableCol.name, fieldFilters, onMatch) }) + //Add any $$skipN from the field filters to the mutableCols + fieldFilters.filter(function(fieldFilter) { + return fieldFilter.indexOf("$$skip") == 0 + }).forEach(function(fieldFilter) { + var index = mutableCols.length + var col = { alias: "", name: fieldFilter, index: index } + matchState[fieldFilter] = [ col ] + mutableCols.push(col) + }) + // Order within each group and apply aliases at the same time var globalList = [] fieldFilters.concat([ defaultMatchField ]).forEach(function(fieldFilter) { var fields = matchState[fieldFilter] || [] var startOfList = [] - var endOfList = [] fieldAliases.forEach(function(alias) { fields.forEach(function(mutableCol) { if (mutableCol.name == alias.from) { @@ -598,8 +612,7 @@ var ElasticsearchResponseUtils_ = (function() { ).replace( "$$docmeta_fields", "/^(_id|_index|_score|_type)$/" ) - }) - .forEach(function(elArrayStr) { + }).forEach(function(elArrayStr) { ((elArrayStr.indexOf("/") >= 0) ? [ elArrayStr ] //(1 regex per line) : elArrayStr.split(",")) @@ -607,16 +620,20 @@ var ElasticsearchResponseUtils_ = (function() { .filter(function(el) { return el && ('+' != el) && ('-' != el) }) .filter(function(el) { return el }) .forEach(function(el) { - var firstEl = ('-' == el[0]) ? '-' : '+' - if (('+' == el[0]) || ('-' == el[0])) { - el = el.substring(1) - } - if (('/' == el[0]) && ('/' == el[el.length - 1])) { //already a regex - el = el.substring(1, el.length - 1) + if (0 == el.indexOf("$$skip")) { + filterFields.push(el) } else { - el = "^" + escapeRegExpNotStar(el).replace(/[*]/g, ".*") + "($|[.].*)" + var firstEl = ('-' == el[0]) ? '-' : '+' + if (('+' == el[0]) || ('-' == el[0])) { + el = el.substring(1) + } + if (('/' == el[0]) && ('/' == el[el.length - 1])) { //already a regex + el = el.substring(1, el.length - 1) + } else { + el = "^" + escapeRegExpNotStar(el).replace(/[*]/g, ".*") + "($|[.].*)" + } + filterFields.push(firstEl + el) } - filterFields.push(firstEl + el) }) }) return filterFields diff --git a/test/server/utils/TestElasticsearchResponseUtils.gs b/test/server/utils/TestElasticsearchResponseUtils.gs index c404bb5..5535096 100644 --- a/test/server/utils/TestElasticsearchResponseUtils.gs +++ b/test/server/utils/TestElasticsearchResponseUtils.gs @@ -88,8 +88,10 @@ var TestElasticsearchResponseUtils_ = (function() { TestService_.Utils.performTest(testResults, "various_usages", function() { var filters1 = [ "/col[34]/", + "$$skip1", "+/col[12][ab]?/", - "-filter_out" + "-filter_out", + "$$skip2" ] var testCols = [ { name: "col1" }, //there will match on group2 @@ -100,6 +102,7 @@ var TestElasticsearchResponseUtils_ = (function() { { name: "col4" } ] var aliases = [ + "$$skip2=Blank", "col4=Column4", "col2b=Column2b", "col2a=Column2a", @@ -112,15 +115,15 @@ var TestElasticsearchResponseUtils_ = (function() { "col1", "Column2a", "Column2b", "col3", "filter_out", "Column4" ] var testCols1 = TestService_.Utils.deepCopyJson(testCols) - var reorderedList = [ 5, 3, 2, 1, 0 ] + var reorderedList = [ 5, 3, 6, 2, 1, 0, 7 ] //(6 and 7 are the two skip fields) var result = ElasticsearchResponseUtils_.calculateFilteredCols(testCols1, headerMeta) - var renamed = testCols1.map(function(el) { return el.alias || el.name }) + var renamed = testCols1.map(function(el) { return (null != el.alias) ? el.alias : el.name }) TestService_.Utils.assertEquals( reorderedList, result, "column order (+ve and -ve)" ) TestService_.Utils.assertEquals( - renamedTestCols, renamed, "column names (+ve and -ve)" + renamedTestCols.concat("", "Blank"), renamed, "column names (+ve and -ve)" ) // Check no field filters @@ -137,8 +140,8 @@ var TestElasticsearchResponseUtils_ = (function() { renamedTestCols, renamed, "column names (no field filters)" ) - // Check no field aliases - headerMeta.field_filters = filters1 + // Check no field aliases (+remove skip indices) + headerMeta.field_filters = filters1.filter(function(x) { return x.indexOf("$$skip") != 0 }) headerMeta.field_aliases = [] var testCols3 = TestService_.Utils.deepCopyJson(testCols)