Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Handle zero execution statistics on metrics collection during license renewal #8463

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class UsageMetricsRepository extends Repository<UsageMetrics> {
`)) as Row[];

const toNumber = (value: string | number) =>
typeof value === 'number' ? value : parseInt(value, 10);
(typeof value === 'number' ? value : parseInt(value, 10)) || 0;

return {
enabledUsers: toNumber(enabledUsers),
Expand Down
24 changes: 18 additions & 6 deletions packages/cli/test/integration/usageMetrics.repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@ import { UsageMetricsRepository } from '@/databases/repositories/usageMetrics.re
import { createAdmin, createMember, createOwner, createUser } from './shared/db/users';
import * as testDb from './shared/testDb';
import Container from 'typedi';
import { CredentialsRepository } from '@/databases/repositories/credentials.repository';
import { createManyWorkflows } from './shared/db/workflows';
import { createManyCredentials } from './shared/db/credentials';
import { WorkflowStatisticsRepository } from '@/databases/repositories/workflowStatistics.repository';
import { StatisticsNames } from '@/databases/entities/WorkflowStatistics';
import { WorkflowRepository } from '@/databases/repositories/workflow.repository';

describe('UsageMetricsRepository', () => {
let usageMetricsRepository: UsageMetricsRepository;
let credentialsRepository: CredentialsRepository;
let workflowStatisticsRepository: WorkflowStatisticsRepository;
let workflowRepository: WorkflowRepository;

beforeAll(async () => {
await testDb.init();

usageMetricsRepository = Container.get(UsageMetricsRepository);
credentialsRepository = Container.get(CredentialsRepository);

workflowStatisticsRepository = Container.get(WorkflowStatisticsRepository);
workflowRepository = Container.get(WorkflowRepository);
});

beforeEach(async () => {
await testDb.truncate(['User', 'Credentials', 'Workflow', 'Execution', 'WorkflowStatistics']);
});

Expand Down Expand Up @@ -74,5 +71,20 @@ describe('UsageMetricsRepository', () => {
manualExecutions: 2,
});
});

test('should handle zero execution statistics correctly', async () => {
await Promise.all([createOwner(), createManyWorkflows(3, { active: true })]);

const metrics = await usageMetricsRepository.getLicenseRenewalMetrics();

expect(metrics).toStrictEqual({
enabledUsers: 1,
totalCredentials: 0,
totalWorkflows: 3,
activeWorkflows: 3,
productionExecutions: 0, // not NaN
manualExecutions: 0, // not NaN
});
});
});
});
Loading