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

Fix metric undefined #2589

Merged
merged 6 commits into from
Jan 18, 2023
Merged

Fix metric undefined #2589

merged 6 commits into from
Jan 18, 2023

Conversation

RobertJoonas
Copy link
Contributor

@RobertJoonas RobertJoonas commented Jan 12, 2023

Changes

This PR fixes a bug introduced in #2560. For a reproduction of the bug, go to this dashboard URL and remove the Signup.logged_in is false filter. After doing so:

  • The graph disappears (main-graph API request returns 500)
  • An error is shown in the console: ApiError: ** (CaseClauseError) no case clause matching: :undefined

This is because metric becomes undefined in the VisitorGraph state when setting it to topStatLabels[0] on this line (if topStatLabels is an empty array).

This PR makes sure that we're not relying on the top stat labels when setting the metric. It also does some refactoring to make the code more readable. Reviewing commit-by-commit suggested.

Tests

  • This PR does not require tests

Changelog

  • This PR does not make a user-facing change

Documentation

  • This change does not need a documentation update

Dark mode

  • This PR does not change the UI

@bundlemon
Copy link

bundlemon bot commented Jan 12, 2023

BundleMon

Files updated (1)
Status Path Size Limits
static/js/dashboard.js
297.78KB (-22B -0.01%) -
Unchanged files (7)
Status Path Size Limits
static/css/app.css
515.18KB -
static/js/sentry-bundle-5.9.1-min.js
15.7KB -
static/js/app.js
12.13KB -
static/js/embed.host.js
5.58KB -
static/js/embed.content.js
5.06KB -
tracker/js/plausible.js
748B -
static/js/applyTheme.js
314B -

Total files change -22B 0%

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

@RobertJoonas RobertJoonas requested a review from a team January 12, 2023 20:12
@ukutaht
Copy link
Contributor

ukutaht commented Jan 17, 2023

I can't seem to reproduce this locally on master, do I need to be on some branch?

EDIT: my bad, I wasn't on the realtime graph

@@ -437,6 +435,7 @@ export default class VisitorGraph extends React.Component {
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/top-stats`, this.props.query)
.then((res) => {
this.setState({ topStatsLoadingState: LOADING_STATE.loaded, topStatData: res })
this.resetMetric()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why move this from componentDidUpdate to the API response callback?

Copy link
Contributor Author

@RobertJoonas RobertJoonas Jan 17, 2023

Choose a reason for hiding this comment

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

I did this because the top stat labels can only change when we fetch new data (the labels are returned from the API). And the only condition where we want to "reset the metric" is when the labels have changed.

This will avoid having to write this (or something similar) in componentDidUpdate

const topStatLabels = topStatData && topStatData.top_stats.map(({ name }) => METRIC_MAPPING[name]).filter(name => name)
const prevTopStatLabels = prevState.topStatData && prevState.topStatData.top_stats.map(({ name }) => METRIC_MAPPING[name]).filter(name => name)

if (topStatLabels && `${topStatLabels}` !== `${prevTopStatLabels}`) {
   //.....
}

(There's another function called updateMetric which is used when a new metric is selected)

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. In this case I think it would be best to run it as a callback to setState like so:

this.setState({ topStatsLoadingState: LOADING_STATE.loaded, topStatData: res }, this.resetMetrics)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in f1ef1f4

@RobertJoonas RobertJoonas merged commit c4b8bf8 into master Jan 18, 2023
@RobertJoonas RobertJoonas deleted the fix-metric-undefined branch January 18, 2023 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants