Skip to content

Commit c9f0d7f

Browse files
RI-6928 fix sessionId calculation to match schema requirements (#4417)
1 parent 3bff851 commit c9f0d7f

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { AppType } from 'src/modules/server/models/server';
1414
import { SettingsService } from 'src/modules/settings/settings.service';
1515
import { ConstantsProvider } from 'src/modules/constants/providers/constants.provider';
1616
import { LocalConstantsProvider } from 'src/modules/constants/providers/local.constants.provider';
17+
import { convertAnyStringToPositiveInteger } from 'src/utils';
1718
import {
1819
AnalyticsService,
1920
Telemetry,
@@ -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: 2 additions & 2 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

@@ -87,8 +88,7 @@ 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;

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)