diff --git a/README.md b/README.md index 93390e0..cc21aa8 100644 --- a/README.md +++ b/README.md @@ -256,7 +256,14 @@ It will return the response that contains `subscriptions` } ``` -We can set this data to [Yabeda] for tracking amount of subscriptions +Also, you can set another `scan_count`, if needed. +The default value is 1_000 + +```ruby + GraphQL::AnyCable.stats(scan_count: 100) +``` + +We can set statistics data to [Yabeda][] for tracking amount of subscriptions ```ruby # config/initializers/metrics.rb diff --git a/lib/graphql-anycable.rb b/lib/graphql-anycable.rb index fc03e9a..97d18f1 100644 --- a/lib/graphql-anycable.rb +++ b/lib/graphql-anycable.rb @@ -21,8 +21,8 @@ def self.use(schema, **options) schema.use GraphQL::Subscriptions::AnyCableSubscriptions, **options end - def self.stats(include_subscriptions: false) - AnyCable::Stats.new(redis: redis, config: config, include_subscriptions: include_subscriptions).collect + def self.stats(**options) + Stats.new(**options).collect end module_function diff --git a/lib/graphql/anycable/stats.rb b/lib/graphql/anycable/stats.rb index 39930c2..94881bb 100644 --- a/lib/graphql/anycable/stats.rb +++ b/lib/graphql/anycable/stats.rb @@ -8,13 +8,11 @@ module AnyCable class Stats SCAN_COUNT_RECORDS_AMOUNT = 1_000 - attr_reader :redis, :config, :list_prefixes_keys, :include_subscriptions + attr_reader :scan_count, :include_subscriptions - def initialize(redis:, config:, include_subscriptions: false) - @redis = redis - @config = config + def initialize(scan_count: SCAN_COUNT_RECORDS_AMOUNT, include_subscriptions: false) + @scan_count = scan_count @include_subscriptions = include_subscriptions - @list_prefix_keys = list_prefixes_keys end def collect @@ -34,12 +32,12 @@ def collect private # Counting all keys, that match the pattern with iterating by count - def count_by_scan(match:, count: SCAN_COUNT_RECORDS_AMOUNT) + def count_by_scan(match:) sb_amount = 0 cursor = '0' loop do - cursor, result = redis.scan(cursor, match: match, count: count) + cursor, result = redis.scan(cursor, match: match, count: scan_count) sb_amount += result.count break if cursor == '0' @@ -51,7 +49,8 @@ def count_by_scan(match:, count: SCAN_COUNT_RECORDS_AMOUNT) # Calculate subscribes, grouped by subscriptions def group_subscription_stats subscription_groups = {} - redis.scan_each(match: "#{list_prefixes_keys[:fingerprints]}*", count: SCAN_COUNT_RECORDS_AMOUNT) do |fingerprint_key| + + redis.scan_each(match: "#{list_prefixes_keys[:fingerprints]}*", count: scan_count) do |fingerprint_key| subscription_name = fingerprint_key.gsub(/#{list_prefixes_keys[:fingerprints]}|:/, "") subscription_groups[subscription_name] = 0 @@ -67,10 +66,6 @@ def group_subscription_stats subscription_groups end - def adapter - GraphQL::Subscriptions::AnyCableSubscriptions - end - def list_prefixes_keys { subscription: redis_key(adapter::SUBSCRIPTION_PREFIX), @@ -80,6 +75,18 @@ def list_prefixes_keys } end + def adapter + GraphQL::Subscriptions::AnyCableSubscriptions + end + + def redis + GraphQL::AnyCable.redis + end + + def config + GraphQL::AnyCable.config + end + def redis_key(prefix) "#{config.redis_prefix}-#{prefix}" end diff --git a/spec/graphql/stats_spec.rb b/spec/graphql/stats_spec.rb index 6e9eee6..f48df1f 100644 --- a/spec/graphql/stats_spec.rb +++ b/spec/graphql/stats_spec.rb @@ -30,12 +30,7 @@ ) end - let(:redis) { AnycableSchema.subscriptions.redis } - let(:config) { GraphQL::AnyCable.config } - context "when include_subscriptions is false" do - subject { described_class.new(redis: redis, config: config) } - let(:expected_result) do {total: {subscription: 1, fingerprints: 2, subscriptions: 2, channel: 1}} end @@ -46,7 +41,7 @@ end context "when include_subscriptions is true" do - subject { described_class.new(redis: redis, config: config, include_subscriptions: true) } + subject { described_class.new(include_subscriptions: true) } let(:expected_result) do {