Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial LogMsg Database Model #1327

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions src/server/models/LogMsg.js
Original file line number Diff line number Diff line change
@@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several imports are not used. Is it planned?

const determineMaxPoints = require('../util/determineMaxPoints');
const log = require('../log');
const { isReadonlyKeywordOrPlusOrMinusToken } = require('typescript');
const LogEmail = require('./LogEmail');

const sqlFile = database.sqlFile;

class LogMsg {
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSDoc should describe each param. Other ones too.

* 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that during testing it needs to be tested if the moment object correctly goes into DB value (and also comes out okay in the other functions to get).

}

/**
* Returns a promise to get all of the logs from the database
* @param conn the connection to be used.
* @returns {Promise.<array.<logMsg>>}
*/
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the SQL may be inclusive not exclusive for the start/end dates.

* 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.<array.<LogMsg>>}
*/
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.<array.<LogMsg>>}
*/
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.<array.<LogMsg>>}
*/
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;
9 changes: 9 additions & 0 deletions src/server/sql/logmsg/create_log_types_enum.sql
Original file line number Diff line number Diff line change
@@ -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 $$;
13 changes: 13 additions & 0 deletions src/server/sql/logmsg/create_logmsg_table.sql
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions src/server/sql/logmsg/get_all_logs.sql
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 12 additions & 0 deletions src/server/sql/logmsg/get_logs_from_dates.sql
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 12 additions & 0 deletions src/server/sql/logmsg/get_logs_from_dates_and_type.sql
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also limits the type.

SELECT
log_type, log_message, log_time
FROM logmsg
WHERE log_type = ${logType}
AND log_time >= COALESCE(${startDate}, '-infinity'::TIMESTAMP)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that we need to think about allowing long time ranges and defaulting to infinity. This, indirectly, applies to getting them all and some others too.

AND log_time <= COALESCE(${endDate}, 'infinity'::TIMESTAMP)
ORDER BY log_time ASC;
10 changes: 10 additions & 0 deletions src/server/sql/logmsg/get_logs_from_type.sql
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is off.

SELECT
log_type, log_message as log_msg, log_time
FROM logmsg
WHERE log_type = ${logType}
ORDER BY log_time ASC;
7 changes: 7 additions & 0 deletions src/server/sql/logmsg/insert_new_log.sql
Original file line number Diff line number Diff line change
@@ -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});
Loading