Skip to content

Commit a53d302

Browse files
db-console: add analytics to hot ranges page
So that we can better understand when the hot ranges page is being used, we want analytics on filters, sort and pagination state anytime the page is rendered. This PR achieves this, by deduplicating with a useEffect. Fixes: #144395 Epic: none Release note: none
1 parent 5069471 commit a53d302

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

pkg/ui/workspaces/db-console/src/views/hotRanges/hotRangesTable.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import {
1212
Anchor,
1313
EmptyTable,
1414
util,
15+
ISortedTablePagination,
1516
} from "@cockroachlabs/cluster-ui";
1617
import { Tooltip } from "antd";
1718
import classNames from "classnames/bind";
1819
import round from "lodash/round";
19-
import React from "react";
20+
import React, { useEffect } from "react";
2021
import { connect } from "react-redux";
2122
import { Link } from "react-router-dom";
2223

@@ -47,6 +48,10 @@ interface HotRangesTableProps {
4748
clearFilterContainer: React.ReactNode;
4849
sortSetting?: SortSetting;
4950
onSortChange?: (ss: SortSetting) => void;
51+
onViewPropertiesChange?: (vp: {
52+
sortSetting: SortSetting;
53+
pagination: ISortedTablePagination;
54+
}) => void;
5055
emptyMessage?: EmptyMessage;
5156
}
5257

@@ -57,6 +62,7 @@ const HotRangesTable = ({
5762
clearFilterContainer,
5863
sortSetting,
5964
onSortChange,
65+
onViewPropertiesChange,
6066
emptyMessage,
6167
}: HotRangesTableProps) => {
6268
const [pagination, updatePagination] = util.usePagination(1, PAGE_SIZE);
@@ -278,6 +284,13 @@ const HotRangesTable = ({
278284
},
279285
];
280286

287+
useEffect(() => {
288+
onViewPropertiesChange?.({
289+
sortSetting,
290+
pagination,
291+
});
292+
}, [sortSetting, pagination, onViewPropertiesChange]);
293+
281294
return (
282295
<div className="section">
283296
<div className={cx("hotranges-heading-container")}>

pkg/ui/workspaces/db-console/src/views/hotRanges/index.tsx

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@
33
// Use of this software is governed by the CockroachDB Software License
44
// included in the /LICENSE file.
55

6+
import { createHash } from "crypto";
7+
68
import {
79
Loading,
810
Text,
911
Anchor,
1012
util,
1113
TimezoneContext,
14+
SortSetting,
15+
ISortedTablePagination,
1216
} from "@cockroachlabs/cluster-ui";
1317
import classNames from "classnames/bind";
14-
import React, { useRef, useMemo, useEffect, useContext } from "react";
18+
import React, { useRef, useMemo, useEffect, useContext, useState } from "react";
1519
import { Helmet } from "react-helmet";
1620
import { useDispatch, useSelector } from "react-redux";
1721

1822
import { cockroach } from "src/js/protos";
23+
import { analytics } from "src/redux/analytics";
1924
import {
2025
refreshHotRanges,
2126
clearHotRanges,
@@ -63,6 +68,8 @@ const HotRangesPage = () => {
6368
const timezone = useContext(TimezoneContext);
6469

6570
const { filters, applyFilters } = useFilters();
71+
const [sortSetting, setSortSetting] = useState<SortSetting>(null);
72+
const [pagination, setPagination] = useState<ISortedTablePagination>(null);
6673

6774
// dispatch hot ranges call whenever the filters change and are not empty
6875
useEffect(() => {
@@ -77,6 +84,28 @@ const HotRangesPage = () => {
7784
}
7885
}, [filters.nodeIds, dispatch]);
7986

87+
// track analytics on filters, pagination and sort.
88+
const analyticsKey = createHash("md5")
89+
.update(JSON.stringify([filters, sortSetting, pagination]))
90+
.digest("hex");
91+
useEffect(() => {
92+
if (!filters.nodeIds.length || !pagination || !sortSetting) {
93+
return;
94+
}
95+
analytics.track({
96+
event: "Hot Ranges Page Load",
97+
properties: {
98+
filters,
99+
pagination,
100+
sortSetting,
101+
},
102+
});
103+
// this is keyed on a hash of the contents for filters, pagination and sortSetting
104+
// as opposed to the references themselves, as we don't want to re-run this effect
105+
// when the contents haven't changed, but the references have.
106+
// eslint-disable-next-line react-hooks/exhaustive-deps
107+
}, [analyticsKey]);
108+
80109
// load the databases if possible.
81110
useEffect(() => {
82111
dispatch(refreshDatabases());
@@ -129,6 +158,16 @@ const HotRangesPage = () => {
129158
nodeIdToLocalityMap={nodeIdToLocalityMap}
130159
clearFilterContainer={<span ref={clearButtonRef} />}
131160
emptyMessage={emptyMessage}
161+
onViewPropertiesChange={({
162+
sortSetting,
163+
pagination,
164+
}: {
165+
sortSetting: SortSetting;
166+
pagination: ISortedTablePagination;
167+
}) => {
168+
setSortSetting(sortSetting);
169+
setPagination(pagination);
170+
}}
132171
/>
133172
)}
134173
page={undefined}

0 commit comments

Comments
 (0)