-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make `GlobalView` render from the provided `SystemState` closes #1286 * fix lints
- Loading branch information
Showing
6 changed files
with
299 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { SystemState } from "types"; | ||
import { aggMonolithRooms, countRoomClients } from "./aggregate"; | ||
|
||
const sampleSystemState: SystemState = [ | ||
{ | ||
id: "154d9d41-128c-45ab-83d8-28661882c9e3", | ||
region: "ewr", | ||
monoliths: [ | ||
{ | ||
id: "2bd5e4a7-14f6-4da4-bedd-72946864a7bf", | ||
region: "ewr", | ||
rooms: [ | ||
{ name: "foo", clients: 2 }, | ||
{ name: "bar", clients: 0 }, | ||
], | ||
}, | ||
{ | ||
id: "419580cb-f576-4314-8162-45340c94bae1", | ||
region: "ewr", | ||
rooms: [{ name: "baz", clients: 3 }], | ||
}, | ||
{ | ||
id: "0c85b46e-d343-46a3-ae4f-5f2aa1a8bdac", | ||
region: "cdg", | ||
rooms: [{ name: "qux", clients: 0 }], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: "c91d183c-980e-4160-b196-43658148f469", | ||
region: "ewr", | ||
monoliths: [ | ||
{ | ||
id: "2bd5e4a7-14f6-4da4-bedd-72946864a7bf", | ||
region: "ewr", | ||
rooms: [ | ||
{ name: "foo", clients: 1 }, | ||
{ name: "bar", clients: 2 }, | ||
], | ||
}, | ||
{ | ||
id: "419580cb-f576-4314-8162-45340c94bae1", | ||
region: "ewr", | ||
rooms: [{ name: "baz", clients: 0 }], | ||
}, | ||
{ | ||
id: "0c85b46e-d343-46a3-ae4f-5f2aa1a8bdac", | ||
region: "cdg", | ||
rooms: [{ name: "qux", clients: 0 }], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: "5a2e3b2d-f27b-4e3d-9b59-c921442f7ff0", | ||
region: "cdg", | ||
monoliths: [ | ||
{ | ||
id: "2bd5e4a7-14f6-4da4-bedd-72946864a7bf", | ||
region: "ewr", | ||
rooms: [ | ||
{ name: "foo", clients: 0 }, | ||
{ name: "bar", clients: 0 }, | ||
], | ||
}, | ||
{ | ||
id: "419580cb-f576-4314-8162-45340c94bae1", | ||
region: "ewr", | ||
rooms: [{ name: "baz", clients: 0 }], | ||
}, | ||
{ | ||
id: "0c85b46e-d343-46a3-ae4f-5f2aa1a8bdac", | ||
region: "cdg", | ||
rooms: [{ name: "qux", clients: 4 }], | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
describe("aggregation helpers", () => { | ||
it("counts room clients", () => { | ||
expect(countRoomClients(sampleSystemState)).toEqual({ | ||
foo: 3, | ||
bar: 2, | ||
baz: 3, | ||
qux: 4, | ||
}); | ||
}); | ||
|
||
it("aggregates monolith rooms", () => { | ||
expect(aggMonolithRooms(sampleSystemState)).toEqual({ | ||
"2bd5e4a7-14f6-4da4-bedd-72946864a7bf": ["foo", "bar"], | ||
"419580cb-f576-4314-8162-45340c94bae1": ["baz"], | ||
"0c85b46e-d343-46a3-ae4f-5f2aa1a8bdac": ["qux"], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { SystemState } from "types"; | ||
|
||
/** | ||
* Builds a map of room names to the number of clients in each room from the room state. | ||
* @param state | ||
*/ | ||
export function countRoomClients(state: SystemState): Record<string, number> { | ||
const roomClients: Record<string, number> = {}; | ||
for (const balancer of state) { | ||
for (const monolith of balancer.monoliths) { | ||
for (const room of monolith.rooms) { | ||
roomClients[room.name] = (roomClients[room.name] ?? 0) + room.clients; | ||
} | ||
} | ||
} | ||
return roomClients; | ||
} | ||
|
||
/** | ||
* Collect a map of monolith ids to room names. | ||
* @param state | ||
* @returns | ||
*/ | ||
export function aggMonolithRooms(state: SystemState): Record<string, string[]> { | ||
const roomClients: Record<string, Set<string>> = {}; | ||
for (const balancer of state) { | ||
for (const monolith of balancer.monoliths) { | ||
for (const room of monolith.rooms) { | ||
const s = roomClients[monolith.id] ?? new Set(); | ||
s.add(room.name); | ||
roomClients[monolith.id] = s; | ||
} | ||
} | ||
} | ||
return Object.fromEntries(Object.entries(roomClients).map(([k, v]) => [k, Array.from(v)])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.