2016-10-20 20:02:13 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('underscore');
|
2016-04-14 23:09:07 +08:00
|
|
|
var camshaft = require('camshaft');
|
2016-09-23 23:11:04 +08:00
|
|
|
var fs = require('fs');
|
2016-04-14 23:09:07 +08:00
|
|
|
|
2016-10-11 21:46:11 +08:00
|
|
|
var REDIS_LIMITS = {
|
|
|
|
DB: 5,
|
|
|
|
PREFIX: 'limits:analyses:' // + username
|
|
|
|
};
|
|
|
|
|
|
|
|
function AnalysisBackend (metadataBackend, options) {
|
|
|
|
this.metadataBackend = metadataBackend;
|
2016-10-20 20:02:13 +08:00
|
|
|
this.options = options || {};
|
2016-11-11 01:41:59 +08:00
|
|
|
this.options.limits = this.options.limits || {};
|
2016-10-20 20:02:13 +08:00
|
|
|
this.setBatchConfig(this.options.batch);
|
|
|
|
this.setLoggerConfig(this.options.logger);
|
2016-09-23 23:11:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AnalysisBackend;
|
|
|
|
|
|
|
|
AnalysisBackend.prototype.setBatchConfig = function (options) {
|
|
|
|
var batchConfig = options || {};
|
2016-04-14 23:25:08 +08:00
|
|
|
batchConfig.endpoint = batchConfig.endpoint || 'http://127.0.0.1:8080/api/v1/sql/job';
|
|
|
|
batchConfig.inlineExecution = batchConfig.inlineExecution || false;
|
2016-04-20 22:36:29 +08:00
|
|
|
batchConfig.hostHeaderTemplate = batchConfig.hostHeaderTemplate || '{{=it.username}}.localhost.lan';
|
2016-04-14 23:25:08 +08:00
|
|
|
this.batchConfig = batchConfig;
|
2016-09-23 23:11:04 +08:00
|
|
|
};
|
2016-04-14 23:09:07 +08:00
|
|
|
|
2016-09-23 23:11:04 +08:00
|
|
|
AnalysisBackend.prototype.setLoggerConfig = function (options) {
|
2016-10-11 22:01:43 +08:00
|
|
|
this.loggerConfig = options || {};
|
2016-09-26 23:39:27 +08:00
|
|
|
|
|
|
|
if (this.loggerConfig.filename) {
|
|
|
|
this.stream = fs.createWriteStream(this.loggerConfig.filename, { flags: 'a', encoding: 'utf8' });
|
|
|
|
|
|
|
|
process.on('SIGHUP', function () {
|
|
|
|
if (this.stream) {
|
|
|
|
this.stream.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stream = fs.createWriteStream(this.loggerConfig.filename, { flags: 'a', encoding: 'utf8' });
|
|
|
|
}.bind(this));
|
|
|
|
}
|
2016-09-23 23:11:04 +08:00
|
|
|
};
|
2016-04-14 23:09:07 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
AnalysisBackend.prototype.create = function (analysisConfiguration, analysisDefinition, callback) {
|
2016-04-14 23:25:08 +08:00
|
|
|
analysisConfiguration.batch.endpoint = this.batchConfig.endpoint;
|
|
|
|
analysisConfiguration.batch.inlineExecution = this.batchConfig.inlineExecution;
|
2016-04-20 22:36:29 +08:00
|
|
|
analysisConfiguration.batch.hostHeaderTemplate = this.batchConfig.hostHeaderTemplate;
|
2016-09-26 23:39:27 +08:00
|
|
|
|
2016-09-23 23:11:04 +08:00
|
|
|
analysisConfiguration.logger = {
|
2016-09-26 23:39:27 +08:00
|
|
|
stream: this.stream ? this.stream : process.stdout
|
2016-09-23 23:11:04 +08:00
|
|
|
};
|
2016-04-14 23:25:08 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
this.getAnalysesLimits(analysisConfiguration.user, function (err, limits) {
|
2019-10-25 00:38:37 +08:00
|
|
|
if (err) {}
|
2016-10-11 21:46:11 +08:00
|
|
|
analysisConfiguration.limits = limits || {};
|
|
|
|
camshaft.create(analysisConfiguration, analysisDefinition, callback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
AnalysisBackend.prototype.getAnalysesLimits = function (username, callback) {
|
2016-10-20 20:02:13 +08:00
|
|
|
var self = this;
|
2016-11-11 01:41:59 +08:00
|
|
|
|
|
|
|
var analysesLimits = {
|
|
|
|
analyses: {
|
|
|
|
// buffer: {
|
|
|
|
// timeout: 1000,
|
|
|
|
// maxNumberOfRows: 1e6
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
Object.keys(self.options.limits).forEach(function (analysisTypeOrTag) {
|
2016-11-11 01:41:59 +08:00
|
|
|
analysesLimits.analyses[analysisTypeOrTag] = _.extend({}, self.options.limits[analysisTypeOrTag]);
|
|
|
|
});
|
|
|
|
|
2016-10-11 21:46:11 +08:00
|
|
|
var analysesLimitsKey = REDIS_LIMITS.PREFIX + username;
|
2019-10-22 01:07:24 +08:00
|
|
|
this.metadataBackend.redisCmd(REDIS_LIMITS.DB, 'HGETALL', [analysesLimitsKey], function (err, analysesTimeouts) {
|
2019-10-25 00:38:37 +08:00
|
|
|
if (err) {}
|
2016-11-11 01:41:59 +08:00
|
|
|
// analysesTimeouts wil be something like: { moran: 3000, intersection: 5000 }
|
2016-10-11 21:46:11 +08:00
|
|
|
analysesTimeouts = analysesTimeouts || {};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
Object.keys(analysesTimeouts).forEach(function (analysisType) {
|
2016-11-11 01:41:59 +08:00
|
|
|
analysesLimits.analyses[analysisType] = _.defaults(
|
|
|
|
{
|
|
|
|
timeout: Number.isFinite(+analysesTimeouts[analysisType]) ? +analysesTimeouts[analysisType] : 0
|
|
|
|
},
|
|
|
|
analysesLimits.analyses[analysisType]
|
|
|
|
);
|
2016-10-11 21:46:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null, analysesLimits);
|
|
|
|
});
|
2016-04-14 23:09:07 +08:00
|
|
|
};
|