-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b023f2c
commit f9da1c4
Showing
6 changed files
with
68 additions
and
108 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,61 @@ | ||
import * as sqlite from "../sqlite.js"; | ||
import { RECENT_MESSAGES_LIMIT } from "../config.js"; | ||
import { toJSON } from "../http.js"; | ||
import Database from "bun:sqlite"; | ||
|
||
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); | ||
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]; | ||
console.log(oldest) | ||
sqlite.replaceRecent(db, "messagesByChain", String(Date.now()), `${chain}:${traceId}`, timestamp ); | ||
sqlite.deleteRow(db, "messagesByChain", `${oldest.key}`); | ||
} | ||
return; | ||
} | ||
// add messages if tables not full | ||
sqlite.replaceRecent(db, "messages", String(Date.now()), `${traceId}`, timestamp); | ||
|
||
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) { | ||
|
||
let messages = sqlite.selectAllRecent(db, "messages", "*", 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") { | ||
|
||
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)); | ||
|
||
distinctChain.push(messages[0]); | ||
chainList.slice(i, 1); | ||
} | ||
let result = distinctChain.filter((notNull) => notNull !== undefined) | ||
return result; | ||
} | ||
return; | ||
} |
This file was deleted.
Oops, something went wrong.