From a31ea40eacc9d04179ab9b39eb4fd7f52a167edd Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Fri, 24 Nov 2023 13:25:30 -0500 Subject: [PATCH] refactor messages handling --- src/fetch/GET.ts | 17 ++-------- src/fetch/messages.ts | 73 ++++++++++++++++++++++++++++++------------- src/http.ts | 10 ------ src/sqlite.ts | 1 - 4 files changed, 53 insertions(+), 48 deletions(-) delete mode 100644 src/http.ts diff --git a/src/fetch/GET.ts b/src/fetch/GET.ts index 8f7f4f1..d82c7e7 100644 --- a/src/fetch/GET.ts +++ b/src/fetch/GET.ts @@ -7,8 +7,7 @@ import openapi from "./openapi.js"; import swaggerHtml from "../../swagger/index.html" import swaggerFavicon from "../../swagger/favicon.png" import { toFile, toJSON, toText } from "./cors.js"; -import { selectMessages } from "./messages.js"; -import { DEFAULT_RECENT_MESSAGES_LIMIT } from "../config.js"; +import { handleMessages } from "./messages.js"; import { checkHealth } from "./health.js"; export default async function (req: Request, server: Server) { @@ -19,19 +18,7 @@ export default async function (req: Request, server: Server) { const key = req.headers.get("sec-websocket-key") const chain = searchParams.get("chain") const moduleHash = searchParams.get("moduleHash"); - const distinct = searchParams.get("distinct"); const success = server.upgrade(req, {data: {key, chain, moduleHash}}); - let limit = Number(searchParams.get("limit")); - let sort = searchParams.get("sort"); - - // error handling - if (limit === null || limit === 0) limit = DEFAULT_RECENT_MESSAGES_LIMIT; - if (isNaN(Number(limit))) return toText("limit must be a number", 400 ); - if (sort === null) sort = "desc"; - if (distinct !== "true" && distinct !== null) return toText("distinct must be set to true if declared", 400 ); - if (distinct === "true" && chain) return toText("chain cannot be set if distinct is set to true", 400 ); - if (sort !== "asc" && sort !== "desc") return toText("sort must be asc or desc", 400 ); - if (success) { logger.info('upgrade', {key, chain, moduleHash}); return; @@ -46,7 +33,7 @@ export default async function (req: Request, server: Server) { if ( pathname === "/traceId") return toJSON(sqlite.selectAll(db, "traceId")); if ( pathname === "/chain") return toJSON(sqlite.selectAll(db, "chain")); if ( pathname === "/openapi") return toJSON(openapi); - if ( pathname === "/messages") return selectMessages(db, chain, moduleHash, limit, distinct, sort); + if ( pathname === "/messages") return handleMessages(req); return toText("Not found", 400 ); } \ No newline at end of file diff --git a/src/fetch/messages.ts b/src/fetch/messages.ts index c790dfc..48bfb31 100644 --- a/src/fetch/messages.ts +++ b/src/fetch/messages.ts @@ -1,14 +1,44 @@ import * as sqlite from "../sqlite.js"; -import { RECENT_MESSAGES_LIMIT } from "../config.js"; -import { toJSON } from "../http.js"; +import { DEFAULT_RECENT_MESSAGES_LIMIT, RECENT_MESSAGES_LIMIT } from "../config.js"; import Database from "bun:sqlite"; +import { db } from "../../index.js"; +import { toJSON } from "./cors.js"; + +export function parseLimit(searchParams: URLSearchParams) { + const value = searchParams.get("limit"); + if (!value) return DEFAULT_RECENT_MESSAGES_LIMIT; + const limit = Number(value); + if (isNaN(limit)) throw new Error("limit must be a number"); + return limit; +} + +export function parseSort(searchParams: URLSearchParams) { + const value = searchParams.get("sort"); + if (!value) return "desc"; + if (!["asc", "desc"].includes(value)) throw new Error("sort must be asc or desc"); + return value; +} + +export function handleMessages(req: Request) { + const { searchParams} = new URL(req.url); + // const distinct = searchParams.get("distinct"); + const limit = parseLimit(searchParams); + const sort = parseSort(searchParams); + const chain = searchParams.get("chain") + const moduleHash = searchParams.get("moduleHash"); + + // // error handling + // if (distinct !== "true" && distinct !== null) return toText("distinct must be set to true if declared", 400 ); + // if (distinct === "true" && chain) return toText("chain cannot be set if distinct is set to true", 400 ); + + selectMessages(db, limit, sort, chain, moduleHash); +} export function insertMessages(db: Database, traceId: string, timestamp: string, chain?: string) { const dbLength = sqlite.count(db, "messages"); if (dbLength >= RECENT_MESSAGES_LIMIT) { let oldest = sqlite.selectAll(db, "messages").sort((a: any, b: any) => a.timestamp - b.timestamp)[0]; - console.log("oldest", oldest) // update messages sqlite.replaceRecent(db, "messages", String(Date.now()), `${traceId}`, timestamp); @@ -17,7 +47,6 @@ export function insertMessages(db: Database, traceId: string, timestamp: string, // update messagesByChain if (chain) { oldest = sqlite.selectAll(db, "messagesByChain").sort((a: any, b: any) => a.timestamp - b.timestamp)[0]; - console.log(oldest) sqlite.replaceRecent(db, "messagesByChain", String(Date.now()), `${chain}:${traceId}`, timestamp ); sqlite.deleteRow(db, "messagesByChain", `${oldest.key}`); } @@ -29,33 +58,33 @@ export function insertMessages(db: Database, traceId: string, timestamp: string, if (chain) sqlite.replaceRecent(db, "messagesByChain", String(Date.now()), `${chain}:${traceId}`, timestamp ); } -export function selectMessages(db: Database, chain?: string, moduleHash?: string, limit?: number, distinct?: string, sortBy?: string) { +export function selectMessages(db: Database, limit: number, sortBy: string, chain?: string, moduleHash?: string,) { let messages = sqlite.selectAllRecent(db, "messages", "*", sortBy, limit); - if (distinct) messages = selectDistinct(distinct, messages, db, chain, sortBy, limit); + // if (distinct) messages = selectDistinct(distinct, messages, db, chain, sortBy, limit); if (chain) messages = sqlite.selectAllRecent(db, "messagesByChain", "*", sortBy, limit).filter((message: any) => message.value.includes(chain)); if (moduleHash) messages = messages.filter((message: any) => message.value.includes(moduleHash)); return toJSON(messages); } -export function selectDistinct(distinct?: string, messages?: any, db?: any, chain?: string, sortBy?: string, limit?: number) { - let chainList = sqlite.selectAll(db, "chain"); - if (distinct === "true") { +// export function selectDistinct(distinct?: string, messages?: any, db?: any, chain?: string, sortBy?: string, limit?: number) { +// let chainList = sqlite.selectAll(db, "chain"); +// if (distinct === "true") { - let distinctChain = []; - for (let i = 0; i < chainList.length; i++) { - let chainName = chainList[i].key; +// let distinctChain = []; +// for (let i = 0; i < chainList.length; i++) { +// let chainName = chainList[i].key; - messages = sqlite.selectAllRecent(db, "messagesByChain", "*", sortBy, limit) - messages = messages.filter((message: any) => message.value.includes(chainName)); +// messages = sqlite.selectAllRecent(db, "messagesByChain", "*", sortBy, limit) +// messages = messages.filter((message: any) => message.value.includes(chainName)); - distinctChain.push(messages[0]); - chainList.slice(i, 1); - } - let result = distinctChain.filter((notNull) => notNull !== undefined) - return result; - } - return; -} +// distinctChain.push(messages[0]); +// chainList.slice(i, 1); +// } +// let result = distinctChain.filter((notNull) => notNull !== undefined) +// return result; +// } +// return; +// } diff --git a/src/http.ts b/src/http.ts deleted file mode 100644 index 1cd5af8..0000000 --- a/src/http.ts +++ /dev/null @@ -1,10 +0,0 @@ -export function toText(data: any, status = 200) { - const headers = { 'Content-Type': 'text/plain; charset=utf-8' }; - return new Response(data, { status, headers }); -} - -export function toJSON(data: any, status = 200) { - const body = JSON.stringify(data, null, 2); - const headers = { 'Content-Type': 'application/json' }; - return new Response(body, { status, headers }); -} \ No newline at end of file diff --git a/src/sqlite.ts b/src/sqlite.ts index 1c85494..beadcb8 100644 --- a/src/sqlite.ts +++ b/src/sqlite.ts @@ -52,7 +52,6 @@ export function replaceRecent(db: Database, table: string, key: string, value: s return db.prepare(`REPLACE INTO ${table} (key, value, payload) VALUES (?, ?, ?)`).run(key, value, payload); } - export function selectAll(db: Database, table: string) { return db.query(`SELECT * FROM ${table}`).all() as KV[]; }