github.com/outbrain/consul@v1.4.5/ui-v2/app/utils/tomography.js (about) 1 // TODO: Istanbul is ignored for the moment as it's not mine, 2 // once I come here properly and 100% follow unignore 3 /* istanbul ignore file */ 4 export default function(distance) { 5 return function(name, coordinates) { 6 var min = 999999999; 7 var max = -999999999; 8 var distances = []; 9 coordinates.forEach(function(node) { 10 if (name == node.Node) { 11 var segment = node.Segment; 12 coordinates.forEach(function(other) { 13 if (node.Node != other.Node && other.Segment == segment) { 14 var dist = distance(node, other); 15 distances.push({ node: other.Node, distance: dist, segment: segment }); 16 if (dist < min) { 17 min = dist; 18 } 19 if (dist > max) { 20 max = dist; 21 } 22 } 23 }); 24 distances.sort(function(a, b) { 25 return a.distance - b.distance; 26 }); 27 } 28 }); 29 var n = distances.length; 30 var halfN = Math.floor(n / 2); 31 var median; 32 33 if (n > 0) { 34 if (n % 2) { 35 // odd 36 median = distances[halfN].distance; 37 } else { 38 median = (distances[halfN - 1].distance + distances[halfN].distance) / 2; 39 } 40 } else { 41 median = 0; 42 min = 0; 43 max = 0; 44 } 45 return { 46 distances: distances, 47 n: distances.length, 48 min: parseInt(min * 100) / 100, 49 median: parseInt(median * 100) / 100, 50 max: parseInt(max * 100) / 100, 51 }; 52 }; 53 }