MCPcopy Index your code
hub / github.com/chartbrew/chartbrew / processAlert

Function processAlert

server/modules/alerts/checkAlerts.js:14–133  ·  view source on GitHub ↗
(chart, alert, alerts)

Source from the content-addressed store, hash-verified

12const fullApiUrl = process.env.NODE_ENV === "production" ? process.env.VITE_APP_API_HOST : process.env.VITE_APP_API_HOST_DEV;
13
14async function processAlert(chart, alert, alerts) {
15 const {
16 rules, mediums, recipients, type
17 } = alert;
18 const {
19 value, lower, upper
20 } = rules;
21
22 const chartController = new ChartController();
23
24 let alertsFound = alerts;
25
26 // for timeseries alerts, check if the alert has occured before
27 if (chart.getDataValue("isTimeseries") && alert.events && alert.events.length > 0) {
28 const lastEvent = alert.events[0];
29
30 if (lastEvent.trigger && lastEvent.trigger.length > 0) {
31 const eventItems = lastEvent.trigger.map((item) => item.label);
32 // find the index of the first event item
33 const firstEventIndex = alertsFound.indexOf((item) => item.label === eventItems[0]);
34 // remove all alerts before the first event item
35 alertsFound = alertsFound.slice(firstEventIndex);
36 // remove alerts that have already been sent
37 alertsFound = alertsFound.filter((item) => !eventItems.includes(item.label));
38 }
39 }
40
41 if (alertsFound.length === 0) {
42 return false;
43 }
44
45 const dashboardUrl = `${settings.client}/dashboard/${chart.project_id}`;
46
47 let thresholdText = "";
48 if (type === "milestone") {
49 thresholdText = `You reached your milestone of ${value}!`;
50 } else if (type === "threshold_above") {
51 thresholdText = `Chartbrew found some values above your threshold of ${value}.`;
52 } else if (type === "threshold_below") {
53 thresholdText = `Chartbrew found some values below your threshold of ${value}.`;
54 } else if (type === "threshold_between") {
55 thresholdText = `Chartbrew found some values between your thresholds of ${lower} and ${upper}.`;
56 } else if (type === "threshold_outside") {
57 thresholdText = `Chartbrew found some values your thresholds of ${lower} and ${upper}.`;
58 }
59
60 // take a snapshot of the chart
61 let snapshotUrl = null;
62 try {
63 snapshotUrl = await chartController.takeSnapshot(chart.id);
64 snapshotUrl = `${fullApiUrl}/${snapshotUrl}`;
65 } catch (err) {
66 console.log("Could not take snapshot", err); // oxlint-disable-line no-console
67 }
68
69 // first process the mediums
70 const stringAlerts = [];
71 Object.keys(mediums).forEach((medium) => {

Callers 1

checkChartForAlertsFunction · 0.85

Calls 4

takeSnapshotMethod · 0.95
filterMethod · 0.80
updateMethod · 0.45
createMethod · 0.45

Tested by

no test coverage detected