mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
refactor(core): Rename EventsService
to WorkflowStatisticsService
(no-changelog) (#10107)
This commit is contained in:
parent
1c666e6e7c
commit
222a0862bd
|
@ -26,7 +26,7 @@ import type {
|
||||||
IExecutionTrackProperties,
|
IExecutionTrackProperties,
|
||||||
} from '@/Interfaces';
|
} from '@/Interfaces';
|
||||||
import { License } from '@/License';
|
import { License } from '@/License';
|
||||||
import { EventsService } from '@/services/events.service';
|
import { WorkflowStatisticsService } from '@/services/workflow-statistics.service';
|
||||||
import { NodeTypes } from '@/NodeTypes';
|
import { NodeTypes } from '@/NodeTypes';
|
||||||
import { Telemetry } from '@/telemetry';
|
import { Telemetry } from '@/telemetry';
|
||||||
import type { Project } from '@db/entities/Project';
|
import type { Project } from '@db/entities/Project';
|
||||||
|
@ -42,18 +42,18 @@ export class InternalHooks {
|
||||||
private readonly nodeTypes: NodeTypes,
|
private readonly nodeTypes: NodeTypes,
|
||||||
private readonly sharedWorkflowRepository: SharedWorkflowRepository,
|
private readonly sharedWorkflowRepository: SharedWorkflowRepository,
|
||||||
private readonly workflowRepository: WorkflowRepository,
|
private readonly workflowRepository: WorkflowRepository,
|
||||||
eventsService: EventsService,
|
workflowStatisticsService: WorkflowStatisticsService,
|
||||||
private readonly instanceSettings: InstanceSettings,
|
private readonly instanceSettings: InstanceSettings,
|
||||||
private readonly license: License,
|
private readonly license: License,
|
||||||
private readonly projectRelationRepository: ProjectRelationRepository,
|
private readonly projectRelationRepository: ProjectRelationRepository,
|
||||||
private readonly sharedCredentialsRepository: SharedCredentialsRepository,
|
private readonly sharedCredentialsRepository: SharedCredentialsRepository,
|
||||||
private readonly _eventBus: MessageEventBus, // needed until we decouple telemetry
|
private readonly _eventBus: MessageEventBus, // needed until we decouple telemetry
|
||||||
) {
|
) {
|
||||||
eventsService.on(
|
workflowStatisticsService.on(
|
||||||
'telemetry.onFirstProductionWorkflowSuccess',
|
'telemetry.onFirstProductionWorkflowSuccess',
|
||||||
async (metrics) => await this.onFirstProductionWorkflowSuccess(metrics),
|
async (metrics) => await this.onFirstProductionWorkflowSuccess(metrics),
|
||||||
);
|
);
|
||||||
eventsService.on(
|
workflowStatisticsService.on(
|
||||||
'telemetry.onFirstWorkflowDataLoad',
|
'telemetry.onFirstWorkflowDataLoad',
|
||||||
async (metrics) => await this.onFirstWorkflowDataLoad(metrics),
|
async (metrics) => await this.onFirstWorkflowDataLoad(metrics),
|
||||||
);
|
);
|
||||||
|
|
|
@ -56,7 +56,7 @@ import * as WorkflowHelpers from '@/WorkflowHelpers';
|
||||||
import { WorkflowRunner } from '@/WorkflowRunner';
|
import { WorkflowRunner } from '@/WorkflowRunner';
|
||||||
import * as WorkflowExecuteAdditionalData from '@/WorkflowExecuteAdditionalData';
|
import * as WorkflowExecuteAdditionalData from '@/WorkflowExecuteAdditionalData';
|
||||||
import { ActiveExecutions } from '@/ActiveExecutions';
|
import { ActiveExecutions } from '@/ActiveExecutions';
|
||||||
import { EventsService } from '@/services/events.service';
|
import { WorkflowStatisticsService } from '@/services/workflow-statistics.service';
|
||||||
import { OwnershipService } from './services/ownership.service';
|
import { OwnershipService } from './services/ownership.service';
|
||||||
import { parseBody } from './middlewares';
|
import { parseBody } from './middlewares';
|
||||||
import { Logger } from './Logger';
|
import { Logger } from './Logger';
|
||||||
|
@ -360,7 +360,11 @@ export async function executeWebhook(
|
||||||
NodeExecuteFunctions,
|
NodeExecuteFunctions,
|
||||||
executionMode,
|
executionMode,
|
||||||
);
|
);
|
||||||
Container.get(EventsService).emit('nodeFetchedData', workflow.id, workflowStartNode);
|
Container.get(WorkflowStatisticsService).emit(
|
||||||
|
'nodeFetchedData',
|
||||||
|
workflow.id,
|
||||||
|
workflowStartNode,
|
||||||
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Send error response to webhook caller
|
// Send error response to webhook caller
|
||||||
const errorMessage = 'Workflow Webhook Error: Workflow could not be started!';
|
const errorMessage = 'Workflow Webhook Error: Workflow could not be started!';
|
||||||
|
|
|
@ -54,7 +54,7 @@ import { findSubworkflowStart, isWorkflowIdValid } from '@/utils';
|
||||||
import { PermissionChecker } from './UserManagement/PermissionChecker';
|
import { PermissionChecker } from './UserManagement/PermissionChecker';
|
||||||
import { InternalHooks } from '@/InternalHooks';
|
import { InternalHooks } from '@/InternalHooks';
|
||||||
import { ExecutionRepository } from '@db/repositories/execution.repository';
|
import { ExecutionRepository } from '@db/repositories/execution.repository';
|
||||||
import { EventsService } from '@/services/events.service';
|
import { WorkflowStatisticsService } from '@/services/workflow-statistics.service';
|
||||||
import { SecretsHelper } from './SecretsHelpers';
|
import { SecretsHelper } from './SecretsHelpers';
|
||||||
import { OwnershipService } from './services/ownership.service';
|
import { OwnershipService } from './services/ownership.service';
|
||||||
import {
|
import {
|
||||||
|
@ -393,7 +393,7 @@ export function hookFunctionsPreExecute(): IWorkflowExecuteHooks {
|
||||||
*/
|
*/
|
||||||
function hookFunctionsSave(): IWorkflowExecuteHooks {
|
function hookFunctionsSave(): IWorkflowExecuteHooks {
|
||||||
const logger = Container.get(Logger);
|
const logger = Container.get(Logger);
|
||||||
const eventsService = Container.get(EventsService);
|
const workflowStatisticsService = Container.get(WorkflowStatisticsService);
|
||||||
const eventRelay = Container.get(EventRelay);
|
const eventRelay = Container.get(EventRelay);
|
||||||
return {
|
return {
|
||||||
nodeExecuteBefore: [
|
nodeExecuteBefore: [
|
||||||
|
@ -524,13 +524,17 @@ function hookFunctionsSave(): IWorkflowExecuteHooks {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
eventsService.emit('workflowExecutionCompleted', this.workflowData, fullRunData);
|
workflowStatisticsService.emit(
|
||||||
|
'workflowExecutionCompleted',
|
||||||
|
this.workflowData,
|
||||||
|
fullRunData,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
nodeFetchedData: [
|
nodeFetchedData: [
|
||||||
async (workflowId: string, node: INode) => {
|
async (workflowId: string, node: INode) => {
|
||||||
eventsService.emit('nodeFetchedData', workflowId, node);
|
workflowStatisticsService.emit('nodeFetchedData', workflowId, node);
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
@ -545,7 +549,7 @@ function hookFunctionsSave(): IWorkflowExecuteHooks {
|
||||||
function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
|
function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
|
||||||
const logger = Container.get(Logger);
|
const logger = Container.get(Logger);
|
||||||
const internalHooks = Container.get(InternalHooks);
|
const internalHooks = Container.get(InternalHooks);
|
||||||
const eventsService = Container.get(EventsService);
|
const workflowStatisticsService = Container.get(WorkflowStatisticsService);
|
||||||
const eventRelay = Container.get(EventRelay);
|
const eventRelay = Container.get(EventRelay);
|
||||||
return {
|
return {
|
||||||
nodeExecuteBefore: [
|
nodeExecuteBefore: [
|
||||||
|
@ -631,7 +635,11 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
|
||||||
this.retryOf,
|
this.retryOf,
|
||||||
);
|
);
|
||||||
} finally {
|
} finally {
|
||||||
eventsService.emit('workflowExecutionCompleted', this.workflowData, fullRunData);
|
workflowStatisticsService.emit(
|
||||||
|
'workflowExecutionCompleted',
|
||||||
|
this.workflowData,
|
||||||
|
fullRunData,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async function (this: WorkflowHooks, runData: IRun): Promise<void> {
|
async function (this: WorkflowHooks, runData: IRun): Promise<void> {
|
||||||
|
@ -667,7 +675,7 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
|
||||||
],
|
],
|
||||||
nodeFetchedData: [
|
nodeFetchedData: [
|
||||||
async (workflowId: string, node: INode) => {
|
async (workflowId: string, node: INode) => {
|
||||||
eventsService.emit('nodeFetchedData', workflowId, node);
|
workflowStatisticsService.emit('nodeFetchedData', workflowId, node);
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,7 +8,7 @@ import { Logger } from '@/Logger';
|
||||||
import { OwnershipService } from './ownership.service';
|
import { OwnershipService } from './ownership.service';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class EventsService extends EventEmitter {
|
export class WorkflowStatisticsService extends EventEmitter {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly logger: Logger,
|
private readonly logger: Logger,
|
||||||
private readonly repository: WorkflowStatisticsRepository,
|
private readonly repository: WorkflowStatisticsRepository,
|
||||||
|
@ -112,7 +112,7 @@ export class EventsService extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export declare interface EventsService {
|
export declare interface WorkflowStatisticsService {
|
||||||
on(
|
on(
|
||||||
event: 'nodeFetchedData',
|
event: 'nodeFetchedData',
|
||||||
listener: (workflowId: string | undefined | null, node: INode) => void,
|
listener: (workflowId: string | undefined | null, node: INode) => void,
|
|
@ -14,13 +14,13 @@ import config from '@/config';
|
||||||
import type { User } from '@db/entities/User';
|
import type { User } from '@db/entities/User';
|
||||||
import type { WorkflowStatistics } from '@db/entities/WorkflowStatistics';
|
import type { WorkflowStatistics } from '@db/entities/WorkflowStatistics';
|
||||||
import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository';
|
import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository';
|
||||||
import { EventsService } from '@/services/events.service';
|
import { WorkflowStatisticsService } from '@/services/workflow-statistics.service';
|
||||||
import { UserService } from '@/services/user.service';
|
import { UserService } from '@/services/user.service';
|
||||||
import { OwnershipService } from '@/services/ownership.service';
|
import { OwnershipService } from '@/services/ownership.service';
|
||||||
import { mockInstance } from '../../shared/mocking';
|
import { mockInstance } from '../../shared/mocking';
|
||||||
import type { Project } from '@/databases/entities/Project';
|
import type { Project } from '@/databases/entities/Project';
|
||||||
|
|
||||||
describe('EventsService', () => {
|
describe('WorkflowStatisticsService', () => {
|
||||||
const fakeUser = mock<User>({ id: 'abcde-fghij' });
|
const fakeUser = mock<User>({ id: 'abcde-fghij' });
|
||||||
const fakeProject = mock<Project>({ id: '12345-67890', type: 'personal' });
|
const fakeProject = mock<Project>({ id: '12345-67890', type: 'personal' });
|
||||||
const ownershipService = mockInstance(OwnershipService);
|
const ownershipService = mockInstance(OwnershipService);
|
||||||
|
@ -44,7 +44,7 @@ describe('EventsService', () => {
|
||||||
mocked(ownershipService.getProjectOwnerCached).mockResolvedValue(fakeUser);
|
mocked(ownershipService.getProjectOwnerCached).mockResolvedValue(fakeUser);
|
||||||
const updateSettingsMock = jest.spyOn(userService, 'updateSettings').mockImplementation();
|
const updateSettingsMock = jest.spyOn(userService, 'updateSettings').mockImplementation();
|
||||||
|
|
||||||
const eventsService = new EventsService(
|
const workflowStatisticsService = new WorkflowStatisticsService(
|
||||||
mock(),
|
mock(),
|
||||||
new WorkflowStatisticsRepository(dataSource, globalConfig),
|
new WorkflowStatisticsRepository(dataSource, globalConfig),
|
||||||
ownershipService,
|
ownershipService,
|
||||||
|
@ -52,8 +52,11 @@ describe('EventsService', () => {
|
||||||
|
|
||||||
const onFirstProductionWorkflowSuccess = jest.fn();
|
const onFirstProductionWorkflowSuccess = jest.fn();
|
||||||
const onFirstWorkflowDataLoad = jest.fn();
|
const onFirstWorkflowDataLoad = jest.fn();
|
||||||
eventsService.on('telemetry.onFirstProductionWorkflowSuccess', onFirstProductionWorkflowSuccess);
|
workflowStatisticsService.on(
|
||||||
eventsService.on('telemetry.onFirstWorkflowDataLoad', onFirstWorkflowDataLoad);
|
'telemetry.onFirstProductionWorkflowSuccess',
|
||||||
|
onFirstProductionWorkflowSuccess,
|
||||||
|
);
|
||||||
|
workflowStatisticsService.on('telemetry.onFirstWorkflowDataLoad', onFirstWorkflowDataLoad);
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
|
@ -91,7 +94,7 @@ describe('EventsService', () => {
|
||||||
};
|
};
|
||||||
mockDBCall();
|
mockDBCall();
|
||||||
|
|
||||||
await eventsService.workflowExecutionCompleted(workflow, runData);
|
await workflowStatisticsService.workflowExecutionCompleted(workflow, runData);
|
||||||
expect(updateSettingsMock).toHaveBeenCalledTimes(1);
|
expect(updateSettingsMock).toHaveBeenCalledTimes(1);
|
||||||
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(1);
|
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(1);
|
||||||
expect(onFirstProductionWorkflowSuccess).toHaveBeenNthCalledWith(1, {
|
expect(onFirstProductionWorkflowSuccess).toHaveBeenNthCalledWith(1, {
|
||||||
|
@ -119,7 +122,7 @@ describe('EventsService', () => {
|
||||||
mode: 'internal' as WorkflowExecuteMode,
|
mode: 'internal' as WorkflowExecuteMode,
|
||||||
startedAt: new Date(),
|
startedAt: new Date(),
|
||||||
};
|
};
|
||||||
await eventsService.workflowExecutionCompleted(workflow, runData);
|
await workflowStatisticsService.workflowExecutionCompleted(workflow, runData);
|
||||||
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(0);
|
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -142,7 +145,7 @@ describe('EventsService', () => {
|
||||||
startedAt: new Date(),
|
startedAt: new Date(),
|
||||||
};
|
};
|
||||||
mockDBCall(2);
|
mockDBCall(2);
|
||||||
await eventsService.workflowExecutionCompleted(workflow, runData);
|
await workflowStatisticsService.workflowExecutionCompleted(workflow, runData);
|
||||||
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(0);
|
expect(onFirstProductionWorkflowSuccess).toBeCalledTimes(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -159,7 +162,7 @@ describe('EventsService', () => {
|
||||||
position: [0, 0] as [number, number],
|
position: [0, 0] as [number, number],
|
||||||
parameters: {},
|
parameters: {},
|
||||||
};
|
};
|
||||||
await eventsService.nodeFetchedData(workflowId, node);
|
await workflowStatisticsService.nodeFetchedData(workflowId, node);
|
||||||
expect(onFirstWorkflowDataLoad).toBeCalledTimes(1);
|
expect(onFirstWorkflowDataLoad).toBeCalledTimes(1);
|
||||||
expect(onFirstWorkflowDataLoad).toHaveBeenNthCalledWith(1, {
|
expect(onFirstWorkflowDataLoad).toHaveBeenNthCalledWith(1, {
|
||||||
user_id: fakeUser.id,
|
user_id: fakeUser.id,
|
||||||
|
@ -187,7 +190,7 @@ describe('EventsService', () => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
await eventsService.nodeFetchedData(workflowId, node);
|
await workflowStatisticsService.nodeFetchedData(workflowId, node);
|
||||||
expect(onFirstWorkflowDataLoad).toBeCalledTimes(1);
|
expect(onFirstWorkflowDataLoad).toBeCalledTimes(1);
|
||||||
expect(onFirstWorkflowDataLoad).toHaveBeenNthCalledWith(1, {
|
expect(onFirstWorkflowDataLoad).toHaveBeenNthCalledWith(1, {
|
||||||
user_id: fakeUser.id,
|
user_id: fakeUser.id,
|
||||||
|
@ -212,7 +215,7 @@ describe('EventsService', () => {
|
||||||
position: [0, 0] as [number, number],
|
position: [0, 0] as [number, number],
|
||||||
parameters: {},
|
parameters: {},
|
||||||
};
|
};
|
||||||
await eventsService.nodeFetchedData(workflowId, node);
|
await workflowStatisticsService.nodeFetchedData(workflowId, node);
|
||||||
expect(onFirstWorkflowDataLoad).toBeCalledTimes(0);
|
expect(onFirstWorkflowDataLoad).toBeCalledTimes(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
Loading…
Reference in a new issue