# frozen_string_literal: true

class Span < ApplicationRecord
  self.primary_key = 'id'

  extend Model::Range
  include Model::Duration

  attribute :id, :uuid
  attribute :trace_id, :uuid
  attribute :platform_id, :uuid
  attribute :application_id, :uuid

  belongs_to :trace
  belongs_to :platform

  has_many :traces, foreign_key: :origin_id, dependent: :destroy, inverse_of: :spans

  class << self
    def retention(period, time = Time.zone.now)
      period = ::Server::Types::Duration.new.cast_value(period)
      tlimit = time - period

      where t[:stop].lt(tlimit)
    end

    alias t arel_table
  end
end

# frozen_string_literal: true

class Span < ApplicationRecord
  self.primary_key = 'id'

  extend Model::Range
  include Model::Duration

  attribute :id, :uuid
  attribute :trace_id, :uuid
  attribute :platform_id, :uuid
  attribute :application_id, :uuid

  belongs_to :trace
  belongs_to :platform

  has_many :traces, foreign_key: :origin_id, dependent: :destroy, inverse_of: :origin

  class << self
    def retention(period, time = Time.zone.now)
      period = ::Server::Types::Duration.new.cast_value(period)
      tlimit = time - period

      where t[:stop].lt(tlimit)
    end

    alias t arel_table
  end
end

Fix wrong inverse_of resulting in wrongly preloaded data
Fix wrong inverse_of resulting in wrongly preloaded data

The wrong inverse_of resulted in the following issue when preloaded
relations on spans:

    irb(main):005:0> t.spans.map(&:trace_id).uniq
    => [<UUID4:3b25610a-145c-49d6-bedf-b0e10b86b174>]

    irb(main):006:0> t.spans.includes(:traces).map(&:trace_id).uniq
    => [<UUID4:3b25610a-145c-49d6-bedf-b0e10b86b174>,
        <UUID4:7c50fe4f-4a1a-4602-90a7-e2699be56f8e>,
        <UUID4:5d5fcf3a-b4f8-4854-b137-41d14e8672c3>]
