This commit is contained in:
Nawaz Dhandala 2022-04-20 21:14:44 +01:00
parent 63cc4732a6
commit 01dc1760b2
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
29 changed files with 2616 additions and 2515 deletions

View File

@ -6,7 +6,7 @@ import mongoose, {
} from '../Infrastructure/ORM';
// A schema definition for a criterion event, i.e up, down, or degraded
const criterionEventSchema: $TSFixMe = {
const criterionEventSchema: Schema = new Schema({
scheduleIds: [String],
createAlert: { type: Boolean, default: false },
autoAcknowledge: { type: Boolean, default: false },
@ -28,7 +28,7 @@ const criterionEventSchema: $TSFixMe = {
},
},
],
};
});
/**
* SAMPLE STRUCTURE OF HOW CRITERIA WILL BE STRUCTURED IN THE DB
@ -86,8 +86,8 @@ const schema: Schema = new Schema({
},
name: String,
slug: { type: String, index: true },
data: Object, //Can be URL, IP address, or anything that depends on the type.
createdById: { type: String, ref: 'User', index: true }, //UserId.
config: {}, //Can be URL, IP address, or anything that depends on the type.
createdById: { type: String, ref: 'User' }, //UserId.
type: {
type: String,
enum: [
@ -105,24 +105,10 @@ const schema: Schema = new Schema({
agentlessConfig: Object,
kubernetesConfig: Schema.Types.Mixed,
kubernetesNamespace: { type: String, default: 'default' },
resourceCategory: {
type: String,
ref: 'ResourceCategory',
index: true,
},
statusPageCategory: {
type: Schema.Types.ObjectId,
ref: 'StatusPageCategory',
index: true,
},
createdAt: {
type: Date,
default: Date.now,
},
pollTime: {
type: Array,
index: true,
},
lastPingTime: {
type: Date,
default: Date.now,
@ -184,16 +170,6 @@ const schema: Schema = new Schema({
scanning: { type: Boolean, default: false },
probeScanning: [String],
monitorStatus: String,
regions: [
{
probeId: {
type: Schema.Types.ObjectId,
indexed: true,
ref: 'Probe',
},
lastPingTime: { type: Date, indexed: true },
},
],
});
schema.virtual('project', {
@ -202,6 +178,7 @@ schema.virtual('project', {
ref: 'Project',
justOne: true,
});
export const requiredFields: RequiredFields = schema.requiredPaths();
export const uniqueFields: UniqueFields = [];

View File

@ -6,7 +6,6 @@ if (AirtableApiKey && AirtableBaseId) {
base = new Airtable({ apiKey: AirtableApiKey }).base(AirtableBaseId);
}
export default class Service {
/*
* Description: Create new user entry on airtable.
@ -177,4 +176,3 @@ export default class Service {
});
}
}

View File

@ -11,7 +11,7 @@ import SmsTemplateService from './SmsTemplateService';
import EscalationService from './EscalationService';
import MailService from '../../MailService/Services/MailService';
import UserService from './UserService';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import TwilioService from './TwilioService';
import ErrorService from '../Utils/error';
import StatusPageService from './StatusPageService';

View File

@ -6,7 +6,7 @@ import RealTimeService from './realTimeService';
import NotificationService from './NotificationService';
import ProjectService from './ProjectService';
import PaymentService from './PaymentService';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import TeamService from './TeamService';
import { IS_SAAS_SERVICE } from '../config/server';
import getSlug from '../Utils/getSlug';

View File

@ -1,5 +1,5 @@
import IncidentCommunicationSlaModel from '../Models/incidentCommunicationSla';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import FindOneBy from '../Types/DB/FindOneBy';
import FindBy from '../Types/DB/FindBy';

View File

@ -2,7 +2,7 @@ import PositiveNumber from 'Common/Types/PositiveNumber';
import IncidentModel from '../Models/incident';
import ObjectID from 'Common/Types/ObjectID';
import IncidentTimelineService from './IncidentTimelineService';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import AlertService from './AlertService';
import RealTimeService from './realTimeService';
import NotificationService from './NotificationService';

View File

@ -1,7 +1,7 @@
import IncomingRequestModel from '../Models/incomingRequest';
import ObjectID from 'Common/Types/ObjectID';
import IncidentService from './IncidentService';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import AlertService from './AlertService';
import ErrorService from '../Utils/error';
import ProjectService from './ProjectService';

View File

@ -251,7 +251,7 @@ export default class Service {
import LighthouseLogModel from '../Models/lighthouseLog';
import ObjectID from 'Common/Types/ObjectID';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import RealTimeService from './realTimeService';
import probeService from './ProbeService';

View File

@ -2,7 +2,7 @@ import MonitorLogModel from '../Models/monitorLog';
import MonitorLogByHourService from './MonitorLogByHourService';
import MonitorLogByDayService from './MonitorLogByDayService';
import MonitorLogByWeekService from './MonitorLogByWeekService';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import RealTimeService from './realTimeService';
import FindOneBy from '../Types/DB/FindOneBy';

2409
CommonServer/Services/MonitorService.ts Executable file → Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
import MonitorSlaModel from '../Models/monitorSla';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import BadDataException from 'Common/Types/Exception/BadDataException';
import FindOneBy from '../Types/DB/FindOneBy';
import FindBy from '../Types/DB/FindBy';

View File

@ -292,7 +292,7 @@ export default class Service {
import MonitorStatusModel from '../Models/monitorStatus';
import ObjectID from 'Common/Types/ObjectID';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import RealTimeService from './realTimeService';
import FindOneBy from '../Types/DB/FindOneBy';

View File

@ -1,6 +1,6 @@
import ProbeModel from '../Models/probe';
import RealTimeService from './realTimeService';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import MonitorStatusService from './MonitorStatusService';
import MonitorLogService from './MonitorLogService';
import IncidentService from './IncidentService';

View File

@ -1,7 +1,7 @@
import ProjectModel from '../Models/project';
import BadDataException from 'Common/Types/Exception/BadDataException';
import { v1 as uuidv1 } from 'uuid';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import PaymentService from './PaymentService';
import UserService from './UserService';
import IncidentPrioritiesService from './IncidentPrioritiesService';

View File

@ -434,4 +434,4 @@ export default class Service {
import moment from 'moment';
import IncidentModel from '../Models/incident';
import UserService from './UserService';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';

View File

@ -7,7 +7,7 @@ import ScheduledEventNoteService from './ScheduledEventNoteService';
import AlertService from './AlertService';
import moment from 'moment';
import getSlug from '../Utils/getSlug';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import FindOneBy from '../Types/DB/FindOneBy';
import FindBy from '../Types/DB/FindBy';

View File

@ -8,7 +8,7 @@ import IncidentModel from '../Models/incident';
import IncidentService from './IncidentService';
import ScheduledEventsService from './ScheduledEventService';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import ErrorService from '../Utils/error';
import SubscriberService from './SubscriberService';
import ProjectService from './ProjectService';

View File

@ -2,7 +2,7 @@ import axios from 'axios';
import ProjectService from './ProjectService';
import IncidentService from './IncidentService';
import IncidentTimelineService from './IncidentTimelineService';
import MonitorService from './MonitorService';
import MonitorService from './MonitorService.ts.temp';
import ZapierModel from '../Models/zapier';
import ObjectID from 'Common/Types/ObjectID';
import IncidentModel from '../Models/incident';

View File

@ -2,7 +2,7 @@ import { JSONValue } from 'Common/Types/JSON';
import { Query as DbQuery } from '../../Infrastructure/ORM';
export interface QueryType {
[x: string]: JSONValue | RegExp | QueryType;
[x: string]: JSONValue | RegExp | QueryType | Array<JSONValue>;
}
export default class Query {
@ -17,6 +17,31 @@ export default class Query {
return this;
}
public in(column: string, value: Array<JSONValue>): Query {
this.query[column] = { $in: value };
return this;
}
public exist(column: string): Query {
this.query[column] = { $exists: true };
return this;
}
public doesNotExist(column: string): Query {
this.query[column] = { $exists: false };
return this;
}
public lessThan(column: string, value: JSONValue): Query {
this.query[column] = { $lt: value };
return this;
}
public greaterThan(column: string, value: JSONValue): Query {
this.query[column] = { $gt: value };
return this;
}
public notEqualTo(column: string, value: JSONValue): Query {
this.query[column] = { $not: value };
return this;

View File

@ -2,7 +2,7 @@ import app from 'CommonServer/utils/StartServer';
// API
import MailAPI from './API/Mail'
import MailAPI from './API/Mail';
app.use(['/mail/email', '/email'], MailAPI);

View File

@ -5,8 +5,8 @@ import Express, {
ProbeRequest,
ExpressRouter,
} from 'CommonServer/utils/Express';
import MonitorService from '../Services/monitorService';
const router: ExpressRouter = Express.getRouter();
import ProbeAuthorization from 'CommonServer/middleware/ProbeAuthorization';
import { sendErrorResponse } from 'CommonServer/utils/Response';
import Exception from 'Common/Types/Exception/Exception';
@ -14,6 +14,8 @@ import Exception from 'Common/Types/Exception/Exception';
import { sendListResponse } from 'CommonServer/utils/Response';
import PositiveNumber from 'Common/Types/PositiveNumber';
const router: ExpressRouter = Express.getRouter();
router.get(
'/monitors',
ProbeAuthorization.isAuthorizedProbe,

View File

@ -1,5 +1,9 @@
import app from 'CommonServer/utils/StartServer';
app.use(['/ProbeAPI/probe', '/probe'], require('./api/probe'));
// API
import ProbeAPI from './API/Probe';
// Attach to the app.
app.use(['/probeapi/probe', '/probe'], ProbeAPI);
export default app;

View File

@ -1,75 +1 @@
import moment from 'moment';
import Database from 'CommonServer/Utils/database';
import PositiveNumber from 'Common/Types/PositiveNumber';
import OneUptimeDate from 'Common/Types/Date';
const monitorCollection: $TSFixMe =
Database.getDatabase().collection('monitors');
export default {
async getProbeMonitors(probeId: String, limit: PositiveNumber): void {
//Get monitors that have not been pinged for the last minute.
const date: $TSFixMe = OneUptimeDate.getOneMinAgo();
const key: string = `${probeId}_pingtime`;
const emptyQuery: $TSFixMe = {
deleted: false,
disabled: false,
type: {
$in: ['url', 'api'],
},
[key]: { $exists: false },
};
const query: $TSFixMe = {
deleted: false,
disabled: false,
type: {
$in: [
'url',
'api',
'incomingHttpRequest',
'kubernetes',
'ip',
'server-monitor',
],
},
[key]: { $lt: date },
};
let monitors: $TSFixMe = [];
const monitorsThatHaveNeverBeenPinged: $TSFixMe =
await monitorCollection.find(emptyQuery).limit(limit).toArray();
monitors = monitors.concat(monitorsThatHaveNeverBeenPinged);
if (monitorsThatHaveNeverBeenPinged.length < limit) {
const monitorsThatHaveBeenPingedBeforeOneMinute: $TSFixMe =
await monitorCollection
.find(query)
.sort({ [key]: 1 })
.limit(limit)
.toArray();
monitors = monitors.concat(
monitorsThatHaveBeenPingedBeforeOneMinute
);
}
if (monitors && monitors.length > 0) {
await monitorCollection.updateMany(
{
_id: {
$in: monitors.map((monitor: $TSFixMe) => {
return monitor._id;
}),
},
},
{ $set: { [key]: new Date(moment().format()) } }
);
return monitors;
}
return [];
},
};
export default {};

View File

@ -1,96 +0,0 @@
import moment from 'moment';
import { ObjectId } from 'mongodb';
import Database from 'CommonServer/utils/database';
const probeCollection: $TSFixMe = Database.getDatabase().collection('probes');
import { v1 as uuidv1 } from 'uuid';
import { post } from '../Utils/api';
import { realtimeUrl } from '../Config';
const realtimeBaseUrl: string = `${realtimeUrl}/realtime`;
export default {
create: async function (data: $TSFixMe): void {
let probeKey: $TSFixMe;
if (data.probeKey) {
probeKey = data.probeKey;
} else {
probeKey = uuidv1();
}
const storedProbe: $TSFixMe = await this.findOneBy({
probeName: data.probeName,
});
if (storedProbe && storedProbe.probeName) {
const error: $TSFixMe = new Error('Probe name already exists.');
error.code = 400;
throw error;
} else {
const probe: $TSFixMe = {};
probe.probeKey = probeKey;
probe.probeName = data.probeName;
probe.version = data.probeVersion;
const now: $TSFixMe = new Date(moment().format());
probe.createdAt = now;
probe.lastAlive = now;
probe.deleted = false;
const result: $TSFixMe = await probeCollection.insertOne(probe);
const savedProbe: $TSFixMe = await this.findOneBy({
_id: ObjectId(result.insertedId),
});
return savedProbe;
}
},
findOneBy: async function (query: $TSFixMe): void {
if (!query) {
query = {};
}
if (!query.deleted) {
query.$or = [{ deleted: false }, { deleted: { $exists: false } }];
}
const probe: $TSFixMe = await probeCollection.findOne(query);
return probe;
},
updateOneBy: async function (query, data): void {
if (!query) {
query = {};
}
if (!query.deleted) {
query.$or = [{ deleted: false }, { deleted: { $exists: false } }];
}
await probeCollection.updateOne(query, { $set: data });
const probe: $TSFixMe = await this.findOneBy(query);
return probe;
},
updateProbeStatus: async function (probeId: $TSFixMe): void {
const now: $TSFixMe = new Date(moment().format());
await probeCollection.updateOne(
{
_id: ObjectId(probeId),
$or: [{ deleted: false }, { deleted: { $exists: false } }],
},
{ $set: { lastAlive: now } }
);
const probe: $TSFixMe = await this.findOneBy({
_id: ObjectId(probeId),
});
// Realtime update for probe
post(`${realtimeBaseUrl}/update-probe`, { data: probe }, true);
return probe;
},
};

0
Workers/Index.ts Normal file
View File

13
Workers/package-lock.json generated Normal file
View File

@ -0,0 +1,13 @@
{
"name": "workers",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "workers",
"version": "1.0.0",
"license": "ISC"
}
}
}

11
Workers/package.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "workers",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

103
Workers/tsconfig.json Normal file
View File

@ -0,0 +1,103 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Projects */
// "incremental": true, /* Enable incremental compilation */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"jsx": "react" /* Specify what JSX code is generated. */,
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
/* Modules */
"module": "commonjs" /* Specify what module code is generated. */,
"rootDir": "", /* Specify the root folder within your source files. */
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
"typeRoots": [
], /* Specify multiple folders that act like `./node_modules/@types`. */
"types": ["node"], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "resolveJsonModule": true, /* Enable importing .json files */
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
"outDir": "build/dist", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
"strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
"strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
"strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
"strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
"noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
"useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
"alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
"noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
"noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
"exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
"noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
"noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
"noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
"noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
"noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}