Skip to content

Commit

Permalink
Merge pull request #1172 from ansible/goneri/telemetry_opt_out-use-a-…
Browse files Browse the repository at this point in the history
…different-name-for-the-property_28405

telemetry_opt_out: use a different name for the property
  • Loading branch information
goneri authored Jul 2, 2024
2 parents f78c5ca + 745e885 commit 0253f30
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion ansible_ai_connect/ai/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def validate_inlineSuggestion(self, value):

if user.rh_user_has_seat is False:
return value
if user.organization and user.organization.telemetry_opt_out is False:
if user.organization and user.organization.has_telemetry_opt_out is False:
return value
else:
raise serializers.ValidationError("invalid feedback type for user")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get(self, request, *args, **kwargs):
if not organization:
return Response(status=HTTP_400_BAD_REQUEST)

return Response(status=HTTP_200_OK, data={"optOut": organization.telemetry_opt_out})
return Response(status=HTTP_200_OK, data={"optOut": organization.has_telemetry_opt_out})

except ServiceUnavailable:
raise
Expand All @@ -88,7 +88,7 @@ def get(self, request, *args, **kwargs):
"duration": duration,
"exception": exception is not None,
"problem": None if exception is None else exception.__class__.__name__,
"opt_out": None if organization is None else organization.telemetry_opt_out,
"opt_out": None if organization is None else organization.has_telemetry_opt_out,
}
send_segment_event(event, "telemetrySettingsGet", request.user)

Expand Down Expand Up @@ -155,7 +155,7 @@ def post(self, request, *args, **kwargs):
"duration": duration,
"exception": exception is not None,
"problem": None if exception is None else exception.__class__.__name__,
"opt_out": None if organization is None else organization.telemetry_opt_out,
"opt_out": None if organization is None else organization.has_telemetry_opt_out,
}
send_segment_event(event, "telemetrySettingsSet", request.user)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def test_get_settings_when_undefined(self, LDClient, *args):
@patch.object(feature_flags, "LDClient")
def test_get_settings_when_defined(self, LDClient, *args):
LDClient.return_value.variation.return_value = True
self.user.organization = Organization.objects.get_or_create(
id=123, _telemetry_opt_out=True
)[0]
self.user.organization = Organization.objects.get_or_create(id=123, telemetry_opt_out=True)[
0
]
self.client.force_authenticate(user=self.user)

with self.assertLogs(logger="root", level="DEBUG") as log:
Expand Down
3 changes: 2 additions & 1 deletion ansible_ai_connect/ai/api/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ def test_invalid_ansible_extension_version_on_inline_suggestion(self):
serializer.is_valid(raise_exception=True)

def test_commercial_user_not_opted_out_passes_on_inlineSuggestion(self):
org = Mock(telemetry_opt_out=False)
org = Mock(has_telemetry_opt_out=False)

user = Mock(rh_user_has_seat=True, organization=org)
request = Mock(user=user)
serializer = FeedbackRequestSerializer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def send_segment_analytics_event(
logger.info("Analytics telemetry not active, because of no organization assigned for user.")
return

if organization.telemetry_opt_out:
if organization.has_telemetry_opt_out:
logger.info(f"Organization '{organization.id}' has opted out of Analytics telemetry.")
return

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.11 on 2024-07-02 17:23

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("organizations", "0002_user_organization_fix_id"),
]

operations = [
migrations.AlterField(
model_name="organization",
name="telemetry_opt_out",
field=models.BooleanField(db_column="telemetry_opt_out", default=False),
),
]
10 changes: 3 additions & 7 deletions ansible_ai_connect/organizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,16 @@

class Organization(models.Model):
id = models.IntegerField(primary_key=True)
_telemetry_opt_out = models.BooleanField(default=False, db_column="telemetry_opt_out")
telemetry_opt_out = models.BooleanField(default=False, db_column="telemetry_opt_out")

@property
def telemetry_opt_out(self):
def has_telemetry_opt_out(self):
# For saas deployment mode, telemetry is opted-in by default (telemetry_opt_out=False)
# For others, telemetry is not supported, considered opted-out (telemetry_opt_out=True)
if settings.DEPLOYMENT_MODE == "saas":
return self._telemetry_opt_out
return self.telemetry_opt_out
return True

@telemetry_opt_out.setter
def telemetry_opt_out(self, value):
self._telemetry_opt_out = value

@cached_property
def is_subscription_check_should_be_bypassed(self) -> bool:
# Avoid circular dependency issue with lazy import
Expand Down
28 changes: 14 additions & 14 deletions ansible_ai_connect/organizations/tests/test_organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,51 @@

class TestOrganization(TestCase):
def test_org_with_telemetry_schema_2_opted_in(self):
organization = Organization.objects.get_or_create(id=123, _telemetry_opt_out=False)[0]
self.assertFalse(organization.telemetry_opt_out)
organization = Organization.objects.get_or_create(id=123, has_telemetry_opt_out=False)[0]
self.assertFalse(organization.has_telemetry_opt_out)

