Skip to content

Commit 3d10d81

Browse files
authored
Merge pull request #5560 from Countly/SER-1896-improve-crash-alerts
[alerts] improvements in crashes
2 parents 189a5fd + df63ca8 commit 3d10d81

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

plugins/alerts/api/alertModules/crashes.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,28 @@ async function triggerByEvent(payload) {
2929
}
3030

3131
//read batcher reads from db and cashes data for some time
32-
const alert = await common.readBatcher.getOne("alerts", {
32+
const alerts = await common.readBatcher.getMany("alerts", {
3333
selectedApps: app._id.toString(),
3434
alertDataType: "crashes",
3535
alertDataSubType: commonLib.TRIGGERED_BY_EVENT.crashes,
3636
});
3737

38-
if (!alert) {
38+
if (!alerts || !alerts.length) {
3939
return;
4040
}
4141

42-
await commonLib.trigger({ alert, app, date: new Date }, log);
42+
// trigger all alerts
43+
await Promise.all(alerts.map(alert => commonLib.trigger({
44+
alert,
45+
app,
46+
date: new Date,
47+
extra: crashObject
48+
}, log)));
4349
}
4450

4551

4652
module.exports.check = async function({ alertConfigs: alert, done, scheduledTo: date }) {
47-
const app = await common.readBatcher.getOne("apps", { _id: ObjectId(alert.selectedApps[0]) });
53+
const app = await common.readBatcher.getOne("apps", { _id: new ObjectId(alert.selectedApps[0]) });
4854
if (!app) {
4955
log.e(`App ${alert.selectedApps[0]} couldn't be found`);
5056
return done();
@@ -57,7 +63,7 @@ module.exports.check = async function({ alertConfigs: alert, done, scheduledTo:
5763

5864
if (compareType === commonLib.COMPARE_TYPE_ENUM.MORE_THAN) {
5965
if (metricValue > compareValue) {
60-
await commonLib.trigger({ alert, app, metricValue, date });
66+
await commonLib.trigger({ alert, app, metricValue, date }, log);
6167
}
6268
}
6369
else {
@@ -73,7 +79,7 @@ module.exports.check = async function({ alertConfigs: alert, done, scheduledTo:
7379
: change <= -compareValue;
7480

7581
if (shouldTrigger) {
76-
await commonLib.trigger({ alert, app, date, metricValue, metricValueBefore });
82+
await commonLib.trigger({ alert, app, date, metricValue, metricValueBefore }, log);
7783
}
7884
}
7985

plugins/alerts/api/parts/common-lib.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/** @typedef {import("mongodb").ObjectId} ObjectId */
2+
13
/**
24
* Alert document object
35
* @typedef {Object} Alert
@@ -30,12 +32,22 @@
3032

3133
/**
3234
* Paired app&alert and the metrics
33-
* @typedef {Object} MatchedResult
34-
* @property {App} app - matched app object
35-
* @property {Alert} alert - matched alert object
36-
* @property {Date} date - alert trigger date
37-
* @property {number} metricValue - metric value that matched the alert condition
38-
* @property {number} metricValueBefore - value that compared with metricValue
35+
* @typedef {Object} MatchedResult
36+
* @property {App} app - matched app object
37+
* @property {Alert} alert - matched alert object
38+
* @property {Date} date - alert trigger date
39+
* @property {number=} metricValue - metric value that matched the alert condition
40+
* @property {number=} metricValueBefore - value that compared with metricValue
41+
* @property {any=} extra - extra parameters to pass to e-mail builder
42+
*/
43+
44+
/**
45+
* Individual components of a date. modified version of the moment.prototype.toObject()
46+
* @typedef {Object} DateComponents
47+
* @property {number} years
48+
* @property {number} months
49+
* @property {number} days
50+
* @property {number} hours
3951
*/
4052

4153
const common = require('../../../../api/utils/common.js');
@@ -85,14 +97,11 @@ module.exports = {
8597
* Returns a date's components based on timezone.
8698
* @param {Date} date - date to get its components
8799
* @param {string} timezone - timezone string
88-
* @returns {object} - { years, months, days, hours }
100+
* @returns {DateComponents} - { years, months, days, hours }
89101
*/
90102
function getDateComponents(date, timezone) {
91-
const dateComponents = moment(date).tz(timezone).toObject();
92-
dateComponents.months += 1; // months are zero indexed
93-
dateComponents.days = dateComponents.date;
94-
delete dateComponents.date;
95-
return dateComponents;
103+
const { years, months, date: days, hours } = moment(date).tz(timezone).toObject();
104+
return { years, months: months + 1, hours, days };
96105
}
97106

98107
/**
@@ -126,6 +135,7 @@ async function determineAudience(alert) {
126135
}
127136
return getUserEmailsBasedOnGroups(alert.allGroups);
128137
}
138+
return [];
129139
}
130140
/**
131141
* Retrieves user emails based on group IDs.
@@ -150,7 +160,7 @@ function getUserEmailsBasedOnGroups(groupIds) {
150160
members.forEach((member) => {
151161
memberEmails.push(member.email);
152162
});
153-
resolve();
163+
resolve(true);
154164
}
155165
});
156166
});
@@ -166,7 +176,7 @@ function getUserEmailsBasedOnGroups(groupIds) {
166176
* @returns {Promise<string>} - compiled e-mail string
167177
*/
168178
async function compileEmail(result) {
169-
const { alert, app, metricValue, metricValueBefore } = result;
179+
const { alert, app, metricValue, metricValueBefore, extra } = result;
170180
const host = await new Promise((res, rej) => mail.lookup((err, _host) => err ? rej(err) : res(_host)));
171181
const metrics = [];
172182
if (metricValue) {
@@ -179,14 +189,16 @@ async function compileEmail(result) {
179189
title: `Countly Alert`,
180190
alertName: alert.alertName,
181191
alertDataType: alert.alertDataType,
192+
alertDataSubType: alert.alertDataSubType,
182193
subTitle: `Uh oh! It seems there's been some activity related to ` + alert.alertDataSubType + ` in the `,
183194
host,
184195
compareDescribe: alert.compareDescribe,
185196
apps: [{
186197
id: app._id.toString(),
187198
name: app.name,
188199
data: metrics
189-
}]
200+
}],
201+
extra,
190202
});
191203
}
192204

@@ -215,7 +227,7 @@ async function trigger(result, log) {
215227
email,
216228
subject,
217229
emailBody,
218-
err => err ? rej(err) : res()
230+
err => err ? rej(err) : res(true)
219231
)
220232
);
221233
// increase counter by date and email

plugins/alerts/frontend/public/templates/email.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@
133133
</tr>
134134
<tr>
135135
<td align="left" style="font-family: Inter; color: #333C48; font-size: 16px; font-weight:400; line-height: 22px;">
136-
Click to access the
137-
<span style="display: inline; font-family: Inter; font-weight: 400; font-size: 16px;"><%= alertDataType %></span>
138-
results.
136+
<% if (alertDataType === "crashes" && alertDataSubType === "new crash/error" && extra && extra._id) { %>
137+
<a href="<%= host %>/dashboard#/<%= apps[i].id %>/crashes/<%= extra._id %>">
138+
Click to access the crash details.
139+
</a>
140+
<% } %>
139141
</td>
140142
</tr>
141143
</table>

0 commit comments

Comments
 (0)