Skip to content

Commit

Permalink
frontend: read communities from response stream
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyuyureka committed Jan 29, 2024
1 parent 4fa76b7 commit bce40e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
6 changes: 1 addition & 5 deletions frontend/src/cache.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export let routers;
export let communities;
export const initCache = async () => {
[routers, communities] = await Promise.all([
fetch("/api/routers").then(resp => resp.json()),
fetch("/communities.json").then(resp => resp.json())
]);
routers = await fetch("/api/routers").then(resp => resp.json());
};
37 changes: 17 additions & 20 deletions frontend/src/resultsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@ import { html, render } from 'lit-html';
import { go } from './router.js';
import { searchTemplate } from './search.js';
import ndjsonStream from 'can-ndjson-stream';
import { routers, communities } from './cache.js';
import { routers } from './cache.js';

const resultTemplate = (result, havePeerColumn, asn_names) => html`
const resultTemplate = (result, havePeerColumn, dnsMap, asnMap, communityMap) => html`
<tr class=${result.state}>
<td><span>${result.client_name}</span></td>
${havePeerColumn ? html`<td><span>${result.peer_address}</span></td>` : ``}
<td><span>${result.net}</span></td>
<td><span>${result.as_path.map(asn => html`
${asn_names[asn] !== undefined
? html`<span title=${asn_names[asn]}>${asn}</span>`
${asn in asnMap
? html`<span title=${asnMap[asn]}>${asn}</span>`
: html`<span>${asn}</span>`}
`)}</span></td>
<td><span>${[
...(result.large_communities || []),
...(result.communities || [])
]
.map(community => community.join(":"))
.map(community => community in communities
? html`<div class="tag named tooltip" title=${community}>${communities[community]}</div>`
.map(community => community in communityMap
? html`<div class="tag named tooltip" title=${community}>${communityMap[community]}</div>`
: html`<div class="tag">${community}</div>`
)
}</span></td>
<td><span>${result.origin}</span></td>
<td><span>${result.med}</span></td>
<td><span>${result.local_pref}</span></td>
<td>
${result?.nexthop_resolved?.ReverseDns !== undefined
? html`<span title=${result.nexthop}>${result?.nexthop_resolved?.ReverseDns}</span>`
${result.nexthop in dnsMap
? html`<span title=${result.nexthop}>${dnsMap[result.nexthop]}</span>`
: html`<span>${result.nexthop}</span>`}
</td>
<td><span>${result.state}</span></td>
</tr>
`;

const resultsTemplate = (query, { routeResults, asn_names }, done) => html`
const resultsTemplate = (query, { routeResults, dnsMap, asnMap, communityMap }, done) => html`
${searchTemplate(query)}
<div class="results">
Expand All @@ -57,7 +57,7 @@ const resultsTemplate = (query, { routeResults, asn_names }, done) => html`
</tr>
</thead>
<tbody>
${routeResults.map(result => resultTemplate(result, routeResults.some(result => result.peer_address), asn_names))}
${routeResults.map(result => resultTemplate(result, routeResults.some(result => result.peer_address), dnsMap, asnMap, communityMap))}
</tbody>
</table>
` : ''}
Expand All @@ -82,9 +82,13 @@ const processResults = (results) => {
const routeResults = results.filter(r => !!r.Route).map(r => r.Route);
const dnsResults = results.filter(r => !!r.ReverseDns).map(r => r.ReverseDns);
const asnResults = results.filter(r => !!r.AsnName).map(r => r.AsnName);
const communityResults = results.filter(r => !!r.CommunityDescription).map(r => r.CommunityDescription);

const asn_names = Object.fromEntries(asnResults.map(r => [r.asn, r.asn_name ]));
const dnsMap = Object.fromEntries(dnsResults.map(r => [r.nexthop, { nexthop_resolved: r.nexthop_resolved }]));
const dnsMap = Object.fromEntries(dnsResults.map(r => [r.nexthop, r.nexthop_resolved]));
const asnMap = Object.fromEntries(asnResults.map(r => [r.asn, r.asn_name ]));
const communityMap = Object.fromEntries(communityResults.map(r => [r.community, r.community_description ]));

console.log(asnMap, communityMap, dnsMap);

// stage 1, combine pre- and post-policy adj-in tables
// start out with PostPolicy
Expand Down Expand Up @@ -157,14 +161,7 @@ const processResults = (results) => {
return 0;
});

// add resolved nexthop data
for (const result of newResults) {
if (result.nexthop in dnsMap) {
Object.assign(result, dnsMap[result.nexthop])
}
}

return { routeResults: newResults, asn_names };
return { routeResults: newResults, asnMap, communityMap, dnsMap };
};

export const resultsView = async (query) => {
Expand Down

0 comments on commit bce40e2

Please sign in to comment.