-
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.
started work on report for commitment by phase & org
- Loading branch information
Showing
3 changed files
with
133 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,50 @@ | ||
require "shared_print/finder" | ||
require "shared_print/phases" | ||
require "utils/report_output" | ||
|
||
# Output a .tsv report of which orgs have how many commitments in a phase. | ||
# Defaults to latest phase. | ||
|
||
module Reports | ||
class SharedPrintPhaseCount | ||
attr_reader :phase, :finder, :output | ||
def initialize(phase: SharedPrint::Phases.list.max) | ||
@phase = phase.to_i | ||
@finder = SharedPrint::Finder.new(phase: [@phase]) | ||
@output = Utils::ReportOutput.new("sp_phase#{@phase}_count") | ||
end | ||
|
||
def run | ||
handle = output.handle("w") | ||
puts "Starting writing to #{output.file}" | ||
handle.puts(header) | ||
organization_tally = {} | ||
# Get relevant commitments, tally organizations. | ||
commitments do |commitment| | ||
organization_tally[commitment.organization] ||= 0 | ||
organization_tally[commitment.organization] += 1 | ||
end | ||
# Output tally. | ||
organization_tally.keys.sort.each do |org| | ||
handle.puts [org, phase, organization_tally[org]].join("\t") | ||
end | ||
|
||
puts "Finished writing to #{output.file}" | ||
handle.close | ||
end | ||
|
||
def commitments | ||
return enum_for(:commitments) unless block_given? | ||
|
||
finder.commitments.each do |commitment| | ||
yield commitment | ||
end | ||
end | ||
|
||
def header | ||
["organization", "phase", "commitment_count"].join("\t") | ||
end | ||
end | ||
end | ||
|
||
Reports::SharedPrintPhaseCount.new(phase: 3).run |
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,59 @@ | ||
require "securerandom" | ||
|
||
# An attempt at simplifying the structure of report output dirs and files. | ||
# Give report_name as input. | ||
# The methods and their return: | ||
# id: the "unique-ish" part of the filename | ||
# dir: path to an output dir | ||
# file: path to an output file | ||
# handle("r"): an open read handle for file | ||
# handle("w"): an open write handle for file | ||
# Both dir and file will contain the report name, | ||
# and the file a timestamp and a unique-ish string. | ||
# dir is autovivified with mkdir_p. | ||
# Example: | ||
# output = Utils::ReportOutput.new("foo", ".txt") | ||
# output.dir # -> "/local_reports/foo" | ||
# output.file # -> "/local_reports/foo/foo_YYYYMMDD_abcd1234.txt" | ||
|
||
module Utils | ||
class ReportOutput | ||
attr_reader :report_name, :ext | ||
def initialize(report_name, ext = ".tsv") | ||
@report_name = report_name # e.g. "cost_report" or "cost_report_umich" | ||
@ext = ext | ||
end | ||
|
||
def file | ||
if @file.nil? | ||
@file = File.join(dir, id) + ext | ||
end | ||
@file | ||
end | ||
|
||
def dir | ||
if @dir.nil? | ||
@dir = FileUtils.mkdir_p( | ||
File.join( | ||
Settings.local_report_path || "/tmp", | ||
report_name | ||
) | ||
).first # mkdir_p returns an array | ||
end | ||
@dir | ||
end | ||
|
||
def id | ||
timestamp = Time.now.strftime("%Y%m%d_%H%M%S") | ||
# Adding a random string in case 2 identical jobs that write a file | ||
# are started in the same second. | ||
rand_str = SecureRandom.hex[0..8] | ||
# e.g. foo_YYYYMMDD_HHMMSS_abcd1234 | ||
[report_name, timestamp, rand_str].join("_") | ||
end | ||
|
||
def handle(rw) # "r", "w" etc | ||
File.open(file, rw) | ||
end | ||
end | ||
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,24 @@ | ||
require "reports/shared_print_phase_count" | ||
require "shared_print/finder" | ||
require "spec_helper" | ||
|
||
RSpec.describe Reports::SharedPrintPhaseCount do | ||
let(:org) { "umich" } | ||
let(:fixt) { fixture("shared_print_umich_phase123.json") } | ||
let(:phase) { 3 } | ||
let(:report) { described_class.new(phase: phase) } | ||
|
||
before(:each) do | ||
Cluster.collection.find.delete_many | ||
end | ||
it "gets commitments per phase, puts them in a file" do | ||
# Make 5 commitments with phase:3 | ||
commitments = [] | ||
1.upto(5) do |i| | ||
commitments << build(:commitment, organization: org, ocn: i, phase: phase) | ||
end | ||
cluster_tap_save(*commitments) | ||
# Verify that the report script gets those 5 | ||
expect(report.commitments.count).to eq 5 | ||
end | ||
end |