Skip to content

Commit

Permalink
[#65] Add support for external status cell
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexP-Elastic committed Sep 28, 2019
1 parent 7c87dbe commit 0f758ab
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
10 changes: 7 additions & 3 deletions src/server/models/TableModel.gs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ var defaultTableConfig_ = {
// "timed": false,
// "refresh_s": 60
// },
//TODO: have content_global_triggers AND control_global_triggers
"global_triggers": [], //array of ranges ("sheet!range") to include when deciding whether to refresh the table
"query": {
// "index_pattern": "tbd",
"source": "none",
//, //^points to field to use ("global", "local")
"global": {
"range_name": "sheet!range"
"range_name": "[sheet!]range"
},
"local": {
"position": "top" //(or "bottom")
Expand All @@ -31,8 +32,11 @@ var defaultTableConfig_ = {
}
},
"status": {
"position": "top", //(or "bottom", "none")
"merge": false //(if false will be its own separate line, else will merge with query/pagination if they exist)
"position": "top", //(or "bottom", "none", "global")
"merge": false, //(if false will be its own separate line, else will merge with query/pagination if they exist)
"global": {
"range_name": "[sheet!]range"
}
},
"headers": {
"position": "top", //(or "bottom", "top_bottom", "none") .. NOT_SUPPORTED: "bottom", "top_bottom"
Expand Down
5 changes: 4 additions & 1 deletion src/server/utils/ElasticsearchRequestUtils.gs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ var ElasticsearchRequestUtils_ = (function() {
statusCells.merge()
break
}
}
} else if (!testMode) { // Check for global status
var ss = SpreadsheetApp.getActive()
TableRangeUtils_.handleGlobalStatusInfo(ss, statusInfo, tableConfig)
}

// Headers

Expand Down
31 changes: 18 additions & 13 deletions src/server/utils/ElasticsearchResponseUtils.gs
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,16 @@ var ElasticsearchResponseUtils_ = (function() {
json, rows, fullCols,
supportsSize, numHits, numHitsOperator
) {
var ss = SpreadsheetApp.getActive()
var tableRange = TableRangeUtils_.findTableRange(ss, tableName)
var range = null
if (null == tableRange) { //(use current selection, test mode
var ss = SpreadsheetApp.getActive()
var tableRange = TableRangeUtils_.findTableRange(ss, tableName)
var range = null
if (null == tableRange) { //(use current selection, test mode
range = ss.getActiveRange()
} else {
range = tableRange.getRange()
}
} else {
range = tableRange.getRange()
}
var globalStatus =
"global" == TableRangeUtils_.getJson(tableConfig, [ "common", "status", "position" ])

if (null != json.response) {
/** Apply the global filters to the cols and re-order as desired */
Expand Down Expand Up @@ -490,13 +492,13 @@ var ElasticsearchResponseUtils_ = (function() {
}

// Write warnings to status (never to toaster)
if (context.table_meta.status_offset) {
if (context.table_meta.status_offset || globalStatus) {
var warningText = ""
if (warnings.length > 0) {
warningText = ": (WARNINGS = " + warnings.map(function(x) { return "[" + x + "]" }).join(", ") + ")"
}
setQueryResponseInStatus_(range, context.table_meta.status_offset,
"SUCCESS [" + TableRangeUtils_.formatDate() + "]" + warningText)
"SUCCESS [" + TableRangeUtils_.formatDate() + "]" + warningText, tableConfig)
}
} else if (null != json.error_message) {
// Write errors to status or toaster
Expand All @@ -505,8 +507,8 @@ var ElasticsearchResponseUtils_ = (function() {
:
"ERROR [" + TableRangeUtils_.formatDate() + "]" + ": status = [" + json.status + "], msg = [" + json.error_message + "], query = [" + json.query_string + "]"

if (context.table_meta.status_offset) {
setQueryResponseInStatus_(range, context.table_meta.status_offset, requestError)
if (context.table_meta.status_offset || globalStatus) {
setQueryResponseInStatus_(range, context.table_meta.status_offset, requestError, tableConfig)
} else { // pop up toaster
showStatus("[" + tableName + "]: " + requestError, "Query Error")
}
Expand Down Expand Up @@ -685,8 +687,11 @@ var ElasticsearchResponseUtils_ = (function() {
}

/** Adds the error info the status, if necessary */
function setQueryResponseInStatus_(range, statusLocation, errorString) {
range.getCell(statusLocation.row, statusLocation.col).setValue(errorString)
function setQueryResponseInStatus_(range, statusLocation, errorString, tableConfig) {
var ss = SpreadsheetApp.getActive()
if (!TableRangeUtils_.handleGlobalStatusInfo(ss, errorString, tableConfig)) {
range.getCell(statusLocation.row, statusLocation.col).setValue(errorString)
}
}

////////////////////////////////////////////////////////
Expand Down
19 changes: 19 additions & 0 deletions src/server/utils/TableRangeUtils.gs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,24 @@
: null //(see above: also support named ranges, cf getJsonLookup)
}

/** Utility function to write global status info out - returns true iff status _is_ global */
function handleGlobalStatusInfo(ss, statusInfo, tableConfig) {
if ( //Global query, get from its external location:
"global" == TableRangeUtils_.getJson(tableConfig, [ "common", "status", "position" ])
) {
var globalSourceRef = TableRangeUtils_.getJson(
tableConfig, [ "common", "status", "global", "range_name" ]
)
var globalSourceRange = globalSourceRef ?
TableRangeUtils_.getRangeFromName(ss, globalSourceRef) : null
if (globalSourceRange) {
globalSourceRange.getCell(1, 1).setValue(statusInfo)
return true
}
}
return false
}

////////////////////////////////////////////////////////

// 3] Internal utils
Expand Down Expand Up @@ -438,6 +456,7 @@
doRangesIntersect: doRangesIntersect,
getExternalTableRanges: getExternalTableRanges,
getRangeFromName: getRangeFromName,
handleGlobalStatusInfo: handleGlobalStatusInfo,

TESTONLY: {

Expand Down
23 changes: 20 additions & 3 deletions test/server/services/TestElasticsearchService.gs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@
},
"status": {
"position": "none",
"merge": false
"merge": false,
"global": {
"range_name": "TBD"
}
},
"formatting": {
"theme": "minimal"
Expand All @@ -130,6 +133,7 @@

var defaultA1Notation = "A1:E10"
var externalQueryNotation = "H20:H20"
var externalStatusNotation = "J22"

TestService_.Utils.performTest(testResults, "no_special_rows_plus_check_es_meta", function() {
var tableConfig = TestService_.Utils.deepCopyJson(baseTableConfig)
Expand Down Expand Up @@ -217,7 +221,7 @@
"pagination_status": { status: "bottom", merge: true, noteMode: 3 },
"pagination_status_test": { status: "bottom", merge: true, testMode: true, noteMode: 3 },
"query_status_pagination_nomerge": { status: "bottom", merge: false, noteMode: 4 },
"global_query": { noteMode: 4 }
"global_query_global_status": { noteMode: 4 }
}

var testRunner = function(testName, testConfig) { TestService_.Utils.performTest(testResults, testName, function() {
Expand All @@ -229,9 +233,10 @@
var testMode = testConfig.testMode

var globalQuery = testName.indexOf("global_query") >= 0
var globalStatus = testName.indexOf("global_status") >= 0
var includeQueryBar = !globalQuery && testName.indexOf("query") >= 0
var includePagination = testName.indexOf("pagination") >= 0
var includeStatus = testName.indexOf("status") >= 0
var includeStatus = !globalStatus && testName.indexOf("status") >= 0

// Note tests:
// 1: add note, replaces no note
Expand Down Expand Up @@ -284,6 +289,10 @@
tableConfig.common.pagination.source = "local"
}
var statusPosition = { }
if (globalStatus) {
tableConfig.common.status.position = "global"
tableConfig.common.status.global.range_name = externalStatusNotation
}
if (includeStatus) {
if (!testConfig.merge) {
expectedDataSize--
Expand Down Expand Up @@ -387,6 +396,14 @@
TestService_.Utils.assertEquals(expectedPage, range.getCell(pagePosition.row, pagePosition.col).getValue(), "page value" + extraTestCaseConfig)
}
}
if (globalStatus) {
var statusShouldBePending = testSheet.getRange(externalStatusNotation).getValue()
if (testMode) {
TestService_.Utils.assertEquals("", statusShouldBePending, "status value: " + statusShouldBePending + extraTestCaseConfig)
} else {
TestService_.Utils.assertEquals(true, 0 == statusShouldBePending.indexOf("PENDING"), "status value: " + statusShouldBePending + extraTestCaseConfig)
}
}
if (includeStatus) {
var statusShouldBePending = range.getCell(statusPosition.row, statusPosition.col).getValue()
if (testMode) {
Expand Down

0 comments on commit 0f758ab

Please sign in to comment.