Skip to content

Commit 3dbcc57

Browse files
authored
Merge pull request #16 from mlibrary/remove-repeated-script-type
fix: sets script_type earlier
2 parents ab9ecf7 + 1f4e4ae commit 3dbcc57

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

lib/process_ldap.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ def filter
7171
end
7272

7373
def write_to_output(&block)
74-
File.open("#{@file_base}.xml", "w") do |output|
75-
output.write "<?xml version=\"1.0\"?>\n"
76-
output.write "<users>\n"
77-
block.call(output)
78-
output.write "</users>"
74+
Yabeda.with_tags(script_type: script_type) do
75+
File.open("#{@file_base}.xml", "w") do |output|
76+
output.write "<?xml version=\"1.0\"?>\n"
77+
output.write "<users>\n"
78+
block.call(output)
79+
output.write "</users>"
80+
end
7981
end
8082
end
8183

@@ -106,7 +108,7 @@ def process
106108
start = Time.now
107109
S.logger.info("start")
108110
S.logger.info("open files", file_base: @file_base) if @file_base
109-
Report.open(file_base: @file_base, script_type: script_type) do |report|
111+
Report.open(@file_base) do |report|
110112
write_to_output do |output|
111113
S.logger.info("begin ldap search")
112114
search do |data|

lib/report.rb

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ class Report
1414
]
1515
def self.configure_yabeda!
1616
Yabeda.configure do
17+
default_tag :script_type, "test"
1718
group :aim_patron_load do
18-
gauge :loaded, comment: "Number of loaded patrons", tags: [:script_type]
19-
gauge :skipped, comment: "Number of skipped patrons", tags: [:script_type]
20-
gauge :found, comment: "Number of found patrons", tags: [:script_type]
21-
gauge :patron_kind, comment: "Number of patrons in a given major category", tags: [:script_type, :name]
22-
gauge :campus, comment: "Number of patrons in a given campus", tags: [:script_type, :name]
23-
gauge :user_group, comment: "Number of patrons in a given Alma User Group", tags: [:script_type, :name]
24-
gauge :statistic_category, comment: "Number of loaded patrons in a statistic category", tags: [:script_type, :name]
25-
gauge :sponsor_reason, comment: "Number of loaded Sponsored Affiliates with a given sponsor reason", tags: [:script_type, :name]
26-
gauge :exclude_reason, comment: "Number of patrons skipped for a given reason", tags: [:script_type, :name]
27-
gauge :error, comment: "Number of errors encountered while running the patron load", tags: [:script_type]
28-
gauge :job_duration_seconds, comment: "Number of seconds it took to run the patron load job", tags: [:script_type]
19+
gauge :loaded, comment: "Number of loaded patrons"
20+
gauge :skipped, comment: "Number of skipped patrons"
21+
gauge :found, comment: "Number of found patrons"
22+
gauge :patron_kind, comment: "Number of patrons in a given major category", tags: [:name]
23+
gauge :campus, comment: "Number of patrons in a given campus", tags: [:name]
24+
gauge :user_group, comment: "Number of patrons in a given Alma User Group", tags: [:name]
25+
gauge :statistic_category, comment: "Number of loaded patrons in a statistic category", tags: [:name]
26+
gauge :sponsor_reason, comment: "Number of loaded Sponsored Affiliates with a given sponsor reason", tags: [:name]
27+
gauge :exclude_reason, comment: "Number of patrons skipped for a given reason", tags: [:name]
28+
gauge :error, comment: "Number of errors encountered while running the patron load"
29+
gauge :job_duration_seconds, comment: "Number of seconds it took to run the patron load job"
2930
end
3031
end
3132
Yabeda.configure!
@@ -53,22 +54,26 @@ def self.push_metrics
5354
end
5455
end
5556

56-
def self.open(file_base:, script_type:, &block)
57+
def self.open(file_base, script_type: "whatever", &block)
5758
if file_base
5859
File.open("#{file_base}.tsv", "w") do |fh|
59-
report = Report.new(fh: fh, script_type: script_type)
60+
report = Report.new(fh)
6061
block.call(report)
6162
end
6263
else
63-
report = Report.new(fh: $stdout, script_type: script_type)
64-
block.call(report)
64+
StringIO.open do |fh|
65+
report = Report.new(fh)
66+
block.call(report)
67+
puts "\n"
68+
puts fh.string
69+
puts "\n"
70+
end
6571
end
6672
end
6773

68-
def initialize(fh:, script_type:)
74+
def initialize(fh)
6975
@fh = fh
7076
@fh.write self.class.column_names.join("\t") + "\n"
71-
@script_type = script_type
7277
end
7378

7479
def metrics
@@ -78,8 +83,8 @@ def metrics
7883
def load(patron)
7984
patron = Patron.new(patron: patron, action: "load")
8085
@fh.write patron.report_string
81-
metrics.found.increment({script_type: @script_type})
82-
metrics.loaded.increment({script_type: @script_type})
86+
metrics.found.increment
87+
metrics.loaded.increment
8388
["patron_kind", "statistic_category", "user_group", "campus", "sponsor_reason"].each do |metric|
8489
increment_metric(metric, patron)
8590
end
@@ -88,8 +93,8 @@ def load(patron)
8893
def skip(patron)
8994
patron = Patron.new(patron: patron, action: "skip")
9095
@fh.write patron.report_string
91-
metrics.found.increment({script_type: @script_type})
92-
metrics.skipped.increment({script_type: @script_type})
96+
metrics.found.increment
97+
metrics.skipped.increment
9398
end
9499

95100
def report_string(kind:, patron:)
@@ -105,7 +110,7 @@ def report_string(kind:, patron:)
105110
end
106111

107112
def increment_metric(metric, patron)
108-
metrics.public_send(metric).increment({script_type: @script_type, name: patron.public_send(metric)}) if patron.public_send(metric)
113+
metrics.public_send(metric).increment({name: patron.public_send(metric)}) if patron.public_send(metric)
109114
end
110115

111116
class Patron

spec/report_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Yabeda.reset!
2222
end
2323
subject do
24-
described_class.new(fh: @file_handle, script_type: "test")
24+
described_class.new(@file_handle)
2525
end
2626

2727
it "includes a header with all of the column names" do
@@ -79,16 +79,12 @@
7979
it "is incremeneted when user a user is a test user" do
8080
test_user = staff_data
8181
test_user["uid"] = ["ststv"]
82-
Yabeda.with_tags(script_type: "test") do
83-
Patron.for(test_user)
84-
end
82+
Patron.for(test_user)
8583
expect(Report.metrics.exclude_reason.get(name: "test_user", script_type: "test")).to eq(1)
8684
end
8785
it "is incremented when a user is skipped for some other reason" do
8886
staff_data["umichhr"][0].sub!("jobCategory=Staff", "jobCateogry=INVALID")
89-
Yabeda.with_tags(script_type: "test") do
90-
Patron.for(staff_data)
91-
end
87+
Patron.for(staff_data)
9288
expect(Report.metrics.exclude_reason.get(name: "job_category_not_staff", script_type: "test")).to eq(1)
9389
end
9490
end

0 commit comments

Comments
 (0)