2018-05-26 00:47:41 +08:00
|
|
|
const { getFormatFromCopyQuery } = require('../utils/query_info');
|
|
|
|
|
|
|
|
module.exports = class StreamCopyMetrics {
|
2018-06-05 00:08:34 +08:00
|
|
|
constructor(logger, type, sql, user, gzip = null) {
|
2018-05-26 00:47:41 +08:00
|
|
|
this.logger = logger;
|
|
|
|
|
2018-06-05 00:08:34 +08:00
|
|
|
this.type = type;
|
2018-05-26 00:47:41 +08:00
|
|
|
this.format = getFormatFromCopyQuery(sql);
|
|
|
|
this.gzip = gzip;
|
2018-06-05 00:08:34 +08:00
|
|
|
this.username = user;
|
2018-05-26 00:47:41 +08:00
|
|
|
this.size = 0;
|
|
|
|
this.rows = 0;
|
|
|
|
|
2018-06-05 00:08:34 +08:00
|
|
|
this.startTime = new Date();
|
2018-05-26 00:50:56 +08:00
|
|
|
this.endTime = null;
|
|
|
|
this.time = null;
|
2018-06-05 00:08:34 +08:00
|
|
|
|
|
|
|
this.error = null;
|
2018-05-26 00:47:41 +08:00
|
|
|
}
|
|
|
|
|
2018-06-05 00:08:34 +08:00
|
|
|
addSize(size) {
|
2018-05-26 00:47:41 +08:00
|
|
|
this.size += size;
|
|
|
|
}
|
|
|
|
|
2018-06-05 00:08:34 +08:00
|
|
|
end(rows = null, error = null) {
|
|
|
|
if (Number.isInteger(rows)) {
|
|
|
|
this.rows = rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
this.error = error;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.endTime = new Date();
|
|
|
|
this.time = (this.endTime.getTime() - this.startTime.getTime()) / 1000;
|
|
|
|
|
|
|
|
this._log({
|
|
|
|
timestamp: this.startTime.toISOString(),
|
|
|
|
errorMessage: this.error ? this.error.message : null
|
|
|
|
})
|
|
|
|
}
|
2018-05-26 00:47:41 +08:00
|
|
|
|
2018-06-05 00:08:34 +08:00
|
|
|
_log({timestamp, errorMessage = null}) {
|
|
|
|
let logData = {
|
2018-05-26 00:47:41 +08:00
|
|
|
type: this.type,
|
|
|
|
format: this.format,
|
|
|
|
size: this.size,
|
|
|
|
rows: this.rows,
|
2018-06-05 00:08:34 +08:00
|
|
|
gzip: this.gzip,
|
|
|
|
username: this.username,
|
|
|
|
time: this.time,
|
|
|
|
timestamp
|
|
|
|
};
|
|
|
|
|
|
|
|
if (errorMessage) {
|
|
|
|
logData.error = errorMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.info(logData);
|
2018-05-26 00:47:41 +08:00
|
|
|
}
|
2018-05-26 00:50:56 +08:00
|
|
|
};
|