Commit 70d49c0e authored by MaxSol1's avatar MaxSol1
Browse files

refactor alerts

parent 347a66bb
...@@ -13,6 +13,17 @@ export function CheckCard({options, slug, dashboard}) { ...@@ -13,6 +13,17 @@ export function CheckCard({options, slug, dashboard}) {
let unknown = false; let unknown = false;
let dash = {}; 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 //Handle state update
const updateState = async () => { const updateState = async () => {
meerkat.getDashboard(slug).then(async d => { meerkat.getDashboard(slug).then(async d => {
...@@ -23,15 +34,6 @@ export function CheckCard({options, slug, dashboard}) { ...@@ -23,15 +34,6 @@ export function CheckCard({options, slug, dashboard}) {
const c = options.criticalSound ? new Audio(options.criticalSound) : new Audio(dash.criticalSound); const c = options.criticalSound ? new Audio(options.criticalSound) : new Audio(dash.criticalSound);
const u = options.unknownSound ? new Audio(options.unknownSound) : new Audio(dash.unknownSound); const u = options.unknownSound ? new Audio(options.unknownSound) : new Audio(dash.unknownSound);
const initState = async (state) => {
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;
}
//get globalMute from dashboard JSON //get globalMute from dashboard JSON
const muteAlerts = () => { const muteAlerts = () => {
meerkat.getDashboard(slug).then(async d => { meerkat.getDashboard(slug).then(async d => {
...@@ -46,24 +48,24 @@ export function CheckCard({options, slug, dashboard}) { ...@@ -46,24 +48,24 @@ export function CheckCard({options, slug, dashboard}) {
const alertSound = (state) => { const alertSound = (state) => {
if (options.objectType !== null) { if (options.objectType !== null) {
const resetState = (o, w, c ,u) => { const resetState = (o, w, c ,u) => {
if (o === 1) ok = false; if (o) ok = false;
if (w === 1) warning = false; if (w) warning = false;
if (c === 1) critical = false; if (c) critical = false;
if (u === 1) unknown = false; if (u) unknown = false;
} }
if(options.objectType === 'service') { if(options.objectType === 'service') {
switch(state){ switch(state){
case 'ok': if (state === 'ok' && !ok) {o.play(); ok = true; resetState(0,1,1,1)} break; case 'ok': if (!ok) {o.play(); ok = true; resetState(0,1,1,1)} break;
case 'warning': if (state === 'warning' && !warning) {w.play(); warning = true; resetState(0,1,1,1)} break; case 'warning': if (!warning) {w.play(); warning = true; resetState(0,1,1,1)} break;
case 'critical': if (state === 'critical' && !critical) {c.play(); critical = true; resetState(1,1,0,1)} break; case 'critical': if (!critical) {c.play(); critical = true; resetState(1,1,0,1)} break;
case 'unknown': if (state === 'unknown' && !unknown) {u.play(); unknown = true; resetState(1,0,1,1)} break; case 'unknown': if (!unknown) {u.play(); unknown = true; resetState(1,0,1,1)} break;
} }
} else if(options.objectType === 'host') { } else if(options.objectType === 'host') {
console.log(state); console.log(state);
switch(state){ switch(state){
case 'up': if (state === 'up' && !ok) { o.play(); ok = true; resetState(0,1,1,1)} break; case 'up': if (!ok) { o.play(); ok = true; resetState(0,1,1,1)} break;
case 'down': if (state === 'down' && !warning) { w.play(); warning = true; resetState(0,1,1,1)} break; case 'down': if (!warning) { w.play(); warning = true; resetState(0,1,1,1)} break;
} }
} }
} }
...@@ -72,7 +74,6 @@ export function CheckCard({options, slug, dashboard}) { ...@@ -72,7 +74,6 @@ export function CheckCard({options, slug, dashboard}) {
if (options.objectType !== null && options.filter !== null) { if (options.objectType !== null && options.filter !== null) {
const res = await meerkat.getIcingaObjectState(options.objectType, options.filter); const res = await meerkat.getIcingaObjectState(options.objectType, options.filter);
const state = icingaResultCodeToCheckState(options.objectType, res); const state = icingaResultCodeToCheckState(options.objectType, res);
initState(state);
setCheckState(state); setCheckState(state);
muteAlerts(); muteAlerts();
alertSound(state); alertSound(state);
...@@ -81,8 +82,9 @@ export function CheckCard({options, slug, dashboard}) { ...@@ -81,8 +82,9 @@ export function CheckCard({options, slug, dashboard}) {
} }
//Setup check refresher //Setup check refresher
useEffect(async () => { useEffect(() => {
if(options.objectType !== null && options.filter !== null) { if(options.objectType !== null && options.filter !== null) {
initState();
updateState(); updateState();
const intervalID = window.setInterval(updateState, 30*1000) const intervalID = window.setInterval(updateState, 30*1000)
return () => window.clearInterval(intervalID); return () => window.clearInterval(intervalID);
......
...@@ -136,6 +136,17 @@ export function CheckImage({options, dashboard, slug}) { ...@@ -136,6 +136,17 @@ export function CheckImage({options, dashboard, slug}) {
let unknown = false; let unknown = false;
let dash = {}; 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 //Handle state update
const updateState = async () => { const updateState = async () => {
meerkat.getDashboard(slug).then(async d => { meerkat.getDashboard(slug).then(async d => {
...@@ -146,15 +157,6 @@ export function CheckImage({options, dashboard, slug}) { ...@@ -146,15 +157,6 @@ export function CheckImage({options, dashboard, slug}) {
const c = options.criticalSound ? new Audio(options.criticalSound) : new Audio(dash.criticalSound); const c = options.criticalSound ? new Audio(options.criticalSound) : new Audio(dash.criticalSound);
const u = options.unknownSound ? new Audio(options.unknownSound) : new Audio(dash.unknownSound); const u = options.unknownSound ? new Audio(options.unknownSound) : new Audio(dash.unknownSound);
const initState = async (state) => {
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;
}
//get globalMute from dashboard JSON //get globalMute from dashboard JSON
const muteAlerts = () => { const muteAlerts = () => {
meerkat.getDashboard(slug).then(async d => { meerkat.getDashboard(slug).then(async d => {
...@@ -169,24 +171,24 @@ export function CheckImage({options, dashboard, slug}) { ...@@ -169,24 +171,24 @@ export function CheckImage({options, dashboard, slug}) {
const alertSound = (state) => { const alertSound = (state) => {
if (options.objectType !== null) { if (options.objectType !== null) {
const resetState = (o, w, c ,u) => { const resetState = (o, w, c ,u) => {
if (o === 1) ok = false; if (o) ok = false;
if (w === 1) warning = false; if (w) warning = false;
if (c === 1) critical = false; if (c) critical = false;
if (u === 1) unknown = false; if (u) unknown = false;
} }
if(options.objectType === 'service') { if(options.objectType === 'service') {
switch(state){ switch(state){
case 'ok': if (state === 'ok' && !ok) {o.play(); ok = true; resetState(0,1,1,1)} break; case 'ok': if (!ok) {o.play(); ok = true; resetState(0,1,1,1)} break;
case 'warning': if (state === 'warning' && !warning) {w.play(); warning = true; resetState(0,1,1,1)} break; case 'warning': if (!warning) {w.play(); warning = true; resetState(0,1,1,1)} break;
case 'critical': if (state === 'critical' && !critical) {c.play(); critical = true; resetState(1,1,0,1)} break; case 'critical': if (!critical) {c.play(); critical = true; resetState(1,1,0,1)} break;
case 'unknown': if (state === 'unknown' && !unknown) {u.play(); unknown = true; resetState(1,0,1,1)} break; case 'unknown': if (!unknown) {u.play(); unknown = true; resetState(1,0,1,1)} break;
} }
} else if(options.objectType === 'host') { } else if(options.objectType === 'host') {
console.log(state); console.log(state);
switch(state){ switch(state){
case 'up': if (state === 'up' && !ok) { o.play(); ok = true; resetState(0,1,1,1)} break; case 'up': if (!ok) { o.play(); ok = true; resetState(0,1,1,1)} break;
case 'down': if (state === 'down' && !warning) { w.play(); warning = true; resetState(0,1,1,1)} break; case 'down': if (!warning) { w.play(); warning = true; resetState(0,1,1,1)} break;
} }
} }
} }
...@@ -194,7 +196,6 @@ export function CheckImage({options, dashboard, slug}) { ...@@ -194,7 +196,6 @@ export function CheckImage({options, dashboard, slug}) {
if (options.objectType !== null && options.filter !== null) { if (options.objectType !== null && options.filter !== null) {
const res = await meerkat.getIcingaObjectState(options.objectType, options.filter); const res = await meerkat.getIcingaObjectState(options.objectType, options.filter);
const state = icingaResultCodeToCheckState(options.objectType, res); const state = icingaResultCodeToCheckState(options.objectType, res);
initState(state);
setCheckState(state); setCheckState(state);
muteAlerts(); muteAlerts();
alertSound(state); alertSound(state);
...@@ -205,6 +206,7 @@ export function CheckImage({options, dashboard, slug}) { ...@@ -205,6 +206,7 @@ export function CheckImage({options, dashboard, slug}) {
//Setup check refresher //Setup check refresher
useEffect(() => { useEffect(() => {
if(options.objectType !== null && options.filter !== null) { if(options.objectType !== null && options.filter !== null) {
initState();
updateState(); updateState();
const intervalID = window.setInterval(updateState, 30*1000) const intervalID = window.setInterval(updateState, 30*1000)
return () => window.clearInterval(intervalID); return () => window.clearInterval(intervalID);
......
...@@ -121,6 +121,16 @@ export function CheckLine({options, dashboard, slug}) { ...@@ -121,6 +121,16 @@ export function CheckLine({options, dashboard, slug}) {
let unknown = false; let unknown = false;
let dash = {}; 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 //Handle state update
const updateState = async () => { const updateState = async () => {
...@@ -132,15 +142,6 @@ export function CheckLine({options, dashboard, slug}) { ...@@ -132,15 +142,6 @@ export function CheckLine({options, dashboard, slug}) {
const c = options.criticalSound ? new Audio(options.criticalSound) : new Audio(dash.criticalSound); const c = options.criticalSound ? new Audio(options.criticalSound) : new Audio(dash.criticalSound);
const u = options.unknownSound ? new Audio(options.unknownSound) : new Audio(dash.unknownSound); const u = options.unknownSound ? new Audio(options.unknownSound) : new Audio(dash.unknownSound);
const initState = async (state) => {
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;
}
//get globalMute from dashboard JSON //get globalMute from dashboard JSON
const muteAlerts = () => { const muteAlerts = () => {
meerkat.getDashboard(slug).then(async d => { meerkat.getDashboard(slug).then(async d => {
...@@ -155,24 +156,24 @@ export function CheckLine({options, dashboard, slug}) { ...@@ -155,24 +156,24 @@ export function CheckLine({options, dashboard, slug}) {
const alertSound = (state) => { const alertSound = (state) => {
if (options.objectType !== null) { if (options.objectType !== null) {
const resetState = (o, w, c ,u) => { const resetState = (o, w, c ,u) => {
if (o === 1) ok = false; if (o) ok = false;
if (w === 1) warning = false; if (w) warning = false;
if (c === 1) critical = false; if (c) critical = false;
if (u === 1) unknown = false; if (u) unknown = false;
} }
if(options.objectType === 'service') { if(options.objectType === 'service') {
switch(state){ switch(state){
case 'ok': if (state === 'ok' && !ok) {o.play(); ok = true; resetState(0,1,1,1)} break; case 'ok': if (!ok) {o.play(); ok = true; resetState(0,1,1,1)} break;
case 'warning': if (state === 'warning' && !warning) {w.play(); warning = true; resetState(0,1,1,1)} break; case 'warning': if (!warning) {w.play(); warning = true; resetState(0,1,1,1)} break;
case 'critical': if (state === 'critical' && !critical) {c.play(); critical = true; resetState(1,1,0,1)} break; case 'critical': if (!critical) {c.play(); critical = true; resetState(1,1,0,1)} break;
case 'unknown': if (state === 'unknown' && !unknown) {u.play(); unknown = true; resetState(1,0,1,1)} break; case 'unknown': if (!unknown) {u.play(); unknown = true; resetState(1,0,1,1)} break;
} }
} else if(options.objectType === 'host') { } else if(options.objectType === 'host') {
console.log(state); console.log(state);
switch(state){ switch(state){
case 'up': if (state === 'up' && !ok) { o.play(); ok = true; resetState(0,1,1,1)} break; case 'up': if (!ok) { o.play(); ok = true; resetState(0,1,1,1)} break;
case 'down': if (state === 'down' && !warning) { w.play(); warning = true; resetState(0,1,1,1)} break; case 'down': if (!warning) { w.play(); warning = true; resetState(0,1,1,1)} break;
} }
} }
} }
...@@ -181,7 +182,6 @@ export function CheckLine({options, dashboard, slug}) { ...@@ -181,7 +182,6 @@ export function CheckLine({options, dashboard, slug}) {
if (options.objectType !== null && options.filter !== null) { if (options.objectType !== null && options.filter !== null) {
const res = await meerkat.getIcingaObjectState(options.objectType, options.filter); const res = await meerkat.getIcingaObjectState(options.objectType, options.filter);
const state = icingaResultCodeToCheckState(options.objectType, res); const state = icingaResultCodeToCheckState(options.objectType, res);
initState(state);
setCheckState(state); setCheckState(state);
muteAlerts(); muteAlerts();
alertSound(state); alertSound(state);
...@@ -192,6 +192,7 @@ export function CheckLine({options, dashboard, slug}) { ...@@ -192,6 +192,7 @@ export function CheckLine({options, dashboard, slug}) {
//Setup check refresher //Setup check refresher
useEffect(() => { useEffect(() => {
if(options.objectType !== null && options.filter !== null) { if(options.objectType !== null && options.filter !== null) {
initState();
updateState(); updateState();
const intervalID = window.setInterval(updateState, 30*1000) const intervalID = window.setInterval(updateState, 30*1000)
return () => window.clearInterval(intervalID); return () => window.clearInterval(intervalID);
......
...@@ -95,6 +95,17 @@ export function CheckSVG({options, dashboard, slug}) { ...@@ -95,6 +95,17 @@ export function CheckSVG({options, dashboard, slug}) {
let unknown = false; let unknown = false;
let dash = {}; 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 //Handle state update
const updateState = async () => { const updateState = async () => {
meerkat.getDashboard(slug).then(async d => { meerkat.getDashboard(slug).then(async d => {
...@@ -105,15 +116,6 @@ export function CheckSVG({options, dashboard, slug}) { ...@@ -105,15 +116,6 @@ export function CheckSVG({options, dashboard, slug}) {
const c = options.criticalSound ? new Audio(options.criticalSound) : new Audio(dash.criticalSound); const c = options.criticalSound ? new Audio(options.criticalSound) : new Audio(dash.criticalSound);
const u = options.unknownSound ? new Audio(options.unknownSound) : new Audio(dash.unknownSound); const u = options.unknownSound ? new Audio(options.unknownSound) : new Audio(dash.unknownSound);
const initState = async (state) => {
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;
}
//get globalMute from dashboard JSON //get globalMute from dashboard JSON
const muteAlerts = () => { const muteAlerts = () => {
meerkat.getDashboard(slug).then(async d => { meerkat.getDashboard(slug).then(async d => {
...@@ -128,24 +130,24 @@ export function CheckSVG({options, dashboard, slug}) { ...@@ -128,24 +130,24 @@ export function CheckSVG({options, dashboard, slug}) {
const alertSound = (state) => { const alertSound = (state) => {
if (options.objectType !== null) { if (options.objectType !== null) {
const resetState = (o, w, c ,u) => { const resetState = (o, w, c ,u) => {
if (o === 1) ok = false; if (o) ok = false;
if (w === 1) warning = false; if (w) warning = false;
if (c === 1) critical = false; if (c) critical = false;
if (u === 1) unknown = false; if (u) unknown = false;
} }
if(options.objectType === 'service') { if(options.objectType === 'service') {
switch(state){ switch(state){
case 'ok': if (state === 'ok' && !ok) {o.play(); ok = true; resetState(0,1,1,1)} break; case 'ok': if (!ok) {o.play(); ok = true; resetState(0,1,1,1)} break;
case 'warning': if (state === 'warning' && !warning) {w.play(); warning = true; resetState(0,1,1,1)} break; case 'warning': if (!warning) {w.play(); warning = true; resetState(0,1,1,1)} break;
case 'critical': if (state === 'critical' && !critical) {c.play(); critical = true; resetState(1,1,0,1)} break; case 'critical': if (!critical) {c.play(); critical = true; resetState(1,1,0,1)} break;
case 'unknown': if (state === 'unknown' && !unknown) {u.play(); unknown = true; resetState(1,0,1,1)} break; case 'unknown': if (!unknown) {u.play(); unknown = true; resetState(1,0,1,1)} break;
} }
} else if(options.objectType === 'host') { } else if(options.objectType === 'host') {
console.log(state); console.log(state);
switch(state){ switch(state){
case 'up': if (state === 'up' && !ok) { o.play(); ok = true; resetState(0,1,1,1)} break; case 'up': if (!ok) { o.play(); ok = true; resetState(0,1,1,1)} break;
case 'down': if (state === 'down' && !warning) { w.play(); warning = true; resetState(0,1,1,1)} break; case 'down': if (!warning) { w.play(); warning = true; resetState(0,1,1,1)} break;
} }
} }
} }
...@@ -153,7 +155,6 @@ export function CheckSVG({options, dashboard, slug}) { ...@@ -153,7 +155,6 @@ export function CheckSVG({options, dashboard, slug}) {
if (options.objectType !== null && options.filter !== null) { if (options.objectType !== null && options.filter !== null) {
const res = await meerkat.getIcingaObjectState(options.objectType, options.filter); const res = await meerkat.getIcingaObjectState(options.objectType, options.filter);
const state = icingaResultCodeToCheckState(options.objectType, res); const state = icingaResultCodeToCheckState(options.objectType, res);
initState(state);
setCheckState(state); setCheckState(state);
muteAlerts(); muteAlerts();
alertSound(state); alertSound(state);
...@@ -164,6 +165,7 @@ export function CheckSVG({options, dashboard, slug}) { ...@@ -164,6 +165,7 @@ export function CheckSVG({options, dashboard, slug}) {
//Setup check refresher //Setup check refresher
useEffect(() => { useEffect(() => {
if(options.objectType !== null && options.filter != null) { if(options.objectType !== null && options.filter != null) {
initState();
updateState(); updateState();
const intervalID = window.setInterval(updateState, 30*1000) const intervalID = window.setInterval(updateState, 30*1000)
return () => window.clearInterval(intervalID); return () => window.clearInterval(intervalID);
......
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