askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_analytics_bounds.py · 2.8 KB · 63 lines
| 1 | """Analytics that measures visitors, and a log that stays bounded. |
| 2 | |
| 3 | Both of these were found the same way: a 961MB demo box went to 675MB |
| 4 | resident and started swapping, and the "Top paths (visitors)" report |
| 5 | turned out to be 45% one-hit rows made of vulnerability scanners and the |
| 6 | operator's own deploy calls. Neither failure was a bug in the code that |
| 7 | was written -- both were things nobody had told the system. |
| 8 | """ |
| 9 | |
| 10 | import object_analytics |
| 11 | |
| 12 | |
| 13 | # --- whose traffic is this --------------------------------------------------- |
| 14 | |
| 15 | def test_the_operators_own_automation_is_not_a_visitor(): |
| 16 | """A deploy call and a package install are the observer, not the |
| 17 | observed; counting them made the top-paths report describe us.""" |
| 18 | record = object_analytics.build_page_view( |
| 19 | path="/packages/app-shop/install", method="POST", status=200, |
| 20 | ip="203.0.113.9", headers={}, owners=frozenset(), is_operator=True) |
| 21 | assert record["is_owner"] == "true" |
| 22 | |
| 23 | |
| 24 | def test_an_owner_ip_still_counts_as_the_owner(): |
| 25 | record = object_analytics.build_page_view( |
| 26 | path="/shop", method="GET", status=200, |
| 27 | ip="203.0.113.9", headers={}, owners=frozenset({"203.0.113.9"})) |
| 28 | assert record["is_owner"] == "true" |
| 29 | |
| 30 | |
| 31 | def test_an_ordinary_visitor_is_a_visitor(): |
| 32 | record = object_analytics.build_page_view( |
| 33 | path="/shop", method="GET", status=200, |
| 34 | ip="198.51.100.4", headers={}, owners=frozenset()) |
| 35 | assert record["is_owner"] == "false" |
| 36 | |
| 37 | |
| 38 | def test_a_signed_in_member_is_still_real_traffic(): |
| 39 | """The line is between somebody USING the site and somebody OPERATING |
| 40 | it. Which pages members actually use is among the most useful things |
| 41 | this collection knows, so a member is never flagged away.""" |
| 42 | record = object_analytics.build_page_view( |
| 43 | path="/invoices", method="GET", status=200, ip="198.51.100.4", |
| 44 | headers={"cookie": "dbbasic_session=abc"}, owners=frozenset(), |
| 45 | user_id="dana") |
| 46 | assert record["is_owner"] == "false" |
| 47 | assert record["user_id"] == "dana" |
| 48 | |
| 49 | |
| 50 | # --- how much of it we keep ---------------------------------------------------- |
| 51 | |
| 52 | def test_the_row_cap_reads_from_the_environment(): |
| 53 | assert object_analytics.max_rows({"DBBASIC_ANALYTICS_MAX_ROWS": "5000"}) == 5000 |
| 54 | assert object_analytics.max_rows({}) == object_analytics.DEFAULT_MAX_ROWS |
| 55 | # 0 is a real answer meaning "no cap", not a missing setting. |
| 56 | assert object_analytics.max_rows({"DBBASIC_ANALYTICS_MAX_ROWS": "0"}) == 0 |
| 57 | assert object_analytics.max_rows( |
| 58 | {"DBBASIC_ANALYTICS_MAX_ROWS": "not a number"}) == object_analytics.DEFAULT_MAX_ROWS |
| 59 | |
| 60 | |
| 61 | def test_retention_days_still_defaults_sanely(): |
| 62 | assert object_analytics.retention_days({"DBBASIC_ANALYTICS_RETENTION_DAYS": "7"}) == 7 |
| 63 | assert object_analytics.retention_days({}) == object_analytics.DEFAULT_RETENTION_DAYS |