Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sqllab): Add logging for actions #28876

Merged
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
19 changes: 17 additions & 2 deletions superset-frontend/src/SqlLab/components/ResultSet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ import {
reRunQuery,
} from 'src/SqlLab/actions/sqlLab';
import { URL_PARAMS } from 'src/constants';
import useLogAction from 'src/logger/useLogAction';
import {
LOG_ACTIONS_SQLLAB_COPY_RESULT_TO_CLIPBOARD,
LOG_ACTIONS_SQLLAB_CREATE_CHART,
LOG_ACTIONS_SQLLAB_DOWNLOAD_CSV,
} from 'src/logger/LogUtils';
import Icons from 'src/components/Icons';
import ExploreCtasResultsButton from '../ExploreCtasResultsButton';
import ExploreResultsButton from '../ExploreResultsButton';
Expand Down Expand Up @@ -163,6 +169,7 @@ const ResultSet = ({
'dbId',
'tab',
'sql',
'sqlEditorId',
'templateParams',
'schema',
'rows',
Expand Down Expand Up @@ -193,6 +200,7 @@ const ResultSet = ({

const history = useHistory();
const dispatch = useDispatch();
const logAction = useLogAction({ queryId, sqlEditorId: query.sqlEditorId });

const reRunQueryIfSessionTimeoutErrorOnMount = useCallback(() => {
if (
Expand Down Expand Up @@ -250,7 +258,7 @@ const ResultSet = ({
const { results } = query;

const openInNewWindow = clickEvent.metaKey;

logAction(LOG_ACTIONS_SQLLAB_CREATE_CHART, {});
if (results?.query_id) {
const key = await postFormData(results.query_id, 'query', {
...EXPLORE_CHART_DEFAULT,
Expand Down Expand Up @@ -313,7 +321,11 @@ const ResultSet = ({
/>
)}
{csv && (
<Button buttonSize="small" href={getExportCsvUrl(query.id)}>
<Button
buttonSize="small"
href={getExportCsvUrl(query.id)}
onClick={() => logAction(LOG_ACTIONS_SQLLAB_DOWNLOAD_CSV, {})}
>
<i className="fa fa-file-text-o" /> {t('Download to CSV')}
</Button>
)}
Expand All @@ -327,6 +339,9 @@ const ResultSet = ({
</Button>
}
hideTooltip
onCopyEnd={() =>
logAction(LOG_ACTIONS_SQLLAB_COPY_RESULT_TO_CLIPBOARD, {})
}
/>
</ResultSetButtons>
{search && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import { DropdownButton } from 'src/components/DropdownButton';
import { detectOS } from 'src/utils/common';
import { QueryButtonProps } from 'src/SqlLab/types';
import useQueryEditor from 'src/SqlLab/hooks/useQueryEditor';
import {
LOG_ACTIONS_SQLLAB_RUN_QUERY,
LOG_ACTIONS_SQLLAB_STOP_QUERY,
} from 'src/logger/LogUtils';
import useLogAction from 'src/logger/useLogAction';

export interface RunQueryActionButtonProps {
queryEditorId: string;
Expand Down Expand Up @@ -57,7 +62,13 @@ const onClick = (
allowAsync: boolean,
runQuery: (c?: boolean) => void = () => undefined,
stopQuery = () => {},
logAction: (name: string, payload: Record<string, any>) => void,
): void => {
const eventName = shouldShowStopButton
? LOG_ACTIONS_SQLLAB_STOP_QUERY
: LOG_ACTIONS_SQLLAB_RUN_QUERY;

logAction(eventName, { shortcut: false });
if (shouldShowStopButton) return stopQuery();
if (allowAsync) {
return runQuery(true);
Expand Down Expand Up @@ -89,6 +100,7 @@ const RunQueryActionButton = ({
stopQuery,
}: RunQueryActionButtonProps) => {
const theme = useTheme();
const logAction = useLogAction({ queryEditorId });
const userOS = detectOS();

const { selectedText, sql } = useQueryEditor(queryEditorId, [
Expand Down Expand Up @@ -121,7 +133,7 @@ const RunQueryActionButton = ({
<ButtonComponent
data-test="run-query-action"
onClick={() =>
onClick(shouldShowStopBtn, allowAsync, runQuery, stopQuery)
onClick(shouldShowStopBtn, allowAsync, runQuery, stopQuery, logAction)
}
disabled={isDisabled}
tooltip={
Expand Down
14 changes: 13 additions & 1 deletion superset-frontend/src/SqlLab/components/SaveQuery/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import {
import { getDatasourceAsSaveableDataset } from 'src/utils/datasourceUtils';
import useQueryEditor from 'src/SqlLab/hooks/useQueryEditor';
import { QueryEditor } from 'src/SqlLab/types';
import useLogAction from 'src/logger/useLogAction';
import {
LOG_ACTIONS_SQLLAB_CREATE_CHART,
LOG_ACTIONS_SQLLAB_SAVE_QUERY,
} from 'src/logger/LogUtils';

interface SaveQueryProps {
queryEditorId: string;
Expand Down Expand Up @@ -91,6 +96,7 @@ const SaveQuery = ({
}),
[queryEditor, columns],
);
const logAction = useLogAction({ queryEditorId });
const defaultLabel = query.name || query.description || t('Undefined');
const [description, setDescription] = useState<string>(
query.description || '',
Expand All @@ -105,7 +111,12 @@ const SaveQuery = ({

const overlayMenu = (
<Menu>
<Menu.Item onClick={() => setShowSaveDatasetModal(true)}>
<Menu.Item
onClick={() => {
logAction(LOG_ACTIONS_SQLLAB_CREATE_CHART, {});
setShowSaveDatasetModal(true);
}}
>
{t('Save dataset')}
</Menu.Item>
</Menu>
Expand All @@ -129,6 +140,7 @@ const SaveQuery = ({
const close = () => setShowSave(false);

const onSaveWrapper = () => {
logAction(LOG_ACTIONS_SQLLAB_SAVE_QUERY, {});
onSave(queryPayload(), query.id);
close();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import withToasts from 'src/components/MessageToasts/withToasts';
import CopyToClipboard from 'src/components/CopyToClipboard';
import { storeQuery } from 'src/utils/common';
import useQueryEditor from 'src/SqlLab/hooks/useQueryEditor';
import { LOG_ACTIONS_SQLLAB_COPY_LINK } from 'src/logger/LogUtils';
import useLogAction from 'src/logger/useLogAction';

interface ShareSqlLabQueryProps {
queryEditorId: string;
Expand All @@ -52,7 +54,7 @@ const ShareSqlLabQuery = ({
addDangerToast,
}: ShareSqlLabQueryProps) => {
const theme = useTheme();

const logAction = useLogAction({ queryEditorId });
const { dbId, name, schema, autorun, sql, remoteId, templateParams } =
useQueryEditor(queryEditorId, [
'dbId',
Expand Down Expand Up @@ -92,6 +94,9 @@ const ShareSqlLabQuery = ({
}
};
const getCopyUrl = (callback: Function) => {
logAction(LOG_ACTIONS_SQLLAB_COPY_LINK, {
shortcut: false,
});
if (isFeatureEnabled(FeatureFlag.ShareQueriesViaKvStore)) {
return getCopyUrlForKvStore(callback);
}
Expand Down
52 changes: 46 additions & 6 deletions superset-frontend/src/SqlLab/components/SqlEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ import {
} from 'src/utils/localStorageHelpers';
import { EmptyStateBig } from 'src/components/EmptyState';
import getBootstrapData from 'src/utils/getBootstrapData';
import useLogAction from 'src/logger/useLogAction';
import {
LOG_ACTIONS_SQLLAB_CREATE_TABLE_AS,
LOG_ACTIONS_SQLLAB_CREATE_VIEW_AS,
LOG_ACTIONS_SQLLAB_ESTIMATE_QUERY_COST,
LOG_ACTIONS_SQLLAB_FORMAT_SQL,
LOG_ACTIONS_SQLLAB_LOAD_TAB_STATE,
LOG_ACTIONS_SQLLAB_RUN_QUERY,
LOG_ACTIONS_SQLLAB_STOP_QUERY,
Logger,
} from 'src/logger/LogUtils';
import TemplateParamsEditor from '../TemplateParamsEditor';
import SouthPane from '../SouthPane';
import SaveQuery, { QueryPayload } from '../SaveQuery';
Expand Down Expand Up @@ -271,6 +282,7 @@ const SqlEditor: React.FC<Props> = ({
};
}, shallowEqual);

const logAction = useLogAction({ queryEditorId: queryEditor.id });
const isActive = currentQueryEditorId === queryEditor.id;
const [height, setHeight] = useState(0);
const [autorun, setAutorun] = useState(queryEditor.autorun);
Expand Down Expand Up @@ -317,9 +329,15 @@ const SqlEditor: React.FC<Props> = ({
[ctas, database, defaultQueryLimit, dispatch, queryEditor],
);

const formatCurrentQuery = useCallback(() => {
dispatch(formatQuery(queryEditor));
}, [dispatch, queryEditor]);
const formatCurrentQuery = useCallback(
(useShortcut?: boolean) => {
logAction(LOG_ACTIONS_SQLLAB_FORMAT_SQL, {
shortcut: Boolean(useShortcut),
});
dispatch(formatQuery(queryEditor));
},
[dispatch, queryEditor, logAction],
);

const stopQuery = useCallback(() => {
if (latestQuery && ['running', 'pending'].indexOf(latestQuery.state) >= 0) {
Expand Down Expand Up @@ -358,6 +376,7 @@ const SqlEditor: React.FC<Props> = ({
descr: KEY_MAP[KeyboardShortcut.CtrlR],
func: () => {
if (queryEditor.sql.trim() !== '') {
logAction(LOG_ACTIONS_SQLLAB_RUN_QUERY, { shortcut: true });
startQuery();
}
},
Expand All @@ -368,6 +387,7 @@ const SqlEditor: React.FC<Props> = ({
descr: KEY_MAP[KeyboardShortcut.CtrlEnter],
func: () => {
if (queryEditor.sql.trim() !== '') {
logAction(LOG_ACTIONS_SQLLAB_RUN_QUERY, { shortcut: true });
startQuery();
}
},
Expand All @@ -384,6 +404,7 @@ const SqlEditor: React.FC<Props> = ({
descr: KEY_MAP[KeyboardShortcut.CtrlT],
}),
func: () => {
Logger.markTimeOrigin();
dispatch(addNewQueryEditor());
},
},
Expand All @@ -398,14 +419,17 @@ const SqlEditor: React.FC<Props> = ({
key: KeyboardShortcut.CtrlE,
descr: KEY_MAP[KeyboardShortcut.CtrlE],
}),
func: stopQuery,
func: () => {
logAction(LOG_ACTIONS_SQLLAB_STOP_QUERY, { shortcut: true });
stopQuery();
},
},
{
name: 'formatQuery',
key: KeyboardShortcut.CtrlShiftF,
descr: KEY_MAP[KeyboardShortcut.CtrlShiftF],
func: () => {
formatCurrentQuery();
formatCurrentQuery(true);
},
},
];
Expand Down Expand Up @@ -503,6 +527,13 @@ const SqlEditor: React.FC<Props> = ({
!queryEditor.loaded;

const loadQueryEditor = useEffectEvent(() => {
const duration = Logger.getTimestamp();
logAction(LOG_ACTIONS_SQLLAB_LOAD_TAB_STATE, {
duration,
queryEditorId: queryEditor.id,
inLocalStorage: Boolean(queryEditor.inLocalStorage),
hasLoaded: !shouldLoadQueryEditor,
});
if (shouldLoadQueryEditor) {
dispatch(switchQueryEditor(queryEditor, displayLimit));
}
Expand Down Expand Up @@ -600,6 +631,7 @@ const SqlEditor: React.FC<Props> = ({
});

const getQueryCostEstimate = () => {
logAction(LOG_ACTIONS_SQLLAB_ESTIMATE_QUERY_COST, { shortcut: false });
if (database) {
dispatch(estimateQueryCost(queryEditor));
}
Expand Down Expand Up @@ -666,7 +698,9 @@ const SqlEditor: React.FC<Props> = ({
/>
</Menu.Item>
)}
<Menu.Item onClick={formatCurrentQuery}>{t('Format SQL')}</Menu.Item>
<Menu.Item onClick={() => formatCurrentQuery()}>
{t('Format SQL')}
</Menu.Item>
{!isEmpty(scheduledQueriesConf) && (
<Menu.Item>
<ScheduleQueryButton
Expand Down Expand Up @@ -704,6 +738,9 @@ const SqlEditor: React.FC<Props> = ({
{allowCTAS && (
<Menu.Item
onClick={() => {
logAction(LOG_ACTIONS_SQLLAB_CREATE_TABLE_AS, {
shortcut: false,
});
setShowCreateAsModal(true);
setCreateAs(CtasEnum.Table);
}}
Expand All @@ -715,6 +752,9 @@ const SqlEditor: React.FC<Props> = ({
{allowCVAS && (
<Menu.Item
onClick={() => {
logAction(LOG_ACTIONS_SQLLAB_CREATE_VIEW_AS, {
shortcut: false,
});
setShowCreateAsModal(true);
setCreateAs(CtasEnum.View);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { connect } from 'react-redux';
import URI from 'urijs';
import type { QueryEditor, SqlLabRootState } from 'src/SqlLab/types';
import { FeatureFlag, styled, t, isFeatureEnabled } from '@superset-ui/core';
import { Logger } from 'src/logger/LogUtils';
import { Tooltip } from 'src/components/Tooltip';
import { detectOS } from 'src/utils/common';
import * as Actions from 'src/SqlLab/actions/sqlLab';
Expand Down Expand Up @@ -222,6 +223,7 @@ class TabbedSqlEditors extends React.PureComponent<TabbedSqlEditorsProps> {
}
}
if (action === 'add') {
Logger.markTimeOrigin();
this.newQueryEditor();
}
}
Expand All @@ -230,6 +232,14 @@ class TabbedSqlEditors extends React.PureComponent<TabbedSqlEditorsProps> {
this.props.actions.removeQueryEditor(qe);
}

onTabClicked = () => {
Logger.markTimeOrigin();
const noQueryEditors = this.props.queryEditors?.length === 0;
if (noQueryEditors) {
this.newQueryEditor();
}
};

render() {
const noQueryEditors = this.props.queryEditors?.length === 0;
const editors = this.props.queryEditors?.map(qe => (
Expand Down Expand Up @@ -290,7 +300,7 @@ class TabbedSqlEditors extends React.PureComponent<TabbedSqlEditorsProps> {
onChange={this.handleSelect}
fullWidth={false}
hideAdd={this.props.offline}
onTabClick={() => noQueryEditors && this.newQueryEditor()}
onTabClick={this.onTabClicked}
onEdit={this.handleEdit}
type={noQueryEditors ? 'card' : 'editable-card'}
addIcon={
Expand Down
16 changes: 16 additions & 0 deletions superset-frontend/src/logger/LogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,29 @@ export const LOG_ACTIONS_DRILL_BY_BREADCRUMB_CLICKED =
'drill_by_breadcrumb_clicked';
export const LOG_ACTIONS_SQLLAB_MONITOR_LOCAL_STORAGE_USAGE =
'sqllab_monitor_local_storage_usage';
export const LOG_ACTIONS_SQLLAB_CREATE_TABLE_AS = 'sqllab_create_table_as';
export const LOG_ACTIONS_SQLLAB_CREATE_VIEW_AS = 'sqllab_create_view_as';
export const LOG_ACTIONS_SQLLAB_RUN_QUERY = 'sqllab_run_query';
export const LOG_ACTIONS_SQLLAB_STOP_QUERY = 'sqllab_stop_query';
export const LOG_ACTIONS_SQLLAB_ESTIMATE_QUERY_COST =
'sqllab_estimate_query_cost';
export const LOG_ACTIONS_SQLLAB_SAVE_QUERY = 'sqllab_save_query';
export const LOG_ACTIONS_SQLLAB_SAVE_DATASET = 'sqllab_save_dataset';
export const LOG_ACTIONS_SQLLAB_COPY_LINK = 'sqllab_copy_link';
export const LOG_ACTIONS_SQLLAB_FORMAT_SQL = 'sqllab_format_sql';
export const LOG_ACTIONS_SQLLAB_DOWNLOAD_CSV = 'sqllab_download_csv';
export const LOG_ACTIONS_SQLLAB_COPY_RESULT_TO_CLIPBOARD =
'sqllab_copy_result_to_clipboard';
export const LOG_ACTIONS_SQLLAB_CREATE_CHART = 'sqllab_create_chart';
export const LOG_ACTIONS_SQLLAB_LOAD_TAB_STATE = 'sqllab_load_tab_state';

// Log event types --------------------------------------------------------------
export const LOG_EVENT_TYPE_TIMING = new Set([
LOG_ACTIONS_LOAD_CHART,
LOG_ACTIONS_RENDER_CHART,
LOG_ACTIONS_HIDE_BROWSER_TAB,
LOG_ACTIONS_SQLLAB_FETCH_FAILED_QUERY,
LOG_ACTIONS_SQLLAB_LOAD_TAB_STATE,
]);
export const LOG_EVENT_TYPE_USER = new Set([
LOG_ACTIONS_MOUNT_DASHBOARD,
Expand Down
Loading
Loading