judges 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be8151f228edd7b8c45f0fcf1f566e8585fae93ce87bcb7f56ca6f452a1b67ca
4
- data.tar.gz: 99e20604a1f8a78a0c5dbf4080522aab725a7e82df5cb87d3b456962636aad10
3
+ metadata.gz: 2cc71c6f27206802eb89caac6ea4cf95558c5f36f484efea771e5ad5c442084d
4
+ data.tar.gz: 717c6de821765a54a916dc2eabafaf63827fd5a522584c7af866f5beea5475c7
5
5
  SHA512:
6
- metadata.gz: 2df81af8fd18810e54c05fc3631a2e38037d45e49a7f87cd4e9e8592155b155b81671ad5641e3923886e86752fa3be8c5f7926f2ce539d48ccfac714269f44d4
7
- data.tar.gz: 6fd2568f060510bc0b0600cc9d186d622025a7f886085bf5993bc3388f246c283c2c08c785199d609fd7a4729eb949a27a829d199785d43334addbdcd7f51248
6
+ metadata.gz: 895ea9f8d9b1527113c1a33d1c5e5a5d18b16994d00593d7f3eaf5c135c9a9eb9787c7ca969bcbced5b025eef8ff03e3927ecc90d3ee0738b9cee3cf1b948799
7
+ data.tar.gz: 2c86328e14c85f7685fb46b8cf78bcac87058725db51b57ca447186c61cbf5650dee93570c7000c11105792a14d8b8879d54e861325c1f12a97f200b09e33bb3
data/Gemfile.lock CHANGED
@@ -9,14 +9,15 @@ PATH
9
9
  iri (~> 0.8)
10
10
  loog (~> 0.2)
11
11
  nokogiri (~> 1.10)
12
+ retries (~> 0.0)
12
13
  typhoeus (~> 1.3)
13
14
 
14
15
  GEM
15
16
  remote: https://rubygems.org/
16
17
  specs:
17
- actionpack (7.1.3.3)
18
- actionview (= 7.1.3.3)
19
- activesupport (= 7.1.3.3)
18
+ actionpack (7.1.3.4)
19
+ actionview (= 7.1.3.4)
20
+ activesupport (= 7.1.3.4)
20
21
  nokogiri (>= 1.8.5)
21
22
  racc
22
23
  rack (>= 2.2.4)
@@ -24,13 +25,13 @@ GEM
24
25
  rack-test (>= 0.6.3)
25
26
  rails-dom-testing (~> 2.2)
26
27
  rails-html-sanitizer (~> 1.6)
27
- actionview (7.1.3.3)
28
- activesupport (= 7.1.3.3)
28
+ actionview (7.1.3.4)
29
+ activesupport (= 7.1.3.4)
29
30
  builder (~> 3.1)
30
31
  erubi (~> 1.11)
31
32
  rails-dom-testing (~> 2.2)
32
33
  rails-html-sanitizer (~> 1.6)
33
- activesupport (7.1.3.3)
34
+ activesupport (7.1.3.4)
34
35
  base64
35
36
  bigdecimal
36
37
  concurrent-ruby (~> 1.0, >= 1.0.2)
@@ -84,7 +85,7 @@ GEM
84
85
  erubi (1.12.0)
85
86
  ethon (0.16.0)
86
87
  ffi (>= 1.15.0)
87
- factbase (0.0.41)
88
+ factbase (0.0.42)
88
89
  json (~> 2.7)
89
90
  loog (~> 0.2)
90
91
  nokogiri (~> 1.10)
@@ -153,9 +154,9 @@ GEM
153
154
  rails-html-sanitizer (1.6.0)
154
155
  loofah (~> 2.21)
155
156
  nokogiri (~> 1.14)
156
- railties (7.1.3.3)
157
- actionpack (= 7.1.3.3)
158
- activesupport (= 7.1.3.3)
157
+ railties (7.1.3.4)
158
+ actionpack (= 7.1.3.4)
159
+ activesupport (= 7.1.3.4)
159
160
  irb
160
161
  rackup (>= 1.0.0)
161
162
  rake (>= 12.2)
@@ -168,6 +169,7 @@ GEM
168
169
  regexp_parser (2.9.2)
169
170
  reline (0.5.8)
170
171
  io-console (~> 0.5)
172
+ retries (0.0.5)
171
173
  rexml (3.2.8)
172
174
  strscan (>= 3.0.9)
173
175
  rspec-core (3.13.0)
data/judges.gemspec CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
26
26
  s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
27
27
  s.required_ruby_version = '>=3.2'
28
28
  s.name = 'judges'
29
- s.version = '0.3.0'
29
+ s.version = '0.4.0'
30
30
  s.license = 'MIT'
31
31
  s.summary = 'Command-Line Tool for a Factbase'
32
32
  s.description =
@@ -42,13 +42,14 @@ Gem::Specification.new do |s|
42
42
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
43
43
  s.rdoc_options = ['--charset=UTF-8']
44
44
  s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
