"""Features logic.""" from owsresponse import response from pythonfeatures.constants import context as context_constants from pythonfeatures.constants import split as split_constants from pythonfeatures.models import context from pythonfeatures.models import split def _get_traffic_type_identifier(attributes): """Get traffic type identifier.""" precedence = [ context_constants.ORCHARD_IDENTITY_UUID, context_constants.IDENTITY_ID, context_constants.PROFILE_TYPE_AND_ID, context_constants.ORCHARD_USER_ID, context_constants.VENDOR_ID, context_constants.SUBACCOUNT_ID ] for key in precedence: attr = attributes.get(key) if attr: if key == context_constants.VENDOR_ID: return '{}{}'.format( split_constants.TRAFFIC_TYPE_VENDOR_PREFIX, attr) elif key == context_constants.SUBACCOUNT_ID: return '{}{}'.format( split_constants.TRAFFIC_TYPE_SUBACCOUNT_PREFIX, attr) return attr return split_constants.TRAFFIC_TYPE_ANONYMOUS def _get_all_features(user_context): """Get variants for all features in user context. Args: user_context(dict): parameter to filter features based on split rules eg: {'user_id': 'oa:562'}, {'user_id': 'alw:46997'}, {'vendor_id': 23904}, {'identity_id': '5c548bd462501638ea3ed444'}, { 'uuid': '77f607ed-c303-4eb7-9c05-f8b19eb94757', 'user_id': 'alw:786'} Returns: oto.response: contains a dict of the feature data """ all_features = split.get_all_features( traffic_type=_get_traffic_type_identifier(user_context), attributes=user_context) for key, value in all_features.items(): if value == split_constants.SPLIT_ENABLED: all_features[key] = split_constants.FEATURE_ENABLED elif value == split_constants.SPLIT_DISABLED: all_features[key] = split_constants.FEATURE_DISABLED return response.Response(all_features) def _get_single_feature(feature_name, user_context): """Get variant for single feature in user context. Args: user_context(dict): parameter to filter features based on split rules eg: {'user_id': 'oa:562'}, {'user_id': 'alw:46997'}, {'vendor_id': 23904}, {'identity_id': '5c548bd462501638ea3ed444'}, { 'uuid': '77f607ed-c303-4eb7-9c05-f8b19eb94757', 'user_id': 'alw:786'} Returns: oto.response: String with feature variant given user context eg: control, enabled. """ variant = split.get_single_feature( traffic_type=_get_traffic_type_identifier(user_context), feature_name=feature_name, attributes=user_context) return response.Response( split_constants.FEATURE_ENABLED if variant == split_constants.SPLIT_ENABLED else split_constants.FEATURE_DISABLED) def get_all_features_with_profile_context( profile_type=None, profile_id=None, identity_id=None, orchard_identity_uuid=None ): """Get variants for all feature in profile context. Args: profile_type (str): the profile type. e.g. 'ArtistProfile', 'LabelProfile', 'PodcastProfile', 'InsightsProfile' profile_id (int): the profile_id within the profile_type identity_id (str): the Auth0 id of the user orchard_identity_uuid (str): the Orchard unique Identity identifier e.g. '77f607ed-c303-4eb7-9c05-f8b19eb94757' Returns: oto.response: contains a dict of the feature data """ context_helper = context.ProfileContext( profile_type, profile_id, identity_id, orchard_identity_uuid) validation_response = context_helper.validate() if not validation_response: return validation_response return _get_all_features(context_helper.to_dict()) def get_single_feature_with_profile_context( feature_name, profile_type=None, profile_id=None, identity_id=None, orchard_identity_uuid=None ): """Get variant for single feature in profile context. Args: feature_name (str): the name of the feature. profile_type (str): the profile type. e.g. 'ArtistProfile', 'LabelProfile', 'PodcastProfile', 'InsightsProfile' profile_id (int): the profile_id within the profile_type identity_id (str): the Auth0 id of the user orchard_identity_uuid (str): the Orchard unique Identity identifier e.g. '77f607ed-c303-4eb7-9c05-f8b19eb94757' Returns: oto.response: String with feature variant given user context eg: control, enabled. """ context_helper = context.ProfileContext( profile_type, profile_id, identity_id, orchard_identity_uuid) validation_response = context_helper.validate() if not validation_response: return validation_response return _get_single_feature(feature_name, context_helper.to_dict()) def get_all_features_with_account_context( orchard_user_id=None, subaccount_id=None, vendor_id=None, identity_id=None, orchard_identity_uuid=None ): """Get variants for all feature in account context. Args: orchard_user_id (str): the orchard user id. e.g. 'oa:456' or 'alw:789'. subaccount_id (int): subaccount id for this user vendor_id (int): vendor_id for this user identity_id (str): the Auth0 id of the user orchard_identity_uuid (str): the Orchard unique Identity identifier e.g. '77f607ed-c303-4eb7-9c05-f8b19eb94757' Returns: oto.response: contains a dict of the feature data """ context_helper = context.AccountContext( orchard_user_id, subaccount_id, vendor_id, identity_id, orchard_identity_uuid) validation_response = context_helper.validate() if not validation_response: return validation_response return _get_all_features(context_helper.to_dict()) def get_single_feature_with_account_context( feature_name, orchard_user_id=None, subaccount_id=None, vendor_id=None, identity_id=None, orchard_identity_uuid=None ): """Get variant for single feature in account context. Args: feature_name (str): the name of the feature. orchard_user_id (str): the orchard user id. e.g. 'oa:456' or 'alw:789'. subaccount_id (int): subaccount id for this user vendor_id (int): vendor_id for this user identity_id (str): the Auth0 id of the user orchard_identity_uuid (str): the Orchard unique Identity identifier e.g. '77f607ed-c303-4eb7-9c05-f8b19eb94757' Returns: oto.response: String with feature variant given user context eg: control, enabled. """ context_helper = context.AccountContext( orchard_user_id, subaccount_id, vendor_id, identity_id, orchard_identity_uuid) validation_response = context_helper.validate() if not validation_response: return validation_response return _get_single_feature(feature_name, context_helper.to_dict()) def get_single_feature_by_attributes(feature_name, attributes): """Get single feature with given attributes. Args: feature_name (str): the name of the feature eg: ows_features_example attributes(dict): parameter to filter features based on split rules eg: {'user_id': 'oa:562'}, {'user_id': 'alw:46997'}, {'vendor_id': 23904}, {'identity_id': '5c548bd462501638ea3ed444'} """ return _get_single_feature( feature_name, attributes)