-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathactiveStatementsView.tsx
170 lines (155 loc) · 4.99 KB
/
activeStatementsView.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
import React, { useEffect, useState } from "react";
import classNames from "classnames/bind";
import { useHistory } from "react-router-dom";
import {
ISortedTablePagination,
Loading,
PageConfig,
PageConfigItem,
Search,
SortSetting,
} from "src";
import { ActiveStatement, ActiveStatementFilters } from "src/activeExecutions";
import { defaultFilters, Filter } from "src/queryFilter";
import SQLActivityError from "src/sqlActivity/errorComponent";
import {
ACTIVE_STATEMENT_SEARCH_PARAM,
appsFromActiveStatements,
} from "../activeExecutions/activeStatementUtils";
import { useQueryParmeters } from "../hooks/useQueryParameters";
import { calculateActiveFilters } from "../queryFilter/filter";
import { useFiltersFromURL } from "../queryFilter/useFiltersFromURL";
import styles from "./statementsPage.module.scss";
import { ActiveStatementsSection } from "../activeExecutions/activeStatementsSection";
import { inactiveFiltersState } from "../queryFilter/filter";
const cx = classNames.bind(styles);
export type ActiveStatementsViewDispatchProps = {
onColumnsSelect: (columns: string[]) => void;
refreshSessions: () => void;
};
export type ActiveStatementsViewStateProps = {
selectedColumns: string[];
statements: ActiveStatement[];
fetchError: Error | null;
};
export type ActiveStatementsViewProps = ActiveStatementsViewStateProps &
ActiveStatementsViewDispatchProps;
export const ActiveStatementsView: React.FC<ActiveStatementsViewProps> = ({
onColumnsSelect,
refreshSessions,
selectedColumns,
statements,
fetchError,
}: ActiveStatementsViewProps) => {
const [pagination, setPagination] = useState<ISortedTablePagination>({
current: 1,
pageSize: 20,
});
const history = useHistory();
const queryParams = useQueryParmeters();
const search = queryParams.get(ACTIVE_STATEMENT_SEARCH_PARAM);
const filters = useFiltersFromURL({ app: defaultFilters.app });
useEffect(() => {
// Refresh every 10 seconds.
const interval = setInterval(refreshSessions, 10 * 1000);
return () => {
clearInterval(interval);
};
}, [refreshSessions]);
const resetPagination = () => {
setPagination({
current: 1,
pageSize: 20,
});
};
const onSubmitSearch = (search: string) => {
queryParams.set(ACTIVE_STATEMENT_SEARCH_PARAM, search);
history.push({
...history.location,
search: queryParams.toString(),
});
resetPagination();
};
const onSubmitFilters = (selectedFilters: ActiveStatementFilters) => {
if (selectedFilters.app === inactiveFiltersState.app) {
queryParams.delete("app");
} else {
queryParams.set("app", selectedFilters.app as string);
}
history.push({
...history.location,
search: queryParams.toString(),
});
resetPagination();
};
const onChangeSortSetting = (ss: SortSetting): void => {
queryParams.set("ascending", ss.ascending.toString());
queryParams.set("columnTitle", ss.columnTitle);
history.push({
...history.location,
search: queryParams.toString(),
});
resetPagination();
};
const clearSearch = () => onSubmitSearch("");
const clearFilters = () => onSubmitFilters(inactiveFiltersState);
const apps = appsFromActiveStatements(statements);
const countActiveFilters = calculateActiveFilters(filters);
const filteredStatements = statements
.filter(stmt => filters.app === "" || stmt.application === filters.app)
.filter(
stmt => search == null || search === "" || stmt.query.includes(search),
);
return (
<div className={cx("root")}>
<PageConfig>
<PageConfigItem>
<Search
onSubmit={onSubmitSearch}
onClear={clearSearch}
defaultValue={search}
/>
</PageConfigItem>
<PageConfigItem>
<Filter
activeFilters={countActiveFilters}
onSubmitFilters={onSubmitFilters}
appNames={apps}
filters={filters}
/>
</PageConfigItem>
</PageConfig>
<div className={cx("table-area")}></div>
<Loading
loading={statements == null}
page="active statements"
error={fetchError}
renderError={() =>
SQLActivityError({
statsType: "statements",
})
}
>
<ActiveStatementsSection
filters={filters}
pagination={pagination}
search={search}
statements={filteredStatements}
selectedColumns={selectedColumns}
onClearFilters={clearFilters}
onChangeSortSetting={onChangeSortSetting}
onColumnsSelect={onColumnsSelect}
/>
</Loading>
</div>
);
};