-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(batch-exports): Support high frequency exports (#17844)
- Loading branch information
1 parent
e3bcb76
commit 09ee6a1
Showing
9 changed files
with
160 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import datetime as dt | ||
import json | ||
from unittest import mock | ||
|
||
import pytest | ||
from asgiref.sync import async_to_sync | ||
|
@@ -12,6 +12,7 @@ | |
from posthog.api.test.test_organization import create_organization | ||
from posthog.api.test.test_team import create_team | ||
from posthog.api.test.test_user import create_user | ||
from posthog.batch_exports.models import BatchExport | ||
from posthog.temporal.client import sync_connect | ||
from posthog.temporal.codec import EncryptionCodec | ||
|
||
|
@@ -20,7 +21,7 @@ | |
] | ||
|
||
|
||
@pytest.mark.parametrize("interval", ["hour", "day"]) | ||
@pytest.mark.parametrize("interval", ["hour", "day", "every 5 minutes"]) | ||
def test_create_batch_export_with_interval_schedule(client: HttpClient, interval): | ||
"""Test creating a BatchExport. | ||
|
@@ -54,11 +55,17 @@ def test_create_batch_export_with_interval_schedule(client: HttpClient, interval | |
client.force_login(user) | ||
|
||
with start_test_worker(temporal): | ||
response = create_batch_export( | ||
client, | ||
team.pk, | ||
batch_export_data, | ||
) | ||
with mock.patch( | ||
"posthog.batch_exports.http.posthoganalytics.feature_enabled", return_value=True | ||
) as feature_enabled: | ||
response = create_batch_export( | ||
client, | ||
team.pk, | ||
batch_export_data, | ||
) | ||
|
||
if interval == "every 5 minutes": | ||
feature_enabled.assert_called_once_with("high-frequency-batch-exports", str(team.uuid)) | ||
|
||
assert response.status_code == status.HTTP_201_CREATED, response.json() | ||
|
||
|
@@ -80,11 +87,8 @@ def test_create_batch_export_with_interval_schedule(client: HttpClient, interval | |
codec = EncryptionCodec(settings=settings) | ||
schedule = describe_schedule(temporal, data["id"]) | ||
|
||
if interval == "hour": | ||
expected_interval = dt.timedelta(hours=1) | ||
else: | ||
expected_interval = dt.timedelta(days=1) | ||
assert schedule.schedule.spec.intervals[0].every == expected_interval | ||
batch_export = BatchExport.objects.get(id=data["id"]) | ||
assert schedule.schedule.spec.intervals[0].every == batch_export.interval_time_delta | ||
|
||
decoded_payload = async_to_sync(codec.decode)(schedule.schedule.action.args) | ||
args = json.loads(decoded_payload[0].data) | ||
|
@@ -138,3 +142,41 @@ def test_cannot_create_a_batch_export_for_another_organization(client: HttpClien | |
) | ||
|
||
assert response.status_code == status.HTTP_403_FORBIDDEN, response.json() | ||
|
||
|
||
def test_cannot_create_a_batch_export_with_higher_frequencies_if_not_enabled(client: HttpClient): | ||
temporal = sync_connect() | ||
|
||
destination_data = { | ||
"type": "S3", | ||
"config": { | ||
"bucket_name": "my-production-s3-bucket", | ||
"region": "us-east-1", | ||
"prefix": "posthog-events/", | ||
"aws_access_key_id": "abc123", | ||
"aws_secret_access_key": "secret", | ||
}, | ||
} | ||
|
||
batch_export_data = { | ||
"name": "my-production-s3-bucket-destination", | ||
"destination": destination_data, | ||
"interval": "every 5 minutes", | ||
} | ||
|
||
organization = create_organization("Test Org") | ||
team = create_team(organization) | ||
user = create_user("[email protected]", "Test User", organization) | ||
|
||
with start_test_worker(temporal): | ||
client.force_login(user) | ||
with mock.patch( | ||
"posthog.batch_exports.http.posthoganalytics.feature_enabled", return_value=False | ||
) as feature_enabled: | ||
response = create_batch_export( | ||
client, | ||
team.pk, | ||
batch_export_data, | ||
) | ||
assert response.status_code == status.HTTP_403_FORBIDDEN, response.json() | ||
feature_enabled.assert_called_once_with("high-frequency-batch-exports", str(team.uuid)) |
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
23 changes: 23 additions & 0 deletions
23
posthog/migrations/0353_add_5_minute_interval_to_batch_exports.py
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,23 @@ | ||
# Generated by Django 3.2.19 on 2023-10-06 11:07 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("posthog", "0352_auto_20230926_1833"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="batchexport", | ||
name="interval", | ||
field=models.CharField( | ||
choices=[("hour", "hour"), ("day", "day"), ("week", "week"), ("every 5 minutes", "every 5 minutes")], | ||
default="hour", | ||
help_text="The interval at which to export data.", | ||
max_length=64, | ||
), | ||
), | ||
] |
Oops, something went wrong.