Skip to content

Commit 9d2e65c

Browse files
committed
- Add null checks for generate profile command functions
- #1537 (comment)
1 parent f770a43 commit 9d2e65c

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

redisinsight/ui/src/pages/workbench/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const GRAPH_COMMANDS = ['graph.query']
3434
const ALLOWED_PROFILE_COMMANDS = [...SEARCH_COMMANDS, ...GRAPH_COMMANDS]
3535

3636
export const isCommandAllowedForProfile = (query: string) => {
37-
return ALLOWED_PROFILE_COMMANDS.includes(query.split(' ')[0].toLowerCase())
37+
return ALLOWED_PROFILE_COMMANDS.includes(query?.split(' ')?.[0]?.toLowerCase())
3838
}
3939

4040
export enum ProfileQueryType {

redisinsight/ui/src/pages/workbench/utils.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ const generateGraphProfileQueryTests: Record<string, any>[] = [
1212
{ input: 'GRAPH.QUERY key "MATCH (n) RETURN n"', output: 'graph.explain key "MATCH (n) RETURN n"', type: ProfileQueryType.Explain },
1313
{ input: 'graph.query key "MATCH (n) RETURN n"', output: 'graph.profile key "MATCH (n) RETURN n"', type: ProfileQueryType.Profile },
1414
{ input: 'graph.query key "MATCH (n) RETURN n"', output: 'graph.explain key "MATCH (n) RETURN n"', type: ProfileQueryType.Explain },
15+
{ input: null, output: null, type: ProfileQueryType.Profile },
16+
{ input: null, output: null, type: ProfileQueryType.Explain },
1517
]
1618

1719
describe('generateGraphProfileQuery', () => {
18-
1920
generateGraphProfileQueryTests.forEach(test => {
2021
it(`should be output: ${test.output} for input: ${test.input} and type: ${test.type}`, () => {
2122
const result = generateGraphProfileQuery(test.input, test.type);
22-
2323
expect(result).toEqual(test.output);
2424
});
2525
})
@@ -29,24 +29,24 @@ describe('generateGraphProfileQuery', () => {
2929
const generateSearchProfileQueryTests: Record<string, any>[] = [
3030
{ input: 'FT.SEARCH index tomatoes', output: 'ft.profile index SEARCH QUERY tomatoes', type: ProfileQueryType.Profile },
3131
{ input: 'FT.AGGREGATE index tomatoes', output: 'ft.profile index AGGREGATE QUERY tomatoes', type: ProfileQueryType.Profile },
32-
{ input: 'FT.SEARCH index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
32+
{ input: 'FT.SEARCH index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
3333
{ input: 'FT.AGGREGATE index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
34-
{ input: 'ft.search index tomatoes', output: 'ft.profile index search QUERY tomatoes', type: ProfileQueryType.Profile },
34+
{ input: 'ft.search index tomatoes', output: 'ft.profile index search QUERY tomatoes', type: ProfileQueryType.Profile },
3535
{ input: 'ft.aggregate index tomatoes', output: 'ft.profile index aggregate QUERY tomatoes', type: ProfileQueryType.Profile },
36-
{ input: 'ft.search index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
36+
{ input: 'ft.search index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
3737
{ input: 'ft.aggregate index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
38-
{ input: 'ft.SEARCH index tomatoes', output: 'ft.profile index SEARCH QUERY tomatoes', type: ProfileQueryType.Profile },
38+
{ input: 'ft.SEARCH index tomatoes', output: 'ft.profile index SEARCH QUERY tomatoes', type: ProfileQueryType.Profile },
3939
{ input: 'ft.AGGREGATE index tomatoes', output: 'ft.profile index AGGREGATE QUERY tomatoes', type: ProfileQueryType.Profile },
40-
{ input: 'ft.SEARCH index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
40+
{ input: 'ft.SEARCH index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
4141
{ input: 'ft.AGGREGATE index tomatoes', output: 'ft.explain index tomatoes', type: ProfileQueryType.Explain },
42+
{ input: null, output: null, type: ProfileQueryType.Profile },
43+
{ input: null, output: null, type: ProfileQueryType.Explain },
4244
]
4345

4446
describe('generateSearchProfileQuery', () => {
45-
4647
generateSearchProfileQueryTests.forEach(test => {
4748
it(`should be output: ${test.output} for input: ${test.input} and type: ${test.type}`, () => {
4849
const result = generateSearchProfileQuery(test.input, test.type);
49-
5050
expect(result).toEqual(test.output);
5151
});
5252
})
@@ -63,9 +63,9 @@ const generateProfileQueryForCommandTests: Record<string, any>[] = [
6363
{ input: 'GRAPH.EXPLAIN key "MATCH (n) RETURN n"', output: null, type: ProfileQueryType.Explain },
6464
{ input: 'ft._LIST', output: null, type: ProfileQueryType.Profile },
6565
{ input: 'ft._LIST', output: null, type: ProfileQueryType.Explain },
66-
{ input: 'ft.profile index SEARCH QUERY tomatoes', output: null, type: ProfileQueryType.Profile },
66+
{ input: 'ft.profile index SEARCH QUERY tomatoes', output: null, type: ProfileQueryType.Profile },
6767
{ input: 'ft.profile index AGGREGATE QUERY tomatoes', output: null, type: ProfileQueryType.Explain },
68-
{ input: 'ft.explain index tomatoes', output: null, type: ProfileQueryType.Profile },
68+
{ input: 'ft.explain index tomatoes', output: null, type: ProfileQueryType.Profile },
6969
{ input: 'ft.explain index tomatoes', output: null, type: ProfileQueryType.Explain },
7070
]
7171
describe('generateProfileQueryForCommand', () => {

redisinsight/ui/src/pages/workbench/utils.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
import { ProfileQueryType, SEARCH_COMMANDS, GRAPH_COMMANDS } from './constants'
22

33
export const generateGraphProfileQuery = (query: string, type: ProfileQueryType) => {
4-
return [`graph.${type.toLowerCase()}`, ...query.split(' ').slice(1)].join(' ')
4+
const q = query?.split(' ')?.slice(1)
5+
6+
if (q) {
7+
return [`graph.${type.toLowerCase()}`, ...q].join(' ')
8+
}
9+
10+
return null
511
}
612

713
export const generateSearchProfileQuery = (query: string, type: ProfileQueryType) => {
8-
const commandSplit = query.split(' ')
9-
const cmd = commandSplit[0]
14+
const commandSplit = query?.split(' ')
15+
const cmd = commandSplit?.[0]
16+
17+
if (!commandSplit || !cmd) {
18+
return null
19+
}
1020

1121
if (type === ProfileQueryType.Explain) {
12-
return [`ft.${type.toLowerCase()}`, ...commandSplit.slice(1)].join(' ')
22+
return [`ft.${type.toLowerCase()}`, ...commandSplit?.slice(1)].join(' ')
1323
} else {
14-
let index = commandSplit[1]
24+
let index = commandSplit?.[1]
1525

16-
const queryType = cmd.split('.')[1] // SEARCH / AGGREGATE
17-
return [`ft.${type.toLowerCase()}`, index, queryType, 'QUERY', ...commandSplit.slice(2)].join(' ')
26+
const queryType = cmd.split('.')?.[1] // SEARCH / AGGREGATE
27+
return [`ft.${type.toLowerCase()}`, index, queryType, 'QUERY', ...commandSplit?.slice(2)].join(' ')
1828
}
1929
}
2030

2131
export const generateProfileQueryForCommand = (query: string, type: ProfileQueryType) => {
22-
const cmd = query.split(' ')[0].toLowerCase()
32+
const cmd = query?.split(' ')?.[0]?.toLowerCase()
2333

2434
if (GRAPH_COMMANDS.includes(cmd)) {
2535
return generateGraphProfileQuery(query, type)

0 commit comments

Comments
 (0)