Skip to content

Commit

Permalink
add ability to set scan_count and added a little refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
prog-supdex committed Sep 15, 2023
1 parent 3c90d19 commit b730e61
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/graphql-anycable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 19 additions & 12 deletions lib/graphql/anycable/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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

Expand All @@ -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),
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions spec/graphql/stats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
{
Expand Down

0 comments on commit b730e61

Please sign in to comment.