Skip to content

Commit e182d4f

Browse files
authored
Merge pull request #4 from Snider/claude/expose-networking-analytics-LNUmx
feat: Expose networking analytics for KD-Tree NAT routing
2 parents 299b01e + 298791e commit e182d4f

9 files changed

Lines changed: 4430 additions & 25 deletions

File tree

dns_tools.go

Lines changed: 1006 additions & 0 deletions
Large diffs are not rendered by default.

dns_tools_test.go

Lines changed: 732 additions & 0 deletions
Large diffs are not rendered by default.

examples/wasm-browser-ts/src/main.ts

Lines changed: 145 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,159 @@ async function run() {
1313

1414
console.log('Poindexter (WASM) version:', await px.version());
1515

16+
// =========================================================================
17+
// Basic KD-Tree operations
18+
// =========================================================================
1619
const tree = await px.newTree(2);
17-
await tree.insert({ id: 'a', coords: [0, 0], value: 'A' });
18-
await tree.insert({ id: 'b', coords: [1, 0], value: 'B' });
19-
await tree.insert({ id: 'c', coords: [0, 1], value: 'C' });
20+
await tree.insert({ id: 'peer-a', coords: [0, 0], value: 'Peer A' });
21+
await tree.insert({ id: 'peer-b', coords: [1, 0], value: 'Peer B' });
22+
await tree.insert({ id: 'peer-c', coords: [0, 1], value: 'Peer C' });
23+
await tree.insert({ id: 'peer-d', coords: [0.5, 0.5], value: 'Peer D' });
2024

25+
console.log('\n=== Basic Queries ===');
2126
const nn = await tree.nearest([0.9, 0.1]);
2227
console.log('Nearest [0.9,0.1]:', nn);
2328

24-
const kn = await tree.kNearest([0.9, 0.9], 2);
25-
console.log('kNN k=2 [0.9,0.9]:', kn);
29+
const kn = await tree.kNearest([0.5, 0.5], 3);
30+
console.log('kNN k=3 [0.5,0.5]:', kn);
2631

2732
const rad = await tree.radius([0, 0], 1.1);
2833
console.log('Radius r=1.1 [0,0]:', rad);
34+
35+
// =========================================================================
36+
// Analytics Demo
37+
// =========================================================================
38+
console.log('\n=== Tree Analytics ===');
39+
40+
// Perform more queries to generate analytics
41+
for (let i = 0; i < 10; i++) {
42+
await tree.nearest([Math.random(), Math.random()]);
43+
}
44+
await tree.kNearest([0.2, 0.8], 2);
45+
await tree.kNearest([0.7, 0.3], 2);
46+
47+
// Get tree-level analytics
48+
const analytics = await tree.getAnalytics();
49+
console.log('Tree Analytics:', {
50+
queryCount: analytics.queryCount,
51+
insertCount: analytics.insertCount,
52+
avgQueryTimeNs: analytics.avgQueryTimeNs,
53+
minQueryTimeNs: analytics.minQueryTimeNs,
54+
maxQueryTimeNs: analytics.maxQueryTimeNs,
55+
});
56+
57+
// =========================================================================
58+
// Peer Selection Analytics
59+
// =========================================================================
60+
console.log('\n=== Peer Selection Analytics ===');
61+
62+
// Get all peer stats
63+
const peerStats = await tree.getPeerStats();
64+
console.log('All Peer Stats:', peerStats);
65+
66+
// Get top 3 most frequently selected peers
67+
const topPeers = await tree.getTopPeers(3);
68+
console.log('Top 3 Peers:', topPeers);
69+
70+
// =========================================================================
71+
// Axis Distribution Analysis
72+
// =========================================================================
73+
console.log('\n=== Axis Distributions ===');
74+
75+
const axisDists = await tree.getAxisDistributions(['latency', 'hops']);
76+
console.log('Axis Distributions:', axisDists);
77+
78+
// =========================================================================
79+
// NAT Routing / Peer Quality Scoring
80+
// =========================================================================
81+
console.log('\n=== NAT Routing & Peer Quality ===');
82+
83+
// Simulate peer network metrics
84+
const peerMetrics = {
85+
connectivityScore: 0.9,
86+
symmetryScore: 0.8,
87+
relayProbability: 0.1,
88+
directSuccessRate: 0.95,
89+
avgRttMs: 50,
90+
jitterMs: 10,
91+
packetLossRate: 0.01,
92+
bandwidthMbps: 100,
93+
natType: 'full_cone' as const,
94+
};
95+
96+
const qualityScore = await px.computePeerQualityScore(peerMetrics);
97+
console.log('Peer Quality Score (0-1):', qualityScore.toFixed(3));
98+
99+
// Get default quality weights
100+
const defaultWeights = await px.getDefaultQualityWeights();
101+
console.log('Default Quality Weights:', defaultWeights);
102+
103+
// =========================================================================
104+
// Trust Score Calculation
105+
// =========================================================================
106+
console.log('\n=== Trust Score ===');
107+
108+
const trustMetrics = {
109+
reputationScore: 0.8,
110+
successfulTransactions: 150,
111+
failedTransactions: 3,
112+
ageSeconds: 86400 * 30, // 30 days
113+
vouchCount: 5,
114+
flagCount: 0,
115+
proofOfWork: 0.5,
116+
};
117+
118+
const trustScore = await px.computeTrustScore(trustMetrics);
119+
console.log('Trust Score (0-1):', trustScore.toFixed(3));
120+
121+
// =========================================================================
122+
// Distribution Statistics
123+
// =========================================================================
124+
console.log('\n=== Distribution Statistics ===');
125+
126+
// Simulate some distance measurements
127+
const distances = [0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.5, 0.8, 1.2];
128+
const distStats = await px.computeDistributionStats(distances);
129+
console.log('Distance Distribution Stats:', {
130+
count: distStats.count,
131+
min: distStats.min.toFixed(3),
132+
max: distStats.max.toFixed(3),
133+
mean: distStats.mean.toFixed(3),
134+
median: distStats.median.toFixed(3),
135+
stdDev: distStats.stdDev.toFixed(3),
136+
p90: distStats.p90.toFixed(3),
137+
});
138+
139+
// =========================================================================
140+
// Feature Normalization for KD-Tree
141+
// =========================================================================
142+
console.log('\n=== Feature Normalization ===');
143+
144+
// Raw peer features: [latency_ms, hops, geo_km, trust_inv, bw_inv, loss, conn_inv, nat_inv]
145+
const rawFeatures = [100, 5, 500, 0.1, 50, 0.02, 5, 0.1];
146+
147+
// Get default feature ranges
148+
const featureRanges = await px.getDefaultPeerFeatureRanges();
149+
console.log('Feature Labels:', featureRanges.labels);
150+
151+
// Normalize features
152+
const normalizedFeatures = await px.normalizePeerFeatures(rawFeatures);
153+
console.log('Normalized Features:', normalizedFeatures.map((f: number) => f.toFixed(3)));
154+
155+
// Apply custom weights
156+
const customWeights = [1.5, 1.0, 0.5, 1.2, 0.8, 2.0, 1.0, 0.7];
157+
const weightedFeatures = await px.weightedPeerFeatures(normalizedFeatures, customWeights);
158+
console.log('Weighted Features:', weightedFeatures.map((f: number) => f.toFixed(3)));
159+
160+
// =========================================================================
161+
// Analytics Reset
162+
// =========================================================================
163+
console.log('\n=== Analytics Reset ===');
164+
await tree.resetAnalytics();
165+
const resetAnalytics = await tree.getAnalytics();
166+
console.log('After Reset - Query Count:', resetAnalytics.queryCount);
167+
168+
console.log('\n=== Demo Complete ===');
29169
}
30170

31171
run().catch((err) => {

0 commit comments

Comments
 (0)