Feat/handle pagespeed (#448)
* Initial commit * Prepare for pagespeed Check * removed unused code, updated JSdoc * reverted controller
This commit is contained in:
1 parent
7751a67372
commit
4071d6c97c
6 files changed
+48
-38
No files matched your search
@@ -1,2 +1,4 @@
|
|||||||
.vite
|
.vite
|
||||||
.vscode
|
.vscode
|
||||||
|
*.sh
|
||||||
|
!uptime.sh
|
||||||
@@ -58,7 +58,7 @@ const createPageSpeedCheck = async (req, res, next) => {
|
|||||||
const categories = pageSpeedResults.lighthouseResult?.categories;
|
const categories = pageSpeedResults.lighthouseResult?.categories;
|
||||||
|
|
||||||
if (!categories) {
|
if (!categories) {
|
||||||
throw new Error('Categories not found in PageSpeed results');
|
throw new Error("Categories not found in PageSpeed results");
|
||||||
}
|
}
|
||||||
|
|
||||||
const newPageSpeedCheck = await pageSpeedService.createPageSpeedCheck({
|
const newPageSpeedCheck = await pageSpeedService.createPageSpeedCheck({
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ const PageSpeedCheck = mongoose.Schema(
|
|||||||
ref: "Monitor",
|
ref: "Monitor",
|
||||||
immutable: true,
|
immutable: true,
|
||||||
},
|
},
|
||||||
|
status: {
|
||||||
|
type: Boolean,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
accessibility: {
|
accessibility: {
|
||||||
type: Number,
|
type: Number,
|
||||||
required: true,
|
required: true,
|
||||||
|
|||||||
@@ -231,9 +231,12 @@ class JobQueue {
|
|||||||
try {
|
try {
|
||||||
const jobs = await this.getJobs();
|
const jobs = await this.getJobs();
|
||||||
for (const job of jobs) {
|
for (const job of jobs) {
|
||||||
|
await this.queue.removeRepeatableByKey(job.key);
|
||||||
await this.queue.remove(job.id);
|
await this.queue.remove(job.id);
|
||||||
}
|
}
|
||||||
console.log(jobs);
|
this.workers.forEach(async (worker) => {
|
||||||
|
await worker.close();
|
||||||
|
});
|
||||||
await this.queue.obliterate();
|
await this.queue.obliterate();
|
||||||
logger.info(successMessages.JOB_QUEUE_OBLITERATE, {
|
logger.info(successMessages.JOB_QUEUE_OBLITERATE, {
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
const ping = require("ping");
|
const ping = require("ping");
|
||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
const Check = require("../models/Check");
|
|
||||||
const { Console } = require("winston/lib/winston/transports");
|
|
||||||
|
|
||||||
class NetworkService {
|
class NetworkService {
|
||||||
constructor(db) {
|
constructor(db) {
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.TYPE_PING = "ping";
|
this.TYPE_PING = "ping";
|
||||||
this.TYPE_HTTP = "http";
|
this.TYPE_HTTP = "http";
|
||||||
|
this.TYPE_PAGESPEED = "pagespeed";
|
||||||
this.SERVICE_NAME = "NetworkService";
|
this.SERVICE_NAME = "NetworkService";
|
||||||
this.NETWORK_ERROR = 5000;
|
this.NETWORK_ERROR = 5000;
|
||||||
}
|
}
|
||||||
@@ -44,24 +43,23 @@ class NetworkService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { responseTime, response } = await this.measureResponseTime(
|
const { responseTime, response } =
|
||||||
operation
|
await this.measureResponseTime(operation);
|
||||||
);
|
|
||||||
const isAlive = response.alive;
|
const isAlive = response.alive;
|
||||||
|
|
||||||
const check = new Check({
|
const checkData = {
|
||||||
monitorId: job.data._id,
|
monitorId: job.data._id,
|
||||||
status: isAlive,
|
status: isAlive,
|
||||||
responseTime,
|
responseTime,
|
||||||
});
|
};
|
||||||
return await this.logAndStoreCheck(check);
|
return await this.logAndStoreCheck(checkData, this.db.createCheck);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const check = new Check({
|
const checkData = {
|
||||||
monitorId: job.data._id,
|
monitorId: job.data._id,
|
||||||
status: false,
|
status: false,
|
||||||
responseTime: error.responseTime,
|
responseTime: error.responseTime,
|
||||||
});
|
};
|
||||||
return await this.logAndStoreCheck(check);
|
return await this.logAndStoreCheck(checkData, this.db.createCheck);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,37 +77,43 @@ class NetworkService {
|
|||||||
|
|
||||||
// attempt connection
|
// attempt connection
|
||||||
try {
|
try {
|
||||||
const { responseTime, response } = await this.measureResponseTime(
|
const { responseTime, response } =
|
||||||
operation
|
await this.measureResponseTime(operation);
|
||||||
);
|
|
||||||
|
|
||||||
// check if response is in the 200 range, if so, service is up
|
// check if response is in the 200 range, if so, service is up
|
||||||
const isAlive = response.status >= 200 && response.status < 300;
|
const isAlive = response.status >= 200 && response.status < 300;
|
||||||
|
|
||||||
//Create a check with relevant data
|
//Create a check with relevant data
|
||||||
const check = new Check({
|
const checkData = {
|
||||||
monitorId: job.data._id,
|
monitorId: job.data._id,
|
||||||
status: isAlive,
|
status: isAlive,
|
||||||
responseTime,
|
responseTime,
|
||||||
statusCode: response.status,
|
statusCode: response.status,
|
||||||
});
|
};
|
||||||
return await this.logAndStoreCheck(check);
|
return await this.logAndStoreCheck(checkData, this.db.createCheck);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const check = new Check({
|
const checkData = {
|
||||||
monitorId: job.data._id,
|
monitorId: job.data._id,
|
||||||
status: false,
|
status: false,
|
||||||
responseTime: error.responseTime,
|
responseTime: error.responseTime,
|
||||||
});
|
};
|
||||||
// The server returned a response
|
// The server returned a response
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
check.statusCode = error.response.status;
|
checkData.statusCode = error.response.status;
|
||||||
} else {
|
} else {
|
||||||
check.statusCode = this.NETWORK_ERROR;
|
checkData.statusCode = this.NETWORK_ERROR;
|
||||||
}
|
}
|
||||||
return await this.logAndStoreCheck(check);
|
return await this.logAndStoreCheck(checkData, this.db.createCheck);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles PageSpeed job types
|
||||||
|
* @param {Object} job - The job object containing data operation.
|
||||||
|
* @returns {Promise<{boolean}} The result of logging and storing the check
|
||||||
|
*/
|
||||||
|
async handlePagespeed(job) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the status of a given job based on its type.
|
* Retrieves the status of a given job based on its type.
|
||||||
* For unsupported job types, it logs an error and returns false.
|
* For unsupported job types, it logs an error and returns false.
|
||||||
@@ -123,6 +127,8 @@ class NetworkService {
|
|||||||
return await this.handlePing(job);
|
return await this.handlePing(job);
|
||||||
case this.TYPE_HTTP:
|
case this.TYPE_HTTP:
|
||||||
return await this.handleHttp(job);
|
return await this.handleHttp(job);
|
||||||
|
case this.TYPE_PAGESPEED:
|
||||||
|
return await this.handlePagespeed(job);
|
||||||
default:
|
default:
|
||||||
logger.error(`Unsupported type: ${job.data.type}`, {
|
logger.error(`Unsupported type: ${job.data.type}`, {
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
@@ -134,28 +140,23 @@ class NetworkService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Logs and stores the result of a check for a specific job.
|
* Logs and stores the result of a check for a specific job.
|
||||||
* This function creates a new Check object with the job's details and the result of the check,
|
|
||||||
* then attempts to save this object to the database. If the save operation is successful,
|
|
||||||
* it returns the status of the inserted check. If an error occurs during the save operation,
|
|
||||||
* it logs the error and returns false.
|
|
||||||
*
|
*
|
||||||
* @param {Object} job - The job object containing data necessary for the check.
|
* @param {Object} data - Data to be written
|
||||||
* @param {boolean} isAlive - The result of the check, indicating if the target is alive.
|
* @param {function} writeToDB - DB write method
|
||||||
* @param {number} responseTime - The response time measured during the check.
|
*
|
||||||
* @param {Error} [error=null] - Optional error object if an error occurred during the check.
|
|
||||||
* @returns {Promise<boolean>} The status of the inserted check if successful, otherwise false.
|
* @returns {Promise<boolean>} The status of the inserted check if successful, otherwise false.
|
||||||
*/
|
*/
|
||||||
async logAndStoreCheck(check) {
|
|
||||||
|
async logAndStoreCheck(data, writeToDB) {
|
||||||
try {
|
try {
|
||||||
const insertedCheck = await check.save();
|
const insertedCheck = await writeToDB(data);
|
||||||
return insertedCheck.status;
|
return insertedCheck.status;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Error wrtiting check for ${check.monitorId}`, {
|
logger.error(`Error wrtiting check for ${data.monitorId}`, {
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
monitorId: check.monitorId,
|
monitorId: data.monitorId,
|
||||||
error: error,
|
error: error,
|
||||||
});
|
});
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class PageSpeedService {
|
|||||||
async runPageSpeedCheck(url) {
|
async runPageSpeedCheck(url) {
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(
|
const response = await axios.get(
|
||||||
"https://pagespeedonline.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://www.google.com&category=seo&category=accessibility&category=best-practices&category=performance",
|
"https://pagespeedonline.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://www.google.com&category=seo&category=accessibility&category=best-practices&category=performance"
|
||||||
);
|
);
|
||||||
|
|
||||||
return response.data;
|
return response.data;
|
||||||
|
|||||||
Reference in new issue
Block a user