<commit_before>require_relative '../../../services/sql-api/sql_api'

module Carto
  class DataLibraryService
    # - source_api_key is used to retrieve metadata from the dataset
    # - granted_api_key is stored as the one to use for the importing
    def load_dataset!(carto_api_client,
                      source_dataset:, source_username:, source_api_key:,
                      target_username:, granted_api_key:)
      target_user = Carto::User.find_by_username(target_username)

      remote_visualization = carto_api_client.get_visualization_v1(
        username: source_username, name: source_dataset, params: { api_key: source_api_key }
      )
      remote_table = remote_visualization[:table]
      privacy = target_user.default_dataset_privacy
      visualization = Carto::Visualization.create!(
        name: remote_visualization[:name],
        display_name: display_name(remote_visualization),
        user: target_user,
        type: Carto::Visualization::TYPE_REMOTE,
        privacy: privacy,
        description: remote_visualization[:description],
        tags: remote_visualization[:tags],
        license: remote_visualization[:license],
        source: remote_visualization[:source],
        attributions: remote_visualization[:attributions]
      )
      base_url = "#{carto_api_client.scheme}://#{carto_api_client.base_url(source_username)}"
      sql_api_url = CartoDB::SQLApi.with_username_api_key(source_username, granted_api_key, privacy, base_url: base_url)
                                   .export_table_url(source_dataset)
      Carto::ExternalSource.create!(
        visualization: visualization,
        import_url: sql_api_url,
        rows_counted: remote_table[:row_count],
        size: remote_table[:size],
        geometry_types: remote_table[:geometry_types],
        username: target_username
      )

      visualization
    end

    private

    def display_name(remote_visualization)
      remote_visualization[:display_name].presence || remote_visualization[:name]
    end<commit_msg>slice refactor<commit_after>require_relative '../../../services/sql-api/sql_api'

module Carto
  class DataLibraryService
    # - source_api_key is used to retrieve metadata from the dataset
    # - granted_api_key is stored as the one to use for the importing
    def load_dataset!(carto_api_client,
                      source_dataset:, source_username:, source_api_key:,
                      target_username:, granted_api_key:)
      target_user = Carto::User.find_by_username(target_username)

      remote_visualization = carto_api_client.get_visualization_v1(
        username: source_username, name: source_dataset, params: { api_key: source_api_key }
      )
      remote_table = remote_visualization[:table]
      privacy = target_user.default_dataset_privacy
      remote_attributes = remote_visualization.slice(:name, :description, :tags, :license, :source, :attributions)
      visualization = Carto::Visualization.create!(
        remote_attributes.merge(
          display_name: display_name(remote_visualization),
          user: target_user,
          type: Carto::Visualization::TYPE_REMOTE,
          privacy: privacy
        )
      )
      base_url = "#{carto_api_client.scheme}://#{carto_api_client.base_url(source_username)}"
      sql_api_url = CartoDB::SQLApi.with_username_api_key(source_username, granted_api_key, privacy, base_url: base_url)
                                   .export_table_url(source_dataset)
      Carto::ExternalSource.create!(
        visualization: visualization,
        import_url: sql_api_url,
        rows_counted: remote_table[:row_count],
        size: remote_table[:size],
        geometry_types: remote_table[:geometry_types],
        username: target_username
      )

      visualization
    end

    private

    def display_name(remote_visualization)
      remote_visualization[:display_name].presence || remote_visualization[:name]
    end
{'fraction_non_alphanumeric': 0.08534417068834138, 'fraction_numerical': 0.000508001016002032, 'mean_word_length': 3.261904761904762, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6528910', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1105, 'n_words': 196}