Skip to content

Commit 01e8c40

Browse files
committed
RI-6634 - Inconsistent behavior in displaying size
1 parent 5da9333 commit 01e8c40

18 files changed

+996
-650
lines changed

redisinsight/api/src/modules/browser/keys/dto/get.keys-info.dto.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ import { IsRedisString, RedisStringType } from 'src/common/decorators';
44
import { RedisString } from 'src/common/constants';
55
import { KeyDto, RedisDataType } from './key.dto';
66

7-
export class GetKeyInfoDto extends KeyDto { }
7+
export class GetKeyInfoDto extends KeyDto {
8+
@ApiPropertyOptional({
9+
description: 'Flag to determine if size should be requested and shown in the response',
10+
type: Boolean,
11+
default: false,
12+
})
13+
@IsOptional()
14+
getSize?: boolean;
15+
}
816

917
export class GetKeysInfoDto {
1018
@ApiProperty({

redisinsight/api/src/modules/browser/keys/key-info/strategies/hash.key-info.strategy.spec.ts

Lines changed: 110 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -34,88 +34,119 @@ describe('HashKeyInfoStrategy', () => {
3434

3535
describe('getInfo', () => {
3636
const key = getKeyInfoResponse.name;
37-
it('should return appropriate value', async () => {
38-
when(mockStandaloneRedisClient.sendPipeline)
39-
.calledWith([
40-
[BrowserToolKeysCommands.Ttl, key],
41-
[BrowserToolHashCommands.HLen, key],
42-
])
43-
.mockResolvedValueOnce([
44-
[null, -1],
45-
[null, 10],
46-
]);
47-
48-
when(mockStandaloneRedisClient.sendPipeline)
49-
.calledWith([
50-
[BrowserToolKeysCommands.MemoryUsage, key, 'samples', '0'],
51-
])
52-
.mockResolvedValueOnce([
53-
[null, 50],
54-
]);
55-
56-
const result = await strategy.getInfo(
57-
mockStandaloneRedisClient,
58-
key,
59-
RedisDataType.Hash,
60-
);
61-
62-
expect(result).toEqual(getKeyInfoResponse);
63-
});
6437

65-
it('should return size with null value when memory usage fails', async () => {
66-
const replyError: ReplyError = {
67-
name: 'ReplyError',
68-
command: BrowserToolKeysCommands.MemoryUsage,
69-
message: "ERR unknown command 'memory'",
70-
};
71-
when(mockStandaloneRedisClient.sendPipeline)
72-
.calledWith([
73-
[BrowserToolKeysCommands.Ttl, key],
74-
[BrowserToolHashCommands.HLen, key],
75-
])
76-
.mockResolvedValueOnce([
77-
[null, -1],
78-
[null, 10],
79-
]);
80-
81-
when(mockStandaloneRedisClient.sendPipeline)
82-
.calledWith([
83-
[BrowserToolKeysCommands.MemoryUsage, key, 'samples', '0'],
84-
])
85-
.mockResolvedValueOnce([
86-
[replyError, null],
87-
]);
88-
89-
const result = await strategy.getInfo(
90-
mockStandaloneRedisClient,
91-
key,
92-
RedisDataType.Hash,
93-
);
94-
95-
expect(result).toEqual({ ...getKeyInfoResponse, size: null });
38+
describe('when getSize is true', () => {
39+
it('should return all info in single pipeline', async () => {
40+
when(mockStandaloneRedisClient.sendPipeline)
41+
.calledWith([
42+
[BrowserToolKeysCommands.Ttl, key],
43+
[BrowserToolHashCommands.HLen, key],
44+
[BrowserToolKeysCommands.MemoryUsage, key, 'samples', '0'],
45+
])
46+
.mockResolvedValueOnce([
47+
[null, -1],
48+
[null, 10],
49+
[null, 50],
50+
]);
51+
52+
const result = await strategy.getInfo(
53+
mockStandaloneRedisClient,
54+
key,
55+
RedisDataType.Hash,
56+
true,
57+
);
58+
59+
expect(result).toEqual(getKeyInfoResponse);
60+
});
9661
});
9762

98-
it('should not check size when length >= 50,000', async () => {
99-
when(mockStandaloneRedisClient.sendPipeline)
100-
.calledWith([
101-
[BrowserToolKeysCommands.Ttl, key],
102-
[BrowserToolHashCommands.HLen, key],
103-
])
104-
.mockResolvedValueOnce([
105-
[null, -1],
106-
[null, 50000],
107-
]);
108-
109-
const result = await strategy.getInfo(
110-
mockStandaloneRedisClient,
111-
key,
112-
RedisDataType.Hash,
113-
);
114-
115-
expect(result).toEqual({
116-
...getKeyInfoResponse,
117-
length: 50000,
118-
size: -1
63+
describe('when getSize is false', () => {
64+
it('should return appropriate value', async () => {
65+
when(mockStandaloneRedisClient.sendPipeline)
66+
.calledWith([
67+
[BrowserToolKeysCommands.Ttl, key],
68+
[BrowserToolHashCommands.HLen, key],
69+
])
70+
.mockResolvedValueOnce([
71+
[null, -1],
72+
[null, 10],
73+
]);
74+
75+
when(mockStandaloneRedisClient.sendPipeline)
76+
.calledWith([
77+
[BrowserToolKeysCommands.MemoryUsage, key, 'samples', '0'],
78+
])
79+
.mockResolvedValueOnce([
80+
[null, 50],
81+
]);
82+
83+
const result = await strategy.getInfo(
84+
mockStandaloneRedisClient,
85+
key,
86+
RedisDataType.Hash,
87+
false,
88+
);
89+
90+
expect(result).toEqual(getKeyInfoResponse);
91+
});
92+
93+
it('should return size with null value when memory usage fails', async () => {
94+
const replyError: ReplyError = {
95+
name: 'ReplyError',
96+
command: BrowserToolKeysCommands.MemoryUsage,
97+
message: "ERR unknown command 'memory'",
98+
};
99+
when(mockStandaloneRedisClient.sendPipeline)
100+
.calledWith([
101+
[BrowserToolKeysCommands.Ttl, key],
102+
[BrowserToolHashCommands.HLen, key],
103+
])
104+
.mockResolvedValueOnce([
105+
[null, -1],
106+
[null, 10],
107+
]);
108+
109+
when(mockStandaloneRedisClient.sendPipeline)
110+
.calledWith([
111+
[BrowserToolKeysCommands.MemoryUsage, key, 'samples', '0'],
112+
])
113+
.mockResolvedValueOnce([
114+
[replyError, null],
115+
]);
116+
117+
const result = await strategy.getInfo(
118+
mockStandaloneRedisClient,
119+
key,
120+
RedisDataType.Hash,
121+
false,
122+
);
123+
124+
expect(result).toEqual({ ...getKeyInfoResponse, size: null });
125+
});
126+
127+
it('should not check size when length >= 50,000', async () => {
128+
when(mockStandaloneRedisClient.sendPipeline)
129+
.calledWith([
130+
[BrowserToolKeysCommands.Ttl, key],
131+
[BrowserToolHashCommands.HLen, key],
132+
])
133+
.mockResolvedValueOnce([
134+
[null, -1],
135+
[null, 50000],
136+
]);
137+
138+
const result = await strategy.getInfo(
139+
mockStandaloneRedisClient,
140+
key,
141+
RedisDataType.Hash,
142+
false,
143+
);
144+
145+
expect(result).toEqual({
146+
...getKeyInfoResponse,
147+
length: 50000,
148+
size: -1
149+
});
119150
});
120151
});
121152
});

redisinsight/api/src/modules/browser/keys/key-info/strategies/hash.key-info.strategy.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,30 @@ export class HashKeyInfoStrategy extends KeyInfoStrategy {
1212
client: RedisClient,
1313
key: RedisString,
1414
type: string,
15+
getSize: boolean,
1516
): Promise<GetKeyInfoResponse> {
1617
this.logger.debug(`Getting ${RedisDataType.Hash} type info.`);
1718

19+
if (getSize !== false) {
20+
const [
21+
[, ttl = null],
22+
[, length = null],
23+
[, size = null],
24+
] = await client.sendPipeline([
25+
[BrowserToolKeysCommands.Ttl, key],
26+
[BrowserToolHashCommands.HLen, key],
27+
[BrowserToolKeysCommands.MemoryUsage, key, 'samples', '0'],
28+
]) as [any, number][];
29+
30+
return {
31+
name: key,
32+
type,
33+
ttl,
34+
size,
35+
length,
36+
};
37+
}
38+
1839
const [
1940
[, ttl = null],
2041
[, length = null],
@@ -38,5 +59,6 @@ export class HashKeyInfoStrategy extends KeyInfoStrategy {
3859
size,
3960
length,
4061
};
62+
4163
}
4264
}

redisinsight/api/src/modules/browser/keys/key-info/strategies/key-info.strategy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export abstract class KeyInfoStrategy {
1111
client: RedisClient,
1212
key: RedisString,
1313
type: string,
14+
getSize: boolean,
1415
): Promise<GetKeyInfoResponse>;
1516
}

0 commit comments

Comments
 (0)