22require "uri"
33require "forwardable"
44class Report
5+ COLUMN_NAMES = [
6+ "action" ,
7+ "umid" ,
8+ "uniqname" ,
9+ "campus" ,
10+ "user_group" ,
11+ "statistic_category" ,
12+ "sponsor_reason" ,
13+ "exclude_reasons"
14+ ]
515 def self . configure_yabeda!
616 Yabeda . configure do
717 group :aim_patron_load do
@@ -12,6 +22,8 @@ def self.configure_yabeda!
1222 gauge :campus , comment : "Number of patrons in a given campus" , tags : [ :script_type , :name ]
1323 gauge :user_group , comment : "Number of patrons in a given Alma User Group" , tags : [ :script_type , :name ]
1424 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 ]
1527 gauge :error , comment : "Number of errors encountered while running the patron load" , tags : [ :script_type ]
1628 gauge :job_duration_seconds , comment : "Number of seconds it took to run the patron load job" , tags : [ :script_type ]
1729 end
@@ -27,6 +39,10 @@ def self.print_metrics
2739 Prometheus ::Client ::Formats ::Text . marshal ( Yabeda ::Prometheus . registry )
2840 end
2941
42+ def self . column_names
43+ COLUMN_NAMES
44+ end
45+
3046 def self . push_metrics
3147 # The env var needs to be set to the push gateway url
3248 if ENV [ "PROMETHEUS_PUSH_GATEWAY" ] &.match? ( URI ::DEFAULT_PARSER . make_regexp )
@@ -51,6 +67,7 @@ def self.open(file_base:, script_type:, &block)
5167
5268 def initialize ( fh :, script_type :)
5369 @fh = fh
70+ @fh . write self . class . column_names . join ( "\t " ) + "\n "
5471 @script_type = script_type
5572 end
5673
@@ -59,17 +76,18 @@ def metrics
5976 end
6077
6178 def load ( patron )
62- patron = Patron . new ( patron )
63- @fh . write report_string ( kind : "LOAD" , patron : patron )
79+ patron = Patron . new ( patron : patron , action : "load" )
80+ @fh . write patron . report_string
6481 metrics . found . increment ( { script_type : @script_type } )
6582 metrics . loaded . increment ( { script_type : @script_type } )
66- [ "patron_kind" , "statistic_category" , "user_group" , "campus" ] . each do |metric |
83+ [ "patron_kind" , "statistic_category" , "user_group" , "campus" , "sponsor_reason" ] . each do |metric |
6784 increment_metric ( metric , patron )
6885 end
6986 end
7087
7188 def skip ( patron )
72- @fh . write report_string ( kind : "SKIP" , patron : patron )
89+ patron = Patron . new ( patron : patron , action : "skip" )
90+ @fh . write patron . report_string
7391 metrics . found . increment ( { script_type : @script_type } )
7492 metrics . skipped . increment ( { script_type : @script_type } )
7593 end
@@ -87,7 +105,7 @@ def report_string(kind:, patron:)
87105 end
88106
89107 def increment_metric ( metric , patron )
90- metrics . public_send ( metric ) . increment ( { script_type : @script_type , name : patron . public_send ( metric ) } )
108+ metrics . public_send ( metric ) . increment ( { script_type : @script_type , name : patron . public_send ( metric ) } ) if patron . public_send ( metric )
91109 end
92110
93111 class Patron
@@ -153,8 +171,13 @@ class Patron
153171 "SH" => "shares"
154172 }
155173
156- def initialize ( patron )
174+ def initialize ( patron : , action : "whatever" )
157175 @patron = patron
176+ @action = action
177+ end
178+
179+ def action
180+ @action . upcase
158181 end
159182
160183 def umid
@@ -173,10 +196,24 @@ def statistic_category
173196 STATISTIC_CATEGORY_MAP [ @patron . statistic_category ] || @patron . statistic_category
174197 end
175198
199+ def sponsor_reason
200+ @patron . sponsor_reason &.downcase
201+ end
202+
176203 def patron_kind
177204 to_snake ( @patron . class . name . split ( "::" ) . last )
178205 end
179206
207+ def exclude_reasons
208+ @patron . exclude_reasons . join ( "," )
209+ end
210+
211+ def report_string
212+ Report . column_names . map do |method |
213+ public_send ( method )
214+ end . join ( "\t " ) + "\n "
215+ end
216+
180217 private
181218
182219 def to_snake ( str )
0 commit comments