-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcore.js
132 lines (121 loc) · 4.09 KB
/
core.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const low = require('lowdb')
const slug = require('slug')
const cuid = require('cuid')
const DSPIpfs = require('./ipfs')
const config = require('./config')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync(config.Client.path)
class DStatusPage {
constructor () {
this.ipfs = undefined
this.db = low(adapter)
this.db.defaults(config.DefaultDatabase).write()
}
async startIPFS (opts) {
this.ipfs = new DSPIpfs(opts)
await this.ipfs.start()
const [ gateway, peer ] = await Promise.all([
this.ipfs.getGatewayAddress(),
this.ipfs.id()
])
.catch(err => {
if (err.code === 'ECONNREFUSED') {
throw new Error(`Unable to connect to IPFS api at: ${opts.apiAddr}`)
}
throw err
})
this.updateSettings({ ipfs: { peer, gateway } })
}
stopIPFS () {
return this.ipfs.stop()
}
async createIncident (incident) {
if (!incident) {
throw new Error('Incident is empty')
}
if (!incident.updates || incident.updates.length === 0) {
throw new Error('Incident has no updates')
}
const id = `${slug(incident.title)}-${cuid.slug()}`.toLowerCase()
const timestamp = new Date().toISOString()
incident.id = id
incident.createdAt = timestamp
incident.status = incident.updates[0].status || incident.status
incident.resolvedAt = incident.status === config.Statuses.RESOLVED ? timestamp : undefined
incident.updates[0].status = incident.status
incident.updates[0].updatedAt = timestamp
const result = this.db.get('incidents').unshift(incident).write()
if (!result[0].id) {
throw new Error('Unable to create incident.')
}
this.db.set('unpublishedChanges', true).write()
return { id }
}
async updateIncident (incident) {
if (!incident) {
throw new Error('Incident is empty')
}
if (!incident.id) {
throw new Error('Incident has no ID')
}
if (!incident.updates || incident.updates.length === 0) {
throw new Error('Incident has no updates')
}
const found = await this.db.get('incidents').find({ id: incident.id }).value()
if (!found) {
throw new Error('Incident not found')
}
const timestamp = new Date().toISOString()
const update = incident.updates[incident.updates.length - 1]
update.updatedAt = timestamp
found.title = incident.title || found.title
found.severity = incident.severity || found.severity
found.services = incident.services || found.services
found.status = update.status || found.status
found.resolvedAt = found.status === config.Statuses.RESOLVED ? timestamp : undefined
found.updatedAt = timestamp
found.updates.unshift(update)
const updated = this.db.get('incidents').find({ id: incident.id }).assign(found).write()
if (!updated.updatedAt === timestamp) {
throw new Error('Unable to update incident.')
}
this.db.set('unpublishedChanges', true).write()
return { incident: updated }
}
getIncidents () {
const incidents = this.db.get('incidents').value()
const open = []
const history = []
incidents.forEach(i => {
if (i.status === config.Statuses.RESOLVED) {
history.push(i)
} else {
open.push(i)
}
})
return { open, history }
}
getSettings () {
return this.db.get('settings').value()
}
updateSettings (settings, unpublished) {
const current = this.db.get('settings').value()
const updated = this.db.set('settings', { ...current, ...settings, ipfs: { ...current.ipfs, ...settings.ipfs } }).write()
if (unpublished === true) {
this.db.set('unpublishedChanges', true).write()
}
return updated
}
async publish () {
const hash = await this.ipfs.publish(this.getIncidents(), this.getSettings())
this.updateSettings({ ipfs: { hash } })
this.db.set('unpublishedChanges', false).write()
return hash
}
async getStatus () {
const peers = await this.ipfs.getConnectedPeers()
const unpublishedChanges = this.db.get('unpublishedChanges').value()
return { peers: peers.length, unpublishedChanges }
}
}
module.exports = DStatusPage