Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modlog: Support logging to a SQLite database #7513

Merged
merged 38 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c6d0097
Modlog: Support logging to a SQLite database
Aug 19, 2020
ac00a28
small fixes
Oct 12, 2020
566ae3a
Add id tokenizer extension
monsanto Oct 14, 2020
e5398b4
Support FTS with chaos' extension
Oct 15, 2020
0a1b76c
code review
Oct 15, 2020
1289778
Default to FTS
Oct 15, 2020
8e80dd4
Fixes so I can run converter
monsanto Oct 15, 2020
2ebe051
Converter: Support FTS
Oct 15, 2020
bd4e291
Fix converter
Oct 16, 2020
96b55c0
Buffer by 25000 instead of 100
monsanto Oct 16, 2020
7d36831
dont insert nulls to FTS
Oct 16, 2020
222c8b9
Merge branch 'modlog-sql-new' of https://github.com/AnnikaCodes/pokem…
Oct 16, 2020
cb93fc9
Converter: Make some fixes
Oct 17, 2020
1a26693
Use FTS more
Oct 17, 2020
d649ef3
Merge remote-tracking branch 'upstream/master' into modlog-sql-new
Oct 17, 2020
236d949
alts_fts stuff
Oct 17, 2020
224ad43
right
Oct 17, 2020
57beee7
simplify
Oct 17, 2020
1f39120
fts changes
Oct 18, 2020
92f714e
simplify
Oct 20, 2020
e7f1b7b
Add Config setting to disable SQLite logging
Oct 22, 2020
4ff626d
Merge remote-tracking branch 'upstream/master' into modlog-sql-new
Oct 22, 2020
12ea945
Support Iterables in the modlog API
Oct 23, 2020
006fd20
More fixes
Oct 23, 2020
6c7b981
Merge branch 'modlog-sql-new' of https://github.com/AnnikaCodes/pokem…
Oct 23, 2020
50f0929
Fix transaction
Oct 23, 2020
6357b18
Fix query
Oct 23, 2020
cdf4444
Rename config setting
Oct 26, 2020
8028535
Don't automatically create a Modlog instance
Oct 26, 2020
3a2fe7d
Merge remote-tracking branch 'upstream/master' into modlog-sql-new
Oct 26, 2020
4de0bf7
Only run schema if database doesn't exist
monsanto Oct 28, 2020
484412d
Fix relative path on native extension
monsanto Oct 28, 2020
5695aec
Remove redundant transaction
monsanto Oct 30, 2020
145e788
Remove unused query functionality
monsanto Oct 30, 2020
bfadd1b
Skip prepareSQLSearch test
monsanto Oct 30, 2020
1f278dd
Remove FTS
monsanto Oct 30, 2020
e9effcb
Slightly optimize schema
monsanto Oct 30, 2020
9961d28
Fix /hotpatch modlog
Oct 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 45 additions & 30 deletions server/modlog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class Modlog {

this.database.exec(FS(MODLOG_SCHEMA_PATH).readIfExistsSync().replace(/%TOKENIZER%/g, tokenizer));

this.database.function('regex', {deterministic: true}, (regexString, toMatch) => {
this.database.function('regex', {deterministic: true}, (regexString: string, toMatch: string) => {
return Number(RegExp(regexString, 'i').test(toMatch));
});

Expand Down Expand Up @@ -281,40 +281,55 @@ export class Modlog {
* Writes to the modlog
*/
write(roomid: string, entry: ModlogEntry, overrideID?: string) {
roomid = entry.roomID || roomid;
this.writeSQL(roomid, entry, overrideID);
this.writeText(roomid, entry, overrideID);
}
if (!entry.roomID) entry.roomID = roomid;
if (entry.isGlobal && entry.roomID !== 'global' && !entry.roomID.startsWith('global-')) {
entry.roomID = `global-${entry.roomID}`;
}
if (overrideID) entry.visualRoomID = overrideID;

writeSQL(roomid: string, entry: ModlogEntry, overrideID?: string) {
if (entry.isGlobal && roomid !== 'global' && !roomid.startsWith('global-')) roomid = `global-${roomid}`;
this.insertionTransaction({
action: entry.action,
roomID: roomid,
visualRoomID: overrideID || entry.visualRoomID,
userid: entry.userid,
autoconfirmedID: entry.autoconfirmedID,
ip: entry.ip,
loggedBy: entry.loggedBy,
note: entry.note,
time: entry.time || Date.now(),
alts: entry.alts,
});
const entries = [entry];
this.writeSQL(entries);
this.writeText(entries);
}

writeText(roomid: string, entry: ModlogEntry, overrideID?: string) {
const stream = this.streams.get(roomid as ModlogID);
if (!stream) throw new Error(`Attempted to write to an uninitialized modlog stream for the room '${roomid}'`);
writeSQL(entries: Iterable<ModlogEntry>) {
for (const entry of entries) {
this.insertionTransaction({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates N transactions instead of inserting N inside 1 transaction

action: entry.action,
roomID: entry.roomID,
visualRoomID: entry.visualRoomID,
userid: entry.userid,
autoconfirmedID: entry.autoconfirmedID,
ip: entry.ip,
loggedBy: entry.loggedBy,
note: entry.note,
time: entry.time || Date.now(),
alts: entry.alts,
});
}
}

let buf = `[${new Date(entry.time || Date.now()).toJSON()}] (${overrideID || entry.visualRoomID || roomid}) ${entry.action}:`;
if (entry.userid) buf += ` [${entry.userid}]`;
if (entry.autoconfirmedID) buf += ` ac:[${entry.autoconfirmedID}]`;
if (entry.alts) buf += ` alts:[${entry.alts.join('], [')}]`;
if (entry.ip) buf += ` [${entry.ip}]`;
if (entry.loggedBy) buf += ` by ${entry.loggedBy}`;
if (entry.note) buf += `: ${entry.note}`;
writeText(entries: Iterable<ModlogEntry>) {
const buffers = new Map<ModlogID, string>();
for (const entry of entries) {
const streamID = entry.roomID as ModlogID;
let buf = buffers.get(streamID) || '';
buf += `[${new Date(entry.time || Date.now()).toJSON()}] (${entry.visualRoomID || entry.roomID}) ${entry.action}:`;
if (entry.userid) buf += ` [${entry.userid}]`;
if (entry.autoconfirmedID) buf += ` ac:[${entry.autoconfirmedID}]`;
if (entry.alts) buf += ` alts:[${entry.alts.join('], [')}]`;
if (entry.ip) buf += ` [${entry.ip}]`;
if (entry.loggedBy) buf += ` by ${entry.loggedBy}`;
if (entry.note) buf += `: ${entry.note}`;
buf += `\n`;
buffers.set(streamID, buf);
}

void stream.write(`${buf}\n`);
for (const [streamID, buffer] of buffers) {
const stream = this.streams.get(streamID);
if (!stream) throw new Error(`Attempted to write to an uninitialized modlog stream for the room '${streamID}'`);
void stream.write(buffer);
}
}

async destroy(roomid: ModlogID) {
Expand Down
21 changes: 1 addition & 20 deletions tools/modlog/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,6 @@ export class ModlogConverterSQLite {
export class ModlogConverterTxt {
readonly databaseFile: string;
readonly modlog: Modlog;
readonly batchInsertTransaction: Database.Transaction;

readonly textLogDir: string;
readonly isTesting: {files: Map<string, string>, ml?: Modlog} | null = null;
Expand All @@ -526,24 +525,6 @@ export class ModlogConverterTxt {
}

this.modlog = new Modlog(this.textLogDir, this.isTesting ? ':memory:' : this.databaseFile);

// Custom transaction to allow for batching
this.batchInsertTransaction = this.modlog.database.transaction((entries: ModlogEntry[]) => {
for (const entry of entries) {
this.modlog.insertionTransaction({
action: entry.action,
roomID: entry.roomID,
visualRoomID: entry.visualRoomID,
userid: entry.userid,
autoconfirmedID: entry.autoconfirmedID,
ip: entry.ip,
loggedBy: entry.loggedBy,
note: entry.note,
time: entry.time || Date.now(),
alts: entry.alts,
});
}
});
}

async toSQLite() {
Expand All @@ -569,7 +550,7 @@ export class ModlogConverterTxt {


const insertEntries = (alwaysShowProgress?: boolean) => {
this.batchInsertTransaction(entries);
this.modlog.writeSQL(entries);
entriesLogged += entries.length;
if (!Config.nofswriting && (
alwaysShowProgress ||
Expand Down