<commit_before>require 'net/http'

class Space < ActiveRecord::Base
  include AlgoliaSearch
  include FakeNameDetector

  belongs_to :user
  belongs_to :copied_from, class_name: 'Space', foreign_key: 'copied_from_id'
  has_many :copies, class_name: 'Space', foreign_key: 'copied_from_id'
  has_many :checkpoints, class_name: 'SpaceCheckpoint', dependent: :destroy

  belongs_to :organization

  validates :user_id, presence: true
  validate :can_create_private_models
  validates :viewcount, numericality: {allow_nil: true, greater_than_or_equal_to: 0}

  after_initialize :init
  after_save :identify_user
  after_save :take_snapshot, if: :needs_new_snapshot?
  after_save :take_checkpoint, if: :needs_checkpoint?
  after_destroy :identify_user

  scope :is_private, -> { where(is_private: true) }
  scope :is_public, -> { where(is_private: false) }
  scope :uncategorized_since, -> (date) { where 'categorized IS NOT true AND DATE(created_at) >= ?', date }

  def init
    self.is_private ||= false
  end

  algoliasearch if: :is_searchable?, per_environment: true, disable_indexing: Rails.env.test? do
    attribute :id, :name, :description, :user_id, :created_at, :updated_at, :is_private, :viewcount, :screenshot, :big_screenshot
    add_attribute :user_info
    add_attribute :organization_info

    # We want to rank equally relevant results by viewcount.
    customRanking ['desc(viewcount)']

    attribute :updated_at_i do
      updated_at.to_i
    end

    attribute :created_at_i do
      created_at.to_i
    end

    attribute :metric_count do
      metrics.length.to_i
    end
  end

  def last_updated_at?(datetime_str)
    updated_at.to_datetime.to_s == DateTime.parse(datetime_str).to_datetime.to_s

  end

  def metrics
    if graph && graph['metrics'].kind_of?(Array)
      graph['metrics'].map{|m| m.slice('name')}
    else
      []
    end
  end

  def guesstimates_not_of_type(types)
    return [] if graph.nil? || graph['guesstimates'].nil?
    graph['guesstimates'].select {|guesstimate| types.exclude? guesstimate['guesstimateType']}
  end

  def guesstimates_of_type(types)
    return [] if graph.nil? || graph['guesstimates'].nil?
    graph['guesstimates'].select {|guesstimate| types.include? guesstimate['guesstimateType']}<commit_msg>Code cleanup.<commit_after>require 'net/http'

class Space < ActiveRecord::Base
  include AlgoliaSearch
  include FakeNameDetector

  belongs_to :user
  belongs_to :copied_from, class_name: 'Space', foreign_key: 'copied_from_id'
  has_many :copies, class_name: 'Space', foreign_key: 'copied_from_id'
  has_many :checkpoints, class_name: 'SpaceCheckpoint', dependent: :destroy

  belongs_to :organization

  validates :user_id, presence: true
  validate :can_create_private_models
  validates :viewcount, numericality: {allow_nil: true, greater_than_or_equal_to: 0}

  after_initialize :init
  after_save :identify_user
  after_save :take_snapshot, if: :needs_new_snapshot?
  after_save :take_checkpoint, if: :needs_checkpoint?
  after_destroy :identify_user

  scope :is_private, -> { where(is_private: true) }
  scope :is_public, -> { where(is_private: false) }
  scope :uncategorized_since, -> (date) { where 'categorized IS NOT true AND DATE(created_at) >= ?', date }

  def init
    self.is_private ||= false
  end

  algoliasearch if: :is_searchable?, per_environment: true, disable_indexing: Rails.env.test? do
    attribute :id, :name, :description, :user_id, :created_at, :updated_at, :is_private, :viewcount, :screenshot, :big_screenshot
    add_attribute :user_info
    add_attribute :organization_info

    # We want to rank equally relevant results by viewcount.
    customRanking ['desc(viewcount)']

    attribute :updated_at_i do
      updated_at.to_i
    end

    attribute :created_at_i do
      created_at.to_i
    end

    attribute :metric_count do
      metrics.length.to_i
    end
  end

  def last_updated_at?(datetime_str)
    updated_at.to_datetime.to_s == DateTime.parse(datetime_str).to_datetime.to_s
  end

  def metrics
    if graph && graph['metrics'].kind_of?(Array)
      graph['metrics'].map{|m| m.slice('name')}
    else
      []
    end
  end

  def guesstimates_not_of_type(types)
    return [] if graph.nil? || graph['guesstimates'].nil?
    graph['guesstimates'].select {|guesstimate| types.exclude? guesstimate['guesstimateType']}
  end

  def guesstimates_of_type(types)
    return [] if graph.nil? || graph['guesstimates'].nil?
    graph['guesstimates'].select {|guesstimate| types.include? guesstimate['guesstimateType']}
{'fraction_non_alphanumeric': 0.10713496796995803, 'fraction_numerical': 0.0004417936823503424, 'mean_word_length': 4.6318407960199, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '1413670', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1538, 'n_words': 336}