Module: INatChannel::Storage

Defined in:
lib/inat-channel/data.rb

Class Method Summary collapse

Class Method Details

.confirm!(msg_id) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/inat-channel/data.rb', line 84

def confirm! msg_id
  # Отправка выполнена, фиксируем
  sent[@in_process[:uuid]] = {
    'msg_id' => msg_id,
    'sent_at' => Date.today
  }
  used[@in_process[:taxon_id]] = Date.today
end

.revert!Object



93
94
95
96
97
98
99
100
# File 'lib/inat-channel/data.rb', line 93

def revert!
  # Отправка не выполнена, возвращаем рабочую запись в пул
  pool[@in_process[:uuid]] = {
    'created_at' => @in_process[:created_at],
    'faves_count' => @in_process[:faves_count],
    'taxon_id' => @in_process[:taxon_id]
  }
end

.saveObject



102
103
104
105
106
107
# File 'lib/inat-channel/data.rb', line 102

def save
  save_pool
  save_sent
  save_used
  IC::logger.info "Saved pool=#{pool.size}, sent=#{sent.size}"
end

.select_uuid(fresh) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/inat-channel/data.rb', line 17

def select_uuid fresh
  # 1. Отфильтровываем уже отправленные
  fresh.reject! { |rec| sent?(rec[:uuid]) }

  # 2. Если нужно, отфильтровываем по таксонам
  taxon_uniq = IC::CONFIG[:unique_taxon]
  fresh.reject! { |rec| used?(rec.dig :taxon, :id) } if taxon_uniq == :strict
  
  if taxon_uniq == :priority
    uniq_fresh = fresh.reject { |rec| used?(rec.dig :taxon, :id) }
    unless uniq_fresh.empty?
      IC::logger.info "Take a fresh & unique (from #{uniq_fresh.size})"
      sample = sample_with_weight uniq_fresh, field: :faves_count
      fresh.reject { |rec| rec[:uuid] == sample[:uuid] }.each do |rec| 
        pool[rec[:uuid]] = {
          'created_at' => Date.parse(rec[:created_at]),
          'faves_count' => rec[:faves_count],
          'taxon_id' => rec.dig(:taxon, :id)
        }
      end
      sample[:taxon_id] = sample.dig :taxon, :id
      @in_process = sample
      return sample[:uuid]
    end
  end

  # 3. Если добрались до этого места, берем просто из свежих
  unless fresh.empty?
    IC::logger.info "Take a fresh (from #{fresh.size})"
    sample = sample_with_weight fresh, field: :faves_count
    fresh.reject { |rec| rec[:uuid] == sample[:uuid] }.each do |rec|
      pool[rec[:uuid]] = {
        "created_at" => Date.parse(rec[:created_at]),
        "faves_count" => rec[:faves_count],
        "taxon_id" => rec.dig(:taxon, :id),
      }
    end
    sample[:taxon_id] = sample.dig :taxon, :id
    @in_process = sample
    return sample[:uuid]
  end
  
  # 4. Если ..., проверяем приоритет уникальных уже для пула
  pool_records = pool.map { |k, v| v.merge({ 'uuid' => k }) }
  if taxon_uniq == :priority
    uniq_pool = pool_records.reject { |rec| used?(rec['taxon_id']) }
    unless uniq_pool.empty?
      IC::logger.info "Take an unique pool record (from #{uniq_pool.size})"
      sample = sample_with_weight uniq_pool, field: 'faves_count'
      sample.transform_keys!(&:to_sym)
      @in_process = sample
      return sample[:uuid]
    end
  end

  # 5. Если ..., берем из пула
  unless pool_records.empty?
    IC::logger.info "Take a pool record (from #{pool.size})"
    sample = sample_with_weight pool_records, field: 'faves_count'
    sample.transform_keys!(&:to_sym)
    @in_process = sample
    return sample[:uuid]
  end

  return nil
end