-
Notifications
You must be signed in to change notification settings - Fork 1
/
ping-formats.js
190 lines (164 loc) · 4.98 KB
/
ping-formats.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// In-house modules
const { Enum } = require('./enum.js')
class PingData {
constructor(data){
this._class = 'PingData'
this.ttlHops = data.ttlHops // num
this.roundTripTimeMs = data.roundTripTimeMs // num
this.responseSize = data.responseSize // num
this.icmpSeq = data.icmpSeq // num
this.timeRequestSent = data.timeRequestSent // Date
this.timeResponseReceived = data.timeResponseReceived // Date
this.errorType = data.errorType || null
this.failure = (data.failure === true || data.failure === false) ? data.failure : undefined
}
static get errorTypes(){
return new Enum([
{
accessor: 'requestTimedOutError',
humanName: 'Request timed out (spent too long waiting for a response from the server)'
},{
accessor: 'destinationUnreachableError',
humanName: 'Destination unreachable'
},{
accessor: 'packetTooBigError',
humanName: 'Packet size too big' // TODO: too big for server or ping client?
},{
accessor: 'parameterProblemError',
humanName: 'Parameter problem (???)' // TODO: What parameters? Where did problem occur?
},{
accessor: 'redirectReceivedError',
humanName: 'Received a redirect' // TODO: On the way to the intended server or on the way back?
},{
accessor: 'sourceQuenchError',
humanName: 'Source quench (???)' // TODO: What is this?
},{
accessor: 'timeExceededError', // TODO: What time?
humanName: 'Time exceeded'
},{
accessor: 'networkDownError',
humanName: 'Local network is down (caused by raw-socket)'
},{
accessor: 'unknownError',
humanName: 'Error with unknown cause / handling'
}
])
}
get revivalPropTypes(){
return [
{ typeClass: Date, propKey: 'timeResponseReceived' },
{ typeClass: Date, propKey: 'timeRequestSent' },
{ propKey: 'errorType', reviveFn: (simpleParseData)=>{
return PingData.errorTypes[simpleParseData.accessor] // Cast to enum
}}
]
}
}
class RequestError {
constructor(errorType, timeRequestSent, timeResponseReceived, errorData){
this._class = 'RequestError'
this.errorData = errorData
this.errorType = errorType
this.timeRequestSent = timeRequestSent
this.timeResponseReceived = timeResponseReceived // OK for this to be undefined
}
static get errorTypes(){
return new Enum([
// Most of these error names/descriptions taked from `net-ping` package
{
accessor: 'destinationUnreachableError',
humanName: 'Destination unreachable'
},{
accessor: 'packetTooBigError',
humanName: 'Packet size too big' // TODO: too big for server or ping client?
},{
accessor: 'parameterProblemError',
humanName: 'Parameter problem (???)' // TODO: What parameters? Where did problem occur?
},{
accessor: 'redirectReceivedError',
humanName: 'Received a redirect' // TODO: On the way to the intended server or on the way back?
},{
accessor: 'sourceQuenchError',
humanName: 'Source quench (???)' // TODO: What is this?
},{
accessor: 'timeExceededError', // TODO: What time?
humanName: 'Time exceeded'
},{
accessor: 'unknownError',
humanName: 'Error with unknown cause / handling'
}
])
}
get revivalPropTypes(){
return [
{ typeClass: Date, propKey: 'timeResponseReceived' },
{ typeClass: Date, propKey: 'timeRequestSent' },
{ propKey: 'errorType', reviveFn: (simpleParseData)=>{
return RequestError.errorTypes[simpleParseData.accessor] // Cast to enum
}}
]
}
}
// TODO: extend from Outage?
class TargetOutage {
constructor(pingList){
this._class = 'TargetOutage'
if ( !(pingList instanceof Array && pingList.length >= 1)){
throw Error('pingList is not a valid array: ' + pingList)
}
this.pingList = pingList
}
get startDate(){
return this.pingList[0].timeResponseReceived
}
get endDate(){
return this.pingList[this.pingList.length - 1].timeResponseReceived
}
get revivalPropTypes(){
return [
{ typeClass: Date, propKey: 'startDate' },
{ typeClass: Date, propKey: 'endDate' }
]
}
}
class Outage {
constructor(startDate, endDate){
this._class = 'Outage'
this.startDate = startDate
this.endDate = endDate
}
get durationSec(){
return (this.endDate - this.startDate) / 1000
}
get revivalPropTypes(){
return [
{ typeClass: Date, propKey: 'startDate' },
{ typeClass: Date, propKey: 'endDate' }
]
}
}
class PingsLog {
constructor(obj){
this._class = 'PingsLog'
this.dateLogCreated = new Date()
this.dateLogLastUpdated = new Date()
this.combinedPingList = []
this.outages = obj.outages
this.sessionStartTime = obj.sessionStartTime
this.sessionEndTime = obj.sessionEndTime
this.targetList = obj.targetList
}
get revivalPropTypes(){
return [
{ typeClass: Date, propKey: 'dateLogCreated' },
{ typeClass: Date, propKey: 'dateLogLastUpdated' },
{ typeClass: Date, propKey: 'sessionStartTime' },
{ typeClass: Date, propKey: 'sessionEndTime' }
]
}
}
exports.RequestError = RequestError
exports.PingData = PingData
exports.Outage = Outage
exports.TargetOutage = TargetOutage
exports.PingsLog = PingsLog