Skip to content

Commit 2c38116

Browse files
authored
feat: support custom metrics events (#3038)
1 parent 7a9d962 commit 2c38116

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
import {useChangedQuerySettings} from '../../../../utils/hooks/useChangedQuerySettings';
4242
import {useLastQueryExecutionSettings} from '../../../../utils/hooks/useLastQueryExecutionSettings';
4343
import {DEFAULT_QUERY_SETTINGS, QUERY_ACTIONS, QUERY_MODES} from '../../../../utils/query';
44+
import {reachMetricaGoal} from '../../../../utils/yaMetrica';
4445
import {useCurrentSchema} from '../../TenantContext';
4546
import type {InitialPaneState} from '../../utils/paneVisibilityToggleHelpers';
4647
import {
@@ -148,6 +149,11 @@ export default function QueryEditor(props: QueryEditorProps) {
148149
queryManagerInstance.abortQuery();
149150

150151
if (isStreamingEnabled) {
152+
reachMetricaGoal('runQuery', {
153+
actionType: 'execute',
154+
isStreaming: true,
155+
...querySettings,
156+
});
151157
const query = streamQuery({
152158
actionType: 'execute',
153159
query: text,
@@ -158,6 +164,7 @@ export default function QueryEditor(props: QueryEditorProps) {
158164

159165
queryManagerInstance.registerQuery(query);
160166
} else {
167+
reachMetricaGoal('runQuery', {actionType: 'execute', ...querySettings});
161168
const query = sendQuery({
162169
actionType: 'execute',
163170
query: text,
@@ -196,6 +203,8 @@ export default function QueryEditor(props: QueryEditorProps) {
196203

197204
const queryId = uuidv4();
198205

206+
reachMetricaGoal('runQuery', {actionType: 'explain', ...querySettings});
207+
199208
const query = sendQuery({
200209
actionType: 'explain',
201210
query: text,

src/containers/Tenant/Query/QueryEditorControls/QueryEditorControls.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {QueryAction} from '../../../../types/store/query';
66
import {cn} from '../../../../utils/cn';
77
import createToast from '../../../../utils/createToast';
88
import {useTypedSelector} from '../../../../utils/hooks';
9+
import {reachMetricaGoal} from '../../../../utils/yaMetrica';
910
import {NewSQL} from '../NewSQL/NewSQL';
1011
import {queryManagerInstance} from '../QueryEditor/helpers';
1112
import {SaveQuery} from '../SaveQuery/SaveQuery';
@@ -90,6 +91,7 @@ export const QueryEditorControls = ({
9091
const [cancelQueryError, setCancelQueryError] = React.useState<boolean>(false);
9192

9293
const onStopButtonClick = React.useCallback(async () => {
94+
reachMetricaGoal('stopQuery');
9395
try {
9496
if (isStreamingEnabled) {
9597
queryManagerInstance.abortQuery();

src/uiFactory/types.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {ETenantType} from '../types/api/tenant';
1616
import type {GetLogsLink} from '../utils/logs';
1717
import type {GetMonitoringClusterLink, GetMonitoringLink} from '../utils/monitoring';
1818

19-
export interface UIFactory<H extends string = CommonIssueType> {
19+
export interface UIFactory<H extends string = CommonIssueType, T extends string = string> {
2020
onCreateDB?: HandleCreateDB;
2121
onEditDB?: HandleEditDB;
2222
onDeleteDB?: HandleDeleteDB;
@@ -46,11 +46,16 @@ export interface UIFactory<H extends string = CommonIssueType> {
4646
};
4747
hasAccess?: boolean;
4848
hideGrantAccess?: boolean;
49-
yaMetricaMap?: Record<string, number>;
5049

5150
useDatabaseId?: boolean;
5251

5352
useMetaProxy?: boolean;
53+
54+
yaMetricaConfig?: {
55+
yaMetricaMap: Record<T, number | undefined>;
56+
goals: UiMetricaGoals;
57+
getMetricaName: (goalKey: UiMetricaGoal) => T;
58+
};
5459
}
5560

5661
export type HandleCreateDB = (params: {clusterName: string}) => Promise<boolean>;
@@ -97,3 +102,9 @@ export type RenderMonitoring = (props: {
97102
additionalTenantProps?: AdditionalTenantsProps;
98103
scrollContainerRef: React.RefObject<HTMLDivElement>;
99104
}) => React.ReactNode;
105+
export interface UiMetricaGoals {
106+
runQuery?: string;
107+
stopQuery?: string;
108+
}
109+
110+
export type UiMetricaGoal = keyof UiMetricaGoals;

src/uiFactory/uiFactory.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ const uiFactoryBase: UIFactory = {
2222
useDatabaseId: false,
2323
};
2424

25-
export function configureUIFactory<H extends string>(overrides: Partial<UIFactory<H>>) {
25+
export function configureUIFactory<H extends string, T extends string = string>(
26+
overrides: Partial<UIFactory<H, T>>,
27+
) {
2628
Object.assign(uiFactoryBase, overrides);
2729
}
2830

src/utils/yaMetrica.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type {UiMetricaGoal} from '../uiFactory/types';
12
import {uiFactory} from '../uiFactory/uiFactory';
23

34
/**
@@ -14,7 +15,7 @@ export interface Counter {
1415
reachGoal: (...args: unknown[]) => void;
1516
}
1617

17-
const yaMetricaMap = uiFactory.yaMetricaMap;
18+
const yaMetricaMap = uiFactory.yaMetricaConfig?.yaMetricaMap;
1819

1920
/**
2021
* A fake implementation of a counter metric for Yandex.Metrica.
@@ -62,11 +63,32 @@ class FakeMetrica implements Counter {
6263
* @param name The name of the metrica to retrieve
6364
* @returns The Yandex Metrica instance if found, otherwise a FakeMetrica instance
6465
*/
65-
export function getMetrica(name: string) {
66+
export function getMetrica(name?: string) {
67+
if (!name) {
68+
return undefined;
69+
}
6670
const yaMetricaId = yaMetricaMap?.[name];
6771
const metricaInstance = yaMetricaId
6872
? (window[`yaCounter${yaMetricaId}`] as Counter)
6973
: undefined;
7074

7175
return metricaInstance ?? new FakeMetrica(name);
7276
}
77+
78+
export function reachMetricaGoal(
79+
goalKey: UiMetricaGoal,
80+
params?: Record<string, boolean | string | number | null>,
81+
) {
82+
const metricaName = uiFactory.yaMetricaConfig?.getMetricaName(goalKey);
83+
const metrica = getMetrica(metricaName);
84+
if (!metrica) {
85+
console.warn('Metrica is not defined');
86+
return;
87+
}
88+
const goal = uiFactory.yaMetricaConfig?.goals[goalKey];
89+
if (!goal) {
90+
console.warn('Metrica goal is not defined');
91+
return;
92+
}
93+
metrica.reachGoal(goal, params);
94+
}

0 commit comments

Comments
 (0)