Skip to content

Commit

Permalink
fixed module hash issue
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelpapineau committed Dec 6, 2023
1 parent e0d4046 commit 14b21b6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/fetch/POST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default async function (req: Request, server: Server) {
sqlite.replace(db, "traceId", `${chain}:${traceId}`, timestamp);

// Set timestamp as key to filter recent messages
insertMessages( db, traceId, text, chain );
insertMessages( db, moduleHash, text, chain );

return toText("OK");
}
Expand Down
13 changes: 7 additions & 6 deletions src/fetch/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,35 @@ export function handleMessages(req: Request) {
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 );
return toJSON(selectMessages(db, limit, sort, chain, moduleHash));
//console.log(messages)
}

export function insertMessages(db: Database, traceId: string, text: string, chain?: string) {
export function insertMessages(db: Database, moduleHash: string, text: 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];

// update messages
sqlite.replace(db, "messages", String(Date.now()), `${traceId}`, "payload", text);
sqlite.replace(db, "messages", String(Date.now()), `${moduleHash}`, "payload", text);
sqlite.deleteRow(db, "messages", oldest.key);

// update messagesByChain
if (chain) {
oldest = sqlite.selectAll(db, "messagesByChain").sort((a: any, b: any) => a.timestamp - b.timestamp)[0];
sqlite.replace(db, "messagesByChain", String(Date.now()), `${chain}:${traceId}`, "payload", text );
sqlite.replace(db, "messagesByChain", String(Date.now()), `${chain}:${moduleHash}`, "payload", text );
sqlite.deleteRow(db, "messagesByChain", `${oldest.key}`);
}
return;
}
// add messages if tables not full
sqlite.replace(db, "messages", String(Date.now()), `${traceId}`, "payload", text);
sqlite.replace(db, "messages", String(Date.now()), `${moduleHash}`, "payload", text);

if (chain) sqlite.replace(db, "messagesByChain", String(Date.now()), `${chain}:${traceId}`, "payload", text );
if (chain) sqlite.replace(db, "messagesByChain", String(Date.now()), `${chain}:${moduleHash}`, "payload", text );
}

export function selectMessages(db: Database, limit: number, sortBy: string, chain?: string, moduleHash?: string,) {
Expand All @@ -64,7 +63,9 @@ export function selectMessages(db: Database, limit: number, sortBy: string, chai

// if (distinct) messages = selectDistinct(distinct, messages, db, chain, sortBy, limit);
if (chain) messages = sqlite.selectAll(db, "messagesByChain", "*", sortBy, limit).filter((message: any) => message.value.includes(chain));
console.log(messages)
if (moduleHash) messages = messages.filter((message: any) => message.value.includes(moduleHash));
console.log(messages)
return messages
}

Expand Down

0 comments on commit 14b21b6

Please sign in to comment.