(sample: LoadSnapshot[], overloadedRatio: number)
| 186 | * evaluation logic used by `SystemStatus` for all signal types. |
| 187 | */ |
| 188 | export function evaluateLoadSignalSample(sample: LoadSnapshot[], overloadedRatio: number): ClientInfo { |
| 189 | if (sample.length === 0) { |
| 190 | return { |
| 191 | isOverloaded: false, |
| 192 | limitRatio: overloadedRatio, |
| 193 | actualRatio: 0, |
| 194 | }; |
| 195 | } |
| 196 | |
| 197 | const weights: number[] = []; |
| 198 | const values: number[] = []; |
| 199 | |
| 200 | for (let i = 1; i < sample.length; i++) { |
| 201 | const previous = sample[i - 1]; |
| 202 | const current = sample[i]; |
| 203 | const weight = +current.createdAt - +previous.createdAt; |
| 204 | weights.push(weight || 1); // Prevent errors from 0ms long intervals (sync) between snapshots. |
| 205 | values.push(+current.isOverloaded); |
| 206 | } |
| 207 | |
| 208 | const wAvg = sample.length === 1 ? +sample[0].isOverloaded : weightedAvg(values, weights); |
| 209 | |
| 210 | return { |
| 211 | isOverloaded: wAvg > overloadedRatio, |
| 212 | limitRatio: overloadedRatio, |
| 213 | actualRatio: Math.round(wAvg * 1000) / 1000, |
| 214 | }; |
| 215 | } |
no test coverage detected
searching dependent graphs…