@@ -80,6 +80,47 @@ class Database
|
||||
CREATE INDEX IF NOT EXISTS idx_alerts_created ON alerts(created_at)
|
||||
");
|
||||
|
||||
$this->pdo->exec("
|
||||
CREATE INDEX IF NOT EXISTS idx_alerts_severity ON alerts(severity)
|
||||
");
|
||||
|
||||
$this->pdo->exec("
|
||||
CREATE VIRTUAL TABLE IF NOT EXISTS alerts_fts USING fts5(
|
||||
message, raw_line, rule_name, source_name,
|
||||
content='alerts',
|
||||
content_rowid='id',
|
||||
tokenize='porter unicode61'
|
||||
)
|
||||
");
|
||||
|
||||
$this->pdo->exec("
|
||||
CREATE TRIGGER IF NOT EXISTS alerts_ai AFTER INSERT ON alerts BEGIN
|
||||
INSERT INTO alerts_fts(rowid, message, raw_line, rule_name, source_name)
|
||||
VALUES (new.id, new.message, new.raw_line, new.rule_name, new.source_name);
|
||||
END;
|
||||
");
|
||||
|
||||
$this->pdo->exec("
|
||||
CREATE TRIGGER IF NOT EXISTS alerts_ad AFTER DELETE ON alerts BEGIN
|
||||
INSERT INTO alerts_fts(alerts_fts, rowid, message, raw_line, rule_name, source_name)
|
||||
VALUES ('delete', old.id, old.message, old.raw_line, old.rule_name, old.source_name);
|
||||
END;
|
||||
");
|
||||
|
||||
$this->pdo->exec("
|
||||
CREATE TRIGGER IF NOT EXISTS alerts_au AFTER UPDATE ON alerts BEGIN
|
||||
INSERT INTO alerts_fts(alerts_fts, rowid, message, raw_line, rule_name, source_name)
|
||||
VALUES ('delete', old.id, old.message, old.raw_line, old.rule_name, old.source_name);
|
||||
INSERT INTO alerts_fts(rowid, message, raw_line, rule_name, source_name)
|
||||
VALUES (new.id, new.message, new.raw_line, new.rule_name, new.source_name);
|
||||
END;
|
||||
");
|
||||
|
||||
$this->pdo->exec("
|
||||
INSERT OR IGNORE INTO alerts_fts(rowid, message, raw_line, rule_name, source_name)
|
||||
SELECT id, message, raw_line, rule_name, source_name FROM alerts
|
||||
");
|
||||
|
||||
$this->pdo->exec("
|
||||
CREATE TABLE IF NOT EXISTS rate_limiter (
|
||||
rule_id INTEGER NOT NULL,
|
||||
|
||||
@@ -145,6 +145,20 @@ class Repository
|
||||
)->fetchAll();
|
||||
}
|
||||
|
||||
public function searchAlerts(string $query, int $limit = 100): array
|
||||
{
|
||||
$stmt = $this->db->pdo()->prepare(
|
||||
"SELECT a.* FROM alerts a
|
||||
JOIN alerts_fts fts ON a.id = fts.rowid
|
||||
WHERE alerts_fts MATCH ?
|
||||
ORDER BY rank
|
||||
LIMIT ?"
|
||||
);
|
||||
$stmt->execute([$query, $limit]);
|
||||
$rows = $stmt->fetchAll();
|
||||
return array_map(fn(array $r) => Alert::fromRow($r), $rows);
|
||||
}
|
||||
|
||||
// --- Config ---
|
||||
|
||||
public function getAllowedUserTokens(): array
|
||||
|
||||
Reference in New Issue
Block a user