<commit_before># Builds a list of distinct channels that are either verified, excluded or both for the Brave Browser.
#
# Each channel is an array:
# [
#   channel_identifier (string),
#   verified (boolean),
#   excluded (boolean),
#   site_banner details
# ]
#
# ex.
# [
#   ["brave.com", true, false, {title: 'Hello', description: 'world'...}],
#   ["google.com", false, true, {}],
#   ["us.gov", false, false, {}]
# ]

class JsonBuilders::ChannelsJsonBuilder
  def initialize
    require "publishers/excluded_channels"
    @excluded_channel_ids = Publishers::ExcludedChannels.brave_publisher_id_list
    @verified_channels = Channel.verified
    @excluded_verified_channel_ids = []
  end

  def build
    channels = []

    [Channel.verified.site_channels, Channel.verified.youtube_channels, Channel.verified.twitch_channels, Channel.verified.twitter_channels].each do |verified_channels|
      verified_channels.find_each do |verified_channel|
        next if verified_channel.details.nil?
        if @excluded_channel_ids.include?(verified_channel.details.channel_identifier)
          excluded = true
          @excluded_verified_channel_ids.push(verified_channel.details.channel_identifier)
        else
          excluded = false
        end
        if verified_channel.publisher.default_site_banner_mode && verified_channel.publisher.default_site_banner_id
          channels.push([verified_channel.details.channel_identifier, true, excluded, verified_channel.publisher.default_site_banner.read_only_react_property])
        elsif verified_channel.site_banner
          channels.push([verified_channel.details.channel_identifier, true, excluded, verified_channel.site_banner.read_only_react_property])
        else
          channels.push([verified_channel.details.channel_identifier, true, excluded, {}])<commit_msg>Big perf improvement. Still a massively ugly N+1 query due to weird complexity on how Rails handles polymorphic lookup.<commit_after># Builds a list of distinct channels that are either verified, excluded or both for the Brave Browser.
#
# Each channel is an array:
# [
#   channel_identifier (string),
#   verified (boolean),
#   excluded (boolean),
#   site_banner details
# ]
#
# ex.
# [
#   ["brave.com", true, false, {title: 'Hello', description: 'world'...}],
#   ["google.com", false, true, {}],
#   ["us.gov", false, false, {}]
# ]

class JsonBuilders::ChannelsJsonBuilder
  def initialize
    require "publishers/excluded_channels"
    @excluded_channel_ids = Publishers::ExcludedChannels.brave_publisher_id_list
    @excluded_verified_channel_ids = []
  end

  def build
    channels = []

    [Channel.verified.site_channels.includes(:site_banner).includes(publisher: :site_banners),
     Channel.verified.youtube_channels.includes(:site_banner).includes(publisher: :site_banners),
     Channel.verified.twitch_channels.includes(:site_banner).includes(publisher: :site_banners),
     Channel.verified.twitter_channels.includes(:site_banner).includes(publisher: :site_banners)
    ].each do |verified_channels|
      verified_channels.find_each do |verified_channel|
        if @excluded_channel_ids.include?(verified_channel.details.channel_identifier)
          excluded = true
          @excluded_verified_channel_ids.push(verified_channel.details.channel_identifier)
        else
          excluded = false
        end
        if verified_channel.publisher.default_site_banner_mode && verified_channel.publisher.default_site_banner_id
          channels.push([verified_channel.details.channel_identifier, true, excluded, verified_channel.publisher.default_site_banner.read_only_react_property])
        elsif verified_channel.site_banner
          channels.push([verified_channel.details.channel_identifier, true, excluded, verified_channel.site_banner.read_only_react_property])
        else
          channels.push([verified_channel.details.channel_identifier, true, excluded, {}])
{'fraction_non_alphanumeric': 0.09964322120285422, 'fraction_numerical': 0.00025484199796126404, 'mean_word_length': 4.43628808864266, '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': '3826767', 'n_tokens_mistral': 1152, 'n_tokens_neox': 1150, 'n_words': 214}