Class: Jumbotron::Services::Canonical::DeriveConsensusLine

Inherits:
CommandTower::Services::ApplicationService
  • Object
show all
Defined in:
app/services/jumbotron/services/canonical/derive_consensus_line.rb

Constant Summary collapse

CONSENSUS_MARKETS =
%w[spread total].freeze
LIVE_ODDS_NAME =
/\blive odds\b/i

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.eligible?(line, latest_at) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
# File 'app/services/jumbotron/services/canonical/derive_consensus_line.rb', line 41

def self.eligible?(line, latest_at)
  return false unless CONSENSUS_MARKETS.include?(line.market)
  return false if line.line_value.nil?
  return false if line.bookmaker_name.to_s.match?(LIVE_ODDS_NAME)
  return false if latest_at.nil?

  line.observed_at == latest_at
end

.from_current_lines(game_id:, current_lines:) ⇒ Object



32
33
34
35
36
37
38
39
# File 'app/services/jumbotron/services/canonical/derive_consensus_line.rb', line 32

def self.from_current_lines(game_id:, current_lines:)
  currents = Array(current_lines)
  latest_at = currents.filter_map(&:observed_at).max
  eligible = currents.select { |line| eligible?(line, latest_at) }
  eligible.group_by { |line| [line.market, line.outcome] }.map do |(_key, members)|
    to_consensus(game_id, members)
  end
end

.to_consensus(game_id, members) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/services/jumbotron/services/canonical/derive_consensus_line.rb', line 50

def self.to_consensus(game_id, members)
  values = members.map { |line| BigDecimal(line.line_value.to_s) }
  mean = values.sum / values.size
  Jumbotron::Canonical::ConsensusLine.new(
    game_id: game_id,
    market: members.first.market,
    outcome: members.first.outcome,
    line_value: mean,
    constituent_count: members.size,
    constituents: members.map do |line|
      Jumbotron::Canonical::ConsensusConstituent.new(
        line_observation_id: line.line_observation_id,
        bookmaker_id: line.bookmaker_id
      )
    end
  )
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/jumbotron/services/canonical/derive_consensus_line.rb', line 15

def call
  currents = current_lines
  if currents.nil?
    resolved = ResolveCurrentLines.call(game: game)
    unless resolved.success?
      context.fail!(application_error: resolved.errors.first)
      return
    end
    currents = Array(resolved.data[:current_lines])
  end

  context.consensus_lines = self.class.from_current_lines(
    game_id: game.id,
    current_lines: currents
  )
end