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

Session time series #659

Merged
merged 17 commits into from
Apr 28, 2020
Merged

Session time series #659

merged 17 commits into from
Apr 28, 2020

Conversation

EDsCODE
Copy link
Member

@EDsCODE EDsCODE commented Apr 24, 2020

Changes

  • adds ability to see session average over time series
  • clicking on a datapoint in line graph will show who had a session that day
  • fixes bug where setting date ranges that didn't have any events would break sessions

Todo:

  • reorganize logic to better integrate into current endpoints
  • fix/add tests

Checklist

  • All querysets/queries filter by Team
  • Code reviewed
  • QA'd

@timgl timgl temporarily deployed to posthog-session-time-se-d2eaz3 April 24, 2020 19:50 Inactive
@timgl timgl temporarily deployed to posthog-session-time-se-d2eaz3 April 27, 2020 14:34 Inactive
@timgl timgl temporarily deployed to posthog-session-time-se-d2eaz3 April 27, 2020 14:54 Inactive
@timgl timgl temporarily deployed to posthog-session-time-se-d2eaz3 April 27, 2020 15:03 Inactive
@EDsCODE EDsCODE marked this pull request as ready for review April 27, 2020 15:08
@EDsCODE
Copy link
Member Author

EDsCODE commented Apr 27, 2020

Noting #668 because it came about after working on this PR. Adding the line graph was pretty trivial but there's growing number of magic strings to navigate which made it more effort than needed. We could benefit from refactoring these out.

@@ -16,7 +15,7 @@ export function ActionsLineGraph({ dashboardItemId = null, filters: filtersParam
}, [toParams(otherFilters)])

return results ? (
results.reduce((total, item) => total + item.count, 0) > 0 ? (
filters.session || results.reduce((total, item) => total + item.count, 0) > 0 ? (
Copy link
Member Author

@EDsCODE EDsCODE Apr 27, 2020

Choose a reason for hiding this comment

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

Referring to comment above. I opted to use a bunch of spot if/elses like this to reduce possible collision with existing logic for this feature. #668 should go back and better centralize how a bunch of conditions like this throughout trends are working

@timgl timgl temporarily deployed to posthog-session-time-se-d2eaz3 April 27, 2020 15:27 Inactive
@EDsCODE EDsCODE requested a review from timgl April 27, 2020 15:29
@@ -112,7 +112,10 @@ export class LineGraph extends Component {
)
return null
var label = data.datasets[tooltipItem.datasetIndex].label || ''
return label + ' - ' + tooltipItem.yLabel.toLocaleString()
let formattedLabel = label + ' - ' + tooltipItem.yLabel.toLocaleString()
if (formattedLabel.includes('Average Duration of Session'))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This feels super brittle. Maybe this should just be a prop that you pass a function to (labelFormat or something), then we can pass that from ActionsLineGraph where at least we have access to the filter object

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed it so that the backend passes an extra chartLabel attribute in case we want to different labels for charts and tables. Let me know fi this works

@@ -185,7 +188,7 @@ export class LineGraph extends Component {
}
}
LineGraph.propTypes = {
datasets: PropTypes.arrayOf(PropTypes.shape({ label: PropTypes.string, count: PropTypes.number })).isRequired,
datasets: PropTypes.arrayOf(PropTypes.shape({ label: PropTypes.string, count: PropTypes.any })).isRequired,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is number any now?

Copy link
Member Author

Choose a reason for hiding this comment

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

Restructured the labels so this is number again!

overall_average = {'label': 'Average Duration of Session', 'count': avg_formatted}

cursor = connection.cursor()
cursor.execute(average_length_time(all_sessions), sessions_sql_params)
Copy link
Collaborator

Choose a reason for hiding this comment

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

These queries aren't cheap. I wonder if you could combine them into one query, maybe by getting both the average and the number of sessions for each day and then summing that.

Also, in overall_average_length, the query might be a bit quicker if you do SELECT COUNT(1) instead of COUNT(*).

Copy link
Member Author

Choose a reason for hiding this comment

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

Condensed this so that it calculates the average outside the query and only has to use one query to get the time series data and totals. This could be a different slow down but I think calculating an average serverside rather than submitting a whole query for that one number is better. Though, this hasn't been clocked so I'm not completely sure.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's still a bit slow (50s with a few million events) though no more so than before

posthog/api/action.py Show resolved Hide resolved
@timgl timgl temporarily deployed to posthog-session-time-se-d2eaz3 April 27, 2020 17:25 Inactive
@timgl timgl temporarily deployed to posthog-session-time-se-d2eaz3 April 27, 2020 17:31 Inactive
@timgl timgl temporarily deployed to posthog-session-time-se-d2eaz3 April 28, 2020 04:15 Inactive
@timgl
Copy link
Collaborator

timgl commented Apr 28, 2020

Sorry, merge conflict! Happy to merge after :)

Edit: Having had a last play (and sorry for reversal on this) but I think it would be good to convert seconds into human readable in the frontend. It's fine with lower numbers, but can anyone intuitively understand what '1400 seconds' means? We could push to different PR but for the experience we should do this.

@timgl timgl temporarily deployed to posthog-session-time-se-d2eaz3 April 28, 2020 16:54 Inactive
@EDsCODE
Copy link
Member Author

EDsCODE commented Apr 28, 2020

Yeah, good point. Made an issue here #680 so we can discuss how to display when there are an assortment of different values

@EDsCODE EDsCODE merged commit d4df762 into master Apr 28, 2020
@EDsCODE EDsCODE deleted the session-time-series branch April 28, 2020 17:00
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.

2 participants