|
| 1 | +const { MonitorType } = require("./monitor-type"); |
| 2 | +const { log, UP } = require("../../src/util"); |
| 3 | +const dayjs = require("dayjs"); |
| 4 | +const mssql = require("mssql"); |
| 5 | +const { ConditionVariable } = require("../monitor-conditions/variables"); |
| 6 | +const { defaultStringOperators } = require("../monitor-conditions/operators"); |
| 7 | +const { |
| 8 | + ConditionExpressionGroup, |
| 9 | +} = require("../monitor-conditions/expression"); |
| 10 | +const { evaluateExpressionGroup } = require("../monitor-conditions/evaluator"); |
| 11 | + |
| 12 | +class MssqlMonitorType extends MonitorType { |
| 13 | + name = "sqlserver"; |
| 14 | + |
| 15 | + supportsConditions = true; |
| 16 | + conditionVariables = [ |
| 17 | + new ConditionVariable("result", defaultStringOperators), |
| 18 | + ]; |
| 19 | + |
| 20 | + /** |
| 21 | + * @inheritdoc |
| 22 | + */ |
| 23 | + async check(monitor, heartbeat, _server) { |
| 24 | + let startTime = dayjs().valueOf(); |
| 25 | + |
| 26 | + let query = monitor.databaseQuery; |
| 27 | + // No query provided by user, use SELECT 1 |
| 28 | + if (!query || (typeof query === "string" && query.trim() === "")) { |
| 29 | + query = "SELECT 1"; |
| 30 | + } |
| 31 | + |
| 32 | + let result; |
| 33 | + try { |
| 34 | + result = await this.mssqlQuery( |
| 35 | + monitor.databaseConnectionString, |
| 36 | + query |
| 37 | + ); |
| 38 | + } catch (error) { |
| 39 | + log.error("sqlserver", "Database query failed:", error.message); |
| 40 | + throw new Error( |
| 41 | + `Database connection/query failed: ${error.message}` |
| 42 | + ); |
| 43 | + } finally { |
| 44 | + heartbeat.ping = dayjs().valueOf() - startTime; |
| 45 | + } |
| 46 | + |
| 47 | + const conditions = ConditionExpressionGroup.fromMonitor(monitor); |
| 48 | + const handleConditions = (data) => |
| 49 | + conditions ? evaluateExpressionGroup(conditions, data) : true; |
| 50 | + |
| 51 | + // Since result is now a single value, pass it directly to conditions |
| 52 | + const conditionsResult = handleConditions({ result: String(result) }); |
| 53 | + |
| 54 | + if (!conditionsResult) { |
| 55 | + throw new Error( |
| 56 | + `Query result did not meet the specified conditions (${result})` |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + heartbeat.msg = ""; |
| 61 | + heartbeat.status = UP; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Run a query on MSSQL server |
| 66 | + * @param {string} connectionString The database connection string |
| 67 | + * @param {string} query The query to validate the database with |
| 68 | + * @returns {Promise<any>} Single value from the first column of the first row |
| 69 | + */ |
| 70 | + async mssqlQuery(connectionString, query) { |
| 71 | + let pool; |
| 72 | + try { |
| 73 | + pool = new mssql.ConnectionPool(connectionString); |
| 74 | + await pool.connect(); |
| 75 | + const result = await pool.request().query(query); |
| 76 | + |
| 77 | + // Check if we have results |
| 78 | + if (!result.recordset || result.recordset.length === 0) { |
| 79 | + throw new Error("Query returned no results"); |
| 80 | + } |
| 81 | + |
| 82 | + // Check if we have multiple rows |
| 83 | + if (result.recordset.length > 1) { |
| 84 | + throw new Error( |
| 85 | + "Multiple values were found, expected only one value" |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + const firstRow = result.recordset[0]; |
| 90 | + const columnNames = Object.keys(firstRow); |
| 91 | + |
| 92 | + // Check if we have multiple columns |
| 93 | + if (columnNames.length > 1) { |
| 94 | + throw new Error( |
| 95 | + "Multiple columns were found, expected only one value" |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + // Return the single value from the first (and only) column |
| 100 | + return firstRow[columnNames[0]]; |
| 101 | + } catch (err) { |
| 102 | + log.debug( |
| 103 | + "sqlserver", |
| 104 | + "Error caught in the query execution.", |
| 105 | + err.message |
| 106 | + ); |
| 107 | + throw err; |
| 108 | + } finally { |
| 109 | + if (pool) { |
| 110 | + await pool.close(); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +module.exports = { |
| 117 | + MssqlMonitorType, |
| 118 | +}; |
0 commit comments