From e95f043b88842f3b87230d65126011fdd11f954e Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Tue, 1 May 2018 13:16:16 -0600 Subject: [PATCH] refactor checkInstructionSetStatus --- .../components/tutorial/instruction_set.js | 4 +- .../home/components/tutorial/tutorial.js | 66 ++++++++++--------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/core_plugins/kibana/public/home/components/tutorial/instruction_set.js b/src/core_plugins/kibana/public/home/components/tutorial/instruction_set.js index e199005e841ab..ca4c220bb950b 100644 --- a/src/core_plugins/kibana/public/home/components/tutorial/instruction_set.js +++ b/src/core_plugins/kibana/public/home/components/tutorial/instruction_set.js @@ -102,7 +102,7 @@ export class InstructionSet extends React.Component { onClick={this.props.onStatusCheck} isLoading={this.props.isCheckingStatus} > - {this.props.statusCheckConfig.btnLabel ? this.props.statusCheckConfig.btnLabel : 'Check status'} + {this.props.statusCheckConfig.btnLabel || 'Check status'} @@ -113,7 +113,7 @@ export class InstructionSet extends React.Component { ); return { - title: this.props.statusCheckConfig.title ? this.props.statusCheckConfig.title : 'Status Check', + title: this.props.statusCheckConfig.title || 'Status Check', status: this.props.statusCheckState, children: checkStausStep, key: 'checkStatusStep' diff --git a/src/core_plugins/kibana/public/home/components/tutorial/tutorial.js b/src/core_plugins/kibana/public/home/components/tutorial/tutorial.js index 69d5e214e12e1..8e24934237759 100644 --- a/src/core_plugins/kibana/public/home/components/tutorial/tutorial.js +++ b/src/core_plugins/kibana/public/home/components/tutorial/tutorial.js @@ -22,7 +22,7 @@ export class Tutorial extends React.Component { this.state = { notFound: false, paramValues: {}, - statusCheck: [], // array holding instruction set status check state + statusCheck: [], tutorial: null }; @@ -118,41 +118,47 @@ export class Tutorial extends React.Component { } checkInstructionSetStatus = async (instructionSetIndex) => { - let isComplete = false; - let hasFailed = false; const instructions = this.getInstructions(); const esHitsCheckConfig = _.get(instructions, `instructionSets[${instructionSetIndex}].statusCheck.esHitsCheck`); - if (esHitsCheckConfig) { - const searchHeader = JSON.stringify({ index: esHitsCheckConfig.index }); - const searchBody = JSON.stringify({ query: esHitsCheckConfig.query, size: 1 }); - const body = `${searchHeader}\n${searchBody}\n`; - - const response = await fetch(this.props.addBasePath('/elasticsearch/_msearch'), { - method: 'post', - body: body, - headers: { - accept: 'application/json', - 'content-type': 'application/x-ndjson', - 'kbn-xsrf': 'kibana', - }, - credentials: 'same-origin' - }); + const { hasFailed, isComplete } = await this.fetchEsHitsStatus(esHitsCheckConfig); - if (response.status < 300) { - const results = await response.json(); - if (_.get(results, 'responses.[0].hits.hits.length', 0) > 0) { - isComplete = true; - } - } - hasFailed = !isComplete; + this.setState((prevState) => { + const statusCheck = _.cloneDeep(prevState.statusCheck); + statusCheck[instructionSetIndex].isComplete = isComplete; + statusCheck[instructionSetIndex].hasFailed = hasFailed; + statusCheck[instructionSetIndex].isFetchingStatus = false; + return { statusCheck }; + }); + } + + fetchEsHitsStatus = async (esHitsCheckConfig) => { + if (!esHitsCheckConfig) { + return { hasFailed: false, isComplete: false }; } - this.setState((prevState) => { - prevState.statusCheck[instructionSetIndex].isComplete = isComplete; - prevState.statusCheck[instructionSetIndex].hasFailed = hasFailed; - prevState.statusCheck[instructionSetIndex].isFetchingStatus = false; - return { statusCheck: prevState.statusCheck }; + const searchHeader = JSON.stringify({ index: esHitsCheckConfig.index }); + const searchBody = JSON.stringify({ query: esHitsCheckConfig.query, size: 1 }); + const response = await fetch(this.props.addBasePath('/elasticsearch/_msearch'), { + method: 'post', + body: `${searchHeader}\n${searchBody}\n`, + headers: { + accept: 'application/json', + 'content-type': 'application/x-ndjson', + 'kbn-xsrf': 'kibana', + }, + credentials: 'same-origin' }); + + if (response.status > 300) { + return { hasFailed: true, isComplete: false }; + } + + const results = await response.json(); + const numHits = _.get(results, 'responses.[0].hits.hits.length', 0); + return { + hasFailed: numHits === 0, + isComplete: numHits > 0 + }; } onPrem = () => {