Class: Fbe::Iterate

Inherits:
Object
  • Object
show all
Defined in:
lib/fbe/iterate.rb

Overview

Iterate.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024 Zerocracy

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(fb, loog) ⇒ Iterate

Returns a new instance of Iterate.



41
42
43
44
45
46
47
48
49
# File 'lib/fbe/iterate.rb', line 41

def initialize(fb, loog)
  @fb = fb
  @loog = loog
  @label = nil
  @since = 0
  @query = nil
  @repeats = 1
  @quota_aware = false
end

Instance Method Details

#as(label) ⇒ Object



67
68
69
70
71
# File 'lib/fbe/iterate.rb', line 67

def as(label)
  raise 'Label is already set' unless @label.nil?
  raise 'Cannot set "label" to nil' if label.nil?
  @label = label
end

#by(query) ⇒ Object



61
62
63
64
65
# File 'lib/fbe/iterate.rb', line 61

def by(query)
  raise 'Query is already set' unless @query.nil?
  raise 'Cannot set query to nil' if query.nil?
  @query = query
end

#overObject

It makes a number of repeats of going through all repositories provided by the “repositories” configuration option. In each “repeat” it yields the repository ID and a number that is retrieved by the “query”. The query is supplied with two parameter: “$before” (the value from the previous repeat and “$rid” (the repo ID).



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/fbe/iterate.rb', line 78

def over(&)
  raise 'Use "as" first' if @label.nil?
  raise 'Use "by" first' if @query.nil?
  seen = {}
  oct = Fbe.octo(loog: @loog)
  repos = Fbe.unmask_repos(loog: @loog)
  restarted = []
  loop do
    repos.each do |repo|
      next if restarted.include?(repo)
      seen[repo] = 0 if seen[repo].nil?
      if seen[repo] >= @repeats
        @loog.debug("We've seen too many in the #{repo} repo, time to move to the next one")
        next
      end
      rid = oct.repo_id_by_name(repo)
      before = Fbe.fb.query(
        "(agg (and (eq what '#{@label}') (eq where 'github') (eq repository #{rid})) (first latest))"
      ).one
      Fbe.fb.query("(and (eq what '#{@label}') (eq where 'github') (eq repository #{rid}))").delete!
      before = before.nil? ? @since : before.first
      nxt = Fbe.fb.query(@query).one(before:, repository: rid)
      after =
        if nxt.nil?
          @loog.debug("Next is nil, starting from the beginning at #{@since}")
          restarted << repo
          @since
        else
          @loog.debug("Next is #{nxt}, starting from it...")
          yield(rid, nxt)
        end
      raise "Iterator must return an Integer, while #{after.class} returned" unless after.is_a?(Integer)
      f = Fbe.fb.insert
      f.where = 'github'
      f.repository = rid
      f.latest =
        if after.nil?
          @loog.debug("After is nil at #{repo}, setting the `latest` to nxt: #{nxt}")
          nxt
        else
          @loog.debug("After is #{after} at #{repo}, setting the `latest` to it")
          after
        end
      f.what = @label
      seen[repo] += 1
      if oct.off_quota
        @loog.debug('We are off GitHub quota, time to stop')
        break
      end
    end
    if oct.off_quota
      @loog.debug('We are off GitHub quota, time to stop')
      break
    end
    unless seen.values.any? { |v| v < @repeats }
      @loog.debug('No more repos to scan, quitting')
      break
    end
  end
  @loog.debug("Finished scanning #{repos.size} repos: #{seen.map { |k, v| "#{k}:#{v}" }.join(', ')}")
end

#quota_awareObject



51
52
53
# File 'lib/fbe/iterate.rb', line 51

def quota_aware
  @quota_aware = true
end

#repeats(repeats) ⇒ Object



55
56
57
58
59
# File 'lib/fbe/iterate.rb', line 55

def repeats(repeats)
  raise 'Cannot set "repeats" to nil' if repeats.nil?
  raise 'The "repeats" must be a positive integer' unless repeats.positive?
  @repeats = repeats
end