-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory_manager.cpp
More file actions
647 lines (552 loc) · 20 KB
/
history_manager.cpp
File metadata and controls
647 lines (552 loc) · 20 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#include "history_manager.h"
#include <sqlite3.h>
#include <filesystem>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <chrono>
#include <iomanip>
#include <ctime>
#include <cmath>
namespace fs = std::filesystem;
static const int MAX_ENTRIES = 10000;
static const int AUTO_PRUNE_DAYS = 90;
// Safe column text extraction — handles NULL without crashing
static std::string col_text(sqlite3_stmt* stmt, int col) {
const unsigned char* p = sqlite3_column_text(stmt, col);
return p ? reinterpret_cast<const char*>(p) : "";
}
// ---------------------------------------------------------------------------
// Construction / destruction
// ---------------------------------------------------------------------------
HistoryManager::HistoryManager() = default;
HistoryManager::~HistoryManager() {
close();
}
// ---------------------------------------------------------------------------
// open / close
// ---------------------------------------------------------------------------
bool HistoryManager::open(const std::string& db_path) {
if (db_ != nullptr) {
close();
}
if (db_path.empty()) {
const char* home = std::getenv("HOME");
if (!home) {
std::cerr << "[history] HOME not set\n";
return false;
}
db_path_ = std::string(home) + "/.recognize/history.db";
} else {
db_path_ = db_path;
}
// Create parent directories
fs::path parent = fs::path(db_path_).parent_path();
if (!parent.empty()) {
std::error_code ec;
fs::create_directories(parent, ec);
if (ec) {
std::cerr << "[history] cannot create directory " << parent
<< ": " << ec.message() << "\n";
return false;
}
}
int rc = sqlite3_open(db_path_.c_str(), &db_);
if (rc != SQLITE_OK) {
std::cerr << "[history] cannot open database: "
<< sqlite3_errmsg(db_) << "\n";
sqlite3_close_v2(db_);
db_ = nullptr;
return false;
}
// WAL mode + NORMAL sync for performance
sqlite3_exec(db_, "PRAGMA journal_mode=WAL;", nullptr, nullptr, nullptr);
sqlite3_exec(db_, "PRAGMA synchronous=NORMAL;", nullptr, nullptr, nullptr);
if (!ensure_schema()) {
close();
return false;
}
return true;
}
void HistoryManager::close() {
if (db_ != nullptr) {
sqlite3_close_v2(db_);
db_ = nullptr;
}
}
// ---------------------------------------------------------------------------
// Schema
// ---------------------------------------------------------------------------
bool HistoryManager::ensure_schema() {
// Core table — must succeed
const char* core_sql = R"(
CREATE TABLE IF NOT EXISTS transcripts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
duration_s REAL,
model TEXT,
mode TEXT,
word_count INTEGER,
text TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_transcripts_ts ON transcripts(timestamp DESC);
)";
char* err = nullptr;
int rc = sqlite3_exec(db_, core_sql, nullptr, nullptr, &err);
if (rc != SQLITE_OK) {
std::cerr << "[history] schema error: " << (err ? err : "unknown") << "\n";
sqlite3_free(err);
return false;
}
// FTS5 — optional, gracefully degrade to LIKE search if unavailable
const char* fts_sql = R"(
CREATE VIRTUAL TABLE IF NOT EXISTS transcripts_fts USING fts5(
text, content='transcripts', content_rowid='id', tokenize='porter unicode61'
);
CREATE TRIGGER IF NOT EXISTS tr_ai AFTER INSERT ON transcripts BEGIN
INSERT INTO transcripts_fts(rowid, text) VALUES (new.id, new.text);
END;
CREATE TRIGGER IF NOT EXISTS tr_ad AFTER DELETE ON transcripts BEGIN
INSERT INTO transcripts_fts(transcripts_fts, rowid, text) VALUES('delete', old.id, old.text);
END;
)";
err = nullptr;
rc = sqlite3_exec(db_, fts_sql, nullptr, nullptr, &err);
if (rc == SQLITE_OK) {
has_fts_ = true;
} else {
std::cerr << "[history] FTS5 unavailable, using basic search: "
<< (err ? err : "unknown") << "\n";
sqlite3_free(err);
has_fts_ = false;
}
return true;
}
// ---------------------------------------------------------------------------
// save
// ---------------------------------------------------------------------------
int64_t HistoryManager::save(const std::string& text, double duration_s,
const std::string& model, const std::string& mode) {
if (db_ == nullptr || text.empty()) {
return -1;
}
const char* sql =
"INSERT INTO transcripts (duration_s, model, mode, word_count, text) "
"VALUES (?, ?, ?, ?, ?);";
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
std::cerr << "[history] save prepare error: " << sqlite3_errmsg(db_) << "\n";
return -1;
}
int wc = count_words(text);
sqlite3_bind_double(stmt, 1, duration_s);
sqlite3_bind_text(stmt, 2, model.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, mode.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 4, wc);
sqlite3_bind_text(stmt, 5, text.c_str(), -1, SQLITE_TRANSIENT);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE) {
std::cerr << "[history] save error: " << sqlite3_errmsg(db_) << "\n";
return -1;
}
int64_t row_id = sqlite3_last_insert_rowid(db_);
auto_prune();
return row_id;
}
// ---------------------------------------------------------------------------
// auto_prune
// ---------------------------------------------------------------------------
void HistoryManager::auto_prune() {
if (db_ == nullptr) {
return;
}
// Delete entries older than AUTO_PRUNE_DAYS (parameterized)
const char* age_sql =
"DELETE FROM transcripts WHERE timestamp < "
"strftime('%Y-%m-%dT%H:%M:%S', 'now', 'localtime', ? || ' days');";
sqlite3_stmt* age_stmt = nullptr;
if (sqlite3_prepare_v2(db_, age_sql, -1, &age_stmt, nullptr) == SQLITE_OK) {
std::string modifier = "-" + std::to_string(AUTO_PRUNE_DAYS);
sqlite3_bind_text(age_stmt, 1, modifier.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_step(age_stmt);
sqlite3_finalize(age_stmt);
}
// Enforce count limit
const char* count_sql =
"DELETE FROM transcripts WHERE id NOT IN "
"(SELECT id FROM transcripts ORDER BY timestamp DESC LIMIT ?);";
sqlite3_stmt* stmt = nullptr;
if (sqlite3_prepare_v2(db_, count_sql, -1, &stmt, nullptr) == SQLITE_OK) {
sqlite3_bind_int(stmt, 1, MAX_ENTRIES);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
}
// ---------------------------------------------------------------------------
// list
// ---------------------------------------------------------------------------
std::vector<HistoryManager::Entry> HistoryManager::list(int limit, int offset) {
std::vector<Entry> results;
if (db_ == nullptr) {
return results;
}
const char* sql =
"SELECT id, timestamp, duration_s, model, mode, word_count, text "
"FROM transcripts ORDER BY timestamp DESC LIMIT ? OFFSET ?;";
sqlite3_stmt* stmt = nullptr;
if (sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr) != SQLITE_OK) {
std::cerr << "[history] list error: " << sqlite3_errmsg(db_) << "\n";
return results;
}
sqlite3_bind_int(stmt, 1, limit);
sqlite3_bind_int(stmt, 2, offset);
while (sqlite3_step(stmt) == SQLITE_ROW) {
Entry e;
e.id = sqlite3_column_int64(stmt, 0);
e.timestamp = col_text(stmt, 1);
e.duration_s = sqlite3_column_double(stmt, 2);
e.model = col_text(stmt, 3);
e.mode = col_text(stmt, 4);
e.word_count = sqlite3_column_int(stmt, 5);
e.text = col_text(stmt, 6);
results.push_back(std::move(e));
}
sqlite3_finalize(stmt);
return results;
}
// ---------------------------------------------------------------------------
// search
// ---------------------------------------------------------------------------
std::vector<HistoryManager::Entry> HistoryManager::search(
const std::string& query, int limit, const std::string& since) {
std::vector<Entry> results;
if (db_ == nullptr || query.empty()) {
return results;
}
std::string sql;
std::string like_query = "%" + query + "%";
if (has_fts_) {
if (since.empty()) {
sql = "SELECT t.id, t.timestamp, t.duration_s, t.model, t.mode, "
"t.word_count, t.text "
"FROM transcripts_fts f "
"JOIN transcripts t ON t.id = f.rowid "
"WHERE transcripts_fts MATCH ? "
"ORDER BY bm25(transcripts_fts) "
"LIMIT ?;";
} else {
sql = "SELECT t.id, t.timestamp, t.duration_s, t.model, t.mode, "
"t.word_count, t.text "
"FROM transcripts_fts f "
"JOIN transcripts t ON t.id = f.rowid "
"WHERE transcripts_fts MATCH ? AND t.timestamp >= ? "
"ORDER BY bm25(transcripts_fts) "
"LIMIT ?;";
}
} else {
// Fallback: LIKE-based search when FTS5 is unavailable
if (since.empty()) {
sql = "SELECT id, timestamp, duration_s, model, mode, "
"word_count, text "
"FROM transcripts "
"WHERE text LIKE ? "
"ORDER BY timestamp DESC "
"LIMIT ?;";
} else {
sql = "SELECT id, timestamp, duration_s, model, mode, "
"word_count, text "
"FROM transcripts "
"WHERE text LIKE ? AND timestamp >= ? "
"ORDER BY timestamp DESC "
"LIMIT ?;";
}
}
sqlite3_stmt* stmt = nullptr;
if (sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
std::cerr << "[history] search error: " << sqlite3_errmsg(db_) << "\n";
return results;
}
const std::string& bind_val = has_fts_ ? query : like_query;
sqlite3_bind_text(stmt, 1, bind_val.c_str(), -1, SQLITE_TRANSIENT);
if (since.empty()) {
sqlite3_bind_int(stmt, 2, limit);
} else {
sqlite3_bind_text(stmt, 2, since.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 3, limit);
}
while (sqlite3_step(stmt) == SQLITE_ROW) {
Entry e;
e.id = sqlite3_column_int64(stmt, 0);
e.timestamp = col_text(stmt, 1);
e.duration_s = sqlite3_column_double(stmt, 2);
e.model = col_text(stmt, 3);
e.mode = col_text(stmt, 4);
e.word_count = sqlite3_column_int(stmt, 5);
e.text = col_text(stmt, 6);
results.push_back(std::move(e));
}
sqlite3_finalize(stmt);
return results;
}
// ---------------------------------------------------------------------------
// get
// ---------------------------------------------------------------------------
std::optional<HistoryManager::Entry> HistoryManager::get(int64_t id) {
if (db_ == nullptr) {
return std::nullopt;
}
const char* sql =
"SELECT id, timestamp, duration_s, model, mode, word_count, text "
"FROM transcripts WHERE id = ?;";
sqlite3_stmt* stmt = nullptr;
if (sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr) != SQLITE_OK) {
std::cerr << "[history] get error: " << sqlite3_errmsg(db_) << "\n";
return std::nullopt;
}
sqlite3_bind_int64(stmt, 1, id);
std::optional<Entry> result;
if (sqlite3_step(stmt) == SQLITE_ROW) {
Entry e;
e.id = sqlite3_column_int64(stmt, 0);
e.timestamp = col_text(stmt, 1);
e.duration_s = sqlite3_column_double(stmt, 2);
e.model = col_text(stmt, 3);
e.mode = col_text(stmt, 4);
e.word_count = sqlite3_column_int(stmt, 5);
e.text = col_text(stmt, 6);
result = std::move(e);
}
sqlite3_finalize(stmt);
return result;
}
// ---------------------------------------------------------------------------
// clear_older_than
// ---------------------------------------------------------------------------
int HistoryManager::clear_older_than(int days) {
if (db_ == nullptr || days <= 0) {
return 0;
}
const char* sql =
"DELETE FROM transcripts WHERE timestamp < "
"strftime('%Y-%m-%dT%H:%M:%S', 'now', 'localtime', ? || ' days');";
sqlite3_stmt* stmt = nullptr;
if (sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr) != SQLITE_OK) {
return 0;
}
std::string modifier = "-" + std::to_string(days);
sqlite3_bind_text(stmt, 1, modifier.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_step(stmt);
int changed = sqlite3_changes(db_);
sqlite3_finalize(stmt);
return changed;
}
// ---------------------------------------------------------------------------
// clear_all
// ---------------------------------------------------------------------------
int HistoryManager::clear_all() {
if (db_ == nullptr) {
return 0;
}
sqlite3_exec(db_, "DELETE FROM transcripts;", nullptr, nullptr, nullptr);
int deleted = sqlite3_changes(db_);
if (has_fts_) {
sqlite3_exec(db_,
"INSERT INTO transcripts_fts(transcripts_fts) VALUES('rebuild');",
nullptr, nullptr, nullptr);
}
return deleted;
}
// ---------------------------------------------------------------------------
// count
// ---------------------------------------------------------------------------
int HistoryManager::count() {
if (db_ == nullptr) {
return 0;
}
const char* sql = "SELECT COUNT(*) FROM transcripts;";
sqlite3_stmt* stmt = nullptr;
if (sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr) != SQLITE_OK) {
return 0;
}
int result = 0;
if (sqlite3_step(stmt) == SQLITE_ROW) {
result = sqlite3_column_int(stmt, 0);
}
sqlite3_finalize(stmt);
return result;
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
int HistoryManager::count_words(const std::string& text) {
int wc = 0;
bool in_word = false;
for (char c : text) {
if (std::isspace(static_cast<unsigned char>(c))) {
in_word = false;
} else if (!in_word) {
in_word = true;
++wc;
}
}
return wc;
}
std::string HistoryManager::relative_time(const std::string& iso_timestamp) {
// Parse ISO timestamp: YYYY-MM-DDTHH:MM:SS
std::tm tm = {};
std::istringstream ss(iso_timestamp);
ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
if (ss.fail()) {
return iso_timestamp;
}
std::time_t entry_time = std::mktime(&tm);
std::time_t now = std::time(nullptr);
double diff = std::difftime(now, entry_time);
if (diff < 0) {
return "just now";
}
int seconds = static_cast<int>(diff);
if (seconds < 60) {
return "just now";
}
int minutes = seconds / 60;
if (minutes < 60) {
return std::to_string(minutes) + " min ago";
}
int hours = minutes / 60;
if (hours < 24) {
return std::to_string(hours) + "h ago";
}
int days = hours / 24;
if (days < 30) {
return std::to_string(days) + "d ago";
}
int months = days / 30;
if (months < 12) {
return std::to_string(months) + "mo ago";
}
int years = months / 12;
return std::to_string(years) + "y ago";
}
std::string HistoryManager::truncate_text(const std::string& text, size_t max_len) {
std::string result;
result.reserve(std::min(text.size(), max_len + 3));
for (size_t i = 0; i < text.size() && result.size() < max_len; ++i) {
char c = text[i];
if (c == '\n' || c == '\r') {
result += ' ';
} else {
result += c;
}
}
if (text.size() > max_len) {
// Trim trailing space before appending ellipsis
while (!result.empty() && result.back() == ' ') {
result.pop_back();
}
result += "...";
}
return result;
}
std::string HistoryManager::escape_json(const std::string& str) {
std::string out;
out.reserve(str.size() + 16);
for (char c : str) {
switch (c) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (static_cast<unsigned char>(c) < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x",
static_cast<unsigned int>(static_cast<unsigned char>(c)));
out += buf;
} else {
out += c;
}
break;
}
}
return out;
}
// ---------------------------------------------------------------------------
// format_table
// ---------------------------------------------------------------------------
std::string HistoryManager::format_table(const std::vector<Entry>& entries) {
if (entries.empty()) {
return "No transcription history found.\n";
}
// Column widths
const int col_id = 5;
const int col_when = 12;
const int col_mode = 10;
const int col_model = 18;
const int col_words = 6;
const int col_preview = 60;
std::ostringstream out;
// Header
out << std::left
<< std::setw(col_id) << "ID"
<< std::setw(col_when) << "When"
<< std::setw(col_mode) << "Mode"
<< std::setw(col_model) << "Model"
<< std::setw(col_words) << "Words"
<< "Preview"
<< "\n";
// Separator
out << std::string(col_id, '-')
<< std::string(col_when, '-')
<< std::string(col_mode, '-')
<< std::string(col_model, '-')
<< std::string(col_words, '-')
<< std::string(col_preview, '-')
<< "\n";
// Rows
for (const auto& e : entries) {
std::string when = truncate_text(relative_time(e.timestamp), col_when - 1);
std::string preview = truncate_text(e.text, col_preview);
std::string model_short = truncate_text(e.model, col_model - 2);
out << std::left
<< std::setw(col_id) << e.id
<< std::setw(col_when) << when
<< std::setw(col_mode) << (e.mode.empty() ? "-" : e.mode)
<< std::setw(col_model) << (model_short.empty() ? "-" : model_short)
<< std::setw(col_words) << e.word_count
<< preview
<< "\n";
}
return out.str();
}
// ---------------------------------------------------------------------------
// format_json / format_entry_json
// ---------------------------------------------------------------------------
std::string HistoryManager::format_entry_json(const Entry& entry) {
std::ostringstream out;
out << "{"
<< "\"id\":" << entry.id << ","
<< "\"timestamp\":\"" << escape_json(entry.timestamp) << "\","
<< "\"duration_s\":" << std::fixed << std::setprecision(1) << entry.duration_s << ","
<< "\"model\":\"" << escape_json(entry.model) << "\","
<< "\"mode\":\"" << escape_json(entry.mode) << "\","
<< "\"word_count\":" << entry.word_count << ","
<< "\"text\":\"" << escape_json(entry.text) << "\""
<< "}";
return out.str();
}
std::string HistoryManager::format_json(const std::vector<Entry>& entries) {
std::ostringstream out;
out << "[";
for (size_t i = 0; i < entries.size(); ++i) {
if (i > 0) {
out << ",";
}
out << format_entry_json(entries[i]);
}
out << "]";
return out.str();
}