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;
|
2018-06-05 19:01:15 +08:00
|
|
|
this.gzipSize = 0;
|
2018-05-26 00:47:41 +08:00
|
|
|
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-06-05 18:36:36 +08:00
|
|
|
|
|
|
|
this.ended = false;
|
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 19:01:15 +08:00
|
|
|
addGzipSize(size) {
|
|
|
|
this.gzipSize += size;
|
|
|
|
}
|
|
|
|
|
2018-06-05 00:08:34 +08:00
|
|
|
end(rows = null, error = null) {
|
2018-06-05 18:36:36 +08:00
|
|
|
if (this.ended) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-05 19:01:15 +08:00
|
|
|
|
2018-06-05 18:36:36 +08:00
|
|
|
this.ended = true;
|
|
|
|
|
2018-06-05 00:08:34 +08:00
|
|
|
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;
|
|
|
|
|
2018-06-05 00:15:00 +08:00
|
|
|
this._log(
|
|
|
|
this.startTime.toISOString(),
|
2018-06-05 19:01:15 +08:00
|
|
|
this.gzip && this.gzipSize ? this.gzipSize : null,
|
2018-06-05 00:15:00 +08:00
|
|
|
this.error ? this.error.message : null
|
|
|
|
);
|
2018-06-05 00:08:34 +08:00
|
|
|
}
|
2018-05-26 00:47:41 +08:00
|
|
|
|
2018-06-05 19:01:15 +08:00
|
|
|
_log(timestamp, gzipSize = null, errorMessage = null) {
|
2018-06-05 00:08:34 +08:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-06-05 19:01:15 +08:00
|
|
|
if (gzipSize) {
|
|
|
|
logData.gzipSize = gzipSize;
|
|
|
|
}
|
|
|
|
|
2018-06-05 00:08:34 +08:00
|
|
|
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
|
|
|
};
|