update security + chart events è anomaly service

This commit is contained in:
Emily
2024-09-24 16:48:23 +02:00
parent 5b7e93bcbb
commit f5edf187fd
18 changed files with 1417 additions and 75 deletions

View File

@@ -1,5 +1,6 @@
{
"dependencies": {
"@getbrevo/brevo": "^2.2.0",
"dayjs": "^1.11.13",
"mongoose": "^8.3.2"
},

1047
security/pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,10 +4,9 @@ import { AnomalyVisitModel } from '@schema/anomalies/AnomalyVisitSchema';
import { AnomalyEventsModel } from '@schema/anomalies/AnomalyEventsSchema';
import { EventModel } from "@schema/metrics/EventSchema";
import { VisitModel } from '@schema/metrics/VisitSchema'
import EmailService from "@services/EmailService";
import * as url from 'url';
import { ProjectModel } from "@schema/ProjectSchema";
import { UserModel } from "@schema/UserSchema";
import { getAggregation } from "./Aggregations";
type TAvgInput = { _id: string, count: number }
@@ -112,7 +111,7 @@ export async function findAnomalies(project_id: string, callback: AnomalyCallbac
visits: [],
events: [],
dns: [],
pid: project_id
pid: project_id,
}
for (const visit of visitAnomalies) {
@@ -136,19 +135,6 @@ export async function findAnomalies(project_id: string, callback: AnomalyCallbac
report.dns.push(website);
}
// const project = await ProjectModel.findById(pid);
// if (!project) return { ok: false, error: 'Cannot find project with id ' + pid.toString() }
// const user = await UserModel.findById(project.owner);
// if (!user) return { ok: false, error: 'Cannot find user with id ' + project.owner.toString() }
// if (shouldSendMail.visitsEvents === true) {
// await EmailService.sendAnomalyVisitsEventsEmail(user.email, project.name);
// }
// if (shouldSendMail.domains === true) {
// await EmailService.sendAnomalyDomainEmail(user.email, project.name);
// }
callback(report);
return report;

View File

@@ -1,18 +1,46 @@
import { anomalyCheckAll, AnomalyReport } from "./AnomalyService";
import { anomalyCheckAll, AnomalyReport, findAnomalies } from "./AnomalyService";
import { connectDatabase } from '@services/DatabaseService'
import { requireEnv } from '@utils/requireEnv'
import EmailService from "@services/EmailService";
import { ProjectModel } from "@schema/ProjectSchema";
import { UserModel } from "@schema/UserSchema";
EmailService.init(requireEnv('BREVO_API_KEY'));
connectDatabase(requireEnv('MONGO_CONNECTION_STRING'));
anomalyCheckAll(async report => {
import fs from 'fs';
const reports: AnomalyReport[] = [];
anomalyCheckAll(report => {
if (report.visits.length > 0 || report.events.length > 0 || report.dns.length > 0) {
reports.push(report);
const project = await ProjectModel.findById(report.pid);
if (!project) return { ok: false, error: 'Cannot find project with id ' + report.pid.toString() }
const user = await UserModel.findById(project.owner);
if (!user) return { ok: false, error: 'Cannot find user with id ' + project.owner.toString() }
if (report.visits.length > 0 || report.events.length > 0) {
await EmailService.sendAnomalyVisitsEventsEmail(
user.email,
project.name,
{ visits: report.visits, events: report.events }
);
}
if (report.visits.length > 0) {
await EmailService.sendAnomalyDomainEmail(
user.email,
project.name,
report.dns
);
}
}
}).then(e => {
fs.writeFileSync('security-report.json', JSON.stringify(reports));
});