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
22 changes: 16 additions & 6 deletions superset-frontend/src/middleware/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,7 +43,6 @@ describe('logger middleware', () => {
eventData: {
key: 'value',
start_offset: 100,
path: `/dashboard/${dashboardId}/`,
},
},
};
Expand Down Expand Up @@ -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({
Expand Down
140 changes: 76 additions & 64 deletions superset-frontend/src/middleware/loggerMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Comment on lines 72 to +74
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global Mutable State category Design

Tell me more
What is the issue?

The middleware uses mutable global state (lastEventId and effectively navPath) which makes the code harder to reason about and test.

Why this matters

Global mutable state can lead to unexpected behavior, makes testing difficult, and violates principles of functional programming that Redux typically encourages.

Suggested change ∙ Feature Preview

Encapsulate the state within a closure or class:

const createLoggerMiddleware = () => {
  let lastEventId = 0;
  let navPath = null;
  
  return store => next => action => {
    // middleware logic
  };
};

export default createLoggerMiddleware();
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

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 { 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 (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,
};
}
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 = {
...eventData,
event_type: 'timing',
trigger_event: lastEventId,
};
} else {
lastEventId = nanoid();
eventData = {
...logMetadata,
ts: new Date().getTime(),
event_name: eventName,
...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;
Loading