From 462b5b095cb514c975d3d4cb73e1ace443b6afb3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 1 Jan 2024 18:06:10 -0600 Subject: [PATCH] debug: add debug to queries --- src/queries.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/queries.ts b/src/queries.ts index de60553..cfeeb36 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -1,11 +1,13 @@ import { eventsDB } from '@/db/events.ts'; import { memorelay } from '@/db/memorelay.ts'; -import { type Event, findReplyTag } from '@/deps.ts'; +import { Debug, type Event, findReplyTag } from '@/deps.ts'; import { type AuthorMicrofilter, type DittoFilter, type IdMicrofilter, type Relation } from '@/filter.ts'; import { reqmeister } from '@/reqmeister.ts'; import { type DittoEvent } from '@/store.ts'; import { getTagSet } from '@/tags.ts'; +const debug = Debug('ditto:queries'); + interface GetEventOpts { /** Signal to abort the request. */ signal?: AbortSignal; @@ -20,12 +22,14 @@ const getEvent = async ( id: string, opts: GetEventOpts = {}, ): Promise | undefined> => { + debug(`getEvent: ${id}`); const { kind, relations, signal = AbortSignal.timeout(1000) } = opts; const microfilter: IdMicrofilter = { ids: [id] }; const [memoryEvent] = await memorelay.getEvents([microfilter], opts) as DittoEvent[]; if (memoryEvent && !relations) { + debug(`getEvent: ${id.slice(0, 8)} found in memory`); return memoryEvent; } @@ -44,16 +48,29 @@ const getEvent = async ( dbEvent.author = author; } - if (dbEvent) return dbEvent; + if (dbEvent) { + debug(`getEvent: ${id.slice(0, 8)} found in db`); + return dbEvent; + } if (memoryEvent && !memoryEvent.author) { const [author] = await memorelay.getEvents([{ kinds: [0], authors: [memoryEvent.pubkey] }], opts); memoryEvent.author = author; } - if (memoryEvent) return memoryEvent; + if (memoryEvent) { + debug(`getEvent: ${id.slice(0, 8)} found in memory`); + return memoryEvent; + } - return await reqmeister.req(microfilter, opts).catch(() => undefined) as Event | undefined; + const reqEvent = await reqmeister.req(microfilter, opts).catch(() => undefined) as Event | undefined; + + if (reqEvent) { + debug(`getEvent: ${id.slice(0, 8)} found by reqmeister`); + return reqEvent; + } + + debug(`getEvent: ${id.slice(0, 8)} not found`); }; /** Get a Nostr `set_medatadata` event for a user's pubkey. */