Skip to content

Commit ede8f2c

Browse files
Merge pull request #4411 from RedisInsight/ric/1.0.x
Ric/1.0.x
2 parents 3d5dc55 + c9f0d7f commit ede8f2c

File tree

7 files changed

+60
-7
lines changed

7 files changed

+60
-7
lines changed

redisinsight/api/src/__mocks__/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import { mockSessionMetadata } from 'src/__mocks__/common';
22

33
export const mockConstantsProvider = jest.fn(() => ({
44
getSystemSessionMetadata: jest.fn().mockReturnValue(mockSessionMetadata),
5+
getAnonymousId: jest.fn().mockReturnValue(mockSessionMetadata.userId),
56
}));

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
mockAppSettings,
44
mockAppSettingsWithoutPermissions,
55
mockAppVersion,
6-
mockConstantsProvider,
76
mockControlGroup,
87
mockControlNumber,
98
mockSessionMetadata,
@@ -14,6 +13,8 @@ import { TelemetryEvents } from 'src/constants';
1413
import { AppType } from 'src/modules/server/models/server';
1514
import { SettingsService } from 'src/modules/settings/settings.service';
1615
import { ConstantsProvider } from 'src/modules/constants/providers/constants.provider';
16+
import { LocalConstantsProvider } from 'src/modules/constants/providers/local.constants.provider';
17+
import { convertAnyStringToPositiveInteger } from 'src/utils';
1718
import {
1819
AnalyticsService,
1920
Telemetry,
@@ -49,7 +50,7 @@ describe('AnalyticsService', () => {
4950
},
5051
{
5152
provide: ConstantsProvider,
52-
useFactory: mockConstantsProvider,
53+
useClass: LocalConstantsProvider,
5354
},
5455
],
5556
}).compile();
@@ -178,7 +179,7 @@ describe('AnalyticsService', () => {
178179

179180
expect(service.getSessionId()).toEqual(-1);
180181
expect(service.getSessionId(mockSessionMetadata))
181-
.toEqual(Number(BigInt(`0x${Buffer.from(mockSessionMetadata.sessionId).toString('hex')}`)));
182+
.toEqual(convertAnyStringToPositiveInteger(mockSessionMetadata.sessionId));
182183
});
183184
});
184185

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { SettingsService } from 'src/modules/settings/settings.service';
99
import { ConstantsProvider } from 'src/modules/constants/providers/constants.provider';
1010
import { ServerService } from 'src/modules/server/server.service';
1111
import { SessionMetadata } from 'src/common/models';
12+
import { convertAnyStringToPositiveInteger } from 'src/utils';
1213

1314
export const NON_TRACKING_ANONYMOUS_ID = '00000000-0000-0000-0000-000000000001';
1415

@@ -70,7 +71,7 @@ export class AnalyticsService {
7071
* @param sessionMetadata
7172
*/
7273
public getAnonymousId(sessionMetadata?: SessionMetadata): string {
73-
return this.anonymousId ?? sessionMetadata?.userId ?? 'unknown';
74+
return this.anonymousId ?? this.constantsProvider.getAnonymousId(sessionMetadata);
7475
}
7576

7677
/**
@@ -87,16 +88,15 @@ export class AnalyticsService {
8788
}
8889

8990
if (sessionMetadata?.sessionId) {
90-
// convert string to number
91-
return Number(BigInt(`0x${Buffer.from(sessionMetadata?.sessionId).toString('hex')}`));
91+
return convertAnyStringToPositiveInteger(sessionMetadata?.sessionId);
9292
}
9393

9494
return -1;
9595
}
9696

9797
public async init(initConfig: ITelemetryInitEvent) {
9898
const {
99-
anonymousId, sessionId, appType, controlNumber, controlGroup, appVersion, firstStart, sessionMetadata
99+
anonymousId, sessionId, appType, controlNumber, controlGroup, appVersion, firstStart, sessionMetadata,
100100
} = initConfig;
101101
this.sessionId = sessionId;
102102
this.anonymousId = anonymousId;

redisinsight/api/src/modules/constants/providers/constants.provider.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ export abstract class ConstantsProvider {
88
* For example onModuleInit step or in some automatic actions generated by system
99
*/
1010
abstract getSystemSessionMetadata(): SessionMetadata;
11+
12+
/**
13+
* Should return generated anonymous id based on sessionMetadata or default value
14+
* @param sessionMetadata
15+
*/
16+
abstract getAnonymousId(sessionMetadata?: SessionMetadata): string;
1117
}

redisinsight/api/src/modules/constants/providers/local.constants.provider.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,11 @@ export class LocalConstantsProvider extends ConstantsProvider {
1111
sessionId: '1',
1212
};
1313
}
14+
15+
/**
16+
* @inheritDoc
17+
*/
18+
getAnonymousId(sessionMetadata?: SessionMetadata): string {
19+
return sessionMetadata?.userId ?? 'unknown';
20+
}
1421
}

redisinsight/api/src/utils/converter.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
convertIntToSemanticVersion,
33
convertStringToNumber,
4+
convertAnyStringToPositiveInteger,
45
} from './converter';
56

67
const convertIntToSemanticVersionTests: Record<string, any>[] = [
@@ -49,3 +50,24 @@ describe('convertStringToNumber', () => {
4950
});
5051
});
5152
});
53+
54+
const convertAnyStringToPositiveIntegerTests = [
55+
[undefined, -1],
56+
[null, -1],
57+
[123123, -1],
58+
[[], -1],
59+
[{}, -1],
60+
[{ length: 12 }, -1],
61+
['', -1],
62+
['1', 49],
63+
['4f5daa5e-6139-4e95-8e7c-3283287f4218', 1347108680],
64+
[
65+
(new Array(1000).fill('4f5daa5e-6139-4e95-8e7c-3283287f4218')).join(),
66+
229890988,
67+
],
68+
] as [string, number][];
69+
describe('convertAnyStringToPositiveInteger', () => {
70+
it.each(convertAnyStringToPositiveIntegerTests)('for input: %s, should return: %s', (input, result) => {
71+
expect(convertAnyStringToPositiveInteger(input)).toEqual(result);
72+
});
73+
});

redisinsight/api/src/utils/converter.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,19 @@ export const convertStringToNumber = (value: any, defaultValue?: number): number
3636

3737
return num;
3838
};
39+
40+
export const convertAnyStringToPositiveInteger = (str: string) => {
41+
if (!isString(str) || !str.length) {
42+
return -1;
43+
}
44+
45+
let hash = 0;
46+
for (let i = 0; i < str.length; i += 1) {
47+
const char = str.charCodeAt(i);
48+
// eslint-disable-next-line no-bitwise
49+
hash = (hash << 5) - hash + char;
50+
// eslint-disable-next-line no-bitwise
51+
hash |= 0;
52+
}
53+
return Math.abs(hash);
54+
};

0 commit comments

Comments
 (0)