Skip to content

Commit 34be197

Browse files
github-actions[bot]Kishore Kumaar Natarajan
and
Kishore Kumaar Natarajan
committed
fetch only once if the first call returns a valid query (#182)
Signed-off-by: Kishore Kumaar Natarajan <[email protected]> Co-authored-by: Kishore Kumaar Natarajan <[email protected]> (cherry picked from commit b00c4b4) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 9bc38a0 commit 34be197

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

common/utils/QueryUtils.test.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,33 @@ describe('retrieveQueryById - Fetch Query Record by ID from API', () => {
3232
},
3333
};
3434

35-
it('should make three GET requests to fetch different query records', async () => {
35+
it('should fetch only once if the first call returns a valid query', async () => {
3636
mockCore.http.get.mockResolvedValue(mockResponse);
3737

3838
await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId);
3939

40-
expect(mockCore.http.get).toHaveBeenCalledTimes(3);
40+
expect(mockCore.http.get).toHaveBeenCalledTimes(1);
4141
expect(mockCore.http.get).toHaveBeenCalledWith('/api/top_queries/latency', {
4242
query: { from: testStart, to: testEnd, id: testId, dataSourceId: undefined },
4343
});
44-
expect(mockCore.http.get).toHaveBeenCalledWith('/api/top_queries/cpu', {
44+
});
45+
46+
it('should try the next endpoints if the first call returns empty', async () => {
47+
mockCore.http.get
48+
.mockResolvedValueOnce({ response: { top_queries: [] } }) // latency - empty
49+
.mockResolvedValueOnce({ response: { top_queries: [] } }) // cpu - empty
50+
.mockResolvedValueOnce(mockResponse); // memory - found
51+
52+
await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId);
53+
54+
expect(mockCore.http.get).toHaveBeenCalledTimes(3);
55+
expect(mockCore.http.get).toHaveBeenNthCalledWith(1, '/api/top_queries/latency', {
56+
query: { from: testStart, to: testEnd, id: testId, dataSourceId: undefined },
57+
});
58+
expect(mockCore.http.get).toHaveBeenNthCalledWith(2, '/api/top_queries/cpu', {
4559
query: { from: testStart, to: testEnd, id: testId, dataSourceId: undefined },
4660
});
47-
expect(mockCore.http.get).toHaveBeenCalledWith('/api/top_queries/memory', {
61+
expect(mockCore.http.get).toHaveBeenNthCalledWith(3, '/api/top_queries/memory', {
4862
query: { from: testStart, to: testEnd, id: testId, dataSourceId: undefined },
4963
});
5064
});
@@ -57,42 +71,39 @@ describe('retrieveQueryById - Fetch Query Record by ID from API', () => {
5771
expect(result).toEqual(mockResponse.response.top_queries[0]);
5872
});
5973

60-
it('should return null if no queries are found', async () => {
74+
it('should return null if all responses are empty', async () => {
6175
mockCore.http.get.mockResolvedValue({ response: { top_queries: [] } });
6276

6377
const result = await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId);
6478

6579
expect(result).toBeNull();
80+
expect(mockCore.http.get).toHaveBeenCalledTimes(3);
6681
});
67-
it('should return null if API response is missing the response field', async () => {
82+
83+
it('should return null if all API responses are missing response field', async () => {
6884
mockCore.http.get.mockResolvedValue({});
6985

7086
const result = await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId);
7187

7288
expect(result).toBeNull();
89+
expect(mockCore.http.get).toHaveBeenCalledTimes(3);
7390
});
7491

75-
it('should return null if API response contains an unexpected structure', async () => {
92+
it('should return null if all API responses have unexpected structure', async () => {
7693
mockCore.http.get.mockResolvedValue({ unexpectedKey: {} });
7794

7895
const result = await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId);
7996

8097
expect(result).toBeNull();
98+
expect(mockCore.http.get).toHaveBeenCalledTimes(3);
8199
});
82100

83-
it('should return null if API request fails', async () => {
101+
it('should return null if all API requests fail', async () => {
84102
mockCore.http.get.mockRejectedValue(new Error('API error'));
85103

86104
const result = await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId);
87105

88106
expect(result).toBeNull();
89-
});
90-
91-
it('should handle cases where API returns an empty object instead of expected response structure', async () => {
92-
mockCore.http.get.mockResolvedValue({});
93-
94-
const result = await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId);
95-
96-
expect(result).toBeNull();
107+
expect(mockCore.http.get).toHaveBeenCalledTimes(3);
97108
});
98109
});

common/utils/QueryUtils.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,19 @@ export const retrieveQueryById = async (
4545
};
4646

4747
try {
48-
const topQueriesResponse = await Promise.any([
49-
fetchMetric(`/api/top_queries/latency`),
50-
fetchMetric(`/api/top_queries/cpu`),
51-
fetchMetric(`/api/top_queries/memory`),
52-
]);
53-
return topQueriesResponse.response.top_queries[0] || null;
48+
const endpoints = [
49+
`/api/top_queries/latency`,
50+
`/api/top_queries/cpu`,
51+
`/api/top_queries/memory`,
52+
];
53+
54+
for (const endpoint of endpoints) {
55+
const result = await fetchMetric(endpoint);
56+
if (result.response.top_queries.length > 0) {
57+
return result.response.top_queries[0];
58+
}
59+
}
60+
return null;
5461
} catch (error) {
5562
console.error('Error retrieving query details:', error);
5663
return null;

0 commit comments

Comments
 (0)