def test_org_with_telemetry_schema_2_opted_out(self):
organization = Organization.objects.get_or_create(id=123, _telemetry_opt_out=True)[0]
self.assertTrue(organization.telemetry_opt_out)
organization = Organization.objects.get_or_create(id=123, has_telemetry_opt_out=True)[0]
self.assertTrue(organization.has_telemetry_opt_out)

@override_settings(LAUNCHDARKLY_SDK_KEY="dummy_key")
@patch.object(feature_flags, "LDClient")
def test_org_with_telemetry_schema_2_opted_in_with_feature_flag_override(self, LDClient):
LDClient.return_value.variation.return_value = True
organization = Organization.objects.get_or_create(id=123, _telemetry_opt_out=True)[0]
self.assertTrue(organization.telemetry_opt_out)
organization = Organization.objects.get_or_create(id=123, has_telemetry_opt_out=True)[0]
self.assertTrue(organization.has_telemetry_opt_out)

@override_settings(LAUNCHDARKLY_SDK_KEY="dummy_key")
@patch.object(feature_flags, "LDClient")
def test_org_with_telemetry_schema_2_opted_in_with_feature_flag_no_override(self, LDClient):
LDClient.return_value.variation.return_value = False
organization = Organization.objects.get_or_create(id=123, _telemetry_opt_out=True)[0]
self.assertTrue(organization.telemetry_opt_out)
organization = Organization.objects.get_or_create(id=123, has_telemetry_opt_out=True)[0]
self.assertTrue(organization.has_telemetry_opt_out)

@override_settings(LAUNCHDARKLY_SDK_KEY="dummy_key")
@patch.object(feature_flags, "LDClient")
def test_org_with_telemetry_schema_2_opted_out_with_feature_flag_override(self, LDClient):
LDClient.return_value.variation.return_value = True
organization = Organization.objects.get_or_create(id=123, _telemetry_opt_out=False)[0]
self.assertFalse(organization.telemetry_opt_out)
organization = Organization.objects.get_or_create(id=123, has_telemetry_opt_out=False)[0]
self.assertFalse(organization.has_telemetry_opt_out)

@override_settings(LAUNCHDARKLY_SDK_KEY="dummy_key")
@patch.object(feature_flags, "LDClient")
def test_org_with_telemetry_schema_2_opted_out_with_feature_flag_no_override(self, LDClient):
LDClient.return_value.variation.return_value = False
organization = Organization.objects.get_or_create(id=123, _telemetry_opt_out=False)[0]
self.assertFalse(organization.telemetry_opt_out)
organization = Organization.objects.get_or_create(id=123, has_telemetry_opt_out=False)[0]
self.assertFalse(organization.has_telemetry_opt_out)

@override_settings(LAUNCHDARKLY_SDK_KEY="dummy_key")
@patch.object(feature_flags, "LDClient")
def test_org_with_unlimited_access_allowed_with_feature_flag_override(self, LDClient):
LDClient.return_value.variation.return_value = True
organization = Organization.objects.get_or_create(id=123, _telemetry_opt_out=False)[0]
organization = Organization.objects.get_or_create(id=123, has_telemetry_opt_out=False)[0]
self.assertTrue(organization.is_subscription_check_should_be_bypassed)

@override_settings(LAUNCHDARKLY_SDK_KEY="dummy_key")
@patch.object(feature_flags, "LDClient")
def test_org_with_no_unlimited_access_allowed_with_feature_flag_no_override(self, LDClient):
LDClient.return_value.variation.return_value = False
organization = Organization.objects.get_or_create(id=123, _telemetry_opt_out=False)[0]
organization = Organization.objects.get_or_create(id=123, has_telemetry_opt_out=False)[0]
self.assertFalse(organization.is_subscription_check_should_be_bypassed)
2 changes: 1 addition & 1 deletion ansible_ai_connect/users/tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def create_user(
rh_org_id: int = 1234567,
org_opt_out: bool = False,
):
(org, _) = Organization.objects.get_or_create(id=rh_org_id, _telemetry_opt_out=org_opt_out)
(org, _) = Organization.objects.get_or_create(id=rh_org_id, telemetry_opt_out=org_opt_out)
username = username or "u" + "".join(random.choices(string.digits, k=5))
password = password or "secret"
email = username + "@example.com"
Expand Down
2 changes: 1 addition & 1 deletion ansible_ai_connect/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def retrieve(self, request, *args, **kwargs):
# Enrich with Organisational data, if necessary
organization = self.request.user.organization
user_data["org_telemetry_opt_out"] = (
organization.telemetry_opt_out if organization else True
organization.has_telemetry_opt_out if organization else True
)

return Response(user_data)
Expand Down

0 comments on commit 0253f30

Please sign in to comment.