feat: 🎸 Add sqlite support for session recordings (#2974)

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-17419
This commit is contained in:
Zhihe Li committed 2025-09-03 11:41:42 -04:00
1 parent 47c1beadcd
commit cb8b21c10c
4 files changed
+96 -7

No files matched your search

+11 -1
View File
@@ -11,6 +11,10 @@ import { typeOf } from '@ember/utils';
import { hashCode } from '../utils/hash-code';
import { generateSQLExpressions } from '../utils/sqlite-query';
const isISODateString = (str) =>
typeOf(str) === 'string' &&
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z?$/.test(str);
export default class SqliteHandler {
@service sqlite;
@@ -138,7 +142,13 @@ export default class SqliteHandler {
});
console.timeLog(`SQLite fetch ${type}`, 'count');
const results = rows.map((item) => JSON.parse(item.data));
const results = rows.map((item) =>
JSON.parse(item.data, (key, value) =>
// Reviver function to convert any ISO strings back to date objects
// as we had to initially convert them to strings when stored in SQLite
isISODateString(value) ? new Date(value) : value,
),
);
// If we are not pushing to the store, use the raw data with id property
const records = pushToStore
+17
View File
@@ -83,6 +83,22 @@ export const modelMapping = {
scope_id: 'scope.scope_id',
created_time: 'created_time',
},
'session-recording': {
id: 'id',
type: 'type',
state: 'state',
start_time: 'start_time',
end_time: 'end_time',
duration: 'duration',
scope_id: 'scope.scope_id',
user_id: 'create_time_values.user.id',
user_name: 'create_time_values.user.name',
target_id: 'create_time_values.target.id',
target_name: 'create_time_values.target.name',
target_scope_id: 'create_time_values.target.scope.id',
target_scope_name: 'create_time_values.target.scope.name',
created_time: 'created_time',
},
};
// A list of tables that we support searching using FTS5 in SQLite.
@@ -96,6 +112,7 @@ export const searchTables = new Set([
'scope',
'auth-method',
'host-catalog',
'session-recording',
]);
export default class SqliteDbService extends Service {
+62
View File
@@ -337,6 +337,57 @@ CREATE TRIGGER IF NOT EXISTS host_catalog_ad AFTER DELETE ON host_catalog BEGIN
VALUES('delete', old.rowid, old.id, old.type, old.name, old.description, old.plugin_name, old.scope_id, old.created_time);
END;`;
const createSessionRecordingTables = `
CREATE TABLE IF NOT EXISTS session_recording (
id TEXT NOT NULL PRIMARY KEY,
type TEXT NOT NULL,
state TEXT,
start_time TEXT,
end_time TEXT,
duration TEXT,
scope_id TEXT NOT NULL,
user_id TEXT,
user_name TEXT,
target_id TEXT,
target_name TEXT,
target_scope_id TEXT,
target_scope_name TEXT,
created_time TEXT NOT NULL,
data TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_session_recording_created_time ON session_recording(created_time DESC);
CREATE VIRTUAL TABLE IF NOT EXISTS session_recording_fts USING fts5(
id,
type,
state,
start_time,
end_time,
duration,
scope_id,
user_id,
user_name,
target_id,
target_name,
target_scope_id,
target_scope_name,
created_time,
content='',
);
CREATE TRIGGER IF NOT EXISTS session_recording_ai AFTER INSERT ON session_recording BEGIN
INSERT INTO session_recording_fts(
id, type, state, start_time, end_time, duration, scope_id, user_id, user_name, target_id, target_name, target_scope_id, target_scope_name, created_time
) VALUES (
new.id, new.type, new.state, new.start_time, new.end_time, new.duration, new.scope_id, new.user_id, new.user_name, new.target_id, new.target_name, new.target_scope_id, new.target_scope_name, new.created_time
);
END;
CREATE TRIGGER IF NOT EXISTS session_recording_ad AFTER DELETE ON session_recording BEGIN
INSERT INTO session_recording_fts(session_recording_fts, rowid, id, type, state, start_time, end_time, duration, scope_id, user_id, user_name, target_id, target_name, target_scope_id, target_scope_name, created_time)
VALUES('delete', old.rowid, old.id, old.type, old.state, old.start_time, old.end_time, old.duration, old.scope_id, old.user_id, old.user_name, old.target_id, old.target_name, old.target_scope_id, old.target_scope_name, old.created_time);
END;`;
export const CREATE_TABLES = (version) => `
BEGIN;
@@ -356,6 +407,17 @@ ${createCredentialStoreTables}
${createScopeTables}
${createAuthMethodTables}
${createHostCatalogTables}
${createSessionRecordingTables}
CREATE TABLE IF NOT EXISTS "group" (
id TEXT NOT NULL PRIMARY KEY,
name TEXT,
description TEXT,
scope_id TEXT NOT NULL,
created_time TEXT NOT NULL,
data TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_group_scope_id_created_time ON "group"(scope_id, created_time DESC);
COMMIT;`;
@@ -96,19 +96,19 @@ export default class ScopesScopeSessionRecordingsIndexRoute extends Route {
let doStorageBucketsExist = false;
const filters = {
created_time: [],
'create_time_values.user.id': [],
'create_time_values.target.scope.id': [],
'create_time_values.target.id': [],
user_id: [],
target_scope_id: [],
target_id: [],
};
if (time) filters.created_time.push({ gte: new Date(time) });
users.forEach((user) => {
filters['create_time_values.user.id'].push({ equals: user });
filters['user_id'].push({ equals: user });
});
scopes.forEach((scope) => {
filters['create_time_values.target.scope.id'].push({ equals: scope });
filters['target_scope_id'].push({ equals: scope });
});
targets.forEach((target) => {
filters['create_time_values.target.id'].push({ equals: target });
filters['target_id'].push({ equals: target });
});
const stateMap = {