-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
160 lines (139 loc) · 3.95 KB
/
Copy pathdb.js
File metadata and controls
160 lines (139 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const mysql = require('mysql2');
const ErrorLogModel = require('./models/errorLogModel');
const util = require('./services/utilService');
const thread = require('./services/threadService');
const Sequelize = require('sequelize');
const _sequealize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, {
dialect:'mysql',
host:process.env.DB_HOST,
port:3306
});
const ERRORLOGS_PROCEDURE_NAME = 'USP_ERRORLOGS_INSERT';
const STATISTICS_PROCEDURE_NAME = 'USP_OperationLogs_Insert';
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
connectionLimit: 10,
});
class OperationLogs{
constructor(procedureName, sqlCall, ticket, is_checkpoint){
this.ticket = ticket;
this.procedureName = procedureName;
this.startTime = performance.now();
this.timeElapsed = null;
this.successfully = false;
this.sqlCall = sqlCall;
this.ticket = ticket;
this.is_checkpoint = is_checkpoint;
}
async commit(successfully){
let finish = performance.now();
this.timeElapsed = finish - this.startTime;
finish = null;
this.successfully = successfully;
await operationLogsInsert(this.ticket,
this.procedureName,
this.timeElapsed,
this.sqlCall,
this.successfully,
this.is_checkpoint);
}
}
async function operationLogsInsert (ticket,
procedureName,
timeElapsed,
sqlCall,
successfully,
is_checkpoint){
await executeProcedure(STATISTICS_PROCEDURE_NAME, [
procedureName,
timeElapsed,
sqlCall,
ticket,
successfully,
is_checkpoint
]);
}
async function errorLogInsert (errorLogModel){
let procedureName = ERRORLOGS_PROCEDURE_NAME;
const {
errorMessage,
errorCode,
errorSeverity,
errorSource,
errorDetails,
userID,
ipAddress,
ticket
} = errorLogModel;
await executeProcedure(procedureName, [
errorMessage,
errorCode,
errorSeverity,
errorSource,
errorDetails,
userID,
ipAddress,
ticket
]);
}
async function executeProcedure(procedureName, params = [], ticket = 'Default', isLogCheckpoint = false){
const placeholders = params?.map(() => '?').join(',');
const callProcedure = `CALL ${procedureName}(${placeholders})`;
let successfully = true;
const formattedParams = params?.map(param => {
let paramType = util.getParameterType(param);
if (paramType === 'int') {
return parseInt(param, 10);
} else if (paramType === 'double') {
return parseFloat(param);
} else if (paramType === 'varchar') {
return param;
} else if (paramType === 'datetime') {
return util.formatDatetime(param);
} else if (paramType === 'boolean') {
return param ? 1 : 0; // Convertendo para 0 ou 1
} else if (paramType === 'json') {
return util.formatJSON(param);
}
return param;
});
const operationLog = new OperationLogs(procedureName,
`CALL ${procedureName}(${formattedParams?.join(',')})`,
ticket,
isLogCheckpoint);
try {
const results = await pool.promise().query(callProcedure, formattedParams);
return results;
} catch (error) {
const errorLog = new ErrorLogModel(
error.message,
error.errno ?? 0,
3,
'SQL Message: ' + error.sqlmessage + '|SQL: ' + error.sql + '|Stack: ' + error.stack,
null,
null,
null,
ticket
);
await errorLogInsert(errorLog);
successfully = false;
throw error;
}
finally{
// operation logs
if(procedureName != ERRORLOGS_PROCEDURE_NAME && procedureName != STATISTICS_PROCEDURE_NAME){
await operationLog.commit(successfully);
}
}
}
module.exports = {
OperationLogs,
pool,
errorLogInsert,
executeProcedure,
operationLogsInsert,
_sequealize
};