-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (73 loc) · 2.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"use strict"
const fs = require('fs');
const fetch = require('node-fetch');
let converter = require('json-2-csv');
const AUTH = ''
const getMilestoneTime = (incident, milestoneType) => {
const found = incident.milestones.filter(milestone => milestone.type === milestoneType)[0]
if (found) {
return found.occurred_at
}
return null;
}
const getRole = (incident, roleName) => {
const found = incident.role_assignments.find(role => role.incident_role.name === 'Commander')
if (found) {
return found.user.name;
}
return null
}
let json2csvCallback = function (err, csv) {
if (err) throw err;
fs.writeFile('incidents.csv', csv, 'utf8', function (err) {
if (err) {
console.log('Some error occured - file either not saved or corrupted file saved.');
} else {
console.log('It\'s saved!');
}
});
};
const getFireHydrantIncidentCount = (startDate = '2021-01-01') => {
fetch(`https://api.firehydrant.io/v1/incidents?start_date=${startDate}`, {
headers: {
Authorization: AUTH,
}
}).then(res => res.json()).then(json => {
return json.pagination.count;
})
}
const getFireHydrantIncidents = (count = 100) => {
fetch(`https://api.firehydrant.io/v1/incidents?per_page=${count}`, {
headers: {
Authorization: AUTH,
}
}).then(res => res.json())
.then(json => {
const parsed = json.data.map(incident => {
return {
number: incident.number,
name: incident.name,
severity: incident.severity,
environment: incident.environments.map(environment => environment.name),
functionality: incident.functionalities.map(functionality => functionality.name),
service: incident.services.map(service => service.name),
started: getMilestoneTime(incident, 'started'),
detected: getMilestoneTime(incident, 'detected'),
acknowledged: getMilestoneTime(incident, 'acknowledged'),
first_action: getMilestoneTime(incident, 'first_action'),
mitigated: getMilestoneTime(incident, 'mitigated'),
resolved: getMilestoneTime(incident, 'resolved'),
opened_at: incident.created_at,
started_by: incident.created_by.name,
elapsed_time: 123,
customers_impacted: incident.customers_impacted,
incident_commander: getRole(incident, 'Commander'),
tickets: incident.incident_tickets.length > 0 ? incident.incident_tickets[0].attachments[0].display_text : 'none',
slack: incident.channel_name
}
})
converter.json2csv(parsed, json2csvCallback);
});
}
const count = getFireHydrantIncidentCount('01-01-2021');
getFireHydrantIncidents(count)