import { h, Fragment, options } from 'preact'; import { useState, useEffect } from 'preact/hooks'; import * as meerkat from '../meerkat'; import { icingaResultCodeToCheckState, icingaCheckTypeFromId, IcingaCheckList } from '../util' import { svgList } from '../svg-list' import { DashboardView } from '../editor'; export function CheckSVGOptions({options, updateOptions}) { const svgOptions = svgList.map(svgName => ) const [showAdvanced, setAdvanced] = useState(false); const onClickAdvanced = () => showAdvanced ? setAdvanced(false) : setAdvanced(true); const clearField = (e, field) => { e.preventDefault(); let opts = {}; opts[field] = null; updateOptions(opts); } return

updateOptions({linkURL: e.currentTarget.value})}>
updateOptions({okStrokeColor: e.currentTarget.value})}/>

updateOptions({warningStrokeColor: e.currentTarget.value})}/>

updateOptions({unknownStrokeColor: e.currentTarget.value})}/>

updateOptions({criticalStrokeColor: e.currentTarget.value})}/>

} //The rendered view (in the actual dashboard) of the Check SVG export function CheckSVG({options, dashboard, slug}) { const [checkState, setCheckState] = useState(null); let ok = false; let warning = false; let critical = false; let unknown = false; let dash = {}; const initState = async () => { const res = await meerkat.getIcingaObjectState(options.objectType, options.filter); const state = icingaResultCodeToCheckState(options.objectType, res); if (state === 'ok') ok = true; if (state === 'up') ok = true; if (state === 'down') warning = true; if (state === 'warning') warning = true; if (state === 'critical') critical = true; if (state === 'unknown') unknown = true; } //Handle state update const updateState = async () => { meerkat.getDashboard(slug).then(async d => { dash = await d const o = options.okSound ? new Audio(options.okSound) : new Audio(dash.okSound); const w = options.warningSound ? new Audio(options.warningSound) : new Audio(dash.warningSound); const c = options.criticalSound ? new Audio(options.criticalSound) : new Audio(dash.criticalSound); const u = options.unknownSound ? new Audio(options.unknownSound) : new Audio(dash.unknownSound); //get globalMute from dashboard JSON const muteAlerts = () => { meerkat.getDashboard(slug).then(async d => { if (options.muteAlerts || d.globalMute) { o.volume = 0.0; w.volume = 0.0; c.volume = 0.0; u.volume = 0.0; } else { o.volume = 1.0; w.volume = 1.0; c.volume = 1.0; u.volume = 1.0; } }); } const alertSound = (state) => { if (options.objectType !== null) { const resetState = (o, w, c ,u) => { if (o) ok = false; if (w) warning = false; if (c) critical = false; if (u) unknown = false; } if(options.objectType === 'service') { switch(state){ case 'ok': if (!ok) {o.play(); ok = true; resetState(0,1,1,1)} break; case 'warning': if (!warning) {w.play(); warning = true; resetState(0,1,1,1)} break; case 'critical': if (!critical) {c.play(); critical = true; resetState(1,1,0,1)} break; case 'unknown': if (!unknown) {u.play(); unknown = true; resetState(1,0,1,1)} break; } } else if(options.objectType === 'host') { console.log(state); switch(state){ case 'up': if (!ok) { o.play(); ok = true; resetState(0,1,1,1)} break; case 'down': if (!warning) { w.play(); warning = true; resetState(0,1,1,1)} break; } } } } if (options.objectType !== null && options.filter !== null) { const res = await meerkat.getIcingaObjectState(options.objectType, options.filter); const state = icingaResultCodeToCheckState(options.objectType, res); setCheckState(state); muteAlerts(); alertSound(state); } }); } //Setup check refresher useEffect(() => { if(options.objectType !== null && options.filter != null) { initState(); updateState(); const intervalID = window.setInterval(updateState, 30*1000) return () => window.clearInterval(intervalID); } }, [options.objectType, options.filter]); //SVG stroke color and icons to the correct version based //on the current check state let styles = ''; let svgName = ''; if(checkState === 'ok' || checkState === 'up') { styles = options.okStrokeColor ? `stroke: ${options.okStrokeColor}` : ''; svgName = options.okSvg; } if(checkState === 'warning') { styles = options.warningStrokeColor ? `stroke: ${options.warningStrokeColor}` : ''; svgName = options.warningSvg; } if(checkState === 'unknown') { styles = options.unknownStrokeColor ? `stroke: ${options.unknownStrokeColor}` : ''; svgName = options.unknownSvg; } if(checkState === 'critical' || checkState === 'down') { styles = options.criticalStrokeColor ? `stroke: ${options.criticalStrokeColor}` : ''; svgName = options.criticalSvg; } return
} const AdvancedSVGOptions = ({options, updateOptions, display}) => { const handleAudioFile = async (fieldName, files) => { const res = await meerkat.uploadFile(files[0]); const opts = {} opts[fieldName] = res.url console.log(opts); updateOptions(opts); } const clearAudioFile = (e, field) => { e.preventDefault(); let opts = {}; opts[field] = null; updateOptions(opts); } const muteAlerts = (e) => { let volumeChecked = options.muteAlerts; volumeChecked = !volumeChecked; updateOptions({ muteAlerts: volumeChecked }) } const audioControls = src => { if(src) { return clearAudioFile(e, field)}>clear  view  } return null; } const resetDefaultOk = (src) => { updateOptions({ okSound: "/dashboards-data/ok.mp3" }) } const defaultAlerts = (src) => { if(src) { return
default
} return null; } return

muteAlerts(e)} class="form-control mute-sounds"/> handleAudioFile('okSound', e.target.files)}> handleAudioFile('warningSound', e.target.files)}> handleAudioFile('criticalSound', e.target.files)}> handleAudioFile('unknownSound', e.target.files)}>
} export const CheckSVGDefaults = { okSvg: 'check-circle', okStrokeColor: '#0ee16a', warningSvg: 'alert-triangle', warningStrokeColor: '#ff9000', unknownSvg: 'help-circle', unknownStrokeColor: '#970ee1', criticalSvg: 'alert-octagon', criticalStrokeColor: '#ff0019', muteAlerts: false, }