Skip to content

Commit

Permalink
Init unit tests for telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsanv committed Jun 16, 2022
1 parent 9df3e30 commit 3696162
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/cli/src/InternalHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
IWorkflowBase,
IWorkflowDb,
} from '.';
import { Telemetry } from './telemetry';
import Telemetry from './telemetry';

export class InternalHooksClass implements IInternalHooksClass {
private versionCli: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/InternalHooksManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/no-cycle */
import { INodeTypes } from 'n8n-workflow';
import { InternalHooksClass } from './InternalHooks';
import { Telemetry } from './telemetry';
import Telemetry from './telemetry';

export class InternalHooksManager {
private static internalHooksInstance: InternalHooksClass;
Expand Down
27 changes: 22 additions & 5 deletions packages/cli/src/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ interface IExecutionsBuffer {
firstExecutions: IFirstExecutions;
}

export class Telemetry {
// eslint-disable-next-line import/no-default-export
export default class Telemetry {
private client?: TelemetryClient;

private instanceId: string;
Expand Down Expand Up @@ -71,14 +72,26 @@ export class Telemetry {
return;
}

this.client = new TelemetryClient(key, url, { logLevel });
this.client = this.createTelemetryClient(key, url, logLevel);

this.pulseIntervalReference = setInterval(async () => {
void this.pulse();
}, 6 * 60 * 60 * 1000); // every 6 hours
this.startPulse();
}
}

private createTelemetryClient(
key: string,
url: string,
logLevel: string,
): TelemetryClient | undefined {
return new TelemetryClient(key, url, { logLevel });
}

private startPulse() {
this.pulseIntervalReference = setInterval(async () => {
void this.pulse();
}, 6 * 60 * 60 * 1000); // every 6 hours
}

private async pulse(): Promise<unknown> {
if (!this.client) {
return Promise.resolve();
Expand All @@ -104,6 +117,10 @@ export class Telemetry {
return Promise.all(allPromises);
}

getTelemetryClient(): unknown {
return this.client;
}

async trackWorkflowExecution(properties: IDataObject): Promise<void> {
if (this.client) {
const workflowId = properties.workflow_id as string;
Expand Down
1 change: 0 additions & 1 deletion packages/cli/test/integration/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import { credentialsController } from '../../../src/api/credentials.api';
import { loadPublicApiVersions } from '../../../src/PublicApi/';
import type { User } from '../../../src/databases/entities/User';
import type { ApiPath, EndpointGroup, PostgresSchemaSection, SmtpTestAccount } from './types';
import { Telemetry } from '../../../src/telemetry';
import type { N8nApp } from '../../../src/UserManagement/Interfaces';
import { set } from 'lodash';
interface TriggerTime {
Expand Down
24 changes: 24 additions & 0 deletions packages/cli/test/unit/Telemetry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Telemetry from '../../src/telemetry';

describe('Telemetry', () => {
jest.spyOn(Telemetry.prototype as any, 'createTelemetryClient').mockImplementation(() => {
return {
flush: () => {},
identify: () => {},
track: () => {},
};
});

jest.spyOn(Telemetry.prototype as any, 'startPulse').mockImplementation(() => {});

const spyTrack = jest.spyOn(Telemetry.prototype, 'track');

const telemetry = new Telemetry('Telemetry unit test', '0.0.0');

describe('trackN8nStop', () => {
test('should call track method', () => {
telemetry.trackN8nStop();
expect(spyTrack).toHaveBeenCalledTimes(1);
});
});
});

0 comments on commit 3696162

Please sign in to comment.