Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LZ-String compression #1058

Merged
merged 5 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions posthog/api/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from datetime import datetime
from dateutil import parser
from sentry_sdk import push_scope
import lzstring # type: ignore
import re
import json
import secrets
Expand All @@ -19,9 +20,6 @@ def _load_data(request) -> Optional[Union[Dict, List]]:
if request.method == "POST":
if request.content_type == "application/json":
data = request.body

if request.headers.get("content-encoding", "").lower() == "gzip":
data = gzip.decompress(data)
else:
data = request.POST.get("data")
else:
Expand All @@ -33,6 +31,20 @@ def _load_data(request) -> Optional[Union[Dict, List]]:
with push_scope() as scope:
scope.set_context("data", data)

compression = (
request.GET.get("compression") or request.POST.get("compression") or request.headers.get("content-encoding", "")
)
compression = compression.lower()

if compression == "gzip":
data = gzip.decompress(data)

if compression == "lz64":
if isinstance(data, str):
data = lzstring.LZString().decompressFromBase64(data.replace(" ", "+"))
else:
data = lzstring.LZString().decompressFromBase64(data.decode().replace(" ", "+"))

# Is it plain json?
try:
data = json.loads(data)
Expand Down
1 change: 1 addition & 0 deletions posthog/api/decide.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def get_decide(request: HttpRequest):
"config": {"enable_collect_everything": True},
"editorParams": {},
"isAuthenticated": False,
"supportedCompression": ["gzip", "lz64"],
}

if request.user.is_authenticated:
Expand Down
62 changes: 61 additions & 1 deletion posthog/api/test/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import base64
import json
import gzip
import lzstring # type: ignore


class TestCapture(BaseTest):
Expand Down Expand Up @@ -133,7 +134,7 @@ def test_batch(self, patch_process_event):

@patch("posthog.models.team.TEAM_CACHE", {})
@patch("posthog.tasks.process_event.process_event.delay")
def test_batch_gzip(self, patch_process_event):
def test_batch_gzip_header(self, patch_process_event):
data = {
"api_key": self.team.api_token,
"batch": [{"type": "capture", "event": "user signed up", "distinct_id": "2"}],
Expand Down Expand Up @@ -161,6 +162,65 @@ def test_batch_gzip(self, patch_process_event):
},
)

@patch("posthog.models.team.TEAM_CACHE", {})
@patch("posthog.tasks.process_event.process_event.delay")
def test_batch_gzip_param(self, patch_process_event):
data = {
"api_key": self.team.api_token,
"batch": [{"type": "capture", "event": "user signed up", "distinct_id": "2"}],
}

response = self.client.generic(
"POST",
"/batch/?compression=gzip",
data=gzip.compress(json.dumps(data).encode()),
content_type="application/json",
)

arguments = patch_process_event.call_args[1]
arguments.pop("now") # can't compare fakedate
arguments.pop("sent_at") # can't compare fakedate
self.assertDictEqual(
arguments,
{
"distinct_id": "2",
"ip": "127.0.0.1",
"site_url": "http://testserver",
"data": data["batch"][0],
"team_id": self.team.pk,
},
)

@patch("posthog.models.team.TEAM_CACHE", {})
@patch("posthog.tasks.process_event.process_event.delay")
def test_batch_lzstring(self, patch_process_event):
data = {
"api_key": self.team.api_token,
"batch": [{"type": "capture", "event": "user signed up", "distinct_id": "2"}],
}

response = self.client.generic(
"POST",
"/batch/",
data=lzstring.LZString().compressToBase64(json.dumps(data)).encode(),
content_type="application/json",
HTTP_CONTENT_ENCODING="lz64",
)

arguments = patch_process_event.call_args[1]
arguments.pop("now") # can't compare fakedate
arguments.pop("sent_at") # can't compare fakedate
self.assertDictEqual(
arguments,
{
"distinct_id": "2",
"ip": "127.0.0.1",
"site_url": "http://testserver",
"data": data["batch"][0],
"team_id": self.team.pk,
},
)

def test_batch_incorrect_token(self):
response = self.client.post(
"/batch/",
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ django-loginas==0.3.8
django-redis==4.12.1
djangorestframework==3.11.0
djangorestframework-csv==2.1.0
future==0.18.2
gunicorn==20.0.4
idna==2.8
importlib-metadata==1.6.0
inflection==0.3.1
itypes==1.1.0
Jinja2==2.11.1
kombu==4.6.8
lzstring==1.0.4
MarkupSafe==1.1.1
monotonic==1.5
numpy==1.18.1
Expand Down