Skip to content
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
1 change: 1 addition & 0 deletions redisinsight/api/config/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export default {
},
analytics: {
writeKey: process.env.SEGMENT_WRITE_KEY || 'SOURCE_WRITE_KEY',
flushInterval: parseInt(process.env.ANALYTICS_FLUSH_INTERVAL, 10) || 3000,
},
logger: {
logLevel: process.env.LOG_LEVEL || 'info', // log level
Expand Down
1 change: 1 addition & 0 deletions redisinsight/api/config/production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default {
},
analytics: {
writeKey: process.env.SEGMENT_WRITE_KEY || 'lK5MNZgHbxj6vQwFgqZxygA0BiDQb32n',
flushInterval: parseInt(process.env.ANALYTICS_FLUSH_INTERVAL, 10) || 10000,
},
db: {
database: join(homedir, 'redisinsight.db'),
Expand Down
1 change: 1 addition & 0 deletions redisinsight/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"mocha": "^8.4.0",
"mocha-junit-reporter": "^2.0.0",
"mocha-multi-reporters": "^1.5.1",
"nock": "^13.3.0",
"nyc": "^15.1.0",
"object-diff": "^0.0.4",
"rimraf": "^3.0.2",
Expand Down
4 changes: 3 additions & 1 deletion redisinsight/api/src/modules/analytics/analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export class AnalyticsService {
this.sessionId = sessionId;
this.anonymousId = anonymousId;
this.appType = appType;
this.analytics = new Analytics(ANALYTICS_CONFIG.writeKey);
this.analytics = new Analytics(ANALYTICS_CONFIG.writeKey, {
flushInterval: ANALYTICS_CONFIG.flushInterval,
});
}

@OnEvent(AppAnalyticsEvents.Track)
Expand Down
34 changes: 34 additions & 0 deletions redisinsight/api/test/api/analytics/analytics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
expect,
describe,
it,
deps,
requirements,
} from '../deps';
const { analytics } = deps;


describe('Analytics', () => {
requirements('rte.serverType=local');

it('APPLICATION_STARTED', () => {
const appStarted = analytics.findEvent({
event: 'APPLICATION_STARTED',
})

const appFirstStarted = analytics.findEvent({
event: 'APPLICATION_FIRST_START',
})

const found = appStarted || appFirstStarted;

if (!found) {
fail('APPLICATION_STARTED or APPLICATION_FIRST_START events were not found');
}

expect(found?.properties).to.have.all.keys('appVersion', 'osPlatform', 'buildType');
expect(found?.properties?.appVersion).to.be.a('string');
expect(found?.properties?.osPlatform).to.be.a('string');
expect(found?.properties?.buildType).to.be.a('string');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
generateInvalidDataTestCases,
validateInvalidDataTestCase,
validateApiCall,
requirements,
requirements, serverConfig
} from '../deps';
const { server, request, constants, rte } = deps;
const { server, request, constants, rte, analytics } = deps;

// endpoint to test
const endpoint = (instanceId = constants.TEST_INSTANCE_ID, uuid = constants.TEST_CLI_UUID_1) =>
Expand Down Expand Up @@ -87,6 +87,18 @@ describe('POST /databases/:instanceId/cli/:uuid/send-command', () => {
},
after: async () => {
expect(await rte.client.get(constants.TEST_STRING_KEY_1)).to.eql(constants.TEST_STRING_VALUE_1);
await analytics.waitForEvent({
event: 'CLI_COMMAND_EXECUTED',
properties: {
databaseId: constants.TEST_INSTANCE_ID,
commandType: 'core',
moduleName: 'n/a',
capability: 'string',
command: 'SET',
outputFormat: 'TEXT',
buildType: serverConfig.get('server').buildType,
},
});
}
},
{
Expand Down
36 changes: 34 additions & 2 deletions redisinsight/api/test/api/database/POST-databases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
requirements,
validateApiCall,
after,
generateInvalidDataTestCases, validateInvalidDataTestCase, getMainCheckFn
generateInvalidDataTestCases, validateInvalidDataTestCase, getMainCheckFn, serverConfig,
} from '../deps';
import { databaseSchema } from './constants';
const { rte, request, server, localDb, constants } = deps;
const { rte, request, server, localDb, constants, analytics } = deps;

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

Expand Down Expand Up @@ -134,6 +134,38 @@ describe('POST /databases', () => {
connectionType: constants.STANDALONE,
new: true,
},
checkFn: async ({ body }) => {
// todo: find a way to test rest of the fields
await analytics.waitForEvent({
event: 'CONFIG_DATABASES_DATABASE_ADDED',
properties: {
databaseId: body.id,
connectionType: body.connectionType,
provider: body.provider,
useTLS: 'disabled',
verifyTLSCertificate: 'disabled',
useTLSAuthClients: 'disabled',
useSNI: 'disabled',
useSSH: 'disabled',
version: rte.env.version,
// numberOfKeys: 8,
// numberOfKeysRange: '0 - 500 000',
// totalMemory: 881632,
// numberedDatabases: 16,
// numberOfModules: 0,
timeout: body.timeout / 1000,
// RediSearch: { loaded: false },
// RedisAI: { loaded: false },
// RedisGraph: { loaded: false },
// RedisGears: { loaded: false },
// RedisBloom: { loaded: false },
// RedisJSON: { loaded: false },
// RedisTimeSeries: { loaded: false },
// customModules: [],
buildType: serverConfig.get('server').buildType,
},
});
},
});
});
describe('Enterprise', () => {
Expand Down
8 changes: 8 additions & 0 deletions redisinsight/api/test/api/deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { getAnalytics } from '../helpers/analytics';
export { createAnalytics } from '../helpers/analytics';

export * from '../helpers/test';
import * as request from 'supertest';
import * as chai from 'chai';
Expand All @@ -16,6 +19,10 @@ export async function depsInit () {
if(constants.TEST_CLOUD_RTE) {
await initCloudDatabase();
}

// initialize analytics module
deps.analytics = await getAnalytics();

// initializing backend server
deps.server = await getServer();

Expand All @@ -40,6 +47,7 @@ export const deps = {
request,
expect: chai.expect,
server: null,
analytics: null,
getSocket,
rte: null,
testEnv,
Expand Down
54 changes: 54 additions & 0 deletions redisinsight/api/test/helpers/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { EventEmitter } from 'events';
import * as nock from 'nock';
import * as _ from 'lodash';
import { isMatch } from 'lodash';

let analytics;

export class Analytics extends EventEmitter {
public messages = [];

constructor() {
super();
const scope = nock('https://api.segment.io')
.post('/v1/batch', (body) => {
const batchMessages = body?.batch || [];
this.messages = this.messages.concat(batchMessages);
this.emit('batch', batchMessages);
return true;
})
.reply(200, {})

scope.persist();
}

public findEvent(event: any, messages = this.messages) {
return _.find(messages, (message) => {
return isMatch(message, event);
});
}

public async waitForEvent(event) {
await new Promise((res, rej) => {
this.once('batch', (batch) => {
const exists = this.findEvent(event, batch);

if (!exists) {
rej(new Error(`Unable to find event:\n${JSON.stringify(event)}\nin the events batch:\n${JSON.stringify(batch)}`));
}

res(exists);
});

setTimeout(() => rej(new Error(`No event ${JSON.stringify(event)} received in 10s`)), 10000);
});
}
}

export const getAnalytics = () => {
return analytics || createAnalytics();
};

export const createAnalytics = () => {
return new Analytics();
}
1 change: 1 addition & 0 deletions redisinsight/api/test/helpers/local-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ export const initAgreements = async () => {
agreements.data = JSON.stringify({
eula: true,
encryption: constants.TEST_ENCRYPTION_STRATEGY === 'KEYTAR',
analytics: true,
});

await rep.save(agreements);
Expand Down
20 changes: 20 additions & 0 deletions redisinsight/api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5389,6 +5389,11 @@ json-stable-stringify-without-jsonify@^1.0.1:
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
integrity sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=

json-stringify-safe@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==

json5@2.x, json5@^2.1.2:
version "2.2.0"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.0.tgz#2dfefe720c6ba525d9ebd909950f0515316c89a3"
Expand Down Expand Up @@ -6108,6 +6113,16 @@ nice-try@^1.0.4:
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==

nock@^13.3.0:
version "13.3.0"
resolved "https://registry.yarnpkg.com/nock/-/nock-13.3.0.tgz#b13069c1a03f1ad63120f994b04bfd2556925768"
integrity sha512-HHqYQ6mBeiMc+N038w8LkMpDCRquCHWeNmN3v6645P3NhN2+qXOBqvPqo7Rt1VyCMzKhJ733wZqw5B7cQVFNPg==
dependencies:
debug "^4.1.0"
json-stringify-safe "^5.0.1"
lodash "^4.17.21"
propagate "^2.0.0"

node-abi@^3.3.0:
version "3.24.0"
resolved "https://registry.yarnpkg.com/node-abi/-/node-abi-3.24.0.tgz#b9d03393a49f2c7e147d0c99f180e680c27c1599"
Expand Down Expand Up @@ -6798,6 +6813,11 @@ prompts@^2.0.1:
kleur "^3.0.3"
sisteransi "^1.0.5"

propagate@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45"
integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==

proxy-addr@~2.0.7:
version "2.0.7"
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.7.tgz#f19fe69ceab311eeb94b42e70e8c2070f9ba1025"
Expand Down