From eeb0d72a4f47c1c0e1ff78d13bfb38f412b98048 Mon Sep 17 00:00:00 2001 From: Julien Date: Tue, 18 Feb 2025 01:11:45 +0100 Subject: [PATCH] feat(simulator): add bus logs --- simulator/inertia/app/app.ts | 1 - simulator/inertia/pages/home.vue | 153 +++++++++++++++++++++++------ simulator/start/chaos/chaos_bus.ts | 26 ++++- simulator/start/routes.ts | 27 +++++ 4 files changed, 174 insertions(+), 33 deletions(-) diff --git a/simulator/inertia/app/app.ts b/simulator/inertia/app/app.ts index ebf6a19..64a0f9c 100644 --- a/simulator/inertia/app/app.ts +++ b/simulator/inertia/app/app.ts @@ -1,7 +1,6 @@ /// /// -import '@unocss/reset/tailwind-compat.css' import 'virtual:uno.css' import 'primeicons/primeicons.css' diff --git a/simulator/inertia/pages/home.vue b/simulator/inertia/pages/home.vue index d91322f..7f2a0c8 100644 --- a/simulator/inertia/pages/home.vue +++ b/simulator/inertia/pages/home.vue @@ -1,18 +1,32 @@ @@ -25,27 +39,44 @@ function setCacheValue(name: string, value: any) { @@ -55,13 +86,22 @@ function setCacheValue(name: string, value: any) { + + + + + + diff --git a/simulator/start/chaos/chaos_bus.ts b/simulator/start/chaos/chaos_bus.ts index 7955836..9ad50a3 100644 --- a/simulator/start/chaos/chaos_bus.ts +++ b/simulator/start/chaos/chaos_bus.ts @@ -2,6 +2,13 @@ import type { Serializable, SubscribeHandler, Transport } from '@boringnode/bus/ import { ChaosInjector } from './chaos_injector.js' +interface BusMessage { + channel: string + message: Serializable + busId: string + timestamp: number +} + export class ChaosBus implements Transport { /** * The inner transport driver that is wrapped @@ -14,6 +21,9 @@ export class ChaosBus implements Transport { #chaosInjector: ChaosInjector id!: string + sentMessages: Array = [] + receivedMessages: Array = [] + constructor(innerTransport: Transport) { this.#innerTransport = innerTransport this.#chaosInjector = new ChaosInjector() @@ -48,11 +58,25 @@ export class ChaosBus implements Transport { async publish(channel: string, message: Serializable) { await this.#chaosInjector.injectChaos() + this.sentMessages.push({ + channel, + message, + busId: this.id, + timestamp: Date.now(), + }) return this.#innerTransport.publish(channel, message) } async subscribe(channel: string, handler: SubscribeHandler) { - return this.#innerTransport.subscribe(channel, handler) + return this.#innerTransport.subscribe(channel, (message) => { + this.receivedMessages.push({ + channel, + message, + busId: this.id, + timestamp: Date.now(), + }) + return handler(message as any) + }) } unsubscribe(channel: string) { diff --git a/simulator/start/routes.ts b/simulator/start/routes.ts index 70d131e..f961321 100644 --- a/simulator/start/routes.ts +++ b/simulator/start/routes.ts @@ -71,6 +71,20 @@ router.get('/', async ({ inertia }) => { correctValue: await trueCache.get({ key: 'value', defaultValue: 0 }), caches: await Promise.all(results), state, + sentMessages: [...nodes.entries()] + .map(([key, cache]) => + cache.bus.sentMessages.map((message) => ({ ...message, cacheName: key })), + ) + .flat() + .sort((a, b) => b.timestamp - a.timestamp) + .slice(0, 40), + receivedMessages: [...nodes.entries()] + .map(([key, cache]) => + cache.bus.receivedMessages.map((message) => ({ ...message, cacheName: key })), + ) + .flat() + .sort((a, b) => b.timestamp - a.timestamp) + .slice(0, 40), }) }) @@ -88,6 +102,19 @@ router.post('/set', async ({ request, response }) => { return response.redirect().toPath('/') }) +router.post('/delete', async ({ request, response }) => { + const cacheName = request.input('name') + + if (!nodes.has(cacheName)) { + return response.status(400).send('Invalid cache name') + } + + const cache = nodes.get(cacheName) + await cache?.bento.delete({ key: 'value' }) + + return response.redirect().toPath('/') +}) + const stateSchema = vine.compile( vine.object({ bus: vine.boolean().optional(),