2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
const { getFormatFromCopyQuery } = require('../utils/query-info');
|
2018-05-26 00:47:41 +08:00
|
|
|
|
|
|
|
module.exports = class StreamCopyMetrics {
|
2019-12-24 01:19:08 +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
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
addSize (size) {
|
2018-05-26 00:47:41 +08:00
|
|
|
this.size += size;
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
addGzipSize (size) {
|
2018-06-05 19:01:15 +08:00
|
|
|
this.gzipSize += size;
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +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
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
_log (timestamp, gzipSize = null, errorMessage = null) {
|
|
|
|
const 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,
|
2020-06-30 23:42:59 +08:00
|
|
|
username: 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;
|
|
|
|
|
2020-07-23 00:29:05 +08:00
|
|
|
this.logger.info({ ingestion: logData }, 'Copy to/from query metrics');
|
2018-05-26 00:47:41 +08:00
|
|
|
}
|
2018-05-26 00:50:56 +08:00
|
|
|
};
|