|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module GraphQL |
| 4 | + module AnyCable |
| 5 | + # Calculates amount of Graphql Redis keys |
| 6 | + # (graphql-subscription, graphql-fingerprints, graphql-subscriptions, graphql-channel) |
| 7 | + # Also, calculate the number of subscribers grouped by subscriptions |
| 8 | + class Stats |
| 9 | + SCAN_COUNT_RECORDS_AMOUNT = 1_000 |
| 10 | + |
| 11 | + attr_reader :scan_count, :include_subscriptions |
| 12 | + |
| 13 | + def initialize(scan_count: SCAN_COUNT_RECORDS_AMOUNT, include_subscriptions: false) |
| 14 | + @scan_count = scan_count |
| 15 | + @include_subscriptions = include_subscriptions |
| 16 | + end |
| 17 | + |
| 18 | + def collect |
| 19 | + total_subscriptions_result = {total: {}} |
| 20 | + |
| 21 | + list_prefixes_keys.each do |name, prefix| |
| 22 | + total_subscriptions_result[:total][name] = count_by_scan(match: "#{prefix}*") |
| 23 | + end |
| 24 | + |
| 25 | + if include_subscriptions |
| 26 | + total_subscriptions_result[:subscriptions] = group_subscription_stats |
| 27 | + end |
| 28 | + |
| 29 | + total_subscriptions_result |
| 30 | + end |
| 31 | + |
| 32 | + private |
| 33 | + |
| 34 | + # Counting all keys, that match the pattern with iterating by count |
| 35 | + def count_by_scan(match:) |
| 36 | + sb_amount = 0 |
| 37 | + cursor = '0' |
| 38 | + |
| 39 | + loop do |
| 40 | + cursor, result = redis.scan(cursor, match: match, count: scan_count) |
| 41 | + sb_amount += result.count |
| 42 | + |
| 43 | + break if cursor == '0' |
| 44 | + end |
| 45 | + |
| 46 | + sb_amount |
| 47 | + end |
| 48 | + |
| 49 | + # Calculate subscribes, grouped by subscriptions |
| 50 | + def group_subscription_stats |
| 51 | + subscription_groups = {} |
| 52 | + |
| 53 | + redis.scan_each(match: "#{list_prefixes_keys[:fingerprints]}*", count: scan_count) do |fingerprint_key| |
| 54 | + subscription_name = fingerprint_key.gsub(/#{list_prefixes_keys[:fingerprints]}|:/, "") |
| 55 | + subscription_groups[subscription_name] = 0 |
| 56 | + |
| 57 | + redis.zscan_each(fingerprint_key) do |data| |
| 58 | + redis.sscan_each("#{list_prefixes_keys[:subscriptions]}#{data[0]}") do |subscription_key| |
| 59 | + next unless redis.exists?("#{list_prefixes_keys[:subscription]}#{subscription_key}") |
| 60 | + |
| 61 | + subscription_groups[subscription_name] += 1 |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + subscription_groups |
| 67 | + end |
| 68 | + |
| 69 | + def list_prefixes_keys |
| 70 | + { |
| 71 | + subscription: redis_key(adapter::SUBSCRIPTION_PREFIX), |
| 72 | + fingerprints: redis_key(adapter::FINGERPRINTS_PREFIX), |
| 73 | + subscriptions: redis_key(adapter::SUBSCRIPTIONS_PREFIX), |
| 74 | + channel: redis_key(adapter::CHANNEL_PREFIX) |
| 75 | + } |
| 76 | + end |
| 77 | + |
| 78 | + def adapter |
| 79 | + GraphQL::Subscriptions::AnyCableSubscriptions |
| 80 | + end |
| 81 | + |
| 82 | + def redis |
| 83 | + GraphQL::AnyCable.redis |
| 84 | + end |
| 85 | + |
| 86 | + def config |
| 87 | + GraphQL::AnyCable.config |
| 88 | + end |
| 89 | + |
| 90 | + def redis_key(prefix) |
| 91 | + "#{config.redis_prefix}-#{prefix}" |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments