Skip to content

Commit

Permalink
refactor checkInstructionSetStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed May 14, 2018
1 parent 19fad02 commit e95f043
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
</EuiButton>
</EuiFlexItem>
</EuiFlexGroup>
Expand All @@ -113,7 +113,7 @@ export class InstructionSet extends React.Component {
</Fragment>
);
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'
Expand Down
66 changes: 36 additions & 30 deletions src/core_plugins/kibana/public/home/components/tutorial/tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -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 = () => {
Expand Down

0 comments on commit e95f043

Please sign in to comment.