41 lines
1.7 KiB
JavaScript
41 lines
1.7 KiB
JavaScript
import test from 'brittle'
|
|
import { pearson, rankRelatedCharts } from '../shared/related-metrics.js'
|
|
|
|
test('pearson correlates aligned series', (t) => {
|
|
const x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
const y = x.map((v) => v * 2)
|
|
t.ok(pearson(x, y) > 0.99)
|
|
const z = x.map((v) => -v)
|
|
t.ok(pearson(x, z) < -0.99)
|
|
})
|
|
|
|
test('rankRelatedCharts prefers same context/family', (t) => {
|
|
const catalog = {
|
|
'system.cpu': { context: 'system.cpu', family: 'cpu', plugin: 'proc', units: '%' },
|
|
'cpu.cpu0': { context: 'cpu.cpu', family: 'cpu', plugin: 'proc', units: '%' },
|
|
'system.ram': { context: 'system.ram', family: 'ram', plugin: 'proc', units: 'MiB' },
|
|
'nginx.connections': { context: 'nginx.connections', family: 'nginx', plugin: 'nginx', units: 'connections' },
|
|
}
|
|
const ranked = rankRelatedCharts('system.cpu', catalog, null, { limit: 5 })
|
|
t.ok(ranked.length >= 1)
|
|
t.is(ranked[0].id, 'cpu.cpu0')
|
|
t.ok(!ranked.some((r) => r.id === 'nginx.connections') || ranked.at(-1)?.id === 'nginx.connections')
|
|
})
|
|
|
|
test('rankRelatedCharts boosts loaded correlation', (t) => {
|
|
const series = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
|
const catalog = {
|
|
a: { context: 'x.a', family: 'f1', plugin: 'p' },
|
|
b: { context: 'y.b', family: 'f2', plugin: 'q' },
|
|
c: { context: 'z.c', family: 'f3', plugin: 'r' },
|
|
}
|
|
const loaded = new Map([
|
|
['a', { dims: new Map([['v', series]]) }],
|
|
['b', { dims: new Map([['v', series.map((n) => n * 3)]]) }],
|
|
['c', { dims: new Map([['v', series.map((_, i) => (i % 2 ? 10 : 0))]]) }],
|
|
])
|
|
const ranked = rankRelatedCharts('a', catalog, loaded, { limit: 5 })
|
|
t.ok(ranked[0].id === 'b')
|
|
t.ok(ranked[0].reason.includes('corr'))
|
|
})
|