2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-26 00:47:41 +08:00
|
|
|
const { getFormatFromCopyQuery } = require('../utils/query_info');
|
|
|
|
|
|
|
|
module.exports = class StreamCopyMetrics {
|
2018-08-14 21:14:22 +08:00
|
|
|
constructor(logger, type, sql, user, isGzip = false) {
|
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);
|
2019-02-27 16:02:49 +08:00
|
|
|
this.sql = sql;
|
2018-06-15 00:29:03 +08:00
|
|
|
this.isGzip = isGzip;
|
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
|
|
|
|
2018-06-15 00:40:06 +08:00
|
|
|
this.success = true;
|
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-15 00:29:03 +08:00
|
|
|
this.isGzip && 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-15 00:29:03 +08:00
|
|
|
gzip: this.isGzip,
|
2018-06-15 19:30:42 +08:00
|
|
|
'cdb-user': this.username,
|
2018-06-05 00:08:34 +08:00
|
|
|
time: this.time,
|
2019-02-27 16:02:49 +08:00
|
|
|
timestamp,
|
|
|
|
sql: this.sql
|
2018-06-05 00:08:34 +08:00
|
|
|
};
|
|
|
|
|
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;
|
2018-06-15 00:40:06 +08:00
|
|
|
this.success = false;
|
2018-06-05 00:08:34 +08:00
|
|
|
}
|
|
|
|
|
2018-06-15 00:40:06 +08:00
|
|
|
logData.success = this.success;
|
|
|
|
|
2018-06-05 00:08:34 +08:00
|
|
|
this.logger.info(logData);
|
2018-05-26 00:47:41 +08:00
|
|
|
}
|
2018-05-26 00:50:56 +08:00
|
|
|
};
|