anything-llm/server/models/eventLogs.js
Sean Hatfield d789920a19
[FEAT] Automated audit logging (#667)
* WIP event logging - new table for events and new settings view for viewing

* WIP add logging

* UI for log rows

* rename files to Logging to prevent getting gitignore

* add metadata for all logging events and colored badges in logs page

* remove unneeded comment

* cleanup namespace for logging

* clean up backend calls

* update logging to show to => from settings changes

* add logging for invitations, created, deleted, and accepted

* add logging for user created, updated, suspended, or removed

* add logging for workspace deleted

* add logging for chat logs exported

* add logging for API keys, LLM, embedder, vector db, embed chat, and reset button

* modify event logs

* update to event log types

* simplify rendering of event badges

---------

Co-authored-by: timothycarambat <rambat1010@gmail.com>
2024-02-06 15:21:40 -08:00

130 lines
3.1 KiB
JavaScript

const prisma = require("../utils/prisma");
const EventLogs = {
logEvent: async function (event, metadata = {}, userId = null) {
try {
const eventLog = await prisma.event_logs.create({
data: {
event,
metadata: metadata ? JSON.stringify(metadata) : null,
userId: userId ? Number(userId) : null,
occurredAt: new Date(),
},
});
console.log(`\x1b[32m[Event Logged]\x1b[0m - ${event}`);
return { eventLog, message: null };
} catch (error) {
console.error(
`\x1b[31m[Event Logging Failed]\x1b[0m - ${event}`,
error.message
);
return { eventLog: null, message: error.message };
}
},
getByEvent: async function (event, limit = null, orderBy = null) {
try {
const logs = await prisma.event_logs.findMany({
where: { event },
...(limit !== null ? { take: limit } : {}),
...(orderBy !== null
? { orderBy }
: { orderBy: { occurredAt: "desc" } }),
});
return logs;
} catch (error) {
console.error(error.message);
return [];
}
},
getByUserId: async function (userId, limit = null, orderBy = null) {
try {
const logs = await prisma.event_logs.findMany({
where: { userId },
...(limit !== null ? { take: limit } : {}),
...(orderBy !== null
? { orderBy }
: { orderBy: { occurredAt: "desc" } }),
});
return logs;
} catch (error) {
console.error(error.message);
return [];
}
},
where: async function (
clause = {},
limit = null,
orderBy = null,
offset = null
) {
try {
const logs = await prisma.event_logs.findMany({
where: clause,
...(limit !== null ? { take: limit } : {}),
...(offset !== null ? { skip: offset } : {}),
...(orderBy !== null
? { orderBy }
: { orderBy: { occurredAt: "desc" } }),
});
return logs;
} catch (error) {
console.error(error.message);
return [];
}
},
whereWithData: async function (
clause = {},
limit = null,
offset = null,
orderBy = null
) {
const { User } = require("./user");
try {
const results = await this.where(clause, limit, orderBy, offset);
for (const res of results) {
const user = res.userId ? await User.get({ id: res.userId }) : null;
res.user = user
? { username: user.username }
: { username: "unknown user" };
}
return results;
} catch (error) {
console.error(error.message);
return [];
}
},
count: async function (clause = {}) {
try {
const count = await prisma.event_logs.count({
where: clause,
});
return count;
} catch (error) {
console.error(error.message);
return 0;
}
},
delete: async function (clause = {}) {
try {
await prisma.event_logs.deleteMany({
where: clause,
});
return true;
} catch (error) {
console.error(error.message);
return false;
}
},
};
module.exports = { EventLogs };