Skip to content

Commit

Permalink
refactor(core): Send active workflow IDs during license renewal (#9804)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov authored Jun 19, 2024
1 parent de3981c commit cfc4db0
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"@n8n/n8n-nodes-langchain": "workspace:*",
"@n8n/permissions": "workspace:*",
"@n8n/typeorm": "0.3.20-10",
"@n8n_io/license-sdk": "2.12.0",
"@n8n_io/license-sdk": "2.13.0",
"@oclif/core": "3.26.6",
"@pinecone-database/pinecone": "2.1.0",
"@rudderstack/rudder-sdk-node": "2.0.7",
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/License.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export class License {
const collectUsageMetrics = isMainInstance
? async () => await this.usageMetricsService.collectUsageMetrics()
: async () => [];
const collectPassthroughData = isMainInstance
? async () => await this.usageMetricsService.getActiveWorkflowIds()
: async () => ({});

const renewalEnabled = this.renewalEnabled(instanceType);

Expand All @@ -101,6 +104,7 @@ export class License {
saveCertStr,
deviceFingerprint: () => this.instanceSettings.instanceId,
collectUsageMetrics,
collectPassthroughData,
onFeatureChange,
});

Expand Down
12 changes: 11 additions & 1 deletion packages/cli/src/services/usageMetrics.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { UsageMetricsRepository } from '@/databases/repositories/usageMetrics.repository';
import { Service } from 'typedi';
import { WorkflowRepository } from '@/databases/repositories/workflow.repository';

@Service()
export class UsageMetricsService {
constructor(private readonly usageMetricsRepository: UsageMetricsRepository) {}
constructor(
private readonly usageMetricsRepository: UsageMetricsRepository,
private readonly workflowRepository: WorkflowRepository,
) {}

async collectUsageMetrics() {
const {
Expand All @@ -26,4 +30,10 @@ export class UsageMetricsService {
{ name: 'manualExecutions', value: manualExecutions },
];
}

async getActiveWorkflowIds() {
return {
activeWorkflowIds: await this.workflowRepository.getActiveIds(),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import Container from 'typedi';
import { WorkflowRepository } from '@/databases/repositories/workflow.repository';

import * as testDb from '../../shared/testDb';
import { createWorkflowWithTrigger, getAllWorkflows } from '../../shared/db/workflows';
import {
createWorkflowWithTrigger,
createWorkflow,
getAllWorkflows,
} from '../../shared/db/workflows';

describe('WorkflowRepository', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -67,4 +71,27 @@ describe('WorkflowRepository', () => {
expect(after).toMatchObject([{ active: false }, { active: false }]);
});
});

describe('getActiveIds', () => {
it('should return active workflow IDs', async () => {
//
// ARRANGE
//
const workflows = await Promise.all([
createWorkflow({ active: true }),
createWorkflow({ active: false }),
createWorkflow({ active: false }),
]);

//
// ACT
//
const activeIds = await Container.get(WorkflowRepository).getActiveIds();

//
// ASSERT
//
expect(activeIds).toEqual([workflows[0].id]);
});
});
});
32 changes: 32 additions & 0 deletions packages/cli/test/integration/usage-metrics.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { mock } from 'jest-mock-extended';
import { UsageMetricsService } from '@/services/usageMetrics.service';
import type { WorkflowRepository } from '@/databases/repositories/workflow.repository';
import type { UsageMetricsRepository } from '@/databases/repositories/usageMetrics.repository';

describe('UsageMetricsService', () => {
const workflowRepository = mock<WorkflowRepository>();
const usageMetricsService = new UsageMetricsService(
mock<UsageMetricsRepository>(),
workflowRepository,
);

describe('getActiveWorkflowIds', () => {
test('should return active workflow IDs', async () => {
/**
* Arrange
*/
const activeWorkflowIds = ['1', '2'];
workflowRepository.getActiveIds.mockResolvedValue(activeWorkflowIds);

/**
* Act
*/
const result = await usageMetricsService.getActiveWorkflowIds();

/**
* Assert
*/
expect(result).toEqual({ activeWorkflowIds });
});
});
});
2 changes: 2 additions & 0 deletions packages/cli/test/unit/License.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('License', () => {
saveCertStr: expect.any(Function),
onFeatureChange: expect.any(Function),
collectUsageMetrics: expect.any(Function),
collectPassthroughData: expect.any(Function),
server: MOCK_SERVER_URL,
tenantId: 1,
});
Expand All @@ -68,6 +69,7 @@ describe('License', () => {
saveCertStr: expect.any(Function),
onFeatureChange: expect.any(Function),
collectUsageMetrics: expect.any(Function),
collectPassthroughData: expect.any(Function),
server: MOCK_SERVER_URL,
tenantId: 1,
});
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cfc4db0

Please sign in to comment.