Skip to content

Commit

Permalink
Use -1 if no port
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Dec 6, 2024
1 parent cf1194f commit db329f7
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions library/agent/Hostnames.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Ports = Map<string, number>;
type Ports = Map<number, number>;

export class Hostnames {
private map: Map<string, Ports> = new Map();
Expand All @@ -13,16 +13,15 @@ export class Hostnames {
return key === "__NO_PORT__" ? undefined : parseInt(key, 10);
}

add(hostname: string, port: number | undefined) {
const portKey = this.portKey(port);
add(hostname: string, port: number | undefined = -1) {
if (!this.map.has(hostname)) {
this.map.set(hostname, new Map([[portKey, 1]]));
this.map.set(hostname, new Map([[port, 1]]));
} else {
const ports = this.map.get(hostname) as Ports;
if (ports.has(portKey)) {
ports.set(portKey, ports.get(portKey)! + 1);
if (!ports.has(port)) {
ports.set(port, 1);
} else {
ports.set(portKey, 1);
ports.set(port, ports.get(port)! + 1);
}
}

Expand Down Expand Up @@ -55,7 +54,7 @@ export class Hostnames {
Array.from(ports.entries()).map(([port, hits]) => {
return {
hostname,
port: this.keyToPort(port),
port: port === -1 ? undefined : port,
hits,
};
})
Expand Down

0 comments on commit db329f7

Please sign in to comment.