-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support blank identifiers, assume transient (#4449)
Co-authored-by: Matthew Elwell <[email protected]>
- Loading branch information
1 parent
2ab73ed
commit 0014a5b
Showing
9 changed files
with
347 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import hashlib | ||
import uuid | ||
from itertools import chain | ||
from operator import itemgetter | ||
from typing import TypeAlias | ||
|
||
from django.utils import timezone | ||
|
||
from environments.identities.models import Identity | ||
from environments.identities.traits.models import Trait | ||
from environments.models import Environment | ||
from environments.sdk.types import SDKTraitData | ||
|
||
IdentityAndTraits: TypeAlias = tuple[Identity, list[Trait]] | ||
|
||
|
||
def get_transient_identity_and_traits( | ||
environment: Environment, | ||
sdk_trait_data: list[SDKTraitData], | ||
) -> IdentityAndTraits: | ||
""" | ||
Get a transient `Identity` instance with a randomly generated identifier. | ||
All traits are marked as transient. | ||
""" | ||
return ( | ||
( | ||
identity := _get_transient_identity( | ||
environment=environment, | ||
identifier=get_transient_identifier(sdk_trait_data), | ||
) | ||
), | ||
identity.generate_traits(_ensure_transient(sdk_trait_data), persist=False), | ||
) | ||
|
||
|
||
def get_identified_transient_identity_and_traits( | ||
environment: Environment, | ||
identifier: str, | ||
sdk_trait_data: list[SDKTraitData], | ||
) -> IdentityAndTraits: | ||
""" | ||
Get a transient `Identity` instance. | ||
If present in storage, it's a previously persisted identity with its traits, | ||
combined with incoming traits provided to `sdk_trait_data` argument. | ||
All traits constructed from `sdk_trait_data` are marked as transient. | ||
""" | ||
sdk_trait_data = _ensure_transient(sdk_trait_data) | ||
if identity := Identity.objects.filter( | ||
environment=environment, | ||
identifier=identifier, | ||
).first(): | ||
return identity, identity.update_traits(sdk_trait_data) | ||
return ( | ||
identity := _get_transient_identity( | ||
environment=environment, | ||
identifier=identifier, | ||
) | ||
), identity.generate_traits(sdk_trait_data, persist=False) | ||
|
||
|
||
def get_persisted_identity_and_traits( | ||
environment: Environment, | ||
identifier: str, | ||
sdk_trait_data: list[SDKTraitData], | ||
) -> IdentityAndTraits: | ||
""" | ||
Retrieve a previously persisted `Identity` instance or persist a new one. | ||
Traits are persisted based on the organisation-level setting or a | ||
`"transient"` attribute provided with each individual trait. | ||
""" | ||
identity, created = Identity.objects.get_or_create( | ||
environment=environment, | ||
identifier=identifier, | ||
) | ||
persist_trait_data = environment.project.organisation.persist_trait_data | ||
if created: | ||
return identity, identity.generate_traits( | ||
sdk_trait_data, | ||
persist=persist_trait_data, | ||
) | ||
if persist_trait_data: | ||
return identity, identity.update_traits(sdk_trait_data) | ||
return identity, list( | ||
{ | ||
trait.trait_key: trait | ||
for trait in chain( | ||
identity.identity_traits.all(), | ||
identity.generate_traits(sdk_trait_data, persist=False), | ||
) | ||
}.values() | ||
) | ||
|
||
|
||
def get_transient_identifier(sdk_trait_data: list[SDKTraitData]) -> str: | ||
if sdk_trait_data: | ||
return hashlib.sha256( | ||
"".join( | ||
f'{trait["trait_key"]}{trait["trait_value"]["value"]}' | ||
for trait in sorted(sdk_trait_data, key=itemgetter("trait_key")) | ||
).encode(), | ||
usedforsecurity=False, | ||
).hexdigest() | ||
return uuid.uuid4().hex | ||
|
||
|
||
def _get_transient_identity( | ||
environment: Environment, | ||
identifier: str, | ||
) -> Identity: | ||
return Identity( | ||
created_date=timezone.now(), | ||
environment=environment, | ||
identifier=identifier, | ||
) | ||
|
||
|
||
def _ensure_transient(sdk_trait_data: list[SDKTraitData]) -> list[SDKTraitData]: | ||
for sdk_trait_data_item in sdk_trait_data: | ||
sdk_trait_data_item["transient"] = True | ||
return sdk_trait_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import typing | ||
|
||
from typing_extensions import NotRequired | ||
|
||
|
||
class SDKTraitValueData(typing.TypedDict): | ||
type: str | ||
value: str | ||
|
||
|
||
class SDKTraitData(typing.TypedDict): | ||
trait_key: str | ||
trait_value: SDKTraitValueData | ||
transient: NotRequired[bool] |
Oops, something went wrong.