Skip to content

fix metric access cache leaks - #5484

Open
IvanIvanoff wants to merge 7 commits into
masterfrom
fix-metric-access-cache-leaks
Open

IvanIvanoff wants to merge 7 commits into
masterfrom
fix-metric-access-cache-leaks

Conversation

@IvanIvanoff

@IvanIvanoff IvanIvanoff commented Dec 6, 2026 •

Copy link
Copy Markdown
Member
  • Fix metricAccessLevel returning the feature access level
  • Cache price pair available slugs per quote asset
  • Key subscription-dependent GraphQL caches correctly
  • Stop alpha metrics leaking through caches and selectors
  • Check sentiment metric status for every social source
  • Restore cache sharing for trending queries and regular users
  • Make the entitlement and whole-query cache keys complete

Changes

Ticket

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have tried to find clearer solution before commenting hard-to-understand parts of code
  • I have added tests that prove my fix is effective or that my feature works

Summary by CodeRabbit

  • Bug Fixes
    • Improved cache separation for subscription plans, bundle entitlements, grants, metric access levels, and available-metrics lookback settings, helping prevent results from being reused across differing access contexts.
    • Corrected metric visibility filtering for address-based queries, including released-only behavior for unrecognized access levels.
    • Fixed price-pair metric results so each quote asset receives its corresponding available slugs.
    • Updated metric access level reporting to use the dedicated metric access setting.
  • Tests
    • Added coverage for cache isolation, metric visibility updates, entitlement-specific access, and sentiment metrics across supported sources.

All price pair metrics shared one cache entry, and the TTL was passed as part of the key.
The key used context[:product_id], which is never set. It now holds the plan, the requested and subscription products, and compact bundle and grant keys (packages and snapshot version, never metric lists). Raw signals and signal metadata now use it too.
Add the user's metric access level and lookback to the whole-query and availableProjects cache keys, filter address selector metrics by access level, fail closed on unknown levels and expire the alpha/beta sets on registry refresh.
Trending words and stories depend only on the plan, so they key on it alone. Released users without a lookback override share entries with anonymous users.
Add the data windows to the entitlement key, and the plan, status and compact entitlement and grant keys to the whole-query key, as updated_at has second precision.
@IvanIvanoff
IvanIvanoff requested a review from tspenov as a code owner December 6, 2026 21:23
@coderabbitai

coderabbitai Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The PR changes GraphQL cache keys to include selected subscription and user details. It updates metric filtering and status-cache refresh behavior, adds quote assets to price-pair cache keys, and expands social metric source test coverage.

Changes: Cache keys and metric visibility

Layer / File(s) Summary
Entitlement and grant cache keys
lib/sanbase/billing/plan/bundle/entitlement.ex, lib/sanbase/billing/subscription/grant.ex, test/sanbase/billing/plan/bundle/bundle_entitlement_test.exs, test/sanbase/billing/subscription/grant_test.exs
Entitlements and grants expose cache keys based on selected package and version fields. Tests cover nil inputs and changes to included values.
GraphQL cache context
lib/sanbase_web/graphql/cache/cache.ex, lib/sanbase_web/graphql/document/document_provider.ex, lib/sanbase_web/graphql/resolvers/access_control_resolver.ex, test/sanbase_web/graphql/cache/cache_key_test.exs
Cache keys include selected subscription, entitlement, grant, metric access, and lookback values. Tests cover subscription, plan, and user-detail key behavior.
Metric access and status-cache refresh
lib/sanbase/metric/metric.ex, lib/sanbase/metric/registry/registry.ex, lib/sanbase_web/graphql/resolvers/user/user_resolver.ex, test/sanbase_web/graphql/metric/api_metric_available_metrics_test.exs, test/sanbase_web/graphql/user/current_user_api_test.exs
Metric selectors filter results by access level, and registry refreshes expire cached status sets. Tests cover access levels, refreshes, and separate current-user metric and feature access fields.
Resolver cache inputs
lib/sanbase_web/graphql/schema/queries/*, lib/sanbase_web/graphql/schema/types/*, test/sanbase_web/graphql/billing/bundle_api_access_test.exs, test/sanbase_web/graphql/metric/api_metric_available_metrics_test.exs
Resolver caches include the subscription, plan, or user details used by their results. Tests cover bundle access and query results that depend on metric access or lookback.

Price-pair slug cache

Layer / File(s) Summary
Quote-asset cache key
lib/sanbase/prices/price_pair/metric_adapter.ex, test/sanbase/price_pair/metric_adapter_test.exs
The available-slug cache key includes the quote asset. Tests verify results for four quote assets.

Social metric source coverage

Layer / File(s) Summary
Sentiment metric source tests
test/sanbase/social_data/metric_adapter_test.exs
The sentiment metric test source list uses SocialHelper.sources() and checks availability for each generated metric.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix

Possibly related PRs

  • santiment/sanbase2#5407: Changes the cache key for trending words and stories to use the plan alone, matching the cache-key change in this PR.
Merge Risk: Minimal · up to f2db3

The change narrows cache keys and access-level filtering so alpha or beta metrics and subscription-specific results are not shared across users with different entitlements. No concrete merge-blocking defect remains after review. The tests were not executed in this review, and the PR checklist leaves its test items unchecked, so a normal CI run is advisable before merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix metric access cache leaks" names the main change. The PR fixes cache keys so metric access levels, subscriptions, grants, and entitlements no longer leak between users. The changeset ad…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix metric access cache leaks" names the main change. The PR fixes cache keys so metric access levels, subscriptions, grants, and entitlements no longer leak between users. The changeset adds cache_key helpers, widens GraphQL cache keys with user access level and lookback days, filters alpha/beta metrics by access level, and expires alpha/beta metric caches on registry refresh. The title is concise and specific, and it does not list files.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0532a9bb-04ce-4b8b-90f0-bbf0b3a1ab8b
Commits

Reviewing files that changed from the base of the PR and between 90ffeee and f2db3bd.

Files selected for processing: 21

  • lib/sanbase/billing/plan/bundle/entitlement.ex
  • lib/sanbase/billing/subscription/grant.ex
  • lib/sanbase/metric/metric.ex
  • lib/sanbase/metric/registry/registry.ex
  • lib/sanbase/prices/price_pair/metric_adapter.ex
  • lib/sanbase_web/graphql/cache/cache.ex
  • lib/sanbase_web/graphql/document/document_provider.ex
  • lib/sanbase_web/graphql/resolvers/access_control_resolver.ex
  • lib/sanbase_web/graphql/resolvers/user/user_resolver.ex
  • lib/sanbase_web/graphql/schema/queries/signal_queries.ex
  • lib/sanbase_web/graphql/schema/queries/social_data_queries.ex
  • lib/sanbase_web/graphql/schema/types/metric_types.ex
  • lib/sanbase_web/graphql/schema/types/signal_types.ex
  • test/sanbase/billing/plan/bundle/bundle_entitlement_test.exs
  • test/sanbase/billing/subscription/grant_test.exs
  • test/sanbase/price_pair/metric_adapter_test.exs
  • test/sanbase/social_data/metric_adapter_test.exs
  • test/sanbase_web/graphql/billing/bundle_api_access_test.exs
  • test/sanbase_web/graphql/cache/cache_key_test.exs
  • test/sanbase_web/graphql/metric/api_metric_available_metrics_test.exs
  • test/sanbase_web/graphql/user/current_user_api_test.exs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant