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

Backend Port of Alerts Schema Changes Including Anomaly Alerts #466

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion packages/api/src/models/alert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mongoose, { Schema } from 'mongoose';

import type { ObjectId } from '.';
import { Chart } from './dashboard';

export type AlertType = 'presence' | 'absence';

Expand All @@ -27,8 +28,29 @@ export type AlertChannel = {
webhookId: string;
};

export type AlertSource = 'LOG' | 'CHART';
export type AlertSource = 'LOG' | 'CHART' | 'CUSTOM';

export type AlertCustomConfig = Pick<Chart, 'series'>;

export enum CheckerType {
Anomaly = 'anomaly',
Threshold = 'threshold',
}

interface AnomalyConfig {
models?: AnomalyModel[];
mode?: 'any' | 'combined';
}

export interface AnomalyModel {
name: string;
enabled: boolean;
params: {
[key: string]: unknown;
};
}

export type CheckerConfig = AnomalyConfig;
export interface IAlert {
_id: ObjectId;
channel: AlertChannel;
Expand Down Expand Up @@ -59,10 +81,35 @@ export interface IAlert {
at: Date;
until: Date;
};

customConfig?: AlertCustomConfig;
historyWindow?: number; // in minutes

checker?: {
type: CheckerType;
config?: CheckerConfig;
};
}

export type AlertDocument = mongoose.HydratedDocument<IAlert>;

interface IChecker {
type: CheckerType;
config?: CheckerConfig;
}

const checkerSchema = new Schema<IChecker>({
type: {
type: String,
enum: Object.values(CheckerType),
required: true,
},
config: {
type: Schema.Types.Mixed,
required: false,
},
});

const AlertSchema = new Schema<IAlert>(
{
type: {
Expand Down Expand Up @@ -151,10 +198,30 @@ const AlertSchema = new Schema<IAlert>(
required: false,
},
},
customConfig: {
type: Schema.Types.Mixed,
required: false,
},
historyWindow: {
type: Number,
required: false,
},
checker: {
type: checkerSchema,
required: false,
default: {
type: CheckerType.Threshold,
},
},
},
{
timestamps: true,
},
);

AlertSchema.index({
state: 1,
checker: 1,
});

export default mongoose.model<IAlert>('Alert', AlertSchema);
2 changes: 1 addition & 1 deletion packages/api/src/models/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type NumberFormat = {
unit?: string;
};

type Chart = {
export type Chart = {
id: string;
name: string;
x: number;
Expand Down
Loading
Loading