diff --git a/bin/cluster_validator.rb b/bin/cluster_validator.rb new file mode 100644 index 00000000..655ae68c --- /dev/null +++ b/bin/cluster_validator.rb @@ -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 diff --git a/spec/cluster_validator_spec.rb b/spec/cluster_validator_spec.rb new file mode 100644 index 00000000..e41807e3 --- /dev/null +++ b/spec/cluster_validator_spec.rb @@ -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