Skip to content

Commit

Permalink
change track to be able to handle batches (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
diego-escobedo authored Sep 14, 2022
1 parent 25d550a commit a7ac103
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 28 deletions.
34 changes: 34 additions & 0 deletions metering_billing/tests/test_track_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,37 @@ def test_request_invalid_if_idempotency_id_repeated(
assert [
getattr(event, "idempotency_id") for event in customer_org_events
].count(setup_dict["idempotency_id"]) == 1

def test_batch_track_event_works(
self,
track_event_test_common_setup,
track_event_payload,
get_events_with_org_customer_id,
):
# batch_events=true
setup_dict = track_event_test_common_setup(
idempotency_already_created=False, customer_id_exists=True
)
time_created = datetime.now()

idem1 = str(uuid.uuid4())
payload1 = track_event_payload(idem1, time_created, setup_dict["customer_id"])
idem2 = str(uuid.uuid4())
payload2 = track_event_payload(idem2, time_created, setup_dict["customer_id"])
response = setup_dict["client"].post(
reverse("track_event"),
data=json.dumps([payload1, payload2], cls=DjangoJSONEncoder),
content_type="application/json",
)

assert response.status_code == status.HTTP_201_CREATED
customer_org_events = get_events_with_org_customer_id(
setup_dict["org"], setup_dict["customer_id"]
)
assert len(customer_org_events) == setup_dict["num_events_in"] + 2
assert [
getattr(event, "idempotency_id") for event in customer_org_events
].count(idem1) == 1
assert [
getattr(event, "idempotency_id") for event in customer_org_events
].count(idem2) == 1
63 changes: 35 additions & 28 deletions metering_billing/views/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ def ingest_event(data: dict, customer_pk: int, organization_pk: int) -> None:
event_kwargs["properties"] = data["properties"]
Event.objects.create(**event_kwargs)

return JsonResponse({"status": "Success"}, status=201)


@csrf_exempt
def track_event(request):
Expand All @@ -73,33 +71,42 @@ def track_event(request):
)
cache.set(prefix, organization_pk, timeout)

data = load_event(request)
if not data:
event_list = load_event(request)
if not event_list:
return HttpResponseBadRequest("No data provided")
if type(event_list) != list:
event_list = [event_list]
bad_events = {}
for data in event_list:
customer_id = data["customer_id"]
customer_cache_key = f"{organization_pk}-{customer_id}"
customer_pk = cache.get(customer_cache_key)
if customer_pk is None:
customer_pk_list = Customer.objects.filter(
organization=organization_pk, customer_id=customer_id
).values_list("id", flat=True)
if len(customer_pk_list) == 0:
bad_events[data["idempotency_id"]] = "Customer does not exist"
continue
else:
customer_pk = customer_pk_list[0]
cache.set(customer_cache_key, customer_pk, 60 * 60 * 24 * 7)

customer_id = data["customer_id"]
customer_cache_key = f"{organization_pk}-{customer_id}"
customer_pk = cache.get(customer_cache_key)
if customer_pk is None:
customer_pk_list = Customer.objects.filter(
organization=organization_pk, customer_id=customer_id
).values_list("id", flat=True)
if len(customer_pk_list) == 0:
return HttpResponseBadRequest("Customer does not exist")
else:
customer_pk = customer_pk_list[0]
cache.set(customer_cache_key, customer_pk, 60 * 60 * 24 * 7)

event_idem_ct = (
Event.objects.filter(
idempotency_id=data["idempotency_id"],
event_idem_ct = (
Event.objects.filter(
idempotency_id=data["idempotency_id"],
)
.values_list("id", flat=True)
.count()
)
.values_list("id", flat=True)
.count()
)
if event_idem_ct > 0:
return HttpResponseBadRequest("Event idempotency already exists")
if event_idem_ct > 0:
bad_events[data["idempotency_id"]] = "Event idempotency already exists"
continue

return ingest_event(
data=data, customer_pk=customer_pk, organization_pk=organization_pk
)
ingest_event(
data=data, customer_pk=customer_pk, organization_pk=organization_pk
)
if len(bad_events) > 0:
return JsonResponse(bad_events, status=400)
else:
return JsonResponse({"success": True}, status=201)

0 comments on commit a7ac103

Please sign in to comment.