diff --git a/src/server/models/LogMsg.js b/src/server/models/LogMsg.js new file mode 100644 index 000000000..b0f67007f --- /dev/null +++ b/src/server/models/LogMsg.js @@ -0,0 +1,126 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +const database = require('./database'); +const { mapToObject, threeDHoleAlgorithm } = require('../util'); +const determineMaxPoints = require('../util/determineMaxPoints'); +const log = require('../log'); +const { isReadonlyKeywordOrPlusOrMinusToken } = require('typescript'); +const LogEmail = require('./LogEmail'); + +const sqlFile = database.sqlFile; + +class LogMsg { + /** + * Creates a new log + * @param logType + * @param logMessage + * @param {Moment} logTime + */ + constructor(logType, logMessage, logTime) { + this.logType = logType; + this.logMessage = logMessage; + this.logTime = logTime; + } + + /** + * Creates a new log from data in the row + * @param {*} row The row from which the log is to be created. + * @returns The new log object. + */ + static mapRow(row) { + return new LogMsg(row.logType, row.logMessage, row.logTime); + } + + /** + * Returns a promise to create the logging table + * @param conn the database connection to use + * @returns {Promise.<>} + */ + static createTable(conn) { + return conn.none(sqlFile('logmsg/create_logmsg_table.sql')); + } + + /** + * Returns a promise to create the logMsgType enum. + * @param {*} conn The connection to use. + * @returns {Promise.<>} + */ + static createLogMsgTypeEnum(conn) { + return conn.none(sqlFile('logmsg/create_log_types_enum.sql')); + } + + /** + * Returns a promise to insert this log into the database + * @param conn the database connection to use + * @returns {Promise.<>} + */ + async insert(conn) { + const logMsg = this; + await conn.none(sqlFile('logmsg/insert_new_log.sql'), logMsg); + } + + /** + * Returns a promise to get all of the logs from the database + * @param conn the connection to be used. + * @returns {Promise.>} + */ + static async getAll(conn) { + const rows = await conn.any(sqlFile('logmsg/get_all_logs.sql')); + if (rows.length > 0) { + return rows.map(LogMsg.mapRow); + } + } + + /** + * Returns a promise to get all of the logs in between two dates. + * If no startDate is specified, all logs before the endDate are returned. + * If no endDate is specified, all logs after the startDate are returned. + * @param {Date} startDate + * @param {Date} endDate + * @param conn is the connection to use. + * @returns {Promise.>} + */ + static async getLogsByDateRange(startDate, endDate, conn) { + const rows = await conn.any(sqlFile('logmsg/get_logs_from_dates.sql'), { + startDate: startDate, + endDate: endDate + }); + + return rows.map(LogMsg.mapRow); + } + + /** + * Returns a promise to get all of the logs of a certain type + * @param logType + * @param conn is the connection to use. + * @returns {Promise.>} + */ + static async getLogsByType(logType, conn){ + const rows = await conn.any(sqlFile('logmsg/get_logs_from_type.sql'), {logType: logType}); + + return rows.map(LogMsg.mapRow); + } + + /** + * Returns a promise to get all of the logs in between two dates. + * If no startDate is specified, all logs before the endDate are returned. + * If no endDate is specified, all logs after the startDate are returned. + * @param {Date} startDate + * @param {Date} endDate + * @param logType + * @param conn is the connection to use. + * @returns {Promise.>} + */ + static async getLogsByDateRangeAndType(startDate, endDate, logType, conn) { + const rows = await conn.any(sqlFile('logmsg/get_logs_from_dates_and_type.sql'), { + startDate: startDate, + endDate: endDate, + logType: logType + }); + + return rows.map(LogMsg.mapRow); + } +} +module.exports = LogMsg; \ No newline at end of file diff --git a/src/server/sql/logmsg/create_log_types_enum.sql b/src/server/sql/logmsg/create_log_types_enum.sql new file mode 100644 index 000000000..64df5b2ab --- /dev/null +++ b/src/server/sql/logmsg/create_log_types_enum.sql @@ -0,0 +1,9 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +DO $$ BEGIN + CREATE TYPE log_msg_type AS ENUM('INFO', 'WARN', 'ERROR', 'DEBUG', 'SILENT'); +EXCEPTION + WHEN duplicate_object THEN null; +END $$; \ No newline at end of file diff --git a/src/server/sql/logmsg/create_logmsg_table.sql b/src/server/sql/logmsg/create_logmsg_table.sql new file mode 100644 index 000000000..1e6cfc5d8 --- /dev/null +++ b/src/server/sql/logmsg/create_logmsg_table.sql @@ -0,0 +1,13 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +--create logmsg table +CREATE TABLE IF NOT EXISTS logmsg ( + id SERIAL PRIMARY KEY, + log_type log_msg_type NOT NULL, + log_message TEXT NOT NULL, + log_time TIMESTAMP NOT NULL +); + +-- TODO Consider index optimization for queries \ No newline at end of file diff --git a/src/server/sql/logmsg/get_all_logs.sql b/src/server/sql/logmsg/get_all_logs.sql new file mode 100644 index 000000000..b44910172 --- /dev/null +++ b/src/server/sql/logmsg/get_all_logs.sql @@ -0,0 +1,6 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +-- gets all logs in the database +SELECT * FROM logmsg; \ No newline at end of file diff --git a/src/server/sql/logmsg/get_logs_from_dates.sql b/src/server/sql/logmsg/get_logs_from_dates.sql new file mode 100644 index 000000000..7e20b1350 --- /dev/null +++ b/src/server/sql/logmsg/get_logs_from_dates.sql @@ -0,0 +1,12 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +-- Gets logs in table by date range. This is then ordered by time ascending. +SELECT + -- Short column names for smaller data. + log_type, log_message as log_msg, log_time +FROM logmsg +WHERE log_time >= COALESCE(${startDate}, '-infinity'::TIMESTAMP) + AND log_time <= COALESCE(${endDate}, 'infinity'::TIMESTAMP) +ORDER BY log_time ASC; \ No newline at end of file diff --git a/src/server/sql/logmsg/get_logs_from_dates_and_type.sql b/src/server/sql/logmsg/get_logs_from_dates_and_type.sql new file mode 100644 index 000000000..48450804a --- /dev/null +++ b/src/server/sql/logmsg/get_logs_from_dates_and_type.sql @@ -0,0 +1,12 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +-- Gets logs in table by date range. This is then ordered by time ascending. +SELECT + log_type, log_message, log_time +FROM logmsg +WHERE log_type = ${logType} + AND log_time >= COALESCE(${startDate}, '-infinity'::TIMESTAMP) + AND log_time <= COALESCE(${endDate}, 'infinity'::TIMESTAMP) +ORDER BY log_time ASC; \ No newline at end of file diff --git a/src/server/sql/logmsg/get_logs_from_type.sql b/src/server/sql/logmsg/get_logs_from_type.sql new file mode 100644 index 000000000..0e72fd0d1 --- /dev/null +++ b/src/server/sql/logmsg/get_logs_from_type.sql @@ -0,0 +1,10 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +-- Gets logs in table by date range. This is then ordered by time ascending. +SELECT + log_type, log_message as log_msg, log_time +FROM logmsg +WHERE log_type = ${logType} +ORDER BY log_time ASC; \ No newline at end of file diff --git a/src/server/sql/logmsg/insert_new_log.sql b/src/server/sql/logmsg/insert_new_log.sql new file mode 100644 index 000000000..2c5726ca7 --- /dev/null +++ b/src/server/sql/logmsg/insert_new_log.sql @@ -0,0 +1,7 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +--Inserts a new log into the table +INSERT INTO logmsg (log_type, log_message, log_time) +VALUES (${logType}, ${logMessage}, ${logTime});