Merge remote-tracking branch 'upstream/master' into feat/notifications

This commit is contained in:
M M
2024-06-14 18:41:20 -07:00
16 changed files with 281 additions and 43 deletions
+107
View File
@@ -0,0 +1,107 @@
# Contributing to Uptime Manager application
First off, thanks for taking the time to contribute! ❤️
All types of contributions are encouraged and valued. See the [Table of Contents](#table-of-contents) for different ways to help and details about how Uptime Manager project handles them. Please make sure to read the relevant section before making your contribution. It will make it a lot easier for us maintainers and smooth out the experience for all involved. The community looks forward to your contributions. 🎉
> And if you like the project, but don't have time to contribute, that's fine. There are other easy ways to support the project and show your appreciation, which we would also be very happy about:
> - Star the project
> - Tweet about it
> - Refer this project in your project's readme
> - Mention the project at local meetups and tell your friends/colleagues
## Table of Contents
- [I Have a Question](#i-have-a-question)
- [I Want To Contribute](#i-want-to-contribute)
- [Reporting Bugs](#reporting-bugs)
- [Suggesting Enhancements](#suggesting-enhancements)
- [Styleguides](#Developer-guide-for-issue-lifecycle-process)
## I Have a Question
> If you want to ask a question, we assume that you have read the available readme.md files. In the near future we'll come up with a proper installation and usage document.
Before you ask a question, it is best to search for existing [Issues](/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first.
If you then still feel the need to ask a question and need clarification, we recommend the following:
- Open an [Issue](/issues/new).
- Provide as much context as you can about what you're running into.
- Provide project and platform versions (nodejs, MongoDB, etc), depending on what seems relevant.
We will then take care of the issue as soon as possible.
## I Want To Contribute
> ### Legal Notice
> When contributing to this project, you must agree that you have authored 100% of the content, that you have the necessary rights to the content and that the content you contribute may be provided under the project license.
### Reporting Bugs
#### Before Submitting a Bug Report
A good bug report shouldn't leave others needing to chase you up for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible.
- Make sure that you are using the latest version.
- Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions. If you are looking for support, you might want to check [this section](#i-have-a-question)).
- To see if other users have experienced (and potentially already solved) the same issue you are having, check if there is not already a bug report existing for your bug or error in the [bug tracker](issues?q=label%3Abug).
- Also make sure to search the internet (including Stack Overflow) to see if users outside of the GitHub community have discussed the issue.
- Collect information about the bug:
- Stack trace (Traceback)
- OS, Platform and Version (Windows, Linux, macOS, x86, ARM)
- Version of the interpreter, compiler, SDK, runtime environment, package manager, depending on what seems relevant.
- Possibly your input and the output
- Can you reliably reproduce the issue? And can you also reproduce it with older versions?
#### How Do I Submit a Good Bug Report?
> You must never report security related issues, vulnerabilities or bugs including sensitive information to the issue tracker, or elsewhere in public. Instead sensitive bugs must be sent by email to security@bluewavelabs.ca
We use GitHub issues to track bugs and errors. If you run into an issue with the project:
- Open an [Issue](/issues/new). (Since we can't be sure at this point whether it is a bug or not, we ask you not to talk about a bug yet and not to label the issue.)
- Explain the behavior you would expect and the actual behavior.
- Please provide as much context as possible and describe the *reproduction steps* that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case.
- Provide the information you collected in the previous section.
Once it's filed:
- The project team will label the issue accordingly.
- A team member will try to reproduce the issue with your provided steps. If there are no reproduction steps or no obvious way to reproduce the issue, the team will ask you for those steps and mark the issue as `needs-repro`. Bugs with the `needs-repro` tag will not be addressed until they are reproduced.
- If the team is able to reproduce the issue, it will be marked `needs-fix`, as well as possibly other tags (such as `critical`), and the issue will be left to be [implemented by someone](#your-first-code-contribution).
### Suggesting Enhancements
This section guides you through submitting an enhancement suggestion for CONTRIBUTING.md, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions.
#### Before Submitting an Enhancement
- Make sure that you are using the latest version.
- Find out if the functionality is already covered, maybe by an individual configuration.
- Perform a [search](/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one.
- Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. If you're just targeting a minority of users, consider writing an add-on/plugin library.
#### How Do I Submit a Good Enhancement Suggestion?
Enhancement suggestions are tracked as [GitHub issues](/issues).
- Use a **clear and descriptive title** for the issue to identify the suggestion.
- Provide a **step-by-step description of the suggested enhancement** in as many details as possible.
- **Describe the current behavior** and **explain which behavior you expected to see instead** and why. At this point you can also tell which alternatives do not work for you.
- You may want to **include screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to.
- **Explain why this enhancement would be useful** to most CONTRIBUTING.md users. You may also want to point out the other projects that solved it better and which could serve as inspiration.
### Developer Guide for Issue Lifecycle Process
[This document](https://docs.google.com/document/d/1Gy3LiimGUNoSiWAMbwyK3SeMADcCMjCLu6cQYoawtSE/edit#heading=h.1lj2lgut6m7h) outlines the process every developer should follow for managing the issues lifecycle.
## Attribution
This guide is based on the **contributing.md**. [Make your own](https://contributing.md/)!
+61 -29
View File
@@ -19,12 +19,15 @@ BlueWave uptime monitoring application
1. [Installation (Client)](#client)
2. [Configuration(Client)](#config-client)
- [Environment](#env-vars-client)
3. [Installation (Server)](#server)
4. [Configuration(Server)](#config-server)
- [Environment](#env-vars-server)
- [Database](#databases)
- [Docker Images](#docker-images)
5. [Endpoints](#endpoints)
3. [Getting Started (Server)](#server)
- [Docker Compose Quickstart](#docker-quickstart)
- [Manual Installation](#manual-install)
- [Install Server](#install-server)
- [Environment](#env-vars-server)
- [Database](#databases)
- [(Optional) Dockerised Databases](#optional-docker-databases)
- [Start Server](#start-server)
4. [Endpoints](#endpoints)
###### Auth
- <code>POST</code> [/api/v1/auth/register](#post-register)
- <code>POST</code> [/api/v1/auth/login](#post-login)
@@ -50,8 +53,8 @@ BlueWave uptime monitoring application
- <code>GET</code> [/api/v1/alerts/{alertId}](#get-alert-alert-id)
- <code>POST</code> [/api/v1/alerts/edit/{alertId}](#edit-alert)
- <code>POST</code> [/api/v1/alerts/delete/{alertId}](#delete-alert)
6. [Error Handling](#error-handling)
7. [Contributors](#contributors)
5. [Error Handling](#error-handling)
6. [Contributors](#contributors)
---
@@ -80,17 +83,43 @@ BlueWave uptime monitoring application
---
### Server
### Getting Started (Server)
#### <u>Installation</u>
#### <u>Docker Quickstart</u> <a id="docker-quickstart"></a>
The fastest way to start the server is to use our Dockerfiles and [Docker Compose](https://docs.docker.com/compose/).
To get the server up and running you need to:
1. In the `Server/docker` directory run the build script `build_images.sh` to build docker images for the server, Redis database, and MongoDB database.
2. In the `Server/docker` directory, create a `.env` file with the [requried environtmental variables](#env-vars-server). Sample file:
```
CLIENT_HOST="http://localhost:5173"
JWT_SECRET="my_secret"
DB_TYPE="MongoDB"
DB_CONNECTION_STRING="mongodb://mongodb:27017/uptime_db"
REDIS_HOST="redis"
REDIS_PORT=6379
SYSTEM_EMAIL_ADDRESS="<email>"
SENDGRID_API_KEY="<api_key>"
LOGIN_PAGE_URL="<login_page"
```
3. In the `Server/docker` directory run `docker compose up` to run the `docker-compose.yaml` file and start all three images.
That's it, the server is ready to use.
<br/>
#### <u>Manual Install</u> <a id="manual-install"></a>
##### Install Server <a id="install-server"></a>
1. Change directory to the `Server` directory
2. Install all dependencies by running `npm install`
<br/>
#### <u>Configuration</u> <a id="config-server"></a>
##### Environmental Variables <a id="env-vars-server"></a>
Configure the server with the following environmental variables:
@@ -110,48 +139,51 @@ Configure the server with the following environmental variables:
<br/>
##### Databases
##### Databases <a id="databases"></a>
This project requires a number of databases to run:
1. Main database for the application. This project includes an implementation for a MongoDB database as well as a MongoDB Docker image.
2. A Redis database is required for the Queue implementation in the PingService. This project includes a Redis docker image.
You may run your own databases locally, or you may use the docker images included in the project to get up and running quickly.
You may use the included Dockerfiles to spin up databases quickly if you wish.
###### (Optional) Running Docker Images <a id="docker-images"></a>
###### (Optional) Dockerised Databases <a id="optional-docker-databases"></a>
Docker images are located in `./Server/docker`
Dockerfiles for the server and databases are located in the `./Server/docker` directory
<details>
<summary><b>MongoDB Image</b></summary>
Located in `./Server/docker/mongo`
Location: `./Server/docker/mongoDB.Dockerfile`
The `./Server/docker/mongo/mongo_data` folder should be mounted to the MongoDB container in order to persist data.
The `./Server/docker/mongo/data` directory should be mounted to the MongoDB container in order to persist data.
From the `mongo` folder run
From the `Server/docker` directory run
1. Build the image: `docker build -t <db_image_name> .`
2. Run the docker image: `docker run -d -p 27017:27017 -v $(pwd)/../mongo/mongo_data:/data/db --name uptime_database_mongo uptime_database_mongo`
1. Build the image: `docker build -f mongoDB.Dockerfile -t uptime_database_mongo .`
2. Run the docker image: `docker run -d -p 27017:27017 -v $(pwd)/mongo/data:/data/db --name uptime_database_mongo uptime_database_mongo`
</details>
<details>
<summary><b>Redis Image</b></summary>
Located in `./Server/docker/redis`
Location `./Server/docker/redislDockerfile`
the `./Server/docker/redis/redis_data` folder should be mounted to the Redis container in order to persist data.
the `./Server/docker/redis/data` directory should be mounted to the Redis container in order to persist data.
From the `Redis` folder run
From the `Server/docker` directory run
1. Build the image: `docker build -t <db_image_name>`
2. Run the image: `docker run -d -p 6379:6379 -v $(pwd)/../redis/redis_data:/data --name uptime_redis uptime_redis`
1. Build the image: `docker build -f redis.Dockerfile -t uptime_redis .`
2. Run the image: `docker run -d -p 6379:6379 -v $(pwd)/redis/data:/data --name uptime_redis uptime_redis`
</details>
<br/>
#### <u>Starting the Development Server</u>
##### Starting the Development Server <a id="start-server"></a>
1. run `npm run dev` to start the development server
- run `npm run dev` to start the development server
OR
- run `node index.js` to start server
---
+1
View File
@@ -0,0 +1 @@
./docker
+3 -2
View File
@@ -2,5 +2,6 @@ node_modules
.env
*.log
*.sh
docker/mongo/mongo_data/*
docker/redis/redis_data/*
docker/mongo/data/*
docker/redis/data/*
!docker/build_images.sh
+2
View File
@@ -24,6 +24,7 @@ const {
* @returns {String}
*/
const issueToken = (payload) => {
//TODO Add proper expiration date
return jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: "99d" });
};
@@ -88,6 +89,7 @@ const loginController = async (req, res, next) => {
const user = await req.db.getUserByEmail(req, res);
// Compare password
const match = await user.comparePassword(req.body.password);
if (match !== true) {
throw new Error("Incorrect password");
+4
View File
@@ -116,6 +116,8 @@ const createMonitor = async (req, res, next) => {
try {
const monitor = await req.db.createMonitor(req, res);
// Add monitor to job queue
req.jobQueue.addJob(monitor._id, monitor);
return res
.status(201)
.json({ success: true, msg: "Monitor created", data: monitor });
@@ -147,6 +149,8 @@ const deleteMonitor = async (req, res, next) => {
try {
const monitor = await req.db.deleteMonitor(req, res, next);
req.jobQueue.deleteJob(monitor);
/**
* TODO
* We should remove all checks and alerts associated with this monitor
+20
View File
@@ -0,0 +1,20 @@
#!/bin/bash
# Change directory to root Server directory for correct Docker Context
cd ..
# MongoDB
mongoDB="./docker/mongoDB.Dockerfile"
# Redis
redis="./docker/redis.Dockerfile"
# Server
server="./docker/server.Dockerfile"
docker build -f $mongoDB -t uptime_database_mongo .
docker build -f $redis -t uptime_redis .
docker build -f $server -t uptime_server .
echo "All images built"
+24
View File
@@ -0,0 +1,24 @@
version: "3"
services:
server:
image: uptime_server:latest
ports:
- "5000:5000"
env_file:
- .env
depends_on:
- redis
- mongodb
redis:
image: uptime_redis:latest
ports:
- "6379:6379"
volumes:
- ./redis/data:/data
mongodb:
image: uptime_database_mongo:latest
command: ["mongod", "--quiet"]
ports:
- "27017:27017"
volumes:
- ./mongo/data:/data/db
+13
View File
@@ -0,0 +1,13 @@
FROM node:20
WORKDIR /app
COPY ../../package*.json ./
RUN npm install
COPY ../../ ./
EXPOSE 5000
CMD ["node", "index.js"]
+3 -3
View File
@@ -38,7 +38,6 @@ const startApp = async () => {
? DB_TYPE[process.env.DB_TYPE]()
: require("./db/FakeDb");
const jobQueue = await JobQueue.createJobQueue();
/**
* NOTES
* Email Service will be added
@@ -69,7 +68,7 @@ const startApp = async () => {
//routes
app.use("/api/v1/auth", authRouter);
app.use("/api/v1/monitors", monitorRouter);
app.use("/api/v1/monitors", verifyJWT, monitorRouter);
app.use("/api/v1/checks", verifyJWT, checkRouter);
app.use("/api/v1/alerts", verifyJWT, alertRouter);
//Temporary route for testing, remove later
@@ -92,7 +91,8 @@ const startApp = async () => {
*/
app.use(handleErrors);
connectDbAndRunServer(app, db);
await connectDbAndRunServer(app, db);
const jobQueue = await JobQueue.createJobQueue(db);
};
startApp().catch((error) => {
+3 -2
View File
@@ -46,8 +46,9 @@ UserSchema.pre("save", async function (next) {
next();
});
UserSchema.methods.comparePassword = function (submittedPassword) {
return bcrypt.compare(submittedPassword, this.password);
UserSchema.methods.comparePassword = async function (submittedPassword) {
res = await bcrypt.compare(submittedPassword, this.password);
return res;
};
module.exports = mongoose.model("User", UserSchema);
+1 -1
View File
@@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"@sendgrid/mail": "^8.1.3",
"axios": "^1.7.2",
"bcrypt": "^5.1.1",
"bullmq": "5.7.15",
"cors": "^2.8.5",
@@ -373,7 +374,6 @@
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.2.tgz",
"integrity": "sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==",
"license": "MIT",
"dependencies": {
"follow-redirects": "^1.15.6",
"form-data": "^4.0.0",
+1
View File
@@ -12,6 +12,7 @@
"license": "ISC",
"dependencies": {
"@sendgrid/mail": "^8.1.3",
"axios": "^1.7.2",
"bcrypt": "^5.1.1",
"bullmq": "5.7.15",
"cors": "^2.8.5",
+38 -6
View File
@@ -6,6 +6,8 @@ const connection = {
};
const JOBS_PER_WORKER = 5;
const logger = require("../utils/logger");
const SERVICE_NAME = "JobQueue";
const axios = require("axios");
class JobQueue {
/**
@@ -18,6 +20,7 @@ class JobQueue {
connection,
});
this.workers = [];
this.db = null;
}
/**
@@ -27,9 +30,11 @@ class JobQueue {
* @returns {Promise<JobQueue>} - Returns a new JobQueue
*
*/
static async createJobQueue() {
static async createJobQueue(db) {
const queue = new JobQueue();
try {
queue.db = db;
const monitors = await db.getAllMonitors();
const workerStats = await queue.getWorkerStats();
await queue.scaleWorkers(workerStats);
return queue;
@@ -47,8 +52,9 @@ class JobQueue {
const worker = new Worker(
QUEUE_NAME,
async (job) => {
// TODO Ping a monitor
console.log(`${job.name} completed, workers: ${this.workers.length}`);
const response = await axios.get(job.data.url);
// TODO create a check object and save it to the db
console.log(response.status);
},
{
connection,
@@ -147,7 +153,6 @@ class JobQueue {
async getJobs() {
try {
const jobs = await this.queue.getRepeatableJobs();
console.log("jobs", jobs);
return jobs;
} catch (error) {
throw error;
@@ -166,8 +171,7 @@ class JobQueue {
try {
await this.queue.add(jobName, payload, {
repeat: {
every: 1000,
limit: 100,
every: payload.interval,
},
});
const workerStats = await this.getWorkerStats();
@@ -177,6 +181,34 @@ class JobQueue {
}
}
/**
* Deletes a job from the queue.
*
* @async
* @param {Monitor} monitor - The monitor to remove.
* @throws {Error}
*/
async deleteJob(monitor) {
try {
const deleted = await this.queue.removeRepeatable(monitor.id, {
every: monitor.interval,
});
if (deleted) {
logger.info("Job removed from queue", {
service: SERVICE_NAME,
jobId: monitor.id,
});
} else {
logger.error("Job not found in queue", {
service: SERVICE_NAME,
jobId: monitor.id,
});
}
} catch (error) {
throw error;
}
}
/**
* @async
* @returns {Promise<boolean>} - Returns true if obliteration is successful