@@ -5,6 +5,15 @@ import * as path from "path";
55
66const is_pg = env . metadata_backend === "postgres" ;
77
8+ const get_mem_table = ( ) => {
9+ if ( is_pg ) {
10+ const sc = process . env . OM_PG_SCHEMA || "public" ;
11+ const tbl = process . env . OM_PG_TABLE || "openmemory_memories" ;
12+ return `"${ sc } "."${ tbl } "` ;
13+ }
14+ return "memories" ;
15+ } ;
16+
817let reqz = {
918 win_start : Date . now ( ) ,
1019 win_cnt : 0 ,
@@ -13,11 +22,11 @@ let reqz = {
1322
1423const log_metric = async ( type : string , value : number ) => {
1524 try {
16- await run_async ( "insert into stats(type,count,ts) values(?,?,?)" , [
17- type ,
18- value ,
19- Date . now ( ) ,
20- ] ) ;
25+ const sc = process . env . OM_PG_SCHEMA || "public" ;
26+ const sql = is_pg
27+ ? `insert into " ${ sc } "."stats"(type,count,ts) values($1,$2,$3)`
28+ : "insert into stats(type,count,ts) values(?,?,?)" ;
29+ await run_async ( sql , [ type , value , Date . now ( ) ] ) ;
2130 } catch ( e ) {
2231 console . error ( "[metrics] log err:" , e ) ;
2332 }
@@ -84,40 +93,48 @@ const get_db_sz = async (): Promise<number> => {
8493export function dash ( app : any ) {
8594 app . get ( "/dashboard/stats" , async ( _req : any , res : any ) => {
8695 try {
96+ const mem_table = get_mem_table ( ) ;
8797 const totmem = await all_async (
88- " SELECT COUNT(*) as count FROM memories" ,
98+ ` SELECT COUNT(*) as count FROM ${ mem_table } ` ,
8999 ) ;
90100 const sectcnt = await all_async ( `
91- SELECT primary_sector, COUNT(*) as count
92- FROM memories
101+ SELECT primary_sector, COUNT(*) as count
102+ FROM ${ mem_table }
93103 GROUP BY primary_sector
94104 ` ) ;
95105 const dayago = Date . now ( ) - 24 * 60 * 60 * 1000 ;
96106 const recmem = await all_async (
97- "SELECT COUNT(*) as count FROM memories WHERE created_at > ?" ,
107+ is_pg
108+ ? `SELECT COUNT(*) as count FROM ${ mem_table } WHERE created_at > $1`
109+ : `SELECT COUNT(*) as count FROM ${ mem_table } WHERE created_at > ?` ,
98110 [ dayago ] ,
99111 ) ;
100112 const avgsal = await all_async (
101- " SELECT AVG(salience) as avg FROM memories" ,
113+ ` SELECT AVG(salience) as avg FROM ${ mem_table } ` ,
102114 ) ;
103115 const decst = await all_async ( `
104- SELECT
116+ SELECT
105117 COUNT(*) as total,
106118 AVG(decay_lambda) as avg_lambda,
107119 MIN(salience) as min_salience,
108120 MAX(salience) as max_salience
109- FROM memories
121+ FROM ${ mem_table }
110122 ` ) ;
111123 const upt = process . uptime ( ) ;
112124
113125 // Calculate QPS stats from database (last hour)
114126 const hour_ago = Date . now ( ) - 60 * 60 * 1000 ;
127+ const sc = process . env . OM_PG_SCHEMA || "public" ;
115128 const qps_data = await all_async (
116- "SELECT count, ts FROM stats WHERE type=? AND ts > ? ORDER BY ts DESC" ,
129+ is_pg
130+ ? `SELECT count, ts FROM "${ sc } "."stats" WHERE type=$1 AND ts > $2 ORDER BY ts DESC`
131+ : "SELECT count, ts FROM stats WHERE type=? AND ts > ? ORDER BY ts DESC" ,
117132 [ "qps" , hour_ago ] ,
118133 ) ;
119134 const err_data = await all_async (
120- "SELECT COUNT(*) as total FROM stats WHERE type=? AND ts > ?" ,
135+ is_pg
136+ ? `SELECT COUNT(*) as total FROM "${ sc } "."stats" WHERE type=$1 AND ts > $2`
137+ : "SELECT COUNT(*) as total FROM stats WHERE type=? AND ts > ?" ,
121138 [ "error" , hour_ago ] ,
122139 ) ;
123140
@@ -230,12 +247,14 @@ export function dash(app: any) {
230247
231248 app . get ( "/dashboard/activity" , async ( req : any , res : any ) => {
232249 try {
250+ const mem_table = get_mem_table ( ) ;
233251 const lim = parseInt ( req . query . limit || "50" ) ;
234252 const recmem = await all_async (
235- `
236- SELECT id, content, primary_sector, salience, created_at, updated_at, last_seen_at
237- FROM memories ORDER BY updated_at DESC LIMIT ?
238- ` ,
253+ is_pg
254+ ? `SELECT id, content, primary_sector, salience, created_at, updated_at, last_seen_at
255+ FROM ${ mem_table } ORDER BY updated_at DESC LIMIT $1`
256+ : `SELECT id, content, primary_sector, salience, created_at, updated_at, last_seen_at
257+ FROM ${ mem_table } ORDER BY updated_at DESC LIMIT ?` ,
239258 [ lim ] ,
240259 ) ;
241260 res . json ( {
@@ -255,13 +274,15 @@ export function dash(app: any) {
255274
256275 app . get ( "/dashboard/sectors/timeline" , async ( req : any , res : any ) => {
257276 try {
277+ const mem_table = get_mem_table ( ) ;
258278 const hrs = parseInt ( req . query . hours || "24" ) ;
259279 const strt = Date . now ( ) - hrs * 60 * 60 * 1000 ;
260280 const tl = await all_async (
261- `
262- SELECT primary_sector, strftime('%H:00', datetime(created_at/1000, 'unixepoch')) as hour, COUNT(*) as count
263- FROM memories WHERE created_at > ? GROUP BY primary_sector, hour ORDER BY hour
264- ` ,
281+ is_pg
282+ ? `SELECT primary_sector, to_char(to_timestamp(created_at/1000), 'HH24:00') as hour, COUNT(*) as count
283+ FROM ${ mem_table } WHERE created_at > $1 GROUP BY primary_sector, hour ORDER BY hour`
284+ : `SELECT primary_sector, strftime('%H:00', datetime(created_at/1000, 'unixepoch')) as hour, COUNT(*) as count
285+ FROM ${ mem_table } WHERE created_at > ? GROUP BY primary_sector, hour ORDER BY hour` ,
265286 [ strt ] ,
266287 ) ;
267288 res . json ( { timeline : tl } ) ;
@@ -272,12 +293,14 @@ export function dash(app: any) {
272293
273294 app . get ( "/dashboard/top-memories" , async ( req : any , res : any ) => {
274295 try {
296+ const mem_table = get_mem_table ( ) ;
275297 const lim = parseInt ( req . query . limit || "10" ) ;
276298 const topm = await all_async (
277- `
278- SELECT id, content, primary_sector, salience, last_seen_at
279- FROM memories ORDER BY salience DESC LIMIT ?
280- ` ,
299+ is_pg
300+ ? `SELECT id, content, primary_sector, salience, last_seen_at
301+ FROM ${ mem_table } ORDER BY salience DESC LIMIT $1`
302+ : `SELECT id, content, primary_sector, salience, last_seen_at
303+ FROM ${ mem_table } ORDER BY salience DESC LIMIT ?` ,
281304 [ lim ] ,
282305 ) ;
283306 res . json ( {
@@ -298,25 +321,21 @@ export function dash(app: any) {
298321 try {
299322 const hrs = parseInt ( req . query . hours || "24" ) ;
300323 const strt = Date . now ( ) - hrs * 60 * 60 * 1000 ;
324+ const sc = process . env . OM_PG_SCHEMA || "public" ;
301325
302326 const ops = await all_async (
303- `
304- SELECT
305- type,
306- strftime('%H:00', datetime(ts/1000, 'unixepoch', 'localtime')) as hour,
307- SUM(count) as cnt
308- FROM stats
309- WHERE ts > ?
310- GROUP BY type, hour
311- ORDER BY hour
312- ` ,
327+ is_pg
328+ ? `SELECT type, to_char(to_timestamp(ts/1000), 'HH24:00') as hour, SUM(count) as cnt
329+ FROM "${ sc } "."stats" WHERE ts > $1 GROUP BY type, hour ORDER BY hour`
330+ : `SELECT type, strftime('%H:00', datetime(ts/1000, 'unixepoch', 'localtime')) as hour, SUM(count) as cnt
331+ FROM stats WHERE ts > ? GROUP BY type, hour ORDER BY hour` ,
313332 [ strt ] ,
314333 ) ;
315334
316335 const totals = await all_async (
317- `
318- SELECT type, SUM(count) as total FROM stats WHERE ts > ? GROUP BY type
319- ` ,
336+ is_pg
337+ ? ` SELECT type, SUM(count) as total FROM " ${ sc } "." stats" WHERE ts > $1 GROUP BY type`
338+ : `SELECT type, SUM(count) as total FROM stats WHERE ts > ? GROUP BY type `,
320339 [ strt ] ,
321340 ) ;
322341
0 commit comments