Skip to content

repo_statistics.rb

Example: parse an account's repo looked up by handle or a local .car file, and print record statistics.

rb
require 'didkit'
require 'minisky'
require 'oxygene'

if ARGV[0] && ARGV[0].start_with?('@')
  $did = DID.resolve_handle(ARGV[0])

  if $did.nil?
    puts "Couldn't resolve handle: #{ARGV[0]}"
    exit 1
  end

  minisky = Minisky.new($did.document.pds_host, nil)

  puts "Fetching repo..."
  repo = minisky.get_request('com.atproto.sync.getRepo', { did: $did })
elsif ARGV[0] && File.exist?(File.expand_path(ARGV[0]))
  repo = File.read(File.expand_path(ARGV[0]))
else
  puts "Usage: #{$PROGRAM_NAME} <filename.car> | @handle"
  exit 1
end

puts "Parsing repo..."
car = Oxygene::CARRepo.new(repo)

collections = {}

car.walk_all_nodes do |key, cid|
  collection = key.split('/').first
  collections[collection] ||= 0
  collections[collection] += 1
end

puts
puts "Found records:"

max_len = collections.map { |k, v| k.length }.max
max_clen = collections.map { |k, v| v.to_s.length }.max
total = collections.map { |k, v| v }.sum

puts "#{"TOTAL:".ljust(max_len + 1)} | #{total.to_s.rjust(max_clen)}"

collections.sort_by { |k, v| -v }.each do |k, v|
  puts "#{(k + ':').ljust(max_len + 1)} | #{v.to_s.rjust(max_clen)}"
end