Skip to content

Commit f28402c

Browse files
authored
feat: duration & endtime in queries history (#1169)
1 parent 345600f commit f28402c

File tree

5 files changed

+94
-8
lines changed

5 files changed

+94
-8
lines changed

src/containers/Tenant/Query/QueriesHistory/QueriesHistory.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import {TENANT_QUERY_TABS_ID} from '../../../../store/reducers/tenant/constants'
1313
import {setQueryTab} from '../../../../store/reducers/tenant/tenant';
1414
import type {QueryInHistory} from '../../../../types/store/executeQuery';
1515
import {cn} from '../../../../utils/cn';
16+
import {formatDateTime} from '../../../../utils/dataFormatters/dataFormatters';
1617
import {useTypedDispatch, useTypedSelector} from '../../../../utils/hooks';
18+
import {formatToMs, parseUsToMs} from '../../../../utils/timeParsers';
1719
import {MAX_QUERY_HEIGHT, QUERY_TABLE_SETTINGS} from '../../utils/constants';
1820
import i18n from '../i18n';
1921

@@ -46,7 +48,7 @@ function QueriesHistory({changeUserInput}: QueriesHistoryProps) {
4648
const columns: Column<QueryInHistory>[] = [
4749
{
4850
name: 'queryText',
49-
header: 'Query Text',
51+
header: i18n('history.queryText'),
5052
render: ({row}) => {
5153
return (
5254
<div className={b('query')}>
@@ -57,6 +59,22 @@ function QueriesHistory({changeUserInput}: QueriesHistoryProps) {
5759
sortable: false,
5860
width: 600,
5961
},
62+
{
63+
name: 'EndTime',
64+
header: i18n('history.endTime'),
65+
render: ({row}) => (row.endTime ? formatDateTime(row.endTime.toString()) : '-'),
66+
align: 'right',
67+
width: 200,
68+
sortable: false,
69+
},
70+
{
71+
name: 'Duration',
72+
header: i18n('history.duration'),
73+
render: ({row}) => (row.durationUs ? formatToMs(parseUsToMs(row.durationUs)) : '-'),
74+
align: 'right',
75+
width: 150,
76+
sortable: false,
77+
},
6078
];
6179

6280
return (

src/containers/Tenant/Query/QueryEditor/QueryEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function QueryEditor(props: QueryEditorProps) {
179179
if (!text) {
180180
const {queries, currentIndex} = history;
181181
if (query !== queries[currentIndex]?.queryText) {
182-
props.saveQueryToHistory(input);
182+
props.saveQueryToHistory(input, queryId);
183183
}
184184
}
185185
dispatchResultVisibilityState(PaneVisibilityActionTypes.triggerExpand);

src/containers/Tenant/Query/i18n/en.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,9 @@
5252
"filter.text.placeholder": "Search by query text...",
5353

5454
"gear.tooltip": "Query execution settings have been changed for ",
55-
"banner.query-settings.message": "Query results are displayed for "
55+
"banner.query-settings.message": "Query results are displayed for ",
56+
57+
"history.queryText": "Query text",
58+
"history.endTime": "End time",
59+
"history.duration": "Duration"
5660
}

src/store/reducers/executeQuery.ts

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const MAXIMUM_QUERIES_IN_HISTORY = 20;
2525

2626
const CHANGE_USER_INPUT = 'query/CHANGE_USER_INPUT';
2727
const SAVE_QUERY_TO_HISTORY = 'query/SAVE_QUERY_TO_HISTORY';
28+
const UPDATE_QUERY_IN_HISTORY = 'query/UPDATE_QUERY_IN_HISTORY';
2829
const SET_QUERY_HISTORY_FILTER = 'query/SET_QUERY_HISTORY_FILTER';
2930
const GO_TO_PREVIOUS_QUERY = 'query/GO_TO_PREVIOUS_QUERY';
3031
const GO_TO_NEXT_QUERY = 'query/GO_TO_NEXT_QUERY';
@@ -65,9 +66,9 @@ const executeQuery: Reducer<ExecuteQueryState, ExecuteQueryAction> = (
6566
}
6667

6768
case SAVE_QUERY_TO_HISTORY: {
68-
const queryText = action.data;
69+
const {queryText, queryId} = action.data;
6970

70-
const newQueries = [...state.history.queries, {queryText}].slice(
71+
const newQueries = [...state.history.queries, {queryText, queryId}].slice(
7172
state.history.queries.length >= MAXIMUM_QUERIES_IN_HISTORY ? 1 : 0,
7273
);
7374
settingsManager.setUserSettingsValue(QUERIES_HISTORY_KEY, newQueries);
@@ -82,6 +83,38 @@ const executeQuery: Reducer<ExecuteQueryState, ExecuteQueryAction> = (
8283
};
8384
}
8485

86+
case UPDATE_QUERY_IN_HISTORY: {
87+
const {queryId, stats} = action.data;
88+
89+
if (!stats) {
90+
return state;
91+
}
92+
93+
const index = state.history.queries.findIndex((item) => item.queryId === queryId);
94+
95+
if (index === -1) {
96+
return state;
97+
}
98+
99+
const newQueries = [...state.history.queries];
100+
const {durationUs, endTime} = stats;
101+
newQueries.splice(index, 1, {
102+
...state.history.queries[index],
103+
durationUs,
104+
endTime,
105+
});
106+
107+
settingsManager.setUserSettingsValue(QUERIES_HISTORY_KEY, newQueries);
108+
109+
return {
110+
...state,
111+
history: {
112+
...state.history,
113+
queries: newQueries,
114+
},
115+
};
116+
}
117+
85118
case GO_TO_PREVIOUS_QUERY: {
86119
const currentIndex = state.history.currentIndex;
87120
if (currentIndex <= 0) {
@@ -150,6 +183,11 @@ interface SendQueryParams extends QueryRequestParams {
150183
enableTracingLevel?: boolean;
151184
}
152185

186+
interface QueryStats {
187+
durationUs?: string | number;
188+
endTime?: string | number;
189+
}
190+
153191
export const executeQueryApi = api.injectEndpoints({
154192
endpoints: (build) => ({
155193
executeQuery: build.mutation<IQueryResult, SendQueryParams>({
@@ -162,7 +200,7 @@ export const executeQueryApi = api.injectEndpoints({
162200
enableTracingLevel,
163201
queryId,
164202
},
165-
{signal},
203+
{signal, dispatch},
166204
) => {
167205
let action: ExecuteActions = 'execute';
168206
let syntax: QuerySyntax = QUERY_SYNTAX.yql;
@@ -175,6 +213,7 @@ export const executeQueryApi = api.injectEndpoints({
175213
}
176214

177215
try {
216+
const timeStart = Date.now();
178217
const response = await window.api.sendQuery(
179218
{
180219
schema,
@@ -201,6 +240,19 @@ export const executeQueryApi = api.injectEndpoints({
201240
}
202241

203242
const data = parseQueryAPIExecuteResponse(response);
243+
244+
const queryStats: QueryStats = {};
245+
if (data.stats) {
246+
const {DurationUs, Executions: [{FinishTimeMs}] = [{}]} = data.stats;
247+
queryStats.durationUs = DurationUs;
248+
queryStats.endTime = FinishTimeMs;
249+
} else {
250+
const now = Date.now();
251+
queryStats.durationUs = (now - timeStart) * 1000;
252+
queryStats.endTime = now;
253+
}
254+
255+
dispatch(updateQueryInHistory(queryStats, queryId));
204256
return {data};
205257
} catch (error) {
206258
return {error};
@@ -211,13 +263,20 @@ export const executeQueryApi = api.injectEndpoints({
211263
overrideExisting: 'throw',
212264
});
213265

214-
export const saveQueryToHistory = (queryText: string) => {
266+
export const saveQueryToHistory = (queryText: string, queryId: string) => {
215267
return {
216268
type: SAVE_QUERY_TO_HISTORY,
217-
data: queryText,
269+
data: {queryText, queryId},
218270
} as const;
219271
};
220272

273+
export function updateQueryInHistory(stats: QueryStats, queryId: string) {
274+
return {
275+
type: UPDATE_QUERY_IN_HISTORY,
276+
data: {queryId, stats},
277+
} as const;
278+
}
279+
221280
export const goToPreviousQuery = () => {
222281
return {
223282
type: GO_TO_PREVIOUS_QUERY,

src/types/store/executeQuery.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import type {
55
saveQueryToHistory,
66
setQueryHistoryFilter,
77
setTenantPath,
8+
updateQueryInHistory,
89
} from '../../store/reducers/executeQuery';
910

1011
export interface QueryInHistory {
12+
queryId?: string;
1113
queryText: string;
1214
syntax?: string;
15+
endTime?: string | number;
16+
durationUs?: string | number;
1317
}
1418

1519
export interface ExecuteQueryState {
@@ -29,6 +33,7 @@ export type ExecuteQueryAction =
2933
| ReturnType<typeof goToPreviousQuery>
3034
| ReturnType<typeof changeUserInput>
3135
| ReturnType<typeof saveQueryToHistory>
36+
| ReturnType<typeof updateQueryInHistory>
3237
| ReturnType<typeof setTenantPath>
3338
| ReturnType<typeof setQueryHistoryFilter>;
3439

0 commit comments

Comments
 (0)