@mediarithmics/plugins-nodejs-sdk
Version:
This is the mediarithmics nodejs to help plugin developers bootstrapping their plugin without having to deal with most of the plugin boilerplate
86 lines • 4.65 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActivityAnalyzerPlugin = void 0;
const lodash_1 = __importDefault(require("lodash"));
const BasePlugin_1 = require("../common/BasePlugin");
class ActivityAnalyzerPlugin extends BasePlugin_1.BasePlugin {
constructor(enableThrottling = false) {
super(enableThrottling);
// We init the specific route to listen for activity analysis requests
this.initActivityAnalysis();
this.setErrorHandler();
}
// Helper to fetch the activity analyzer resource with caching
async fetchActivityAnalyzer(activityAnalyzerId) {
const activityAnalyzerResponse = await super.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/activity_analyzers/${activityAnalyzerId}`);
this.logger.debug(`Fetched Activity Analyzer: ${activityAnalyzerId} - ${JSON.stringify(activityAnalyzerResponse.data)}`);
return activityAnalyzerResponse.data;
}
// Method to build an instance context
// To be overriden to get a cutom behavior
// Helper to fetch the activity analyzer resource with caching
async fetchActivityAnalyzerProperties(activityAnalyzerId) {
const activityAnalyzerPropertyResponse = await super.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/activity_analyzers/${activityAnalyzerId}/properties`);
this.logger.debug(`Fetched Activity Analyzer Properties: ${activityAnalyzerId} - ${JSON.stringify(activityAnalyzerPropertyResponse.data)}`);
return activityAnalyzerPropertyResponse.data;
}
// Method to process an Activity Analysis
// This is a default provided implementation
async instanceContextBuilder(activityAnalyzerId) {
const activityAnalyzerP = this.fetchActivityAnalyzer(activityAnalyzerId);
const activityAnalyzerPropsP = this.fetchActivityAnalyzerProperties(activityAnalyzerId);
const results = await Promise.all([activityAnalyzerP, activityAnalyzerPropsP]);
const activityAnalyzer = results[0];
const activityAnalyzerProps = results[1];
return {
properties: new BasePlugin_1.PropertiesWrapper(activityAnalyzerProps),
activityAnalyzer: activityAnalyzer,
};
}
async getInstanceContext(activityAnalyzerId) {
if (!this.pluginCache.get(activityAnalyzerId)) {
void this.pluginCache.put(activityAnalyzerId, this.instanceContextBuilder(activityAnalyzerId).catch((err) => {
this.logger.error(`Error while caching instance context: ${err.message}`);
this.pluginCache.del(activityAnalyzerId);
throw err;
}), this.getInstanceContextCacheExpiration());
}
return this.pluginCache.get(activityAnalyzerId);
}
initActivityAnalysis() {
this.app.post('/v1/activity_analysis', this.asyncMiddleware(async (req, res) => {
if (!this.httpIsReady()) {
const msg = {
error: 'Plugin not initialized',
};
this.logger.error('POST /v1/activity_analysis : %s', JSON.stringify(msg));
return res.status(500).json(msg);
}
else if (!req.body || lodash_1.default.isEmpty(req.body)) {
const msg = {
error: 'Missing request body',
};
this.logger.error('POST /v1/activity_analysis : %s', JSON.stringify(msg));
return res.status(500).json(msg);
}
else {
this.logger.debug(`POST /v1/activity_analysis ${JSON.stringify(req.body)}`);
const activityAnalyzerRequest = req.body;
if (!this.onActivityAnalysis) {
const errMsg = 'No Activity Analyzer listener registered!';
this.logger.error(errMsg);
return res.status(500).json({ error: errMsg });
}
const instanceContext = await this.getInstanceContext(activityAnalyzerRequest.activity_analyzer_id);
const pluginResponse = await this.onActivityAnalysis(activityAnalyzerRequest, instanceContext);
this.logger.debug(`Returning: ${JSON.stringify(pluginResponse)}`);
return res.status(200).send(JSON.stringify(pluginResponse));
}
}));
}
}
exports.ActivityAnalyzerPlugin = ActivityAnalyzerPlugin;
//# sourceMappingURL=ActivityAnalyzerBasePlugin.js.map