2024-10-15 11:57:37 +08:00
|
|
|
import mongoose from "mongoose";
|
2024-10-30 10:20:42 +08:00
|
|
|
import { BaseCheckSchema } from "./Check.js";
|
2024-10-24 15:35:30 +08:00
|
|
|
import logger from "../../utils/logger.js";
|
2024-10-30 10:20:42 +08:00
|
|
|
import { time } from "console";
|
2024-08-02 12:32:08 -07:00
|
|
|
const AuditSchema = mongoose.Schema({
|
2024-10-17 12:07:30 +08:00
|
|
|
id: { type: String, required: true },
|
|
|
|
|
title: { type: String, required: true },
|
|
|
|
|
description: { type: String, required: true },
|
|
|
|
|
score: { type: Number, required: true },
|
|
|
|
|
scoreDisplayMode: { type: String, required: true },
|
|
|
|
|
displayValue: { type: String, required: true },
|
|
|
|
|
numericValue: { type: Number, required: true },
|
|
|
|
|
numericUnit: { type: String, required: true },
|
2024-08-02 12:32:08 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const AuditsSchema = mongoose.Schema({
|
2024-10-17 12:07:30 +08:00
|
|
|
cls: {
|
|
|
|
|
type: AuditSchema,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
si: {
|
|
|
|
|
type: AuditSchema,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
fcp: {
|
|
|
|
|
type: AuditSchema,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
lcp: {
|
|
|
|
|
type: AuditSchema,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
tbt: {
|
|
|
|
|
type: AuditSchema,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
2024-08-02 12:32:08 -07:00
|
|
|
});
|
|
|
|
|
|
2024-07-15 14:42:48 -07:00
|
|
|
/**
|
|
|
|
|
* Mongoose schema for storing metrics from Google Lighthouse.
|
2024-07-15 14:48:13 -07:00
|
|
|
* @typedef {Object} PageSpeedCheck
|
2024-07-15 14:42:48 -07:00
|
|
|
* @property {mongoose.Schema.Types.ObjectId} monitorId - Reference to the Monitor model.
|
|
|
|
|
* @property {number} accessibility - Accessibility score.
|
|
|
|
|
* @property {number} bestPractices - Best practices score.
|
|
|
|
|
* @property {number} seo - SEO score.
|
|
|
|
|
* @property {number} performance - Performance score.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-07-15 14:48:13 -07:00
|
|
|
const PageSpeedCheck = mongoose.Schema(
|
2024-10-17 12:07:30 +08:00
|
|
|
{
|
2024-10-30 10:20:42 +08:00
|
|
|
...BaseCheckSchema.obj,
|
2024-10-17 12:07:30 +08:00
|
|
|
accessibility: {
|
|
|
|
|
type: Number,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
bestPractices: {
|
|
|
|
|
type: Number,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
seo: {
|
|
|
|
|
type: Number,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
performance: {
|
|
|
|
|
type: Number,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
audits: {
|
|
|
|
|
type: AuditsSchema,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-10-30 10:20:42 +08:00
|
|
|
{ timestamps: true }
|
2024-07-15 14:06:41 -07:00
|
|
|
);
|
|
|
|
|
|
2024-07-15 14:42:48 -07:00
|
|
|
/**
|
|
|
|
|
* Mongoose model for storing metrics from Google Lighthouse.
|
2024-07-15 14:48:13 -07:00
|
|
|
* @typedef {mongoose.Model<PageSpeedCheck>} LighthouseMetricsModel
|
2024-07-15 14:42:48 -07:00
|
|
|
*/
|
|
|
|
|
|
2024-07-30 11:24:25 -07:00
|
|
|
PageSpeedCheck.pre("save", async function (next) {
|
2024-10-17 12:07:30 +08:00
|
|
|
try {
|
|
|
|
|
const monitor = await mongoose.model("Monitor").findById(this.monitorId);
|
|
|
|
|
if (monitor && monitor.status !== this.status) {
|
|
|
|
|
if (monitor.status === true && this.status === false) {
|
2024-10-24 15:35:30 +08:00
|
|
|
logger.info({ message: "Monitor went down", monitorId: monitor._id });
|
2024-10-17 12:07:30 +08:00
|
|
|
}
|
2024-07-25 14:54:50 -07:00
|
|
|
|
2024-10-17 12:07:30 +08:00
|
|
|
if (monitor.status === false && this.status === true) {
|
2024-10-24 15:35:30 +08:00
|
|
|
logger.info({ message: "Monitor went up", monitorId: monitor._id });
|
2024-10-17 12:07:30 +08:00
|
|
|
}
|
|
|
|
|
monitor.status = this.status;
|
|
|
|
|
await monitor.save();
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2024-10-24 15:35:30 +08:00
|
|
|
logger.error({
|
|
|
|
|
message: error.message,
|
|
|
|
|
service: "PageSpeedCheck",
|
|
|
|
|
method: "pre-save",
|
|
|
|
|
stack: error.stack,
|
|
|
|
|
});
|
2024-10-17 12:07:30 +08:00
|
|
|
} finally {
|
|
|
|
|
next();
|
|
|
|
|
}
|
2024-07-30 11:24:25 -07:00
|
|
|
});
|
2024-07-25 14:54:50 -07:00
|
|
|
|
2024-10-30 10:20:42 +08:00
|
|
|
PageSpeedCheck.index({ createdAt: 1 });
|
2025-01-01 13:27:23 -08:00
|
|
|
PageSpeedCheck.index({ monitorId: 1, createdAt: 1 });
|
|
|
|
|
PageSpeedCheck.index({ monitorId: 1, createdAt: -1 });
|
2024-10-30 10:20:42 +08:00
|
|
|
|
2024-10-15 11:57:37 +08:00
|
|
|
export default mongoose.model("PageSpeedCheck", PageSpeedCheck);
|