mirror of
https://github.com/apache/superset.git
synced 2026-05-06 16:34:32 +00:00
fix(log): store navigation path to get correct logging path (#32795)
(cherry picked from commit 4a70065e5f)
This commit is contained in:
committed by
Michael S. Molina
parent
7ed236170d
commit
e593bc0a2e
@@ -20,7 +20,10 @@ import sinon from 'sinon';
|
||||
import { SupersetClient } from '@superset-ui/core';
|
||||
import logger from 'src/middleware/loggerMiddleware';
|
||||
import { LOG_EVENT } from 'src/logger/actions';
|
||||
import { LOG_ACTIONS_LOAD_CHART } from 'src/logger/LogUtils';
|
||||
import {
|
||||
LOG_ACTIONS_LOAD_CHART,
|
||||
LOG_ACTIONS_SPA_NAVIGATION,
|
||||
} from 'src/logger/LogUtils';
|
||||
|
||||
describe('logger middleware', () => {
|
||||
const dashboardId = 123;
|
||||
@@ -40,7 +43,6 @@ describe('logger middleware', () => {
|
||||
eventData: {
|
||||
key: 'value',
|
||||
start_offset: 100,
|
||||
path: `/dashboard/${dashboardId}/`,
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -82,11 +84,19 @@ describe('logger middleware', () => {
|
||||
});
|
||||
|
||||
it('should include ts, start_offset, event_name, impression_id, source, and source_id in every event', () => {
|
||||
logger(mockStore)(next)(action);
|
||||
const fetchLog = logger(mockStore)(next);
|
||||
fetchLog({
|
||||
type: LOG_EVENT,
|
||||
payload: {
|
||||
eventName: LOG_ACTIONS_SPA_NAVIGATION,
|
||||
eventData: { path: `/dashboard/${dashboardId}/` },
|
||||
},
|
||||
});
|
||||
timeSandbox.clock.tick(2000);
|
||||
|
||||
expect(SupersetClient.post.callCount).toBe(1);
|
||||
const { events } = SupersetClient.post.getCall(0).args[0].postPayload;
|
||||
fetchLog(action);
|
||||
timeSandbox.clock.tick(2000);
|
||||
expect(SupersetClient.post.callCount).toBe(2);
|
||||
const { events } = SupersetClient.post.getCall(1).args[0].postPayload;
|
||||
const mockEventdata = action.payload.eventData;
|
||||
const mockEventname = action.payload.eventName;
|
||||
expect(events[0]).toMatchObject({
|
||||
|
||||
@@ -23,7 +23,10 @@ import { SupersetClient } from '@superset-ui/core';
|
||||
|
||||
import { safeStringify } from '../utils/safeStringify';
|
||||
import { LOG_EVENT } from '../logger/actions';
|
||||
import { LOG_EVENT_TYPE_TIMING } from '../logger/LogUtils';
|
||||
import {
|
||||
LOG_EVENT_TYPE_TIMING,
|
||||
LOG_ACTIONS_SPA_NAVIGATION,
|
||||
} from '../logger/LogUtils';
|
||||
import DebouncedMessageQueue from '../utils/DebouncedMessageQueue';
|
||||
|
||||
const LOG_ENDPOINT = '/superset/log/?explode=events';
|
||||
@@ -67,78 +70,87 @@ const logMessageQueue = new DebouncedMessageQueue({
|
||||
delayThreshold: 1000,
|
||||
});
|
||||
let lastEventId = 0;
|
||||
const loggerMiddleware = store => next => action => {
|
||||
if (action.type !== LOG_EVENT) {
|
||||
return next(action);
|
||||
}
|
||||
const loggerMiddleware = store => next => {
|
||||
let navPath;
|
||||
return action => {
|
||||
if (action.type !== LOG_EVENT) {
|
||||
return next(action);
|
||||
}
|
||||
|
||||
const { dashboardInfo, explore, impressionId, dashboardLayout, sqlLab } =
|
||||
store.getState();
|
||||
let logMetadata = {
|
||||
impression_id: impressionId,
|
||||
version: 'v2',
|
||||
};
|
||||
const { eventName } = action.payload;
|
||||
let { eventData = {} } = action.payload;
|
||||
|
||||
const path = eventData.path || window?.location?.href;
|
||||
|
||||
if (dashboardInfo?.id && path?.includes('/dashboard/')) {
|
||||
logMetadata = {
|
||||
source: 'dashboard',
|
||||
source_id: dashboardInfo.id,
|
||||
dashboard_id: dashboardInfo.id,
|
||||
...logMetadata,
|
||||
const { dashboardInfo, explore, impressionId, dashboardLayout, sqlLab } =
|
||||
store.getState();
|
||||
let logMetadata = {
|
||||
impression_id: impressionId,
|
||||
version: 'v2',
|
||||
};
|
||||
} else if (explore?.slice) {
|
||||
logMetadata = {
|
||||
source: 'explore',
|
||||
source_id: explore.slice ? explore.slice.slice_id : 0,
|
||||
...(explore.slice.slice_id && { slice_id: explore.slice.slice_id }),
|
||||
...logMetadata,
|
||||
};
|
||||
} else if (path?.includes('/sqllab/')) {
|
||||
const editor = sqlLab.queryEditors.find(
|
||||
({ id }) => id === sqlLab.tabHistory.slice(-1)[0],
|
||||
);
|
||||
logMetadata = {
|
||||
source: 'sqlLab',
|
||||
source_id: editor?.id,
|
||||
db_id: editor?.dbId,
|
||||
schema: editor?.schema,
|
||||
};
|
||||
}
|
||||
const { eventName } = action.payload;
|
||||
let { eventData = {} } = action.payload;
|
||||
|
||||
if (eventName === LOG_ACTIONS_SPA_NAVIGATION) {
|
||||
navPath = eventData.path;
|
||||
}
|
||||
const path = navPath || window?.location?.href;
|
||||
|
||||
if (dashboardInfo?.id && path?.includes('/dashboard/')) {
|
||||
logMetadata = {
|
||||
source: 'dashboard',
|
||||
source_id: dashboardInfo.id,
|
||||
dashboard_id: dashboardInfo.id,
|
||||
...logMetadata,
|
||||
};
|
||||
} else if (explore?.slice) {
|
||||
logMetadata = {
|
||||
source: 'explore',
|
||||
source_id: explore.slice ? explore.slice.slice_id : 0,
|
||||
...(explore.slice.slice_id && { slice_id: explore.slice.slice_id }),
|
||||
...logMetadata,
|
||||
};
|
||||
} else if (path?.includes('/sqllab/')) {
|
||||
const editor = sqlLab.queryEditors.find(
|
||||
({ id }) => id === sqlLab.tabHistory.slice(-1)[0],
|
||||
);
|
||||
logMetadata = {
|
||||
source: 'sqlLab',
|
||||
source_id: editor?.id,
|
||||
db_id: editor?.dbId,
|
||||
schema: editor?.schema,
|
||||
};
|
||||
}
|
||||
|
||||
eventData = {
|
||||
...logMetadata,
|
||||
ts: new Date().getTime(),
|
||||
event_name: eventName,
|
||||
...eventData,
|
||||
};
|
||||
if (LOG_EVENT_TYPE_TIMING.has(eventName)) {
|
||||
eventData = {
|
||||
...logMetadata,
|
||||
ts: new Date().getTime(),
|
||||
event_name: eventName,
|
||||
...eventData,
|
||||
event_type: 'timing',
|
||||
trigger_event: lastEventId,
|
||||
};
|
||||
} else {
|
||||
lastEventId = nanoid();
|
||||
eventData = {
|
||||
...eventData,
|
||||
event_type: 'user',
|
||||
event_id: lastEventId,
|
||||
visibility: document.visibilityState,
|
||||
};
|
||||
}
|
||||
if (LOG_EVENT_TYPE_TIMING.has(eventName)) {
|
||||
eventData = {
|
||||
...eventData,
|
||||
event_type: 'timing',
|
||||
trigger_event: lastEventId,
|
||||
};
|
||||
} else {
|
||||
lastEventId = nanoid();
|
||||
eventData = {
|
||||
...eventData,
|
||||
event_type: 'user',
|
||||
event_id: lastEventId,
|
||||
visibility: document.visibilityState,
|
||||
};
|
||||
}
|
||||
|
||||
if (eventData.target_id && dashboardLayout?.present?.[eventData.target_id]) {
|
||||
const { meta } = dashboardLayout.present[eventData.target_id];
|
||||
// chart name or tab/header text
|
||||
eventData.target_name = meta.chartId ? meta.sliceName : meta.text;
|
||||
}
|
||||
if (
|
||||
eventData.target_id &&
|
||||
dashboardLayout?.present?.[eventData.target_id]
|
||||
) {
|
||||
const { meta } = dashboardLayout.present[eventData.target_id];
|
||||
// chart name or tab/header text
|
||||
eventData.target_name = meta.chartId ? meta.sliceName : meta.text;
|
||||
}
|
||||
|
||||
logMessageQueue.append(eventData);
|
||||
return eventData;
|
||||
logMessageQueue.append(eventData);
|
||||
return eventData;
|
||||
};
|
||||
};
|
||||
|
||||
export default loggerMiddleware;
|
||||
|
||||
Reference in New Issue
Block a user