enrichment-retry-picker 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9b9d9d1a56be1368415b791999ffb3a04f2cba0c65db3b3ef2c5aec95b037bf8
4
+ data.tar.gz: a745dc68bb6be13bf2eefb8cb6faee9604ff9ec7f97fac7adfb8aac0858ba279
5
+ SHA512:
6
+ metadata.gz: a0a6afa8dc7e459a0819dd3c9c8bc12d5548389e376b502f0cd4f267e2745c18ffaf35cad40162fabcf935fe3abc770a6334f3e33410c8aa8f3d4f385bff86db
7
+ data.tar.gz: 984eb2e0440d32ad05bff3e6a9fdbf773dc4823dbf1387a7698ca79e78dcf4a3c50d63931369d78f5bdfa201f942e732e2edea32ecee821f4a35a204d1937b79
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 OneGTMLab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # enrichment-retry-picker
2
+
3
+ Ruby command line tool and library. Give it a CSV with one row per record: the status of the last enrichment lookup (ok, not_found or error) and how many lookups you have made so far. It decides for every record whether it is done, worth a retry or time to give up, prints the totals and lists the record ids to retry. It calls no API and changes nothing.
4
+
5
+ Part of the [OneGTMLab](https://www.onegtmlab.dev) free GTM tools. Background on the idea: [enrichment APIs](https://www.onegtmlab.dev/tool-directory/enrichment).
6
+
7
+ ## Install
8
+
9
+ ```
10
+ gem install enrichment-retry-picker
11
+ ```
12
+
13
+ ## Use
14
+
15
+ CSV columns: record_id, status, attempts. Status is ok, not_found or error (ignoring upper and lower case); attempts is a whole number of 1 or more, the lookups made so far, from your own records. The rules are fixed: ok means done; not_found means give up, because the record is not in that source; error with fewer attempts than the limit means retry; error with attempts at or above the limit means give up and needs a look. The limit is --max-attempts, set by you (default 3, a starting value and not a fact about any provider). Output is one line per record in file order, then the totals and the retry ids; the share to retry is rounded half up to 1 decimal. Option --only retry prints just the retry ids, one per line, for piping into another step. A bad status or bad attempts (the row is named), a duplicate record_id (named), a missing file, a missing column, a bad option or a CSV with no rows stops the run with an error and a non-zero exit code.
16
+
17
+ ```
18
+ $ ruby bin/enrichment-retry-picker sample.csv --max-attempts 3
19
+ r001 done lookup succeeded
20
+ r002 give up not found: the record is not in that source
21
+ r003 retry error after 1 of 3 attempts
22
+ r004 retry error after 2 of 3 attempts
23
+ r005 give up error after 3 attempts, limit 3 reached, needs a look
24
+ r006 done lookup succeeded
25
+ r007 give up error after 5 attempts, limit 3 reached, needs a look
26
+ r008 give up not found: the record is not in that source
27
+ r009 retry error after 1 of 3 attempts
28
+ r010 done lookup succeeded
29
+
30
+ records: 10
31
+ done: 3
32
+ retry: 3
33
+ give up: 4
34
+ share to retry: 30.0%
35
+
36
+ retry record_ids: r003, r004, r009
37
+ ```
38
+
39
+ ## Test
40
+
41
+ ```
42
+ ruby test/run.rb
43
+ ```
44
+
45
+ Offline, no network calls, no dependencies beyond the language runtime. MIT licensed.
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/enrichment_retry_picker'
5
+
6
+ if ARGV.empty? || ARGV.include?('-h') || ARGV.include?('--help')
7
+ warn "usage: enrichment-retry-picker records.csv [--max-attempts 3] [--only retry]\n" \
8
+ "CSV columns: record_id, status (ok, not_found, error), attempts. More: https://www.onegtmlab.dev/tool-directory/enrichment"
9
+ exit 2
10
+ end
11
+
12
+ begin
13
+ path, max, only = EnrichmentRetryPicker.parse_args(ARGV)
14
+ result = EnrichmentRetryPicker.analyze(EnrichmentRetryPicker.from_csv(File.read(path)), max)
15
+ print(only ? EnrichmentRetryPicker.render_only_retry(result) : EnrichmentRetryPicker.render(result))
16
+ rescue ArgumentError, Errno::ENOENT, Errno::EISDIR, CSV::MalformedCSVError => e
17
+ abort "error: #{e.message}"
18
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+
5
+ # Decide, record by record, whether to retry a failed enrichment lookup, from the status and attempt count you recorded.
6
+ # The tool calls no API and changes nothing. The attempt limit is a number set by you.
7
+ module EnrichmentRetryPicker
8
+ STATUSES = %w[ok not_found error].freeze
9
+ DEFAULT_MAX_ATTEMPTS = 3
10
+
11
+ Record = Struct.new(:record_id, :status, :attempts, keyword_init: true)
12
+ Decision = Struct.new(:record_id, :decision, :reason, keyword_init: true)
13
+
14
+ def self.pct(n, d)
15
+ return 0.0 if d.zero?
16
+
17
+ Rational(n * 100, d).round(1).to_f
18
+ end
19
+
20
+ def self.from_csv(text)
21
+ table = CSV.parse(text.sub(/\A/, ''), headers: true, header_converters: ->(h) { h.to_s.strip.downcase })
22
+ missing = %w[record_id status attempts] - table.headers.compact
23
+ raise ArgumentError, "missing column(s): #{missing.join(', ')}" unless missing.empty?
24
+ raise ArgumentError, 'no rows in the CSV' if table.empty?
25
+
26
+ seen = {}
27
+ table.each_with_index.map do |r, i|
28
+ line = i + 2
29
+ id = r['record_id'].to_s.strip
30
+ raise ArgumentError, "row #{line}: empty record_id" if id.empty?
31
+ raise ArgumentError, "duplicate record_id #{id.inspect} (rows #{seen[id]} and #{line})" if seen.key?(id)
32
+
33
+ seen[id] = line
34
+ status = r['status'].to_s.strip.downcase
35
+ unless STATUSES.include?(status)
36
+ raise ArgumentError, "row #{line}: bad status #{r['status'].to_s.strip.inspect} for #{id.inspect}, expected ok, not_found or error"
37
+ end
38
+
39
+ att = r['attempts'].to_s.strip
40
+ unless att.match?(/\A\d+\z/) && att.to_i.positive?
41
+ raise ArgumentError, "row #{line}: bad attempts #{att.inspect} for #{id.inspect}, expected a whole number of 1 or more"
42
+ end
43
+
44
+ Record.new(record_id: id, status: status, attempts: att.to_i)
45
+ end
46
+ end
47
+
48
+ def self.decide(record, max_attempts)
49
+ case record.status
50
+ when 'ok'
51
+ Decision.new(record_id: record.record_id, decision: 'done', reason: 'lookup succeeded')
52
+ when 'not_found'
53
+ Decision.new(record_id: record.record_id, decision: 'give up', reason: 'not found: the record is not in that source')
54
+ else
55
+ if record.attempts < max_attempts
56
+ Decision.new(record_id: record.record_id, decision: 'retry',
57
+ reason: "error after #{record.attempts} of #{max_attempts} attempts")
58
+ else
59
+ Decision.new(record_id: record.record_id, decision: 'give up',
60
+ reason: "error after #{record.attempts} attempts, limit #{max_attempts} reached, needs a look")
61
+ end
62
+ end
63
+ end
64
+
65
+ def self.analyze(records, max_attempts = DEFAULT_MAX_ATTEMPTS)
66
+ decisions = records.map { |r| decide(r, max_attempts) }
67
+ counts = Hash.new(0)
68
+ decisions.each { |d| counts[d.decision] += 1 }
69
+ { decisions: decisions, records: records.size, done: counts['done'], retry: counts['retry'], give_up: counts['give up'],
70
+ retry_share: pct(counts['retry'], records.size),
71
+ retry_ids: decisions.select { |d| d.decision == 'retry' }.map(&:record_id) }
72
+ end
73
+
74
+ def self.render(result)
75
+ out = +''
76
+ result[:decisions].each { |d| out << format("%-10s %-8s %s\n", d.record_id, d.decision, d.reason) }
77
+ out << "\nrecords: #{result[:records]}\n"
78
+ out << "done: #{result[:done]}\n"
79
+ out << "retry: #{result[:retry]}\n"
80
+ out << "give up: #{result[:give_up]}\n"
81
+ out << format("share to retry: %.1f%%\n", result[:retry_share])
82
+ out << "\nretry record_ids: #{result[:retry_ids].empty? ? 'none' : result[:retry_ids].join(', ')}\n"
83
+ out
84
+ end
85
+
86
+ def self.render_only_retry(result)
87
+ result[:retry_ids].map { |id| "#{id}\n" }.join
88
+ end
89
+
90
+ # Returns [path, max_attempts, only]; raises ArgumentError on bad input.
91
+ def self.parse_args(argv)
92
+ args = argv.dup
93
+ path = nil
94
+ max = DEFAULT_MAX_ATTEMPTS
95
+ only = nil
96
+ until args.empty?
97
+ arg = args.shift
98
+ case arg
99
+ when '--max-attempts'
100
+ raise ArgumentError, '--max-attempts needs a value' if args.empty?
101
+
102
+ val = args.shift
103
+ unless val.match?(/\A\d+\z/) && val.to_i.positive?
104
+ raise ArgumentError, "bad --max-attempts #{val.inspect}, expected a whole number of 1 or more"
105
+ end
106
+
107
+ max = val.to_i
108
+ when '--only'
109
+ raise ArgumentError, '--only needs a value (retry)' if args.empty?
110
+
111
+ val = args.shift
112
+ raise ArgumentError, "bad --only #{val.inspect}, only retry is supported" unless val == 'retry'
113
+
114
+ only = val
115
+ when /\A-/ then raise ArgumentError, "unknown option #{arg}"
116
+ else
117
+ raise ArgumentError, "unexpected extra argument #{arg.inspect}" if path
118
+
119
+ path = arg
120
+ end
121
+ end
122
+ raise ArgumentError, 'no CSV file given' unless path
123
+
124
+ [path, max, only]
125
+ end
126
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enrichment-retry-picker
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - OneGTMLab
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: csv
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: "0"
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: "0"
28
+ description: "Ruby CLI and library: from a CSV of record_id, status (ok, not_found, error) and attempts, decide done, retry or give up for each record against an attempt limit you set, with totals and the list of ids to retry. It calls no API. Offline."
29
+ executables:
30
+ - enrichment-retry-picker
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - bin/enrichment-retry-picker
37
+ - lib/enrichment_retry_picker.rb
38
+ homepage: "https://www.onegtmlab.dev/tool-directory/enrichment"
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ source_code_uri: "https://github.com/jaizendra-netizen/enrichment-retry-picker"
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ -
49
+ - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "2.7"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ -
55
+ - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ requirements: []
59
+ rubygems_version: 4.1.0
60
+ specification_version: 4
61
+ summary: "Pick which failed enrichment lookups to retry and which to give up on, from the status and attempts you recorded"
62
+ test_files: []