asksuite-core
Version:
126 lines (108 loc) • 3.57 kB
JavaScript
const request = require('request');
const AsksuiteProcessingNaturalUtils = require('../AsksuiteProcessingNaturalUtils');
const { ErrorHandlerService } = require('./ErrorHandlerService');
const { CacheRedis } = require('../util/CacheRedis');
const StringUtils = require('../util/StringUtils');
class PrometheusAcessor {
constructor(config, redis) {
const DEFAULT_CACHE_TTL = 60 * 60 * 2; // 2 hours
this.CACHE_PREFIX = 'prometheus';
this.RESOLVER_NAME = 'PROMETHEUS';
this.PROMETHEUS_URL = config.PROMETHEUS_URL;
this.PROMETHEUS_LAYER_PARAMS = config.PROMETHEUS_LAYER_PARAMS;
this.CACHE_TTL = config.PROMETHEUS_CACHE_TTL || DEFAULT_CACHE_TTL;
this.utils = new AsksuiteProcessingNaturalUtils();
this.cacheRedis = this.getCacheConfig(redis);
}
/**
* @param {*} request
* @param {*} originalRequest
* @param {boolean} useCache
*/
async resolveText(request, originalRequest, useCache = false) {
let dialog = null;
const companyId = request.companyId;
const text = originalRequest.text;
const language = request.language;
try {
const languages = request.languages;
const cacheKey = this.generateCacheKey(companyId, language, languages, text);
let dialogFromCache;
if (useCache && this.cacheRedis) {
dialogFromCache = await this.cacheRedis.get(cacheKey);
}
if (dialogFromCache) {
dialog = {
...dialogFromCache,
processingStep: { ...dialogFromCache.processingStep, fromCache: true },
};
return dialog;
}
const response = await this.requestData(this.PROMETHEUS_URL, {
companyId,
language,
sentence: text,
...this.PROMETHEUS_LAYER_PARAMS,
});
const data = JSON.parse(response.body);
const intent = data.intentResult.intent;
if (!intent) {
throw { message: response.statusMessage, code: response.statusCode };
}
dialog = this.utils.findDialogByIntent(request, intent);
dialog.processingStep = {
...dialog.processingStep,
text,
language,
resolver: this.RESOLVER_NAME,
fromCache: false,
...data,
};
if (useCache && this.cacheRedis) {
await this.cacheRedis.set(cacheKey, dialog);
}
} catch (e) {
const errorData = ErrorHandlerService.accessorErrorHandler(e);
dialog = {
processingStep: {
text,
language,
fallbackReason: 'intent_not_found',
intentFound: null,
resolver: this.RESOLVER_NAME,
errorData,
},
};
console.log('[PrometheusAccessor error]', e);
}
return dialog;
}
requestData(url, params) {
return new Promise((resolve, reject) => {
request.get(url, { qs: params, timeout: 20 * 1000 }, (err, res) => {
if (err) {
reject(err);
}
resolve(res);
});
});
}
generateCacheKey(companyId, language, languages, sentence) {
const resolvedLanguage = this.utils.resolveLanguage(language, languages);
const normalizedSentence = StringUtils.normalizeText(sentence);
const sentenceHash = StringUtils.generateHash(normalizedSentence);
return `${companyId}:${resolvedLanguage}:${sentenceHash}`;
}
getCacheConfig(redis) {
if (!redis) {
return null;
}
return new CacheRedis({
client: redis,
prefixKey: this.CACHE_PREFIX,
expirationTime: this.CACHE_TTL,
isJson: true,
});
}
}
module.exports = PrometheusAcessor;