adding format to copy metrics

This commit is contained in:
Simon Martín 2018-05-21 19:13:44 +02:00
parent 816779fefd
commit 81be15fbc3
4 changed files with 94 additions and 0 deletions

View File

@ -8,6 +8,7 @@ const timeoutLimitsMiddleware = require('../middlewares/timeout-limits');
const { initializeProfilerMiddleware } = require('../middlewares/profiler');
const rateLimitsMiddleware = require('../middlewares/rate-limit');
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimitsMiddleware;
const { getFormatFromCopyQuery } = require('../utils/query_info');
const zlib = require('zlib');
const PSQL = require('cartodb-psql');
@ -157,6 +158,7 @@ CopyController.prototype.responseCopyFrom = function (req, res, next) {
if (req.profiler) {
const copyFromMetrics = {
size: res.locals.copyFromSize, //bytes
format: getFormatFromCopyQuery(req.query.sql),
time: res.body.time, //seconds
total_rows: res.body.total_rows,
gzip: req.get('content-encoding') === 'gzip'

21
app/utils/query_info.js Normal file
View File

@ -0,0 +1,21 @@
const COPY_FORMATS = ['TEXT', 'CSV', 'BINARY'];
module.exports = {
getFormatFromCopyQuery(copyQuery) {
let format = 'TEXT'; // Postgres default format
copyQuery = copyQuery.toUpperCase();
if (!copyQuery.toUpperCase().startsWith("COPY ")) {
return false;
}
const regex = /(\bFORMAT\s+)(\w+)/;
const result = regex.exec(copyQuery);
if (result && result.length >= 3 && COPY_FORMATS.includes(result[2])) {
format = result[2];
}
return format;
}
};

View File

@ -19,6 +19,17 @@ describe('copy-endpoints', function() {
const response = JSON.parse(res.body);
assert.equal(!!response.time, true);
assert.strictEqual(response.total_rows, 6);
assert.ok(res.headers['x-sqlapi-profiler']);
const headers = JSON.parse(res.headers['x-sqlapi-profiler']);
assert.ok(headers.copyFrom);
const metrics = headers.copyFrom;
assert.equal(metrics.size, 57);
assert.equal(metrics.format, 'CSV');
assert.equal(metrics.time, response.time);
assert.equal(metrics.total_rows, response.total_rows);
assert.equal(metrics.gzip, false);
done();
});
});
@ -114,6 +125,17 @@ describe('copy-endpoints', function() {
const response = JSON.parse(res.body);
assert.equal(!!response.time, true);
assert.strictEqual(response.total_rows, 6);
assert.ok(res.headers['x-sqlapi-profiler']);
const headers = JSON.parse(res.headers['x-sqlapi-profiler']);
assert.ok(headers.copyFrom);
const metrics = headers.copyFrom;
assert.equal(metrics.size, 57);
assert.equal(metrics.format, 'CSV');
assert.equal(metrics.time, response.time);
assert.equal(metrics.total_rows, response.total_rows);
assert.equal(metrics.gzip, true);
done();
});
});

View File

@ -0,0 +1,49 @@
const assert = require('assert');
const queryInfo = require('../../app/utils/query_info');
describe('query info', function () {
describe('copy format', function() {
describe('csv', function() {
const csvValidQueries = [
"COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)",
"COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)",
"COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV , DELIMITER ',', HEADER true)",
"COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV)",
];
csvValidQueries.forEach(query => {
it(query, function() {
const result = queryInfo.getFormatFromCopyQuery(query);
assert.equal(result, 'CSV');
});
});
});
describe('text', function() {
const csvValidQueries = [
"COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT TEXT)",
"COPY copy_endpoints_test (id, name) FROM STDIN",
];
csvValidQueries.forEach(query => {
it(query, function() {
const result = queryInfo.getFormatFromCopyQuery(query);
assert.equal(result, 'TEXT');
});
});
});
describe('text', function() {
const csvValidQueries = [
"COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT BINARY)",
];
csvValidQueries.forEach(query => {
it(query, function() {
const result = queryInfo.getFormatFromCopyQuery(query);
assert.equal(result, 'BINARY');
});
});
});
});
});