Skip to content

Commit e763c96

Browse files
FelipeGLopezsadpandajoe
authored andcommitted
fix(SQLLab): most recent queries at the top in Query History without refreshing the page (#36359)
(cherry picked from commit 62d86ab)
1 parent 0753fd4 commit e763c96

2 files changed

Lines changed: 114 additions & 1 deletion

File tree

superset-frontend/src/SqlLab/components/QueryHistory/QueryHistory.test.tsx

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,112 @@ test('fetches the query history by the tabViewId', async () => {
133133
expect(queryResultText).toBeInTheDocument();
134134
isFeatureEnabledMock.mockClear();
135135
});
136+
137+
test('displays multiple queries with newest query first', async () => {
138+
const isFeatureEnabledMock = mockedIsFeatureEnabled.mockImplementation(
139+
featureFlag => featureFlag === FeatureFlag.SqllabBackendPersistence,
140+
);
141+
142+
const multipleQueriesApiResult = {
143+
count: 2,
144+
ids: [694, 693],
145+
result: [
146+
{
147+
changed_on: '2024-03-12T20:10:02.497775',
148+
client_id: 'd2ZDzRYzn',
149+
database: {
150+
database_name: 'examples',
151+
id: 1,
152+
},
153+
end_time: '1710274202496.047852',
154+
error_message: null,
155+
executed_sql: 'SELECT COUNT(*) from "FCC 2018 Survey"\nLIMIT 1001',
156+
id: 694,
157+
limit: 1000,
158+
limiting_factor: 'DROPDOWN',
159+
progress: 100,
160+
results_key: null,
161+
rows: 1,
162+
schema: 'main',
163+
select_as_cta: false,
164+
sql: 'SELECT COUNT(*) from "FCC 2018 Survey"',
165+
sql_editor_id: '22',
166+
start_time: '1710274202445.992920',
167+
status: QueryState.Success,
168+
tab_name: 'Untitled Query 2',
169+
tmp_table_name: null,
170+
tracking_url: null,
171+
user: {
172+
first_name: 'admin',
173+
id: 1,
174+
last_name: 'user',
175+
},
176+
},
177+
{
178+
changed_on: '2024-03-12T20:01:02.497775',
179+
client_id: 'b0ZDzRYzn',
180+
database: {
181+
database_name: 'examples',
182+
id: 1,
183+
},
184+
end_time: '1710273662496.047852',
185+
error_message: null,
186+
executed_sql: 'SELECT * from "FCC 2018 Survey"\nLIMIT 1001',
187+
id: 693,
188+
limit: 1000,
189+
limiting_factor: 'DROPDOWN',
190+
progress: 100,
191+
results_key: null,
192+
rows: 443,
193+
schema: 'main',
194+
select_as_cta: false,
195+
sql: 'SELECT * from "FCC 2018 Survey"',
196+
sql_editor_id: '22',
197+
start_time: '1710273662445.992920',
198+
status: QueryState.Success,
199+
tab_name: 'Untitled Query 1',
200+
tmp_table_name: null,
201+
tracking_url: null,
202+
user: {
203+
first_name: 'admin',
204+
id: 1,
205+
last_name: 'user',
206+
},
207+
},
208+
],
209+
};
210+
211+
const editorQueryApiRoute = `glob:*/api/v1/query/?q=*`;
212+
fetchMock.get(editorQueryApiRoute, multipleQueriesApiResult);
213+
const { container } = render(setup(), { useRedux: true, initialState });
214+
215+
await waitFor(() =>
216+
expect(fetchMock.calls(editorQueryApiRoute).length).toBe(1),
217+
);
218+
219+
expect(screen.getByTestId('listview-table')).toBeVisible();
220+
expect(screen.getByRole('table')).toBeVisible();
221+
222+
const tableRows = container.querySelectorAll(
223+
'table > tbody > tr:not(.ant-table-measure-row)',
224+
);
225+
expect(tableRows).toHaveLength(2);
226+
227+
// Check that both queries are present
228+
const olderQueryRow = screen.getByText('443');
229+
const newerQueryElements = screen.getAllByText('1');
230+
expect(olderQueryRow).toBeInTheDocument();
231+
expect(newerQueryElements.length).toBeGreaterThan(0);
232+
233+
// Verify ordering: newer query (1 row) should appear before older query (443 rows)
234+
// Find the actual row elements to check their order
235+
const firstDataRow = tableRows[0];
236+
const secondDataRow = tableRows[1];
237+
238+
// The newer query should be in the first row (has 1 result row)
239+
expect(firstDataRow).toHaveTextContent('1');
240+
// The older query should be in the second row (has 443 result rows)
241+
expect(secondDataRow).toHaveTextContent('443');
242+
243+
isFeatureEnabledMock.mockClear();
244+
});

superset-frontend/src/SqlLab/components/QueryHistory/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ const QueryHistory = ({
9797
editorId,
9898
)
9999
.concat(data.result)
100-
.reverse()
100+
.sort((a, b) => {
101+
const aTime = a.startDttm || 0;
102+
const bTime = b.startDttm || 0;
103+
return aTime - bTime;
104+
})
101105
: getEditorQueries(queries, editorId),
102106
[queries, data, editorId],
103107
);

0 commit comments

Comments
 (0)