Skip to content

Commit 0b89d1b

Browse files
authored
Merge pull request #70 from ajitam/main
Fix PostgreSQL compatibility and user tracking consistency
2 parents 46e3d56 + 9dd2651 commit 0b89d1b

File tree

4 files changed

+86
-60
lines changed

4 files changed

+86
-60
lines changed

backend/bin/opm.js

100644100755
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ examples:
5959

6060
const hdrs = {
6161
'content-type': 'application/json',
62-
...(key && { authorization: `bearer ${key}` }),
62+
...(key && { authorization: `Bearer ${key}` }),
6363
};
6464

6565
const req = async (pth, opts = {}) => {
@@ -120,14 +120,15 @@ const listmem = async (opts) => {
120120
const off = 0;
121121
let r;
122122
if (opts.usr) {
123-
r = await req(`/memory/user/${opts.usr}?limit=${lim}&offset=${off}`);
123+
r = await req(`/users/${opts.usr}/memories?l=${lim}&u=${off}`);
124124
} else {
125125
r = await req(`/memory/all?limit=${lim}&offset=${off}`);
126126
}
127-
console.log(`[memories] showing ${r.memories.length}\n`);
128-
r.memories.forEach((m, i) => {
127+
const items = r.items || r.memories || [];
128+
console.log(`[memories] showing ${items.length}\n`);
129+
items.forEach((m, i) => {
129130
console.log(`${i + 1}. [${m.primary_sector}] ${m.content}`);
130-
console.log(` id: ${m.id} | user: ${m.user_id || 'system'}`);
131+
console.log(` id: ${m.id} | user: ${opts.usr || 'system'}`);
131132
console.log(` sal: ${m.salience.toFixed(3)}`);
132133
if (m.tags) console.log(` tags: ${m.tags}`);
133134
console.log();
@@ -140,15 +141,21 @@ const delmem = async (id) => {
140141
};
141142

142143
const getstats = async () => {
143-
const r = await req('/stats');
144+
const r = await req('/dashboard/stats');
144145
console.log('[stats]\n');
145-
console.log(`total memories: ${r.total_memories}`);
146-
console.log(`total users: ${r.total_users}`);
147-
console.log(`avg salience: ${r.avg_salience?.toFixed(3) || 'n/a'}`);
146+
console.log(`total memories: ${r.totalMemories || 0}`);
147+
console.log(`recent memories (24h): ${r.recentMemories || 0}`);
148+
console.log(`avg salience: ${r.avgSalience || 'n/a'}`);
148149
console.log(`\nmemories by sector:`);
149-
Object.entries(r.memories_by_sector || {}).forEach(([sec, cnt]) => {
150+
Object.entries(r.sectorCounts || {}).forEach(([sec, cnt]) => {
150151
console.log(` ${sec}: ${cnt}`);
151152
});
153+
if (r.decayStats) {
154+
console.log(`\ndecay stats:`);
155+
console.log(` avg lambda: ${r.decayStats.avgLambda}`);
156+
console.log(` min salience: ${r.decayStats.minSalience}`);
157+
console.log(` max salience: ${r.decayStats.maxSalience}`);
158+
}
152159
};
153160

154161
const listusers = async () => {

backend/src/memory/hsg.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -425,15 +425,15 @@ export async function create_cross_sector_waypoints(
425425
await q.ins_waypoint.run(
426426
prim_id,
427427
`${prim_id}:${sec}`,
428-
user_id || null,
428+
user_id || "anonymous",
429429
wt,
430430
now,
431431
now,
432432
);
433433
await q.ins_waypoint.run(
434434
`${prim_id}:${sec}`,
435435
prim_id,
436-
user_id || null,
436+
user_id || "anonymous",
437437
wt,
438438
now,
439439
now,
@@ -490,13 +490,13 @@ export async function create_single_waypoint(
490490
await q.ins_waypoint.run(
491491
new_id,
492492
best.id,
493-
user_id || null,
493+
user_id || "anonymous",
494494
best.similarity,
495495
ts,
496496
ts,
497497
);
498498
} else {
499-
await q.ins_waypoint.run(new_id, new_id, user_id || null, 1.0, ts, ts);
499+
await q.ins_waypoint.run(new_id, new_id, user_id || "anonymous", 1.0, ts, ts);
500500
}
501501
}
502502
export async function create_inter_mem_waypoints(
@@ -517,15 +517,15 @@ export async function create_inter_mem_waypoints(
517517
await q.ins_waypoint.run(
518518
new_id,
519519
vr.id,
520-
user_id || null,
520+
user_id || "anonymous",
521521
wt,
522522
ts,
523523
ts,
524524
);
525525
await q.ins_waypoint.run(
526526
vr.id,
527527
new_id,
528-
user_id || null,
528+
user_id || "anonymous",
529529
wt,
530530
ts,
531531
ts,
@@ -550,7 +550,7 @@ export async function create_contextual_waypoints(
550550
await q.ins_waypoint.run(
551551
mem_id,
552552
rel_id,
553-
user_id || null,
553+
user_id || "anonymous",
554554
base_wt,
555555
now,
556556
now,
@@ -1047,7 +1047,7 @@ export async function add_hsg_memory(
10471047
);
10481048
await q.ins_mem.run(
10491049
id,
1050-
user_id || null,
1050+
user_id || "anonymous",
10511051
cur_seg,
10521052
stored_content,
10531053
simhash,
@@ -1076,7 +1076,7 @@ export async function add_hsg_memory(
10761076
await q.ins_vec.run(
10771077
id,
10781078
result.sector,
1079-
user_id || null,
1079+
user_id || "anonymous",
10801080
vec_buf,
10811081
result.dim,
10821082
);
@@ -1148,7 +1148,7 @@ export async function update_memory(
11481148
await q.ins_vec.run(
11491149
id,
11501150
result.sector,
1151-
mem.user_id || null,
1151+
mem.user_id || "anonymous",
11521152
vec_buf,
11531153
result.dim,
11541154
);

backend/src/ops/ingest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const mkRoot = async (
6464
1.0,
6565
0.1,
6666
1,
67-
user_id || null,
67+
user_id || "anonymous",
6868
null,
6969
);
7070
await transaction.commit();

backend/src/server/routes/dashboard.ts

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import * as path from "path";
55

66
const 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+
817
let reqz = {
918
win_start: Date.now(),
1019
win_cnt: 0,
@@ -13,11 +22,11 @@ let reqz = {
1322

1423
const 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> => {
8493
export 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

Comments
 (0)