Skip to content

Commit 9e31449

Browse files
haackedsakce
authored andcommitted
chore(flags): Update stats at the end of each command (#42545)
1 parent aff93bb commit 9e31449

File tree

6 files changed

+458
-8
lines changed

6 files changed

+458
-8
lines changed

posthog/helpers/tests/test_geoip.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
def test_geoip_results(test_input, expected_country):
3131
properties = get_geoip_properties(test_input)
3232
assert properties["$geoip_country_name"] == expected_country
33-
assert len(properties) == 7
33+
# GeoIP databases may have varying levels of detail for different IPs
34+
# Minimum properties: country_code, country_name, continent_code, continent_name, time_zone
35+
# Optional properties: city_name, city_confidence (depend on database version/coverage)
36+
assert len(properties) >= 5
3437

3538

3639
class TestGeoIPDBError(TestCase):

posthog/management/commands/_base_hypercache_command.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from posthog.caching.flags_redis_cache import FLAGS_DEDICATED_CACHE_ALIAS
1414
from posthog.models.team.team import Team
15-
from posthog.storage.hypercache_manager import HyperCacheManagementConfig, warm_caches
15+
from posthog.storage.hypercache_manager import HyperCacheManagementConfig, get_cache_stats, warm_caches
1616

1717

1818
class BaseHyperCacheCommand(BaseCommand):
@@ -317,6 +317,9 @@ def process_chunk(chunk):
317317

318318
self._print_verification_results(stats, mismatches, verbose, fix)
319319
finally:
320+
# Update cache metrics after verification completes (even on failure)
321+
get_cache_stats(self.get_hypercache_config())
322+
320323
if not settings.TEST:
321324
connection.close()
322325

@@ -595,8 +598,6 @@ def run_warm(
595598
Subclasses must implement:
596599
- get_hypercache_config() -> HyperCacheManagementConfig
597600
"""
598-
from posthog.storage.hypercache_manager import get_cache_stats
599-
600601
config = self.get_hypercache_config()
601602
cache_name = config.cache_display_name
602603

@@ -669,6 +670,9 @@ def progress_callback(processed: int, total: int, successful: int, failed: int):
669670
if not team_ids and failed > 0:
670671
self.stdout.write(self.style.WARNING(f"Warning: {failed} teams failed to cache. Check logs for details."))
671672

673+
# Update cache metrics after warming completes
674+
get_cache_stats(config)
675+
672676
# Optional methods that subclasses can override for enhanced functionality
673677

674678
def format_verbose_diff(self, diff: dict):

posthog/management/commands/analyze_flags_cache_sizes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
_get_feature_flags_for_teams_batch,
1414
)
1515
from posthog.models.team import Team
16+
from posthog.storage.hypercache_manager import get_cache_stats
1617

1718

1819
class Command(BaseHyperCacheCommand):
@@ -185,3 +186,6 @@ def handle(self, *args, **options):
185186
"\nS3 storage will be similar to compressed sizes shown above."
186187
)
187188
)
189+
190+
# Update cache metrics
191+
get_cache_stats(self.get_hypercache_config())

posthog/management/commands/analyze_team_cache_sizes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from posthog.management.commands._base_hypercache_command import BaseHyperCacheCommand
1313
from posthog.models.team import Team
14+
from posthog.storage.hypercache_manager import get_cache_stats
1415
from posthog.storage.team_metadata_cache import TEAM_HYPERCACHE_MANAGEMENT_CONFIG, _load_team_metadata
1516

1617

@@ -168,3 +169,6 @@ def handle(self, *args, **options):
168169
"\nS3 storage will be similar to compressed sizes shown above."
169170
)
170171
)
172+
173+
# Update cache metrics
174+
get_cache_stats(self.get_hypercache_config())

posthog/storage/hypercache_manager.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,31 @@ def cache_display_name(self) -> str:
147147
"""Human-readable cache name for display (e.g., "flags", "team metadata")."""
148148
return self.cache_name.replace("_", " ")
149149

150+
@property
151+
def _django_key_prefix(self) -> str:
152+
"""Get Django cache key prefix (e.g., 'posthog:1:')."""
153+
# Django redis cache uses KEY_PREFIX + VERSION to build the full prefix
154+
# Default version is 1, resulting in "posthog:1:" prefix
155+
cache_client = self.hypercache.cache_client
156+
key_prefix = getattr(cache_client, "key_prefix", "")
157+
version = getattr(cache_client, "version", 1)
158+
if key_prefix:
159+
return f"{key_prefix}:{version}:"
160+
return ""
161+
150162
@property
151163
def redis_pattern(self) -> str:
152164
"""Redis key pattern for scanning all cache entries."""
153165
prefix = "team_tokens" if self.hypercache.token_based else "teams"
154-
return f"cache/{prefix}/*/{self.namespace}/*"
166+
django_prefix = self._django_key_prefix
167+
return f"{django_prefix}cache/{prefix}/*/{self.namespace}/*"
155168

156169
@property
157170
def redis_stats_pattern(self) -> str:
158171
"""Specific Redis pattern for stats (includes value file)."""
159172
prefix = "team_tokens" if self.hypercache.token_based else "teams"
160-
return f"cache/{prefix}/*/{self.namespace}/{self.hypercache.value}"
173+
django_prefix = self._django_key_prefix
174+
return f"{django_prefix}cache/{prefix}/*/{self.namespace}/{self.hypercache.value}"
161175

162176
@property
163177
def expiry_sorted_set_key(self) -> str:
@@ -211,7 +225,7 @@ def invalidate_all_caches(config: HyperCacheManagementConfig) -> int:
211225
Number of cache keys deleted
212226
"""
213227
try:
214-
redis_client = get_client()
228+
redis_client = get_client(config.hypercache.redis_url)
215229

216230
deleted = 0
217231
for key in redis_client.scan_iter(match=config.redis_pattern, count=1000):
@@ -417,7 +431,7 @@ def get_cache_stats(config: HyperCacheManagementConfig) -> dict[str, Any]:
417431
Dictionary with cache statistics including size information
418432
"""
419433
try:
420-
redis_client = get_client()
434+
redis_client = get_client(config.hypercache.redis_url)
421435

422436
total_keys = 0
423437
ttl_buckets = {

0 commit comments

Comments
 (0)