<commit_before>class OnlineCompetitionsService
  def initialize(track)
    @track = track
  end

  def execute
    track.delete_online_competitions_results

    competitions = OnlineEventsFinder.new(track).execute
    competitions.each do |competition|
      score_track_in_competition(competition)
    end
  end

  private

  attr_reader :track

  def score_track_in_competition(competition)
    track_segment =
      WindowRangeFinder.new(track_points).execute(competition.window_params)

    track.virtual_comp_results.create(
      virtual_competition_id: competition.id,
      result: track_segment.public_send(competition.task),
      highest_gr: track_segment.max_gr,
      highest_speed: track_segment.max_ground_speed
    )
  rescue WindowRangeFinder::ValueOutOfRange
    return
  end

  def track_points
    @track_points ||=
      track.points
           .trimmed(seconds_before_start: 20)
           .pluck_to_hash(
             'to_timestamp(gps_time_in_seconds) AT TIME ZONE \'UTC\' as gps_time',
             "#{@track.point_altitude_field} AS altitude",
             :latitude,
             :longitude,
             'h_speed AS h_speed',
             'v_speed AS v_speed',
             'CASE WHEN v_speed = 0 THEN h_speed / 0.1
                   ELSE h_speed / ABS(v_speed)
             END AS glide_ratio'
           )
  end
end<commit_msg>use points query object in online competition service #71<commit_after>class OnlineCompetitionsService
  def initialize(track)
    @track = track
  end

  def execute
    track.delete_online_competitions_results

    competitions = OnlineEventsFinder.new(track).execute
    competitions.each do |competition|
      score_track_in_competition(competition)
    end
  end

  private

  attr_reader :track

  def score_track_in_competition(competition)
    track_segment =
      WindowRangeFinder.new(track_points).execute(competition.window_params)

    track.virtual_comp_results.create(
      virtual_competition_id: competition.id,
      result: track_segment.public_send(competition.task),
      highest_gr: track_segment.max_gr,
      highest_speed: track_segment.max_ground_speed
    )
  rescue WindowRangeFinder::ValueOutOfRange
    return
  end

  def track_points
    @track_points ||= begin
      raw_points = PointsQuery.execute(
        track,
        trimmed: { seconds_before_start: 20 },
        only: [:gps_time, :altitude, :latitude, :longitude, :h_speed, :v_speed, :glide_ratio]
      )

      PointsPostprocessor.for(track.gps_type).new(raw_points).execute
    end
  end
end
{'fraction_non_alphanumeric': 0.07221350078492936, 'fraction_numerical': 0.003532182103610675, 'mean_word_length': 3.0849358974358974, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2129053', 'n_tokens_mistral': 794, 'n_tokens_neox': 780, 'n_words': 145}