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

fix(api) adds v4 usage to api tracking #4825

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion cl/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,10 @@ def invert_user_logs(

dates = make_date_str_list(start, end)
for d in dates:
pipe.zrange(f"api:v3.user.d:{d}.counts", 0, -1, withscores=True)
for version in ["v3", "v4"]:
pipe.zrange(
f"api:{version}.user.d:{d}.counts", 0, -1, withscores=True
)
mlissner marked this conversation as resolved.
Show resolved Hide resolved
results = pipe.execute()

# results is a list of results for each of the zrange queries above. Zip
Expand Down
48 changes: 48 additions & 0 deletions cl/tests/api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from unittest.mock import MagicMock, patch

from cl.api.utils import invert_user_logs
from cl.tests.cases import SimpleTestCase

USER_1_ID = 1
USER_2_ID = 2


class TestApiUsage(SimpleTestCase):
def setUp(self):
self.start_date = "2023-01-01"
self.end_date = "2023-01-01"
self.expected_dates = ["2023-01-01"]
self.v3_results = [(USER_1_ID, 10), (USER_2_ID, 20)]
self.v4_results = [(USER_1_ID, 15), (USER_2_ID, 25)]

@patch("cl.api.utils.get_redis_interface")
def test_invert_user_logs_shows_v4_logs(self, mock_get_redis_interface):
mock_redis = MagicMock()
mock_pipeline = MagicMock()
mock_get_redis_interface.return_value = mock_redis
mock_redis.pipeline.return_value = mock_pipeline

mock_pipeline.execute.return_value = [
self.v3_results,
self.v4_results,
]

results = invert_user_logs(
self.start_date, self.end_date, add_usernames=False
)

for date in self.expected_dates:
mock_pipeline.zrange.assert_any_call(
f"api:v3.user.d:{date}.counts", 0, -1, withscores=True
)
mock_pipeline.zrange.assert_any_call(
f"api:v4.user.d:{date}.counts", 0, -1, withscores=True
)

self.assertIn(USER_1_ID, results)
self.assertIn(USER_2_ID, results)

self.assertEqual(results[USER_1_ID]["2023-01-01"], 25)
self.assertEqual(results[USER_1_ID]["total"], 25)
self.assertEqual(results[USER_2_ID]["2023-01-01"], 45)
self.assertEqual(results[USER_2_ID]["total"], 45)
Loading