-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
121 lines (107 loc) · 3.87 KB
/
Copy pathdatabase.js
File metadata and controls
121 lines (107 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import Database from 'better-sqlite3';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { mkdirSync, existsSync } from 'fs';
const __dirname = dirname(fileURLToPath(import.meta.url));
const dataDir = join(__dirname, 'data');
// Ensure data directory exists
if (!existsSync(dataDir)) {
mkdirSync(dataDir, { recursive: true });
}
const dbPath = join(dataDir, 'tldr.db');
let db = null;
export function initDatabase() {
db = new Database(dbPath);
db.pragma('journal_mode = WAL');
// Server configuration
db.exec(`
CREATE TABLE IF NOT EXISTS server_config (
guild_id TEXT PRIMARY KEY,
guild_name TEXT,
tier TEXT DEFAULT 'free',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
settings_json TEXT
)
`);
// Message cache for summarization
db.exec(`
CREATE TABLE IF NOT EXISTS message_cache (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guild_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
message_id TEXT UNIQUE NOT NULL,
author_id TEXT NOT NULL,
author_name TEXT NOT NULL,
content TEXT,
created_at DATETIME NOT NULL,
cached_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
db.exec(`CREATE INDEX IF NOT EXISTS idx_cache_guild_channel ON message_cache(guild_id, channel_id)`);
db.exec(`CREATE INDEX IF NOT EXISTS idx_cache_created ON message_cache(created_at)`);
// Usage tracking
db.exec(`
CREATE TABLE IF NOT EXISTS usage_tracking (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guild_id TEXT NOT NULL,
user_id TEXT NOT NULL,
command TEXT NOT NULL,
tokens_used INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
db.exec(`CREATE INDEX IF NOT EXISTS idx_usage_guild_date ON usage_tracking(guild_id, created_at)`);
// Digest subscriptions
db.exec(`
CREATE TABLE IF NOT EXISTS digest_subscriptions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guild_id TEXT NOT NULL,
user_id TEXT NOT NULL,
channel_ids TEXT, -- JSON array of channel IDs
frequency TEXT DEFAULT 'daily', -- 'daily', 'weekly'
time TEXT DEFAULT '09:00', -- HH:MM format
timezone TEXT DEFAULT 'UTC',
enabled INTEGER DEFAULT 1,
last_sent DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE(guild_id, user_id)
)
`);
// Summary history (for caching/avoiding re-summarizing)
db.exec(`
CREATE TABLE IF NOT EXISTS summary_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guild_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
start_time DATETIME NOT NULL,
end_time DATETIME NOT NULL,
message_count INTEGER,
summary TEXT,
highlights_json TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
db.exec(`CREATE INDEX IF NOT EXISTS idx_summary_lookup ON summary_history(guild_id, channel_id, start_time, end_time)`);
console.log('Database initialized at:', dbPath);
return db;
}
export function getDb() {
if (!db) {
throw new Error('Database not initialized. Call initDatabase() first.');
}
return db;
}
export function closeDatabase() {
if (db) {
db.close();
console.log('Database connection closed.');
}
}
// Cleanup old cached messages
export function cleanupOldCache(daysOld = 7) {
const db = getDb();
const cutoff = new Date(Date.now() - daysOld * 86400000).toISOString();
const result = db.prepare('DELETE FROM message_cache WHERE cached_at < ?').run(cutoff);
console.log(`Cleaned up ${result.changes} old cached messages`);
return result.changes;
}