45
- s.add_runtime_dependency 'backtrace', '~> 0.3'
45
+ s.add_runtime_dependency 'backtrace', '~>0.3'
46
46
  s.add_runtime_dependency 'concurrent-ruby', '1.2.3'
47
47
  s.add_runtime_dependency 'factbase', '~>0.0'
48
48
  s.add_runtime_dependency 'gli', '~>2.21'
49
49
  s.add_runtime_dependency 'iri', '~>0.8'
50
50
  s.add_runtime_dependency 'loog', '~>0.2'
51
- s.add_runtime_dependency 'nokogiri', '~> 1.10'
51
+ s.add_runtime_dependency 'nokogiri', '~>1.10'
52
+ s.add_runtime_dependency 'retries', '~>0.0'
52
53
  s.add_runtime_dependency 'typhoeus', '~>1.3'
53
54
  s.metadata['rubygems_mfa_required'] = 'true'
54
55
  end
data/lib/judges/baza.rb CHANGED
@@ -21,6 +21,7 @@
21
21
  # SOFTWARE.
22
22
 
23
23
  require 'typhoeus'
24
+ require 'retries'
24
25
  require 'iri'
25
26
  require 'loog'
26
27
  require_relative '../judges'
@@ -45,17 +46,20 @@ class Judges::Baza
45
46
  def push(name, data)
46
47
  id = 0
47
48
  elapsed(@loog) do
48
- ret = Typhoeus::Request.put(
49
- home.append('push').append(name).to_s,
50
- body: data,
51
- headers: headers.merge(
52
- 'Content-Type' => 'application/octet-stream',
53
- 'Content-Length' => data.size
54
- ),
55
- connecttimeout: @timeout,
56
- timeout: @timeout
57
- )
58
- check_code(ret)
49
+ ret = with_retries do
50
+ checked(
51
+ Typhoeus::Request.put(
52
+ home.append('push').append(name).to_s,
53
+ body: data,
54
+ headers: headers.merge(
55
+ 'Content-Type' => 'application/octet-stream',
56
+ 'Content-Length' => data.size
57
+ ),
58
+ connecttimeout: @timeout,
59
+ timeout: @timeout
60
+ )
61
+ )
62
+ end
59
63
  id = ret.body.to_i
60
64
  throw :"Pushed #{data.size} bytes to #{@host}, job ID is ##{id}"
61
65
  end
@@ -79,7 +83,7 @@ class Judges::Baza
79
83
  f.write(chunk)
80
84
  end
81
85
  request.run
82
- check_code(request.response)
86
+ checked(request.response)
83
87
  end
84
88
  data = File.binread(file)
85
89
  throw :"Pulled #{data.size} bytes of job ##{id} factbase at #{@host}"
@@ -92,11 +96,14 @@ class Judges::Baza
92
96
  def finished?(id)
93
97
  finished = false
94
98
  elapsed(@loog) do
95
- ret = Typhoeus::Request.get(
96
- home.append('finished').append(id).to_s,
97
- headers:
98
- )
99
- check_code(ret)
99
+ ret = with_retries do
100
+ checked(
101
+ Typhoeus::Request.get(
102
+ home.append('finished').append(id).to_s,
103
+ headers:
104
+ )
105
+ )
106
+ end
100
107
  finished = ret.body == 'yes'
101
108
  throw :"The job ##{id} is #{finished ? '' : 'not yet '}finished at #{@host}"
102
109
  end
@@ -106,11 +113,14 @@ class Judges::Baza
106
113
  def recent(name)
107
114
  job = 0
108
115
  elapsed(@loog) do
109
- ret = Typhoeus::Request.get(
110
- home.append('recent').append("#{name}.txt").to_s,
111
- headers:
112
- )
113
- check_code(ret)
116
+ ret = with_retries do
117
+ checked(
118
+ Typhoeus::Request.get(
119
+ home.append('recent').append("#{name}.txt").to_s,
120
+ headers:
121
+ )
122
+ )
123
+ end
114
124
  job = ret.body.to_i
115
125
  throw :"The recent \"#{name}\" job's ID is ##{job} at #{@host}"
116
126
  end
@@ -120,11 +130,12 @@ class Judges::Baza
120
130
  def name_exists?(name)
121
131
  exists = 0
122
132
  elapsed(@loog) do
