<commit_before>require_relative '../../classes/hash_utils'

module Queries
  # Given a list of channels this query returns a list of
  # authorities, which match in position
  #
  # Performance
  #
  # We make sure to retrieve all the topics needed for this
  # query at once, but don't cache the authorities. In the usecase
  # which demanded this query, it never happens that we have
  # multiple channels with the same topic, so optimization wouldn't
  # actually optimize anything
  #
  # We cache the graph_users after they're retrieved, since
  # the most common use case is to get the channels of one
  # user, which means that we will now typically get
  # O(constant) graph_user retrievals (2 redis commands)
  # instead of O(N).
  # We also set this property on the channel. This is a nasty hack,
  # but otherwise we would still get this O(N) retrieval in
  # interactors/queries which get run after this one on the same
  # channel
  class CreatorAuthoritiesForChannels
    include Pavlov::Query

    arguments :channels

    def execute
      channels.map &method(:authority_for)
    end

    def authority_for(channel)
      if channel.type == 'channel'
        topic = topic_for(channel)
        graph_user = graph_user_for(channel)
        query(:'authority_on_topic_for', topic: topic, graph_user: graph_user)
      else
        # channel is a userstream or a created facts channel,
        # so there is no topic, therefor we define authority as 0
        fail "channel is a userstream or a created facts and should never be requested"
      end
    end

    def graph_user_for channel
      gu = graph_user channel.created_by_id
      channel.send :write_local, :created_by, gu
      gu
    end

    def graph_user id
      @graph_users     ||= {}
      @graph_users[id] ||= GraphUser[id]
    end

    def topics
      @topics ||= query(:'topics_for_channels', channels: channels)
    end
<commit_msg>removed comment which is now duplicated in exception<commit_after>require_relative '../../classes/hash_utils'

module Queries
  # Given a list of channels this query returns a list of
  # authorities, which match in position
  #
  # Performance
  #
  # We make sure to retrieve all the topics needed for this
  # query at once, but don't cache the authorities. In the usecase
  # which demanded this query, it never happens that we have
  # multiple channels with the same topic, so optimization wouldn't
  # actually optimize anything
  #
  # We cache the graph_users after they're retrieved, since
  # the most common use case is to get the channels of one
  # user, which means that we will now typically get
  # O(constant) graph_user retrievals (2 redis commands)
  # instead of O(N).
  # We also set this property on the channel. This is a nasty hack,
  # but otherwise we would still get this O(N) retrieval in
  # interactors/queries which get run after this one on the same
  # channel
  class CreatorAuthoritiesForChannels
    include Pavlov::Query

    arguments :channels

    def execute
      channels.map &method(:authority_for)
    end

    def authority_for(channel)
      if channel.type == 'channel'
        topic = topic_for(channel)
        graph_user = graph_user_for(channel)
        query(:'authority_on_topic_for', topic: topic, graph_user: graph_user)
      else
        fail "channel is a userstream or a created facts and should never be requested"
      end
    end

    def graph_user_for channel
      gu = graph_user channel.created_by_id
      channel.send :write_local, :created_by, gu
      gu
    end

    def graph_user id
      @graph_users     ||= {}
      @graph_users[id] ||= GraphUser[id]
    end

    def topics
      @topics ||= query(:'topics_for_channels', channels: channels)
    end

{'fraction_non_alphanumeric': 0.06398293788323114, 'fraction_numerical': 0.0007997867235403892, 'mean_word_length': 3.043103448275862, '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': '4895628', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1038, 'n_words': 437}