mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
fix anomaly service
This commit is contained in:
@@ -3,6 +3,9 @@ import { Redis } from "~/server/services/CacheService";
|
|||||||
import EmailService from '@services/EmailService';
|
import EmailService from '@services/EmailService';
|
||||||
import StripeService from '~/server/services/StripeService';
|
import StripeService from '~/server/services/StripeService';
|
||||||
import { anomalyLoop } from "./services/AnomalyService";
|
import { anomalyLoop } from "./services/AnomalyService";
|
||||||
|
import { logger } from "./Logger";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const config = useRuntimeConfig();
|
const config = useRuntimeConfig();
|
||||||
let connection: mongoose.Mongoose;
|
let connection: mongoose.Mongoose;
|
||||||
@@ -10,36 +13,37 @@ let connection: mongoose.Mongoose;
|
|||||||
|
|
||||||
export default async () => {
|
export default async () => {
|
||||||
|
|
||||||
console.log('[SERVER] Initializing');
|
logger.info('[SERVER] Initializing');
|
||||||
|
|
||||||
if (config.EMAIL_SERVICE) {
|
if (config.EMAIL_SERVICE) {
|
||||||
EmailService.init(config.BREVO_API_KEY);
|
EmailService.init(config.BREVO_API_KEY);
|
||||||
console.log('[EMAIL] Initialized')
|
logger.info('[EMAIL] Initialized');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (config.STRIPE_SECRET) {
|
if (config.STRIPE_SECRET) {
|
||||||
StripeService.init(config.STRIPE_SECRET, config.STRIPE_WH_SECRET, false);
|
StripeService.init(config.STRIPE_SECRET, config.STRIPE_WH_SECRET, false);
|
||||||
console.log('[STRIPE] Initialized')
|
logger.info('[STRIPE] Initialized');
|
||||||
} else {
|
} else {
|
||||||
StripeService.disable();
|
StripeService.disable();
|
||||||
console.log('[STRIPE] No stripe key - Disabled mode')
|
logger.warn('[STRIPE] No stripe key - Disabled mode');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!connection || connection.connection.readyState == mongoose.ConnectionStates.disconnected) {
|
if (!connection || connection.connection.readyState == mongoose.ConnectionStates.disconnected) {
|
||||||
console.log('[DATABASE] Connecting');
|
logger.info('[DATABASE] Connecting');
|
||||||
connection = await mongoose.connect(config.MONGO_CONNECTION_STRING);
|
connection = await mongoose.connect(config.MONGO_CONNECTION_STRING);
|
||||||
console.log('[DATABASE] Connected');
|
logger.info('[DATABASE] Connected');
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[REDIS] Connecting');
|
logger.info('[REDIS] Connecting');
|
||||||
await Redis.init();
|
await Redis.init();
|
||||||
console.log('[REDIS] Connected');
|
logger.info('[REDIS] Connected');
|
||||||
|
|
||||||
console.log('[SERVER] Completed');
|
logger.info('[SERVER] Completed');
|
||||||
|
|
||||||
console.log('[ANOMALY LOOP] Started');
|
logger.warn('[ANOMALY LOOP] Disabled');
|
||||||
anomalyLoop();
|
// anomalyLoop();
|
||||||
|
logger.error(new Error('test error'))
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -17,12 +17,12 @@ const anomalyData = { minutes: 0 }
|
|||||||
|
|
||||||
async function anomalyCheckAll() {
|
async function anomalyCheckAll() {
|
||||||
const start = performance.now();
|
const start = performance.now();
|
||||||
console.log('START ANOMALY CHECK');
|
console.log('[ANOMALY] START ANOMALY CHECK');
|
||||||
const projects = await ProjectModel.find({}, { _id: 1 });
|
const projects = await ProjectModel.find({}, { _id: 1 });
|
||||||
for (const project of projects) {
|
for (const project of projects) {
|
||||||
await findAnomalies(project.id);
|
await findAnomalies(project.id);
|
||||||
}
|
}
|
||||||
const end = start - performance.now();
|
const end = performance.now() - start;
|
||||||
console.log('END ANOMALY CHECK', end, 'ms');
|
console.log('END ANOMALY CHECK', end, 'ms');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,6 +31,7 @@ export function anomalyLoop() {
|
|||||||
anomalyCheckAll();
|
anomalyCheckAll();
|
||||||
anomalyData.minutes = 0;
|
anomalyData.minutes = 0;
|
||||||
}
|
}
|
||||||
|
anomalyData.minutes++;
|
||||||
setTimeout(() => anomalyLoop(), 1000 * 60);
|
setTimeout(() => anomalyLoop(), 1000 * 60);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,22 +84,25 @@ export async function findAnomalies(project_id: string) {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
const detectedWebsites: string[] = [];
|
||||||
|
|
||||||
|
if (websites.length > 0) {
|
||||||
const rootWebsite = websites.reduce((a, e) => {
|
const rootWebsite = websites.reduce((a, e) => {
|
||||||
return a.count > e.count ? a : e;
|
return a.count > e.count ? a : e;
|
||||||
});
|
});
|
||||||
|
|
||||||
const rootDomain = new url.URL(getUrlFromString(rootWebsite._id)).hostname;
|
const rootDomain = new url.URL(getUrlFromString(rootWebsite._id)).hostname;
|
||||||
|
|
||||||
const detectedWebsites: string[] = [];
|
|
||||||
|
|
||||||
for (const website of websites) {
|
for (const website of websites) {
|
||||||
const websiteDomain = new url.URL(getUrlFromString(website._id)).hostname;
|
const websiteDomain = new url.URL(getUrlFromString(website._id)).hostname;
|
||||||
|
|
||||||
if (!websiteDomain.includes(rootDomain)) {
|
if (websiteDomain === 'localhost') continue;
|
||||||
detectedWebsites.push(website._id);
|
if (websiteDomain === '127.0.0.1') continue;
|
||||||
|
if (websiteDomain === '0.0.0.0') continue;
|
||||||
|
|
||||||
|
if (!websiteDomain.includes(rootDomain)) { detectedWebsites.push(website._id); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const visitAnomalies = movingAverageAnomaly(visitsTimelineData, WINDOW_SIZE, THRESHOLD);
|
const visitAnomalies = movingAverageAnomaly(visitsTimelineData, WINDOW_SIZE, THRESHOLD);
|
||||||
const eventAnomalies = movingAverageAnomaly(eventsTimelineData, WINDOW_SIZE, THRESHOLD);
|
const eventAnomalies = movingAverageAnomaly(eventsTimelineData, WINDOW_SIZE, THRESHOLD);
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import { model, Schema, Types } from 'mongoose';
|
|||||||
|
|
||||||
export type TAnomalyVisit = {
|
export type TAnomalyVisit = {
|
||||||
project_id: Schema.Types.ObjectId
|
project_id: Schema.Types.ObjectId
|
||||||
visitDate: string,
|
visitDate: Date,
|
||||||
created_at: Date
|
created_at: Date
|
||||||
}
|
}
|
||||||
|
|
||||||
const AnomalyVisitSchema = new Schema<TAnomalyVisit>({
|
const AnomalyVisitSchema = new Schema<TAnomalyVisit>({
|
||||||
project_id: { type: Types.ObjectId, required: true },
|
project_id: { type: Types.ObjectId, required: true },
|
||||||
visitDate: { type: String, required: true },
|
visitDate: { type: Date, required: true },
|
||||||
created_at: { type: Date, required: true },
|
created_at: { type: Date, required: true },
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user