Skip to content

Commit

Permalink
fix: reduce payload size by removing aggregated report from json webh… (
Browse files Browse the repository at this point in the history
  • Loading branch information
NivLipetz committed Sep 20, 2020
1 parent 670d0d6 commit bc77c4a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 29 deletions.
7 changes: 3 additions & 4 deletions src/reports/models/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ async function handleFirstIntermediate(report, job) {
if (!reportUtil.isAllRunnersInExpectedPhase(report, constants.SUBSCRIBER_FIRST_INTERMEDIATE_STAGE)) {
return;
}
let aggregatedReport = await aggregateReportGenerator.createAggregateReport(report.test_id, report.report_id);
await webhooksManager.fireWebhookByEvent(job, WEBHOOK_EVENT_TYPE_IN_PROGRESS, report, { aggregatedReport });
await webhooksManager.fireWebhookByEvent(job, WEBHOOK_EVENT_TYPE_IN_PROGRESS, report);
}

async function handleDone(report, job, reportBenchmark) {
Expand All @@ -100,9 +99,9 @@ async function handleDone(report, job, reportBenchmark) {
const lastReports = await reportsManager.getReports(aggregatedReport.test_id);
const lastScores = lastReports.slice(0, 3).filter(report => report.score).map(report => report.score.toFixed(1));
const { event, icon } = reportBenchmark.score < benchmarkThreshold ? { event: WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, icon: slackEmojis.CRY } : { event: WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, icon: slackEmojis.GRIN };
await webhooksManager.fireWebhookByEvent(job, event, report, { aggregatedReport, score: reportBenchmark.score, lastScores, benchmarkThreshold }, { icon });
await webhooksManager.fireWebhookByEvent(job, event, report, { aggregatedReport: aggregatedReport.aggregate, score: reportBenchmark.score, lastScores, benchmarkThreshold }, { icon });
}
await webhooksManager.fireWebhookByEvent(job, WEBHOOK_EVENT_TYPE_FINISHED, report, { aggregatedReport, score: reportBenchmark.score }, { icon: slackEmojis.ROCKET });
await webhooksManager.fireWebhookByEvent(job, WEBHOOK_EVENT_TYPE_FINISHED, report, { aggregatedReport: aggregatedReport.aggregate, score: reportBenchmark.score }, { icon: slackEmojis.ROCKET });
}

async function handleAbort(report, job) {
Expand Down
18 changes: 11 additions & 7 deletions src/webhooks/models/webhooksFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function getThresholdSlackMessage(state, { testName, benchmarkThreshold, lastSco
}
return `${icon} *Test ${testName} got a score of ${score.toFixed(1)}` +
` this is ${resultText} the threshold of ${benchmarkThreshold}. ${lastScores.length > 0 ? `last 3 scores are: ${lastScores.join()}` : 'no last score to show'}` +
`.*\n${statsFormatter.getStatsFormatted('aggregate', aggregatedReport.aggregate, { score })}\n`;
`.*\n${statsFormatter.getStatsFormatted('aggregate', aggregatedReport, { score })}\n`;
}

function slackWebhookFormat(message, options) {
Expand Down Expand Up @@ -62,19 +62,26 @@ function slack(event, testId, jobId, report, additionalInfo, options) {
parallelism = 1,
ramp_to: rampTo,
arrival_rate: arrivalRate,
arrival_count: arrivalCount,
test_name: testName,
grafana_report: grafanaReport
} = report;
const { score, aggregatedReport, reportBenchmark, benchmarkThreshold, lastScores, stats } = additionalInfo;
switch (event) {
case WEBHOOK_EVENT_TYPE_STARTED: {
let rampToMessage = rampTo ? `ramp to: ${rampTo} scenarios per second` : '';
const rampToMessage = `, ramp to: ${rampTo} scenarios per second`;
let requestRateMessage = arrivalRate ? `arrival rate: ${arrivalRate} scenarios per second` : `arrival count: ${arrivalCount} scenarios`;
requestRateMessage = rampTo ? requestRateMessage + rampToMessage : requestRateMessage;

message = `🤓 *Test ${testName} with id: ${testId} has started*.\n
*test configuration:* environment: ${environment} duration: ${duration} seconds, arrival rate: ${arrivalRate} scenarios per second, number of runners: ${parallelism}, ${rampToMessage}`;
*test configuration:* environment: ${environment} duration: ${duration} seconds, ${requestRateMessage}, number of runners: ${parallelism}`;
break;
}
case WEBHOOK_EVENT_TYPE_FINISHED: {
message = `😎 *Test ${testName} with id: ${testId} is finished.*\n ${statsFormatter.getStatsFormatted('aggregate', aggregatedReport.aggregate, reportBenchmark)}\n`;
message = `😎 *Test ${testName} with id: ${testId} is finished.*\n ${statsFormatter.getStatsFormatted('aggregate', aggregatedReport, reportBenchmark)}\n`;
if (grafanaReport) {
message += `<${grafanaReport} | View final grafana dashboard report>`;
}
break;
}
case WEBHOOK_EVENT_TYPE_FAILED: {
Expand Down Expand Up @@ -105,9 +112,6 @@ function slack(event, testId, jobId, report, additionalInfo, options) {
throw unknownWebhookEventTypeError();
}
}
if (grafanaReport) {
message += `<${grafanaReport} | View final grafana dashboard report>`;
}
return slackWebhookFormat(message, options);
}

Expand Down
16 changes: 8 additions & 8 deletions tests/unit-tests/reporter/models/notifier-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('Webhook/email notifier test ', () => {
await notifier.notifyIfNeeded(report, stats);

webhooksManagerFireWebhookStub.callCount.should.equal(1);
webhooksManagerFireWebhookStub.args[0].should.containDeep([job, WEBHOOK_EVENT_TYPE_IN_PROGRESS, report, { aggregatedReport }]);
webhooksManagerFireWebhookStub.args[0].should.containDeep([job, WEBHOOK_EVENT_TYPE_IN_PROGRESS, report]);
loggerInfoStub.callCount.should.equal(1);
});

Expand Down Expand Up @@ -300,7 +300,7 @@ describe('Webhook/email notifier test ', () => {
await notifier.notifyIfNeeded(report, stats, { score });

webhooksManagerFireWebhookStub.callCount.should.equal(1);
webhooksManagerFireWebhookStub.args[0].should.containDeep([job, WEBHOOK_EVENT_TYPE_FINISHED, report, { aggregatedReport, score }, { icon: ':rocket:' }]);
webhooksManagerFireWebhookStub.args[0].should.containDeep([job, WEBHOOK_EVENT_TYPE_FINISHED, report, { score }, { icon: ':rocket:' }]);

reportEmailSenderStub.callCount.should.equal(0);
});
Expand Down Expand Up @@ -344,8 +344,8 @@ describe('Webhook/email notifier test ', () => {
await notifier.notifyIfNeeded(report, stats, reportBenchmark);

webhooksManagerFireWebhookStub.callCount.should.equal(2);
webhooksManagerFireWebhookStub.args[0].should.containDeep([job, WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, report, { aggregatedReport, score, lastScores: ['90.0', '95.0', '95.4'], benchmarkThreshold }, { icon: ':grin:' }]);
webhooksManagerFireWebhookStub.args[1].should.containDeep([job, WEBHOOK_EVENT_TYPE_FINISHED, report, { aggregatedReport, score }, { icon: ':rocket:' }]);
webhooksManagerFireWebhookStub.args[0].should.containDeep([job, WEBHOOK_EVENT_TYPE_BENCHMARK_PASSED, report, { score, lastScores: ['90.0', '95.0', '95.4'], benchmarkThreshold }, { icon: ':grin:' }]);
webhooksManagerFireWebhookStub.args[1].should.containDeep([job, WEBHOOK_EVENT_TYPE_FINISHED, report, { score }, { icon: ':rocket:' }]);

reportEmailSenderStub.callCount.should.equal(1);
reportEmailSenderStub.args[0].should.containDeep([aggregatedReport, job, job.emails, reportBenchmark]);
Expand Down Expand Up @@ -390,8 +390,8 @@ describe('Webhook/email notifier test ', () => {
await notifier.notifyIfNeeded(report, stats, reportBenchmark);

webhooksManagerFireWebhookStub.callCount.should.equal(2);
webhooksManagerFireWebhookStub.args[0].should.containDeep([job, WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, report, { aggregatedReport, score, lastScores: ['20.0', '20.0', '20.6'], benchmarkThreshold }, { icon: ':cry:' }]);
webhooksManagerFireWebhookStub.args[1].should.containDeep([job, WEBHOOK_EVENT_TYPE_FINISHED, report, { aggregatedReport, score }, { icon: ':rocket:' }]);
webhooksManagerFireWebhookStub.args[0].should.containDeep([job, WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, report, { score, lastScores: ['20.0', '20.0', '20.6'], benchmarkThreshold }, { icon: ':cry:' }]);
webhooksManagerFireWebhookStub.args[1].should.containDeep([job, WEBHOOK_EVENT_TYPE_FINISHED, report, { score }, { icon: ':rocket:' }]);

reportEmailSenderStub.callCount.should.equal(1);
reportEmailSenderStub.args[0].should.containDeep([aggregatedReport, job, job.emails, reportBenchmark]);
Expand Down Expand Up @@ -436,8 +436,8 @@ describe('Webhook/email notifier test ', () => {
await notifier.notifyIfNeeded(report, stats, reportBenchmark);

webhooksManagerFireWebhookStub.callCount.should.equal(2);
webhooksManagerFireWebhookStub.args[0].should.containDeep([job, WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, report, { aggregatedReport, score, lastScores: [], benchmarkThreshold }, { icon: ':cry:' }]);
webhooksManagerFireWebhookStub.args[1].should.containDeep([job, WEBHOOK_EVENT_TYPE_FINISHED, report, { aggregatedReport, score }, { icon: ':rocket:' }]);
webhooksManagerFireWebhookStub.args[0].should.containDeep([job, WEBHOOK_EVENT_TYPE_BENCHMARK_FAILED, report, { score, lastScores: [], benchmarkThreshold }, { icon: ':cry:' }]);
webhooksManagerFireWebhookStub.args[1].should.containDeep([job, WEBHOOK_EVENT_TYPE_FINISHED, report, { score }, { icon: ':rocket:' }]);

reportEmailSenderStub.callCount.should.equal(1);
reportEmailSenderStub.args[0].should.containDeep([aggregatedReport, job, job.emails, reportBenchmark]);
Expand Down
25 changes: 20 additions & 5 deletions tests/unit-tests/webhooks/webhooksFormatter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe('webhooksFormatter', function () {
sandbox = sinon.sandbox.create();
statsFormatterStub = sandbox.stub();
webhooksFormatter.__set__('statsFormatter.getStatsFormatted', statsFormatterStub);
// statsFormatterStub = sandbox.stub(webhooksFormatter, 'statsFormatter.getStatsFormatted');
});
afterEach(() => {
sandbox.resetHistory();
Expand All @@ -35,7 +34,7 @@ describe('webhooksFormatter', function () {
sandbox.restore();
});
describe(EVENT_FORMAT_TYPE_SLACK, function() {
it(WEBHOOK_EVENT_TYPE_STARTED, function() {
it(WEBHOOK_EVENT_TYPE_STARTED + ' - load test', function() {
const testId = uuid.v4();
const jobId = uuid.v4();
const report = {
Expand All @@ -47,7 +46,24 @@ describe('webhooksFormatter', function () {
parallelism: 10
};
const expectedResult = `🤓 *Test ${report.test_name} with id: ${testId} has started*.\n
*test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival rate: ${report.arrival_rate} scenarios per second, number of runners: ${report.parallelism}, ramp to: ${report.ramp_to} scenarios per second`;
*test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival rate: ${report.arrival_rate} scenarios per second, ramp to: ${report.ramp_to} scenarios per second, number of runners: ${report.parallelism}`;

const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report);

expect(payload.text).to.be.equal(expectedResult);
});
it(WEBHOOK_EVENT_TYPE_STARTED + ' - functional test', function() {
const testId = uuid.v4();
const jobId = uuid.v4();
const report = {
test_name: 'some test name',
arrival_count: 5,
duration: 120,
environment: 'test',
parallelism: 10
};
const expectedResult = `🤓 *Test ${report.test_name} with id: ${testId} has started*.\n
*test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival count: ${report.arrival_count} scenarios, number of runners: ${report.parallelism}`;

const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report);

Expand Down Expand Up @@ -195,8 +211,7 @@ describe('webhooksFormatter', function () {
grafana_report: 'http://local.grafana.io/predator'
};
let expectedResult = `🤓 *Test ${report.test_name} with id: ${testId} has started*.\n
*test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival rate: ${report.arrival_rate} scenarios per second, number of runners: ${report.parallelism}, ramp to: ${report.ramp_to} scenarios per second`;
expectedResult += `<${report.grafana_report} | View final grafana dashboard report>`;
*test configuration:* environment: ${report.environment} duration: ${report.duration} seconds, arrival rate: ${report.arrival_rate} scenarios per second, ramp to: ${report.ramp_to} scenarios per second, number of runners: ${report.parallelism}`;
const payload = webhooksFormatter.format(EVENT_FORMAT_TYPE_SLACK, WEBHOOK_EVENT_TYPE_STARTED, jobId, testId, report);

expect(payload.text).to.be.equal(expectedResult);
Expand Down
2 changes: 1 addition & 1 deletion ui/src/features/components/WebhooksList/WebhookForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import TitleInput from "../../../components/TitleInput";
import Input from "../../../components/Input";
import RectangleAlignChildrenLeft from "../../../components/RectangleAlign/RectangleAlignChildrenLeft";
import React, {useState} from "react";
import {EVENTS, WEBHOOK_TYPES} from "./constatns";
import {EVENTS, WEBHOOK_TYPES} from "./constants";
import SimpleTable from "../SimpleTable";
import RadioOptions from "../../../components/RadioOptions";
import UiSwitcher from "../../../components/UiSwitcher";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@


export const EVENTS = [
"started",
"in_progress",
"api_failure",
"aborted",
"failed",
Expand All @@ -10,7 +9,6 @@ export const EVENTS = [
"benchmark_failed"
];


export const WEBHOOK_TYPES = [
'json','slack'
]
]

0 comments on commit bc77c4a

Please sign in to comment.