123
- ret = Typhoeus::Request.get(
124
- home.append('exists').append(name).to_s,
125
- headers:
133
+ ret = checked(
134
+ Typhoeus::Request.get(
135
+ home.append('exists').append(name).to_s,
136
+ headers:
137
+ )
126
138
  )
127
- check_code(ret)
128
139
  exists = ret.body == 'yes'
129
140
  throw :"The name \"#{name}\" #{exists ? 'exists' : "doesn't exist"} at #{@host}"
130
141
  end
@@ -148,14 +159,14 @@ class Judges::Baza
148
159
  .scheme(@ssl ? 'https' : 'http')
149
160
  end
150
161
 
151
- def check_code(ret, allowed = [200])
162
+ def checked(ret, allowed = [200])
152
163
  allowed = [allowed] unless allowed.is_a?(Array)
153
164
  mtd = (ret.request.original_options[:method] || '???').upcase
154
165
  url = ret.effective_url
155
166
  log = "#{mtd} #{url} -> #{ret.code}"
156
167
  if allowed.include?(ret.code)
157
168
  @loog.debug(log)
158
- return
169
+ return ret
159
170
  end
160
171
  @loog.debug("#{log}\n #{(ret.headers || {}).map { |k, v| "#{k}: #{v}" }.join("\n ")}")
161
172
  msg =
@@ -56,9 +56,9 @@ class Judges::Update
56
56
  if c > 1
57
57
  @loog.info("\n\nStarting cycle ##{c}#{opts['max-cycles'] ? " (out of #{opts['max-cycles']})" : ''}...")
58
58
  end
59
- diff = cycle(opts, judges, fb, options)
59
+ churn = cycle(opts, judges, fb, options)
60
60
  impex.export(fb)
61
- if diff.zero?
61
+ if churn.zero?
62
62
  @loog.info("The update cycle ##{c} has made no changes to the factbase, let's stop")
63
63
  break
64
64
  end
@@ -66,36 +66,50 @@ class Judges::Update
66
66
  @loog.info("Too many cycles already, as set by --max-cycles=#{opts['max-cycles']}, breaking")
67
67
  break
68
68
  end
69
- @loog.info(
70
- "By #{diff.abs} fact(s) the factbase " \
71
- "#{diff.positive? ? 'increased' : 'decreased'} " \
72
- "its size at the cycle ##{c}"
73
- )
69
+ @loog.info("At the cycle #{c}, the factbase was modified by #{churn} fact(s)")
74
70
  end
75
71
  throw :"Update finished in #{c} cycle(s), #{format('+%d', fb.size - before)} fact(s)"
76
72
  end
77
73
  end
78
74
 
75
+ # How many facts were modified.
76
+ class Churn
77
+ attr_reader :added, :removed
78
+
79
+ def initialize(added, removed)
80
+ @added = added
81
+ @removed = removed
82
+ end
83
+
84
+ def to_s
85
+ "#{format('+%d', @added)}/#{format('+%d', -@removed)}"
86
+ end
87
+
88
+ def zero?
89
+ @added.zero? && @removed.zero?
90
+ end
91
+
92
+ def +(other)
93
+ Churn.new(@added + other.added, @removed + other.removed)
94
+ end
95
+ end
96
+
79
97
  private
80
98
 
99
+ # Run all judges in a full cycle, one by one.
100
+ # @return [Churn] How many modifications have been made
81
101
  def cycle(opts, judges, fb, options)
82
102
  errors = []
83
- diff = 0
103
+ churn = Churn.new(0, 0)
84
104
  global = {}
85
105
  elapsed(@loog) do
86
106
  done = judges.each_with_index do |p, i|
87
- local = {}
88
107
  @loog.info("\nšŸ‘‰ Running #{p.name} (##{i}) at #{p.dir.to_rel}...")
89
- before = fb.size
90
- begin
91
- p.run(fb, global, local, options)
92
- rescue StandardError, SyntaxError => e
93
- @loog.warn(Backtrace.new(e))
94
- errors << p.script
95
- end
96
- after = fb.size
97
- @loog.info("šŸ‘ The judge #{p.dir.to_rel} added #{after - before} facts") if after > before
98
- diff += after - before
108
+ churn += one_judge(fb, p, global, options)
109
+ @loog.info("šŸ‘ The judge #{p.name} modified #{churn} facts")
110
+ rescue StandardError, SyntaxError => e
111
+ @loog.warn(Backtrace.new(e))
112
+ errors << p.script
99
113
  end
100
114
  throw :"šŸ‘ #{done} judge(s) processed" if errors.empty?
101
115
  throw :"āŒ #{done} judge(s) processed with #{errors.size} errors"
@@ -104,6 +118,21 @@ class Judges::Update
104
118
  raise "Failed to update correctly (#{errors.size} errors)" unless opts['quiet']
105
119
  @loog.info('Not failing because of the --quiet flag provided')
106
120
  end
107
- diff
121
+ churn
122
+ end
123
+
124
+ # Run a single judge.
125
+ # @return [Churn] How many modifications have been made
126
+ def one_judge(fb, judge, global, options)
127
+ local = {}
128
+ before = fb.size
129
+ judge.run(fb, global, local, options)
130
+ after = fb.size
131
+ diff = after - before
132
+ if diff.positive?
133
+ Churn.new(diff, 0)
134
+ else
135
+ Churn.new(0, diff)
136
+ end
108
137
  end
109
138
  end
data/lib/judges.rb CHANGED
@@ -25,5 +25,5 @@
25
25
  # Copyright:: Copyright (c) 2024 Yegor Bugayenko
26
26
  # License:: MIT
27
27
  module Judges
28
- VERSION = '0.3.0'
28
+ VERSION = '0.4.0'
29
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: judges
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-04 00:00:00.000000000 Z
11
+ date: 2024-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '1.10'
111
+ - !ruby/object:Gem::Dependency
112
+ name: retries
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: typhoeus
113
127
  requirement: !ruby/object:Gem::Requirement