-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a script to check which clusters are invalid
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |