Skip to content

Instantly share code, notes, and snippets.

@ArmandoAssuncao
Last active May 4, 2020 23:23
Show Gist options
  • Save ArmandoAssuncao/3c6a2da233df8208873790eeace55679 to your computer and use it in GitHub Desktop.
Save ArmandoAssuncao/3c6a2da233df8208873790eeace55679 to your computer and use it in GitHub Desktop.
Calculates time between updated_at and created_at from all databases
Mongo::Logger.logger.level = Logger::FATAL
mongo_client = Mongo::Client.new(['127.0.0.1'])
ignore_dbs = [
'admin',
'config',
].freeze
dbs_data = mongo_client.list_mongo_databases.reject { |db| ignore_dbs.include?(db.name) }.map do |mongo_database|
puts mongo_database.name
collections = mongo_database.collections.map do |collection|
puts "- #{collection.name}"
query_filter = {
# some filter
}
limit = 100
query = collection.find(query_filter).sort({ created_at: -1 }).limit(limit)
time_to_process_array = query.map do |document|
updated_at = document['updated_at']
created_at = document['created_at']
next 0 unless [updated_at, created_at].all? { |time| time.instance_of?(Time) }
updated_at - created_at
end
empty = time_to_process_array.size == 0
{
name: collection.name,
total_document: collection.count_documents({}),
time_to_process_min: empty ? 0 : time_to_process_array.min,
time_to_process_max: empty ? 0 : time_to_process_array.max,
time_to_process_average: empty ? 0 : time_to_process_array.sum / time_to_process_array.size.to_f,
}
end
{
name: mongo_database.name,
collections: collections,
}
end
mongo_client.close
puts JSON.pretty_generate(dbs_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment