errsole-mysql
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -56,3 +56,4 @@ /** | ||
this.pool = mysql.createPool(options); | ||
this.logBatch = []; | ||
this.pendingLogs = []; | ||
this.batchSize = 100; | ||
@@ -234,17 +235,23 @@ this.flushInterval = 1000; | ||
/** | ||
* Adds log entries to the database. | ||
* Adds log entries to the pending logs. | ||
* | ||
* @async | ||
* @function postLogs | ||
* @param {Log[]} logEntries - An array of log entries to be added to the database. | ||
* @returns {Promise<{}>} - A Promise that resolves with an empty object. | ||
* @throws {Error} - Throws an error if the operation fails. | ||
* @param {Log[]} logEntries - An array of log entries to be added to the pending logs. | ||
* @returns {{}} - A Promise that resolves with an empty object. | ||
*/ | ||
postLogs (logEntry) { | ||
this.logBatch.push(...logEntry); | ||
if (this.logBatch.length >= this.batchSize) { | ||
this.pendingLogs.push(...logEntry); | ||
if (this.pendingLogs.length >= this.batchSize) { | ||
this.flushLogs(); | ||
} | ||
return {}; | ||
} | ||
/** | ||
* Flushes pending logs to the database. | ||
* | ||
* @async | ||
* @function flushLogs | ||
* @returns {Promise<{}>} - A Promise that resolves with an empty object. | ||
* @throws {Error} - Throws an error if the operation fails. | ||
*/ | ||
async flushLogs () { | ||
@@ -255,3 +262,3 @@ while (this.isConnectionInProgress) { | ||
const logsToPost = this.logBatch.splice(0, this.logBatch.length); | ||
const logsToPost = this.pendingLogs.splice(0, this.pendingLogs.length); | ||
const values = logsToPost.map(logEntry => [ | ||
@@ -269,10 +276,6 @@ new Date(logEntry.timestamp), | ||
this.pool.getConnection((err, connection) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
if (err) return reject(err); | ||
connection.query('INSERT INTO errsole_logs_v1 (timestamp, hostname, pid, source, level, message, meta) VALUES ?', [values], () => { | ||
connection.release(); | ||
if (err) { | ||
return reject(err); | ||
} | ||
if (err) return reject(err); | ||
resolve({}); | ||
@@ -295,3 +298,2 @@ }); | ||
const DEFAULT_LOGS_LIMIT = 100; | ||
filters.limit = filters.limit || DEFAULT_LOGS_LIMIT; | ||
@@ -378,3 +380,2 @@ | ||
const DEFAULT_LOGS_LIMIT = 100; | ||
filters.limit = filters.limit || DEFAULT_LOGS_LIMIT; | ||
@@ -411,2 +412,3 @@ | ||
} | ||
if (filters.lt_id) { | ||
@@ -417,3 +419,4 @@ whereClauses.push('id < ?'); | ||
shouldReverse = true; | ||
} else if (filters.gt_id) { | ||
} | ||
if (filters.gt_id) { | ||
whereClauses.push('id > ?'); | ||
@@ -423,3 +426,5 @@ values.push(filters.gt_id); | ||
shouldReverse = false; | ||
} else if (filters.lte_timestamp || filters.gte_timestamp) { | ||
} | ||
if (filters.lte_timestamp || filters.gte_timestamp) { | ||
if (filters.lte_timestamp) { | ||
@@ -437,2 +442,16 @@ whereClauses.push('timestamp <= ?'); | ||
} | ||
if (filters.lte_timestamp && !filters.gte_timestamp) { | ||
filters.lte_timestamp = new Date(filters.lte_timestamp); | ||
const gteTimestamp = new Date(filters.lte_timestamp.getTime() - 24 * 60 * 60 * 1000); | ||
whereClauses.push('timestamp >= ?'); | ||
values.push(gteTimestamp); | ||
filters.gte_timestamp = gteTimestamp; | ||
} | ||
if (filters.gte_timestamp && !filters.lte_timestamp) { | ||
filters.gte_timestamp = new Date(filters.gte_timestamp); | ||
const lteTimestamp = new Date(filters.gte_timestamp.getTime() + 24 * 60 * 60 * 1000); | ||
whereClauses.push('timestamp <= ?'); | ||
values.push(lteTimestamp); | ||
filters.lte_timestamp = lteTimestamp; | ||
} | ||
} | ||
@@ -448,3 +467,3 @@ | ||
if (shouldReverse) results.reverse(); | ||
resolve({ items: results }); | ||
resolve({ items: results, filters }); | ||
}); | ||
@@ -451,0 +470,0 @@ }); |
{ | ||
"name": "errsole-mysql", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "MySQL storage plugin for Errsole", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "jest --coverage", | ||
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls" | ||
}, | ||
@@ -25,3 +26,8 @@ "keywords": [ | ||
"lib/index.js" | ||
] | ||
], | ||
"devDependencies": { | ||
"@jest/globals": "^29.7.0", | ||
"coveralls": "^3.1.1", | ||
"jest": "^29.7.0" | ||
} | ||
} |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
26602
673
1
3