Skip to content

Commit d5fb515

Browse files
author
arthosofteq
authored
Merge pull request #1822 from RedisInsight/be/feature/RI-telemetry_tests
#RI-4175 cover telemetry events with ITests
2 parents 87f5fce + 0fd2df2 commit d5fb515

File tree

11 files changed

+171
-5
lines changed

11 files changed

+171
-5
lines changed

redisinsight/api/config/default.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export default {
9797
},
9898
analytics: {
9999
writeKey: process.env.SEGMENT_WRITE_KEY || 'SOURCE_WRITE_KEY',
100+
flushInterval: parseInt(process.env.ANALYTICS_FLUSH_INTERVAL, 10) || 3000,
100101
},
101102
logger: {
102103
logLevel: process.env.LOG_LEVEL || 'info', // log level

redisinsight/api/config/production.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default {
2525
},
2626
analytics: {
2727
writeKey: process.env.SEGMENT_WRITE_KEY || 'lK5MNZgHbxj6vQwFgqZxygA0BiDQb32n',
28+
flushInterval: parseInt(process.env.ANALYTICS_FLUSH_INTERVAL, 10) || 10000,
2829
},
2930
db: {
3031
database: join(homedir, 'redisinsight.db'),

redisinsight/api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"mocha": "^8.4.0",
111111
"mocha-junit-reporter": "^2.0.0",
112112
"mocha-multi-reporters": "^1.5.1",
113+
"nock": "^13.3.0",
113114
"nyc": "^15.1.0",
114115
"object-diff": "^0.0.4",
115116
"rimraf": "^3.0.2",

redisinsight/api/src/modules/analytics/analytics.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export class AnalyticsService {
4545
this.sessionId = sessionId;
4646
this.anonymousId = anonymousId;
4747
this.appType = appType;
48-
this.analytics = new Analytics(ANALYTICS_CONFIG.writeKey);
48+
this.analytics = new Analytics(ANALYTICS_CONFIG.writeKey, {
49+
flushInterval: ANALYTICS_CONFIG.flushInterval,
50+
});
4951
}
5052

5153
@OnEvent(AppAnalyticsEvents.Track)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {
2+
expect,
3+
describe,
4+
it,
5+
deps,
6+
requirements,
7+
} from '../deps';
8+
const { analytics } = deps;
9+
10+
11+
describe('Analytics', () => {
12+
requirements('rte.serverType=local');
13+
14+
it('APPLICATION_STARTED', () => {
15+
const appStarted = analytics.findEvent({
16+
event: 'APPLICATION_STARTED',
17+
})
18+
19+
const appFirstStarted = analytics.findEvent({
20+
event: 'APPLICATION_FIRST_START',
21+
})
22+
23+
const found = appStarted || appFirstStarted;
24+
25+
if (!found) {
26+
fail('APPLICATION_STARTED or APPLICATION_FIRST_START events were not found');
27+
}
28+
29+
expect(found?.properties).to.have.all.keys('appVersion', 'osPlatform', 'buildType');
30+
expect(found?.properties?.appVersion).to.be.a('string');
31+
expect(found?.properties?.osPlatform).to.be.a('string');
32+
expect(found?.properties?.buildType).to.be.a('string');
33+
});
34+
});

redisinsight/api/test/api/cli/POST-databases-id-cli-uuid-send_command.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
generateInvalidDataTestCases,
1010
validateInvalidDataTestCase,
1111
validateApiCall,
12-
requirements,
12+
requirements, serverConfig
1313
} from '../deps';
14-
const { server, request, constants, rte } = deps;
14+
const { server, request, constants, rte, analytics } = deps;
1515

1616
// endpoint to test
1717
const endpoint = (instanceId = constants.TEST_INSTANCE_ID, uuid = constants.TEST_CLI_UUID_1) =>
@@ -87,6 +87,18 @@ describe('POST /databases/:instanceId/cli/:uuid/send-command', () => {
8787
},
8888
after: async () => {
8989
expect(await rte.client.get(constants.TEST_STRING_KEY_1)).to.eql(constants.TEST_STRING_VALUE_1);
90+
await analytics.waitForEvent({
91+
event: 'CLI_COMMAND_EXECUTED',
92+
properties: {
93+
databaseId: constants.TEST_INSTANCE_ID,
94+
commandType: 'core',
95+
moduleName: 'n/a',
96+
capability: 'string',
97+
command: 'SET',
98+
outputFormat: 'TEXT',
99+
buildType: serverConfig.get('server').buildType,
100+
},
101+
});
90102
}
91103
},
92104
{

redisinsight/api/test/api/database/POST-databases.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import {
77
requirements,
88
validateApiCall,
99
after,
10-
generateInvalidDataTestCases, validateInvalidDataTestCase, getMainCheckFn
10+
generateInvalidDataTestCases, validateInvalidDataTestCase, getMainCheckFn, serverConfig,
1111
} from '../deps';
1212
import { databaseSchema } from './constants';
13-
const { rte, request, server, localDb, constants } = deps;
13+
const { rte, request, server, localDb, constants, analytics } = deps;
1414

1515
const endpoint = () => request(server).post(`/${constants.API.DATABASES}`);
1616

@@ -134,6 +134,38 @@ describe('POST /databases', () => {
134134
connectionType: constants.STANDALONE,
135135
new: true,
136136
},
137+
checkFn: async ({ body }) => {
138+
// todo: find a way to test rest of the fields
139+
await analytics.waitForEvent({
140+
event: 'CONFIG_DATABASES_DATABASE_ADDED',
141+
properties: {
142+
databaseId: body.id,
143+
connectionType: body.connectionType,
144+
provider: body.provider,
145+
useTLS: 'disabled',
146+
verifyTLSCertificate: 'disabled',
147+
useTLSAuthClients: 'disabled',
148+
useSNI: 'disabled',
149+
useSSH: 'disabled',
150+
version: rte.env.version,
151+
// numberOfKeys: 8,
152+
// numberOfKeysRange: '0 - 500 000',
153+
// totalMemory: 881632,
154+
// numberedDatabases: 16,
155+
// numberOfModules: 0,
156+
timeout: body.timeout / 1000,
157+
// RediSearch: { loaded: false },
158+
// RedisAI: { loaded: false },
159+
// RedisGraph: { loaded: false },
160+
// RedisGears: { loaded: false },
161+
// RedisBloom: { loaded: false },
162+
// RedisJSON: { loaded: false },
163+
// RedisTimeSeries: { loaded: false },
164+
// customModules: [],
165+
buildType: serverConfig.get('server').buildType,
166+
},
167+
});
168+
},
137169
});
138170
});
139171
describe('Enterprise', () => {

redisinsight/api/test/api/deps.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { getAnalytics } from '../helpers/analytics';
2+
export { createAnalytics } from '../helpers/analytics';
3+
14
export * from '../helpers/test';
25
import * as request from 'supertest';
36
import * as chai from 'chai';
@@ -16,6 +19,10 @@ export async function depsInit () {
1619
if(constants.TEST_CLOUD_RTE) {
1720
await initCloudDatabase();
1821
}
22+
23+
// initialize analytics module
24+
deps.analytics = await getAnalytics();
25+
1926
// initializing backend server
2027
deps.server = await getServer();
2128

@@ -40,6 +47,7 @@ export const deps = {
4047
request,
4148
expect: chai.expect,
4249
server: null,
50+
analytics: null,
4351
getSocket,
4452
rte: null,
4553
testEnv,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { EventEmitter } from 'events';
2+
import * as nock from 'nock';
3+
import * as _ from 'lodash';
4+
import { isMatch } from 'lodash';
5+
6+
let analytics;
7+
8+
export class Analytics extends EventEmitter {
9+
public messages = [];
10+
11+
constructor() {
12+
super();
13+
const scope = nock('https://api.segment.io')
14+
.post('/v1/batch', (body) => {
15+
const batchMessages = body?.batch || [];
16+
this.messages = this.messages.concat(batchMessages);
17+
this.emit('batch', batchMessages);
18+
return true;
19+
})
20+
.reply(200, {})
21+
22+
scope.persist();
23+
}
24+
25+
public findEvent(event: any, messages = this.messages) {
26+
return _.find(messages, (message) => {
27+
return isMatch(message, event);
28+
});
29+
}
30+
31+
public async waitForEvent(event) {
32+
await new Promise((res, rej) => {
33+
this.once('batch', (batch) => {
34+
const exists = this.findEvent(event, batch);
35+
36+
if (!exists) {
37+
rej(new Error(`Unable to find event:\n${JSON.stringify(event)}\nin the events batch:\n${JSON.stringify(batch)}`));
38+
}
39+
40+
res(exists);
41+
});
42+
43+
setTimeout(() => rej(new Error(`No event ${JSON.stringify(event)} received in 10s`)), 10000);
44+
});
45+
}
46+
}
47+
48+
export const getAnalytics = () => {
49+
return analytics || createAnalytics();
50+
};
51+
52+
export const createAnalytics = () => {
53+
return new Analytics();
54+
}

redisinsight/api/test/helpers/local-db.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ export const initAgreements = async () => {
483483
agreements.data = JSON.stringify({
484484
eula: true,
485485
encryption: constants.TEST_ENCRYPTION_STRATEGY === 'KEYTAR',
486+
analytics: true,
486487
});
487488

488489
await rep.save(agreements);

0 commit comments

Comments
 (0)