Skip to content

Commit

Permalink
Merge pull request #32 from reddit/update_to_event_dict
Browse files Browse the repository at this point in the history
Append v2 event structs in to_event_dict()
  • Loading branch information
mrlevitas authored May 6, 2022
2 parents 821d5d9 + 0ffd75d commit 4ccac86
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
46 changes: 32 additions & 14 deletions reddit_decider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,52 @@ def to_dict(self) -> Dict:
}

def to_event_dict(self) -> Dict:
# prepend required v2 event schema prefixes
modified_extracted_fields = (self._extracted_fields or {}).copy()
user_fields = {
"id": self._user_id,
"logged_in": self._logged_in,
"cookie_created_timestamp": self._cookie_created_timestamp,
"is_employee": self._user_is_employee
}

ef = self._extracted_fields or {}

build_number = modified_extracted_fields.get("build_number")
if build_number:
modified_extracted_fields["app_build_number"] = build_number
app_fields = {}
if ef.get("app_name"):
app_fields["name"] = ef["app_name"]
if ef.get("build_number"):
app_fields["build_number"] = ef["build_number"]
if self._locale:
app_fields["relevant_locale"] = self._locale

canonical_url = modified_extracted_fields.get("canonical_url")
if canonical_url:
modified_extracted_fields["request_canonical_url"] = canonical_url
geo_fields = {}
if self._country_code:
geo_fields["country_code"] = self._country_code

request_fields = {}
if ef.get("canonical_url"):
request_fields["canonical_url"] = ef["canonical_url"]

platform_fields = {}
if self._device_id:
platform_fields["device_id"] = self._device_id

return {
"user_id": self._user_id,
"country_code": self._country_code,
"geo_country_code": self._country_code,
"locale": self._locale,
"app_relevant_locale": self._locale,
"user_is_employee": self._user_is_employee,
"logged_in": self._logged_in,
"user_logged_in": self._logged_in,
"device_id": self._device_id,
"platform_device_id": self._device_id,
"origin_service": self._origin_service,
"cookie_created_timestamp": self._cookie_created_timestamp,
**modified_extracted_fields,
"user": user_fields,
"app": app_fields,
"geo": geo_fields,
"request": request_fields,
"platform": platform_fields,
**ef,
}


def init_decider_parser(file: IO) -> Any:
return rust_decider.init("darkmode overrides targeting holdout mutex_group fractional_availability value", file.name)

Expand Down
20 changes: 14 additions & 6 deletions tests/decider_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,34 @@ def test_make_object_for_context_and_decider_context(self, _filewatcher):

decider_event_dict = decider_context.to_event_dict()
self.assertEqual(decider_event_dict["user_id"], USER_ID)
self.assertEqual(decider_event_dict["user"]["id"], USER_ID)
self.assertEqual(decider_event_dict["country_code"], COUNTRY_CODE)
self.assertEqual(decider_event_dict["geo_country_code"], COUNTRY_CODE)
self.assertEqual(decider_event_dict["geo"]["country_code"], COUNTRY_CODE)
self.assertEqual(decider_event_dict["user_is_employee"], True)
self.assertEqual(decider_event_dict["user"]["is_employee"], True)
self.assertEqual(decider_event_dict["logged_in"], IS_LOGGED_IN)
self.assertEqual(decider_event_dict["user_logged_in"], IS_LOGGED_IN)
self.assertEqual(decider_event_dict["user"]["logged_in"], IS_LOGGED_IN)

self.assertEqual(decider_event_dict["device_id"], DEVICE_ID)
self.assertEqual(decider_event_dict["platform_device_id"], DEVICE_ID)
self.assertEqual(decider_event_dict["platform"]["device_id"], DEVICE_ID)
self.assertEqual(decider_event_dict["locale"], LOCALE_CODE)
self.assertEqual(decider_event_dict["app_relevant_locale"], LOCALE_CODE)
self.assertEqual(decider_event_dict["app"]["relevant_locale"], LOCALE_CODE)
self.assertEqual(decider_event_dict["origin_service"], ORIGIN_SERVICE)
self.assertEqual(decider_event_dict.get("auth_client_id"), None)
self.assertEqual(
decider_event_dict["cookie_created_timestamp"],
self.mock_span.context.edge_context.user.event_fields().get("cookie_created_timestamp"),
)
self.assertEqual(
decider_event_dict["user"]["cookie_created_timestamp"],
self.mock_span.context.edgecontext.user.event_fields().get("cookie_created_timestamp"),
)
self.assertEqual(decider_event_dict["app_name"], APP_NAME)
self.assertEqual(decider_event_dict["app"]["name"], APP_NAME)
self.assertEqual(decider_event_dict["build_number"], BUILD_NUMBER)
self.assertEqual(decider_event_dict["app_build_number"], BUILD_NUMBER)
self.assertEqual(decider_event_dict["app"]["build_number"], BUILD_NUMBER)
self.assertEqual(decider_event_dict["canonical_url"], CANONICAL_URL)
self.assertEqual(decider_event_dict["request_canonical_url"], CANONICAL_URL)
self.assertEqual(decider_event_dict["request"]["canonical_url"], CANONICAL_URL)

# Todo: test DeciderClient()
# @mock.patch("reddit_decider.FileWatcher")
Expand Down

0 comments on commit 4ccac86

Please sign in to comment.