n8n
Version:
n8n Workflow Automation Tool
108 lines • 5.29 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventsService = void 0;
const events_1 = require("events");
const typedi_1 = require("typedi");
const workflowStatistics_repository_1 = require("../databases/repositories/workflowStatistics.repository");
const user_service_1 = require("../services/user.service");
const Logger_1 = require("../Logger");
const ownership_service_1 = require("./ownership.service");
let EventsService = class EventsService extends events_1.EventEmitter {
constructor(logger, repository, ownershipService) {
super({ captureRejections: true });
this.logger = logger;
this.repository = repository;
this.ownershipService = ownershipService;
if ('SKIP_STATISTICS_EVENTS' in process.env)
return;
this.on('nodeFetchedData', async (workflowId, node) => await this.nodeFetchedData(workflowId, node));
this.on('workflowExecutionCompleted', async (workflowData, runData) => await this.workflowExecutionCompleted(workflowData, runData));
}
async workflowExecutionCompleted(workflowData, runData) {
var _a;
const finished = runData.finished ? runData.finished : false;
const manual = runData.mode === 'manual';
let name;
if (finished) {
if (manual)
name = "manual_success";
else
name = "production_success";
}
else {
if (manual)
name = "manual_error";
else
name = "production_error";
}
const workflowId = workflowData.id;
if (!workflowId)
return;
try {
const upsertResult = await this.repository.upsertWorkflowStatistics(name, workflowId);
if (name === "production_success" && upsertResult === 'insert') {
const project = await typedi_1.Container.get(ownership_service_1.OwnershipService).getWorkflowProjectCached(workflowId);
if (project.type === 'personal') {
const owner = await typedi_1.Container.get(ownership_service_1.OwnershipService).getProjectOwnerCached(project.id);
const metrics = {
project_id: project.id,
workflow_id: workflowId,
user_id: owner === null || owner === void 0 ? void 0 : owner.id,
};
if (owner && !((_a = owner.settings) === null || _a === void 0 ? void 0 : _a.userActivated)) {
await typedi_1.Container.get(user_service_1.UserService).updateSettings(owner.id, {
firstSuccessfulWorkflowId: workflowId,
userActivated: true,
});
}
this.emit('telemetry.onFirstProductionWorkflowSuccess', metrics);
}
}
}
catch (error) {
this.logger.verbose('Unable to fire first workflow success telemetry event');
}
}
async nodeFetchedData(workflowId, node) {
if (!workflowId)
return;
const insertResult = await this.repository.insertWorkflowStatistics("data_loaded", workflowId);
if (insertResult === 'failed' || insertResult === 'alreadyExists')
return;
const project = await this.ownershipService.getWorkflowProjectCached(workflowId);
const owner = await this.ownershipService.getProjectOwnerCached(project.id);
let metrics = {
user_id: owner === null || owner === void 0 ? void 0 : owner.id,
project_id: project.id,
workflow_id: workflowId,
node_type: node.type,
node_id: node.id,
};
if (node.credentials) {
Object.entries(node.credentials).forEach(([credName, credDetails]) => {
metrics = Object.assign(metrics, {
credential_type: credName,
credential_id: credDetails.id,
});
});
}
this.emit('telemetry.onFirstWorkflowDataLoad', metrics);
}
};
exports.EventsService = EventsService;
exports.EventsService = EventsService = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [Logger_1.Logger,
workflowStatistics_repository_1.WorkflowStatisticsRepository,
ownership_service_1.OwnershipService])
], EventsService);
//# sourceMappingURL=events.service.js.map
;