-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathsnapshotPage.tsx
169 lines (151 loc) · 4.86 KB
/
snapshotPage.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
// 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 { InlineAlert } from "@cockroachlabs/ui-components";
import moment from "moment";
import React, { useEffect } from "react";
import { Helmet } from "react-helmet";
import { RouteComponentProps } from "react-router-dom";
import { generatePath } from "react-router";
import {
ListTracingSnapshotsResponseMessage,
GetTracingSnapshotResponseMessage,
} from "src/api/tracezApi";
import { Delayed } from "src/delayed";
import { Dropdown, DropdownOption } from "src/dropdown";
import { Loading } from "src/loading";
import { PageConfig, PageConfigItem } from "src/pageConfig";
import { SortSetting } from "src/sortedtable";
import { getMatchParamByName, TimestampToMoment } from "src/util";
import { syncHistory } from "src/util";
import { Location, History } from "history";
import { SpanTable } from "./spanTable";
import { commonStyles } from "src/common";
import styles from "../snapshot.module.scss";
import classNames from "classnames/bind";
const cx = classNames.bind(styles);
export interface SnapshotPageStateProps {
sort: SortSetting;
snapshotsError: Error | null;
snapshotsLoading: boolean;
snapshots: ListTracingSnapshotsResponseMessage;
snapshot: GetTracingSnapshotResponseMessage;
snapshotError: Error | null;
snapshotLoading: boolean;
}
export interface SnapshotPageDispatchProps {
setSort: (value: SortSetting) => void;
refreshSnapshots: () => void;
refreshSnapshot: (id: number) => void;
}
export type SnapshotPageProps = SnapshotPageStateProps &
SnapshotPageDispatchProps &
RouteComponentProps;
export const SnapshotPage: React.FC<SnapshotPageProps> = props => {
const {
history,
match,
refreshSnapshots,
refreshSnapshot,
snapshots,
snapshot,
sort,
setSort,
} = props;
const searchParams = new URLSearchParams(history.location.search);
// Sort Settings.
const ascending = (searchParams.get("ascending") || undefined) === "true";
const columnTitle = searchParams.get("columnTitle") || "";
const snapshotIDStr = getMatchParamByName(match, "snapshotID");
useEffect(() => {
refreshSnapshots();
});
useEffect(() => {
if (snapshotIDStr || !snapshots) {
return;
}
const snapArray = snapshots.snapshots;
const lastSnapshotID = snapArray[snapArray.length - 1].snapshot_id;
history.location.pathname = "/debug/tracez_v2/snapshot/" + lastSnapshotID;
history.replace(history.location);
}, [snapshots, snapshotIDStr, history]);
useEffect(() => {
if (!columnTitle) {
return;
}
setSort({ columnTitle, ascending });
}, [setSort, columnTitle, ascending]);
useEffect(() => {
if (!snapshotIDStr) {
return;
}
refreshSnapshot(parseInt(snapshotIDStr));
}, [snapshotIDStr, refreshSnapshot]);
const onSnapshotSelected = (item: string) => {
history.location.pathname = "/debug/tracez_v2/snapshot/" + item;
history.push(history.location);
};
const changeSortSetting = (ss: SortSetting): void => {
setSort(ss);
syncHistory(
{
ascending: ss.ascending.toString(),
columnTitle: ss.columnTitle,
},
history,
);
};
const snapshotItems = !snapshots
? []
: snapshots.snapshots.map(snapshotInfo => {
const id = snapshotInfo.snapshot_id.toString();
const time = TimestampToMoment(snapshotInfo.captured_at).format(
"MMM D, YYYY [at] HH:mm:ss",
);
return {
name: "Snapshot " + id + ": " + time,
value: id,
};
});
const isLoading = props.snapshotsLoading || props.snapshotLoading;
const error = props.snapshotsError || props.snapshotError;
return (
<div className={cx("snapshots-page")}>
<Helmet title="Snapshots" />
<h3 className={commonStyles("base-heading")}>Snapshots</h3>
<div>
<PageConfig>
<PageConfigItem>
<Dropdown items={snapshotItems} onChange={onSnapshotSelected}>
{snapshotIDStr &&
snapshotItems.length &&
snapshotItems.find(option => option["value"] === snapshotIDStr)[
"name"
]}
</Dropdown>
</PageConfigItem>
</PageConfig>
</div>
<section className={cx("section")}>
<Loading
loading={isLoading}
page={"snapshots"}
error={error}
render={() => (
<SpanTable
snapshot={snapshot?.snapshot}
setSort={changeSortSetting}
sort={sort}
/>
)}
/>
</section>
</div>
);
};