Commit 96a5b405 authored by Ken Nguyen's avatar Ken Nguyen
Browse files

Merge branch '59-performance-default-value' into 'master'

performance data can have default value

Closes #59

See merge request !9
parents 08e65b61 e51729e9
Pipeline #1246 passed with stages
in 2 minutes and 24 seconds
......@@ -51,4 +51,38 @@ describe('Element - Check Card - Check Data Mode', () => {
cy.get('.check-content .check-state').invoke('text').should('match', /ok|warning|critical|unknown/)
})
})
describe('When service has performance data', () => {
it('displays check state', () => {
cy.intercept("/icinga/services").as("getServicesFromIcinga")
// select services
cy.get('[data-cy="card:check"]').within(() => {
cy.get('button').click()
cy.get('li').contains(/^Services$/).click()
})
cy.wait("@getServicesFromIcinga")
// select name of service
cy.get('[data-cy="card:check_options"]').within(() => {
cy.get('button').click()
cy.get('li').contains('suboptimal.colo.sol1.net!ping4').click()
})
// select performance data
cy.get('[data-cy="card:checkDataSelection"]').select('Performance RTA')
// Check Card content
// no default
cy.get('.check-content .check-state').invoke('text').should('match', /\d+\.\d+/)
// has default, has performance data
cy.get('[data-cy="card:checkDataDefault"]').type('none')
cy.get('.check-content .check-state').invoke('text').should('match', /\d+\.\d+/)
// TODO how can this case be simulated and tested?
// has default, but no performance data
//cy.get('.check-content .check-state').should('have.text', 'none')
})
})
})
......@@ -10,37 +10,37 @@ function useCheckCard({options, dashboard}) {
const [acknowledged, setAcknowledged] = useState("");
const extractAndSetCheckValue = useCallback(checkData => {
let newCheckValue
let newCheckValue = options.checkDataDefault || 'useCheckState'
// extract and use plugin output
if (options.checkDataSelection === 'pluginOutput') {
if (options.checkDataSelection === 'pluginOutput' && checkData.pluginOutput) {
//console.log('Plugin Output:', checkData.pluginOutput)
let pattern, extractedValues
try {
pattern = new RegExp(options.checkDataPattern, 'im')
extractedValues = (checkData.pluginOutput || "").match(pattern)
} catch (e) {
console.error(e)
}
if (!options.checkDataPattern) {
newCheckValue = options.checkDataDefault || checkData.pluginOutput
} else if (extractedValues) {
newCheckValue = extractedValues.length > 1 ? extractedValues[extractedValues.length-1] : extractedValues[0]
} else {
newCheckValue = options.checkDataDefault
if (options.checkDataPattern) {
try {
const pattern = new RegExp(options.checkDataPattern, 'im')
const extractedValues = (checkData.pluginOutput).match(pattern)
if (extractedValues) {
newCheckValue = extractedValues.length > 1 ? extractedValues[extractedValues.length-1] : extractedValues[0]
}
} catch (e) {
// catch invalid regexp
console.error(e)
}
} else if (!options.checkDataDefault) {
newCheckValue = checkData.pluginOutput
}
// extract and use performance data
} else if (checkData.performance) {
for (const [key, value] of Object.entries(checkData.performance)) {
if (options.checkDataSelection === key && value) {
newCheckValue = Number(value.replace(/[^\d.-]/g, ''))
}
} else if (options.checkDataSelection && checkData.performance) {
const value = checkData.performance[options.checkDataSelection]
if (value) {
newCheckValue = Number(value.replace(/[^\d.-]/g, ''))
}
}
setCheckValue(newCheckValue || 'useCheckState')
setCheckValue(newCheckValue)
}, [
options.checkDataSelection,
options.checkDataPattern,
......@@ -167,7 +167,7 @@ const CheckDataOptions = ({options, updateOptions}) => {
{options.checkDataSelection === 'pluginOutput' ?
<div>
<input
class="form-control"
class="form-control mb-1"
name="checkDataPattern"
type="text"
title="Regexp Pattern"
......@@ -176,12 +176,16 @@ const CheckDataOptions = ({options, updateOptions}) => {
value={options.checkDataPattern}
data-cy="card:checkDataRegexp"
/>
</div>
: null}
{options.checkDataSelection ?
<div>
<input
class="form-control my-2"
class="form-control mb-2"
name="checkDataDefault"
type="text"
title="Value to display when regexp does NOT match"
placeholder="Enter value when regexp does NOT match"
title="Check data default value"
placeholder="Enter default value for check data"
onInput={debounce(e => updateOptions({[e.target.name]: e.target.value}), 300)}
value={options.checkDataDefault}
data-cy="card:checkDataDefault"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment