Skip to content

Commit

Permalink
a script to check which clusters are invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarin committed Oct 10, 2023
1 parent 23fb6f9 commit abb11fe
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
57 changes: 57 additions & 0 deletions bin/cluster_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require "cluster"
require "services"
Services.mongo!

# Goes through all clusters, checks if they are valid,
# and prints the first ocn of any invalid cluster to a file.

class ClusterValidator
attr_reader :output_path # file name
def initialize(buffer_max: 500)
@buffer = []
@buffer_max = buffer_max
# Make an output file in the right place
ymd = Time.now.strftime("%Y-%m-%d")
dir = Settings.local_report_path
FileUtils.mkdir_p(dir)
@output_path = "#{dir}/cluster_validator_#{ymd}.txt"
end

def run
puts "Logging to #{output_path}"
@outf = File.open(output_path, "w")
# Go through each cluster and check if valid.
comment "These are ocns of invalid clusters:"
Cluster.each do |c|
unless c.valid?
@buffer << c.ocns.first
end
empty_buffer?
end
ensure
# Empty bufferfer one last time and be done.
empty_buffer?
comment "Done"
@outf.close
end

private

# Make a comment in the outfile
def comment(msg)
@outf.puts "# " + msg
end

# So we don't write to file every iteration.
def empty_buffer?
if @buffer.size > @buffer_max
puts "empty buffer..."
@outf.puts(@buffer.join("\n"))
@buffer = []
end
end
end

if __FILE__ == $0 then
ClusterValidator.new.run
end
27 changes: 27 additions & 0 deletions spec/cluster_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require "spec_helper"
require_relative "../bin/cluster_validator"

RSpec.describe ClusterValidator do
let(:cluster_validator) { described_class.new }

before(:each) do
Cluster.collection.find.delete_many
end

it "makes an outfile when it runs" do
expect(File.exists?(cluster_validator.output_path)).to be false
cluster_validator.run
expect(File.exists?(cluster_validator.output_path)).to be true
end

it "makes an empty-ish outfile if there are no clusters" do
# empty-ish meaning it'll only have the header and footer, which begin with "#".
cluster_validator.run
lines = File.read(cluster_validator.output_path).split("\n")
expect(lines.count).to eq 2
expect(lines[0]).to start_with("#")
expect(lines[1]).to start_with("#")
end
end

0 comments on commit abb11fe

Please sign in to comment.