-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from sergiitk/feature/incidents
[FEATURE core-incidents] Poll for active incidents BREAKING CHANGE On-Call and Schedule JSON response schemas are changed
- Loading branch information
Showing
14 changed files
with
404 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// ------- Imports ------------------------------------------------------------- | ||
|
||
// This model to be compatible both with backend and frontend. | ||
|
||
// ------- OnCall -------------------------------------------------------------- | ||
|
||
export class Incident { | ||
constructor({ | ||
id, | ||
status, | ||
scheduleId, | ||
title, | ||
summary, | ||
url, | ||
serviceName, | ||
}) { | ||
this.id = id; | ||
this.scheduleId = scheduleId; | ||
this.status = status; | ||
this.title = title; | ||
this.summary = summary; | ||
this.url = url; | ||
this.serviceName = serviceName; | ||
} | ||
|
||
serialize() { | ||
return { | ||
id: this.id, | ||
scheduleId: this.scheduleId, | ||
status: this.status, | ||
title: this.title, | ||
summary: this.summary, | ||
serviceName: this.serviceName, | ||
url: this.url, | ||
}; | ||
} | ||
|
||
toString() { | ||
return JSON.stringify(this.serialize()); | ||
} | ||
|
||
static fromApiRecord(record, scheduleId) { | ||
const attributes = { | ||
id: record.id, | ||
status: record.status, | ||
description: record.description, | ||
title: record.title, | ||
summary: record.summary, | ||
url: record.html_url, | ||
serviceName: record.service ? record.service.summary : 'Unknown', | ||
scheduleId, | ||
}; | ||
return new Incident(attributes); | ||
} | ||
// ------- Class end -------------------------------------------------------- | ||
} | ||
|
||
// ------- End ----------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// ------- Imports ------------------------------------------------------------- | ||
|
||
import logger from 'winston'; | ||
|
||
// ------- Internal imports ---------------------------------------------------- | ||
|
||
import { Incident } from '../models/Incident'; | ||
|
||
// ------- IncidentsService ---------------------------------------------------- | ||
|
||
export class IncidentsService { | ||
constructor(pagerDutyClient) { | ||
this.client = pagerDutyClient; | ||
this.incidentsRepo = new Map(); | ||
} | ||
|
||
async load(onCallsService) { | ||
const onCalls = onCallsService.onCallRepo; | ||
if (!onCalls.size) { | ||
logger.verbose('Skipping incidents load: OnCalls not loaded yet'); | ||
return false; | ||
} | ||
|
||
const missingSchedules = new Set(); | ||
|
||
for (const [scheduleId, onCall] of onCalls.entries()) { | ||
try { | ||
// Limit the number of requests by sending them in sync. | ||
// eslint-disable-next-line no-await-in-loop | ||
const record = await this.client.getActiveIncidentForUserOnSchedule( | ||
onCall.userId, | ||
onCall.schedule.escalationPolicies, | ||
); | ||
|
||
if (record) { | ||
const incident = Incident.fromApiRecord(record, scheduleId); | ||
this.incidentsRepo.set(scheduleId, incident); | ||
// @todo: log when oncall already has an incident and it's different | ||
onCall.setIncident(incident); | ||
logger.verbose( | ||
`Incident ${incident.id} is active for schedule ${scheduleId}`, | ||
); | ||
logger.silly(`Incident ${incident.toString()}`); | ||
} else { | ||
// No incidents, clear. | ||
const existed = this.incidentsRepo.delete(scheduleId); | ||
onCall.clearIncident(); | ||
if (existed) { | ||
logger.verbose(`Incident resolved for schedule ${scheduleId}`); | ||
} | ||
} | ||
} catch (e) { | ||
logger.warn( | ||
`Error loading incident for user ${onCall.userId} ` | ||
+ `on schedule ${scheduleId}: ${e}`, | ||
); | ||
} | ||
} | ||
|
||
if (missingSchedules.size) { | ||
logger.warn( | ||
`Missing incidents data for schedules: ${Array.from(missingSchedules).join()}`, | ||
); | ||
} | ||
return true; | ||
} | ||
|
||
serialize() { | ||
return Array.from(this.incidentsRepo.values(), r => r.serialize()); | ||
} | ||
// ------- Class end -------------------------------------------------------- | ||
} | ||
|
||
// ------- End ----------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.