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

feat(insights): Log scale option in Trends #22995

Merged
merged 15 commits into from
Jun 27, 2024

Conversation

nikitaevg
Copy link
Contributor

@nikitaevg nikitaevg commented Jun 16, 2024

Problem

#22564

Changes

Add support for log scale for trends.

image

Does this work well for both Cloud and self-hosted?

Yes

How did you test this code?

Manually + a storybook test

Copy link
Contributor Author

@nikitaevg nikitaevg left a comment

Choose a reason for hiding this comment

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

@pauldambra please take a look (or reroute to somebody?)

About tests, the only proper way I see to test this is a visual test. It turns out visual tests don't draw the graphs (see this for example), so I actually don't see any good way to test this 🤷

I also wonder if I should keep the y axis tick values, or remove them, like this.

@@ -16,6 +16,7 @@ export const isLegacyTrendsFilter = (filters: Record<string, any> | undefined):
'show_labels_on_series',
'compare',
'compare_to',
'y_axis_scale_type',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if this part is needed? What is the legacy trends, are they still present?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

(Thomas will be back on Monday, so will wait with merging this till then)

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not necessary to add here. This is a frontend side migration for notebook nodes (these already store "queries", whereas insights store "filters"). Legacy in this case refers to some properties that had been snake_cased and are now camelCased.

callback: (value) => {
return formatPercentStackAxisValue(trendsFilter, value, isPercentStackView)
},
},
grid: gridOptions,
type: yAxisScaleType === 'log10' ? 'logarithmic' : 'linear',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Log scale graphs don't show zero values, see the screenshot. This can be fixed (as per this thread), I wonder if it's needed?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I think complete omission – as in the screenshot – is pretty confusing. But Grafana seems to do this pretty well! They seem to treat 0 values as essentially negative infinite on the log scale:

Screenshot 2024-06-19 at 15 00 30

Can we pull this off?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only workaround I found that might work is

  1. Replace zeroes with 1e-12
  2. Set minimum of the graph to the minimum value among the trend values (so that the graph doesn't show 1e-12, like here)
  3. Figure out how to show "0" instead of "1e-12" when hovering, this person couldn't do it.

Do you think it's worth trying? Because that sounds like a lot and the result is not guaranteed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Played with this a bit and got Grafana-like results (pushed a commit):

Before

Screenshot 2024-06-20 at 14 03 24

After

Screenshot 2024-06-20 at 14 04 34

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Damn, thanks!

@nikitaevg nikitaevg marked this pull request as ready for review June 16, 2024 12:05
@pauldambra pauldambra requested a review from a team June 16, 2024 12:24
Copy link
Collaborator

@Twixes Twixes left a comment

Choose a reason for hiding this comment

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

Looks great to me, just got a suggestion about the zeros!

As for the schema handling, I'll tag @thmsobrmlr here, since he knows much better about the cleaning/conversion functions we've got

@Twixes Twixes requested a review from thmsobrmlr June 19, 2024 13:34
@Twixes Twixes changed the title feat(insights): support log scale feat(insights): Log scale option in Trends Jun 19, 2024
@nikitaevg nikitaevg requested a review from Twixes June 19, 2024 18:31
Copy link
Collaborator

@Twixes Twixes left a comment

Choose a reason for hiding this comment

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

Dug into Chart.js a bit, the chart feels pretty smooth now 😄

@@ -2159,6 +2159,7 @@ export interface TrendsFilterType extends FilterType {
show_values_on_series?: boolean
show_labels_on_series?: boolean
show_percent_stack_view?: boolean
y_axis_scale_type?: string // "log10" or "linear"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe

Suggested change
y_axis_scale_type?: string // "log10" or "linear"
y_axis_scale_type?: 'log10' | 'linear'

@thmsobrmlr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, makes sense, updated, thanks!

@@ -16,6 +16,7 @@ export const isLegacyTrendsFilter = (filters: Record<string, any> | undefined):
'show_labels_on_series',
'compare',
'compare_to',
'y_axis_scale_type',
Copy link
Collaborator

Choose a reason for hiding this comment

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

...tickOptions,
precision,
display: !hideYAxis,
...(yAxisScaleType !== 'log10' && { precision }), // Precision is not supported for the log scale
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't leave precision as is here because types don't match then, because precision is not a field for log scale.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh of course, I didn't notice TypeScript alerting me about this in VS Code, so missed that

nikitaevg and others added 6 commits June 20, 2024 18:28
# Conflicts:
#	frontend/src/queries/nodes/InsightQuery/utils/queryNodeToFilter.test.ts
#	frontend/src/queries/nodes/InsightQuery/utils/queryNodeToFilter.ts
#	frontend/src/queries/nodes/InsightViz/utils.ts
#	frontend/src/queries/schema.ts
#	frontend/src/scenes/insights/insightVizDataLogic.ts
#	frontend/src/scenes/insights/utils/compareInsightQuery.ts
Copy link
Contributor

@thmsobrmlr thmsobrmlr left a comment

Choose a reason for hiding this comment

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

Great work!

@@ -16,6 +16,7 @@ export const isLegacyTrendsFilter = (filters: Record<string, any> | undefined):
'show_labels_on_series',
'compare',
'compare_to',
'y_axis_scale_type',
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not necessary to add here. This is a frontend side migration for notebook nodes (these already store "queries", whereas insights store "filters"). Legacy in this case refers to some properties that had been snake_cased and are now camelCased.

@thmsobrmlr thmsobrmlr merged commit 6144971 into PostHog:master Jun 27, 2024
88 of 90 checks passed
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.

4 participants