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

Refactor analytics queries #1280

Merged
merged 14 commits into from Jul 29, 2020
Merged

Refactor analytics queries #1280

merged 14 commits into from Jul 29, 2020

Conversation

timgl
Copy link
Collaborator

@timgl timgl commented Jul 27, 2020

Changes

Move most of the analytics logic into a queries folder. This will make sure we have all of our analytics logic in one place, and will make it much easier to change and extend that logic.

Checklist

  • All querysets/queries filter by Team (if applicable)
  • Backend tests (if applicable)
  • Cypress E2E tests (if applicable)

@timgl timgl temporarily deployed to posthog-refactor-querie-kn6ncb July 27, 2020 10:56 Inactive
@timgl timgl marked this pull request as draft July 27, 2020 13:13
self.assertEqual(action_response[0]["data"][1], 1.0)
self.assertEqual(action_response[0]["labels"][1], "Thu. 2 January")

self.assertTrue(self._compare_entity_response(action_response, event_response))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@EDsCODE I might remove all of these comparisons and just the actions functionality once to simplify and speed up tests. Thoughts? I'm not super sure what they were originally supposed to test

Copy link
Member

Choose a reason for hiding this comment

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

the compare entity response was meant to make sure the responses for actions and event responses don't differ if the entities are the same

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, i've left most of those in noww

@timgl timgl temporarily deployed to posthog-refactor-querie-kn6ncb July 27, 2020 13:49 Inactive

return entities_list

def run(self, filter: Filter, team: Team, actions: QuerySet):
Copy link
Member

Choose a reason for hiding this comment

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

This pattern helps a lot to unload logic from the /api.

I think for the case of reusability it may help to provide detailed comments above some of the utility functions (such as describing the params needed and the shape of the output). Mainly suggesting this because I was reading a function handle_compare and there was just a bit too much time required to remember what the function actually produces and how to use the output. This would allow us to reference the functions more easily and know what the inputs and outputs of a function are and more easily reuse them heavily.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, I've added comments. I'm still not really happy with these functions but I want to minimise the actual code changed in this PR, but will tackle those in the next ones

@timgl timgl temporarily deployed to posthog-refactor-querie-kn6ncb July 28, 2020 11:09 Inactive
@timgl timgl temporarily deployed to posthog-refactor-querie-kn6ncb July 28, 2020 16:34 Inactive
@timgl timgl temporarily deployed to posthog-refactor-querie-kn6ncb July 28, 2020 18:06 Inactive
@timgl timgl marked this pull request as ready for review July 28, 2020 18:06
@timgl timgl requested a review from EDsCODE July 28, 2020 18:06
@timgl timgl temporarily deployed to posthog-refactor-querie-kn6ncb July 29, 2020 08:55 Inactive
@timgl timgl temporarily deployed to posthog-refactor-querie-kn6ncb July 29, 2020 09:19 Inactive
@timgl timgl temporarily deployed to posthog-refactor-querie-kn6ncb July 29, 2020 09:36 Inactive
@timgl timgl temporarily deployed to posthog-refactor-querie-kn6ncb July 29, 2020 09:48 Inactive
Copy link
Member

@EDsCODE EDsCODE left a comment

Choose a reason for hiding this comment

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

A few lost params for retention

class Retention(BaseQuery):
def calculate_retention(self, filter: Filter, team: Team, start_entity: Optional[Entity] = None, total_days=11):
date_from: datetime.datetime = filter.date_from # type: ignore
filter._date_to = (date_from + timedelta(days=total_days)).isoformat()
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps in a followup refactor since this refactor is overall restructuring. We should standardize how/when to handle filters/params. For example, in this instance above we don't check any conditions and just write in something extra to the filter whereas in trends.py line 298 we do explicit checks before reassigning

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep agree, something I will tackle. It's annoying b/c the way they change the dates isn't consistent

posthog/queries/retention.py Outdated Show resolved Hide resolved
posthog/queries/retention.py Show resolved Hide resolved
posthog/api/action.py Show resolved Hide resolved
@timgl timgl temporarily deployed to posthog-refactor-querie-kn6ncb July 29, 2020 18:19 Inactive
@timgl timgl merged commit bfb258c into master Jul 29, 2020
@timgl timgl deleted the refactor-queries branch July 29, 2020 19:07
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