Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/containers/Tenant/Query/QueryEditor/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
import {useChangedQuerySettings} from '../../../../utils/hooks/useChangedQuerySettings';
import {useLastQueryExecutionSettings} from '../../../../utils/hooks/useLastQueryExecutionSettings';
import {DEFAULT_QUERY_SETTINGS, QUERY_ACTIONS, QUERY_MODES} from '../../../../utils/query';
import {reachMetricaGoal} from '../../../../utils/yaMetrica';
import {useCurrentSchema} from '../../TenantContext';
import type {InitialPaneState} from '../../utils/paneVisibilityToggleHelpers';
import {
Expand Down Expand Up @@ -148,6 +149,11 @@ export default function QueryEditor(props: QueryEditorProps) {
queryManagerInstance.abortQuery();

if (isStreamingEnabled) {
reachMetricaGoal('runQuery', {
actionType: 'execute',
isStreaming: true,
...querySettings,
});
const query = streamQuery({
actionType: 'execute',
query: text,
Expand All @@ -158,6 +164,7 @@ export default function QueryEditor(props: QueryEditorProps) {

queryManagerInstance.registerQuery(query);
} else {
reachMetricaGoal('runQuery', {actionType: 'execute', ...querySettings});
const query = sendQuery({
actionType: 'execute',
query: text,
Expand Down Expand Up @@ -196,6 +203,8 @@ export default function QueryEditor(props: QueryEditorProps) {

const queryId = uuidv4();

reachMetricaGoal('runQuery', {actionType: 'explain', ...querySettings});

const query = sendQuery({
actionType: 'explain',
query: text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {QueryAction} from '../../../../types/store/query';
import {cn} from '../../../../utils/cn';
import createToast from '../../../../utils/createToast';
import {useTypedSelector} from '../../../../utils/hooks';
import {reachMetricaGoal} from '../../../../utils/yaMetrica';
import {NewSQL} from '../NewSQL/NewSQL';
import {queryManagerInstance} from '../QueryEditor/helpers';
import {SaveQuery} from '../SaveQuery/SaveQuery';
Expand Down Expand Up @@ -90,6 +91,7 @@ export const QueryEditorControls = ({
const [cancelQueryError, setCancelQueryError] = React.useState<boolean>(false);

const onStopButtonClick = React.useCallback(async () => {
reachMetricaGoal('stopQuery');
try {
if (isStreamingEnabled) {
queryManagerInstance.abortQuery();
Expand Down
15 changes: 13 additions & 2 deletions src/uiFactory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {ETenantType} from '../types/api/tenant';
import type {GetLogsLink} from '../utils/logs';
import type {GetMonitoringClusterLink, GetMonitoringLink} from '../utils/monitoring';

export interface UIFactory<H extends string = CommonIssueType> {
export interface UIFactory<H extends string = CommonIssueType, T extends string = string> {
onCreateDB?: HandleCreateDB;
onEditDB?: HandleEditDB;
onDeleteDB?: HandleDeleteDB;
Expand Down Expand Up @@ -46,11 +46,16 @@ export interface UIFactory<H extends string = CommonIssueType> {
};
hasAccess?: boolean;
hideGrantAccess?: boolean;
yaMetricaMap?: Record<string, number>;

useDatabaseId?: boolean;

useMetaProxy?: boolean;

yaMetricaConfig?: {
yaMetricaMap: Record<T, number | undefined>;
goals: UiMetricaGoals;
getMetricaName: (goalKey: UiMetricaGoal) => T;
};
}

export type HandleCreateDB = (params: {clusterName: string}) => Promise<boolean>;
Expand Down Expand Up @@ -97,3 +102,9 @@ export type RenderMonitoring = (props: {
additionalTenantProps?: AdditionalTenantsProps;
scrollContainerRef: React.RefObject<HTMLDivElement>;
}) => React.ReactNode;
export interface UiMetricaGoals {
runQuery?: string;
stopQuery?: string;
}

export type UiMetricaGoal = keyof UiMetricaGoals;
4 changes: 3 additions & 1 deletion src/uiFactory/uiFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const uiFactoryBase: UIFactory = {
useDatabaseId: false,
};

export function configureUIFactory<H extends string>(overrides: Partial<UIFactory<H>>) {
export function configureUIFactory<H extends string, T extends string = string>(
overrides: Partial<UIFactory<H, T>>,
) {
Object.assign(uiFactoryBase, overrides);
}

Expand Down
26 changes: 24 additions & 2 deletions src/utils/yaMetrica.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {UiMetricaGoal} from '../uiFactory/types';
import {uiFactory} from '../uiFactory/uiFactory';

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

const yaMetricaMap = uiFactory.yaMetricaMap;
const yaMetricaMap = uiFactory.yaMetricaConfig?.yaMetricaMap;

/**
* A fake implementation of a counter metric for Yandex.Metrica.
Expand Down Expand Up @@ -62,11 +63,32 @@ class FakeMetrica implements Counter {
* @param name The name of the metrica to retrieve
* @returns The Yandex Metrica instance if found, otherwise a FakeMetrica instance
*/
export function getMetrica(name: string) {
export function getMetrica(name?: string) {
if (!name) {
return undefined;
}
const yaMetricaId = yaMetricaMap?.[name];
const metricaInstance = yaMetricaId
? (window[`yaCounter${yaMetricaId}`] as Counter)
: undefined;

return metricaInstance ?? new FakeMetrica(name);
}

export function reachMetricaGoal(
goalKey: UiMetricaGoal,
params?: Record<string, boolean | string | number | null>,
) {
const metricaName = uiFactory.yaMetricaConfig?.getMetricaName(goalKey);
const metrica = getMetrica(metricaName);
if (!metrica) {
console.warn('Metrica is not defined');
return;
}
const goal = uiFactory.yaMetricaConfig?.goals[goalKey];
if (!goal) {
console.warn('Metrica goal is not defined');
return;
}
metrica.reachGoal(goal, params);
}
Loading