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: support for custom time granularities #54

Closed
wants to merge 3 commits into from

Conversation

serramatutu
Copy link
Collaborator

Summary

This PR adds support for custom time granularities in the SDK.

Changes

  • Introduced new Grain type that can be either the standard TimeGranularity or str, representing a custom grain.
  • Changed all references to TimeGranularity in our input/output models to use Grain
  • Introduced some logic to query for the extra GraphQL field and merge it to queryable_granularities at __post_init__ in Metric and Dimension.

I recommend reviewing this commit by commit.

Breaking changes

Users expecting Metric.queryable_granularities and Dimension.queryable_granularities to be only TimeGranularity now need to account for them being str.

We introduced custom granularities, and now we need to add support for
it in the SDK. This commit is the first step towards that. It changes
all usages of `TimeGranularity` by a new `Grain` type, which can be
either the standard `TimeGranularity` or a `str` that represents a
custom granilarity.

We still haven't changed the underlying implementation to allow for
fetching these custom grains or sending them via GraphQL. This will be
next.
This commit adds a new classmethod called `extra_gql_fields` to
`GraphQLFragmentMixin`. This method should return any extra fields that
should be added to the GraphQL fragment that represents that dataclass.

This can be paired with `dataclasses.InitVar` to create "hidden" fields
that get queried via GraphQL and fed into `__post_init__` without
actually exposing them to the dataclass.

I used this to combine `queryableGranularities` and
`queryableTimeGranularities` in one single field that is a list of
`Grain`, so that it's less confusing to users.
@@ -32,7 +33,7 @@ def _serialize_val(cls, val: Any) -> str:
if isinstance(val, OrderByGroupBy):
d = f'Dimension("{val.name}")'
if val.grain:
grain_str = val.grain.name.lower()
grain_str = val.grain.name.lower() if isinstance(val.grain, TimeGranularity) else val.grain.lower()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@courtneyholcomb This supposes custom grains are not case-sensitive. Is this true?

Choose a reason for hiding this comment

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

They will be lowercase! This is enforced with a validation error in DSI

@serramatutu
Copy link
Collaborator Author

I closed this since I realized at the end that we're actually returning the existing grains from queryableTimeGranularity so I think it makes sense to just use strings everywhere and be happy.

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