-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from prog-supdex/feat/add-stats
feat: add ability to fetch subscription stats
- Loading branch information
Showing
5 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# frozen_string_literal: true | ||
|
||
module GraphQL | ||
module AnyCable | ||
# Calculates amount of Graphql Redis keys | ||
# (graphql-subscription, graphql-fingerprints, graphql-subscriptions, graphql-channel) | ||
# Also, calculate the number of subscribers grouped by subscriptions | ||
class Stats | ||
SCAN_COUNT_RECORDS_AMOUNT = 1_000 | ||
|
||
attr_reader :scan_count, :include_subscriptions | ||
|
||
def initialize(scan_count: SCAN_COUNT_RECORDS_AMOUNT, include_subscriptions: false) | ||
@scan_count = scan_count | ||
@include_subscriptions = include_subscriptions | ||
end | ||
|
||
def collect | ||
total_subscriptions_result = {total: {}} | ||
|
||
list_prefixes_keys.each do |name, prefix| | ||
total_subscriptions_result[:total][name] = count_by_scan(match: "#{prefix}*") | ||
end | ||
|
||
if include_subscriptions | ||
total_subscriptions_result[:subscriptions] = group_subscription_stats | ||
end | ||
|
||
total_subscriptions_result | ||
end | ||
|
||
private | ||
|
||
# Counting all keys, that match the pattern with iterating by count | ||
def count_by_scan(match:) | ||
sb_amount = 0 | ||
cursor = '0' | ||
|
||
loop do | ||
cursor, result = redis.scan(cursor, match: match, count: scan_count) | ||
sb_amount += result.count | ||
|
||
break if cursor == '0' | ||
end | ||
|
||
sb_amount | ||
end | ||
|
||
# Calculate subscribes, grouped by subscriptions | ||
def group_subscription_stats | ||
subscription_groups = {} | ||
|
||
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 | ||
|
||
redis.zscan_each(fingerprint_key) do |data| | ||
redis.sscan_each("#{list_prefixes_keys[:subscriptions]}#{data[0]}") do |subscription_key| | ||
next unless redis.exists?("#{list_prefixes_keys[:subscription]}#{subscription_key}") | ||
|
||
subscription_groups[subscription_name] += 1 | ||
end | ||
end | ||
end | ||
|
||
subscription_groups | ||
end | ||
|
||
def list_prefixes_keys | ||
{ | ||
subscription: redis_key(adapter::SUBSCRIPTION_PREFIX), | ||
fingerprints: redis_key(adapter::FINGERPRINTS_PREFIX), | ||
subscriptions: redis_key(adapter::SUBSCRIPTIONS_PREFIX), | ||
channel: redis_key(adapter::CHANNEL_PREFIX) | ||
} | ||
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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe GraphQL::AnyCable::Stats do | ||
describe "#collect" do | ||
let(:query) do | ||
<<~GRAPHQL | ||
subscription SomeSubscription { | ||
productCreated { id title } | ||
productUpdated { id } | ||
} | ||
GRAPHQL | ||
end | ||
|
||
let(:channel) do | ||
socket = double("Socket", istate: AnyCable::Socket::State.new({})) | ||
connection = double("Connection", anycable_socket: socket) | ||
double("Channel", id: "legacy_id", params: { "channelId" => "legacy_id" }, stream_from: nil, connection: connection) | ||
end | ||
|
||
let(:subscription_id) do | ||
"some-truly-random-number" | ||
end | ||
|
||
before do | ||
AnycableSchema.execute( | ||
query: query, | ||
context: { channel: channel, subscription_id: subscription_id }, | ||
variables: {}, | ||
operation_name: "SomeSubscription", | ||
) | ||
end | ||
|
||
context "when include_subscriptions is false" do | ||
let(:expected_result) do | ||
{total: {subscription: 1, fingerprints: 2, subscriptions: 2, channel: 1}} | ||
end | ||
|
||
it "returns total stat" do | ||
expect(subject.collect).to eq(expected_result) | ||
end | ||
end | ||
|
||
context "when include_subscriptions is true" do | ||
subject { described_class.new(include_subscriptions: true) } | ||
|
||
let(:expected_result) do | ||
{ | ||
total: {subscription: 1, fingerprints: 2, subscriptions: 2, channel: 1}, | ||
subscriptions: { | ||
"productCreated"=> 1, | ||
"productUpdated"=> 1 | ||
} | ||
} | ||
end | ||
|
||
it "returns total stat with grouped subscription stats" do | ||
expect(subject.collect).to eq(expected_result) | ||
end | ||
end | ||
end | ||
end |