<commit_before>require_dependency 'carto/api/layer_vizjson_adapter'
require_dependency 'carto/api/infowindow_migrator'
require_dependency 'cartodb/redis_vizjson_cache'
require_dependency 'carto/named_maps/template'

module Carto
  module Api
    module ApiTemplates
      API_TEMPLATE_PUBLIC = 'public'.freeze
      API_TEMPLATE_PRIVATE = 'private'.freeze

      def api_templates_type(options)
        options.fetch(:https_request, false) ? API_TEMPLATE_PRIVATE : API_TEMPLATE_PUBLIC
      end
    end

    module DisplayVizjsonMode
      def display_named_map?(visualization, forced_privacy_version)
        forced_privacy_version != :force_anonymous && (visualization.retrieve_named_map? || forced_privacy_version == :force_named)
      end
    end

    class VizJSON3Presenter
      include ApiTemplates
      include DisplayVizjsonMode

      def initialize(visualization,
                     redis_vizjson_cache = CartoDB::Visualization::RedisVizjsonCache.new($tables_metadata, 3))
        @visualization = visualization
        @redis_vizjson_cache = redis_vizjson_cache
      end

      def to_vizjson(https_request: false, vector: false)
        generate_vizjson(https_request: https_request, vector: vector, forced_privacy_version: nil)
      end

      def to_named_map_vizjson(https_request: false, vector: false)
        generate_vizjson(https_request: https_request, vector: vector, forced_privacy_version: :force_named)
      end

      def to_anonymous_map_vizjson(https_request: false, vector: false)
        generate_vizjson(https_request: https_request, vector: vector, forced_privacy_version: :force_anonymous)
      end

      private

      def generate_vizjson(https_request:, vector:, forced_privacy_version:)
        https_request ||= false
        vector ||= false
        version = case forced_privacy_version
                  when :force_named
                    '3n'
                  when :force_anonymous
                    '3a'
                  else
                    3
                  end

        vizjson = if @redis_vizjson_cache
                    @redis_vizjson_cache.cached(@visualization.id, https_request, version) do
                      calculate_vizjson(https_request: https_request, vector: vector, forced_privacy_version: forced_privacy_version)
                    end
                  else
                    calculate_vizjson(https_request: https_request, vector: vector, forced_privacy_version: forced_privacy_version)
                  end

        vizjson[:vector] = vector

        vizjson
      end

      VIZJSON_VERSION = '3.0.0'.freeze

      def default_options
        user = @visualization.user

        {
          full: false,
          visualization_id: @visualization.id,
          https_request: false,
          attributions: @visualization.attributions_from_derived_visualizations,
          user_name: user.username,
          user: user,
          vector: false
        }
      end

      def calculate_vizjson(https_request: false,
                            vector: false,
                            forced_privacy_version: nil,
                            updated_at: map.viz_updated_at)
        options = default_options.merge(https_request: https_request, vector: vector)

        user = @visualization.user
        map = @visualization.map

        vizjson = {
          id:             @visualization.id,
          version:        VIZJSON_VERSION,
          title:          @visualization.qualified_name(user),
          likes:          @visualization.likes.count,
          description:    html_safe(@visualization.description),
          scrollwheel:    map.scrollwheel,
          legends:        map.legends,
          map_provider:   map.provider,
          bounds:         bounds_from(map),
          center:         map.center,
          zoom:           map.zoom,
          updated_at:     updated_at,
          layers:         layers_vizjson(options, forced_privacy_version),
          overlays:       @visualization.overlays.map { |o| Carto::Api::OverlayPresenter.new(o).to_vizjson },
          prev:           @visualization.prev_id,
          next:           @visualization.next_id,
          transition_options: @visualization.transition_options,
          widgets:        widgets_vizjson,
          datasource:     datasource_vizjson(options, forced_privacy_version),
          user:           user_info_vizjson(user)
        }

        visualization_analyses = @visualization.analyses
        vizjson[:analyses] = if display_named_map?(@visualization, forced_privacy_version)
                               visualization_analyses.map { |a| named_map_analysis_json(a) }
                             else
                               visualization_analyses.map(&:analysis_definition)
                             end

        auth_tokens = @visualization.needed_auth_tokens
        vizjson[:auth_tokens] = auth_tokens unless auth_tokens.empty?

        parent = @visualization.parent
        if parent
          vizjson[:title] = parent.qualified_name(user)
          vizjson[:description] = html_safe(parent.description)
        end

        vizjson
      end
<commit_msg>Revert "add optional param for updated_at to vizjson"<commit_after>require_dependency 'carto/api/layer_vizjson_adapter'
require_dependency 'carto/api/infowindow_migrator'
require_dependency 'cartodb/redis_vizjson_cache'
require_dependency 'carto/named_maps/template'

module Carto
  module Api
    module ApiTemplates
      API_TEMPLATE_PUBLIC = 'public'.freeze
      API_TEMPLATE_PRIVATE = 'private'.freeze

      def api_templates_type(options)
        options.fetch(:https_request, false) ? API_TEMPLATE_PRIVATE : API_TEMPLATE_PUBLIC
      end
    end

    module DisplayVizjsonMode
      def display_named_map?(visualization, forced_privacy_version)
        forced_privacy_version != :force_anonymous && (visualization.retrieve_named_map? || forced_privacy_version == :force_named)
      end
    end

    class VizJSON3Presenter
      include ApiTemplates
      include DisplayVizjsonMode

      def initialize(visualization,
                     redis_vizjson_cache = CartoDB::Visualization::RedisVizjsonCache.new($tables_metadata, 3))
        @visualization = visualization
        @redis_vizjson_cache = redis_vizjson_cache
      end

      def to_vizjson(https_request: false, vector: false)
        generate_vizjson(https_request: https_request, vector: vector, forced_privacy_version: nil)
      end

      def to_named_map_vizjson(https_request: false, vector: false)
        generate_vizjson(https_request: https_request, vector: vector, forced_privacy_version: :force_named)
      end

      def to_anonymous_map_vizjson(https_request: false, vector: false)
        generate_vizjson(https_request: https_request, vector: vector, forced_privacy_version: :force_anonymous)
      end

      private

      def generate_vizjson(https_request:, vector:, forced_privacy_version:)
        https_request ||= false
        vector ||= false
        version = case forced_privacy_version
                  when :force_named
                    '3n'
                  when :force_anonymous
                    '3a'
                  else
                    3
                  end

        vizjson = if @redis_vizjson_cache
                    @redis_vizjson_cache.cached(@visualization.id, https_request, version) do
                      calculate_vizjson(https_request: https_request, vector: vector, forced_privacy_version: forced_privacy_version)
                    end
                  else
                    calculate_vizjson(https_request: https_request, vector: vector, forced_privacy_version: forced_privacy_version)
                  end

        vizjson[:vector] = vector

        vizjson
      end

      VIZJSON_VERSION = '3.0.0'.freeze

      def default_options
        user = @visualization.user

        {
          full: false,
          visualization_id: @visualization.id,
          https_request: false,
          attributions: @visualization.attributions_from_derived_visualizations,
          user_name: user.username,
          user: user,
          vector: false
        }
      end

      def calculate_vizjson(https_request: false, vector: false, forced_privacy_version: nil)
        options = default_options.merge(https_request: https_request, vector: vector)

        user = @visualization.user
        map = @visualization.map

        vizjson = {
          id:             @visualization.id,
          version:        VIZJSON_VERSION,
          title:          @visualization.qualified_name(user),
          likes:          @visualization.likes.count,
          description:    html_safe(@visualization.description),
          scrollwheel:    map.scrollwheel,
          legends:        map.legends,
          map_provider:   map.provider,
          bounds:         bounds_from(map),
          center:         map.center,
          zoom:           map.zoom,
          updated_at:     @visualization.mapcapped? ? @visualization.latest_mapcap.created_at : map.viz_updated_at,
          layers:         layers_vizjson(options, forced_privacy_version),
          overlays:       @visualization.overlays.map { |o| Carto::Api::OverlayPresenter.new(o).to_vizjson },
          prev:           @visualization.prev_id,
          next:           @visualization.next_id,
          transition_options: @visualization.transition_options,
          widgets:        widgets_vizjson,
          datasource:     datasource_vizjson(options, forced_privacy_version),
          user:           user_info_vizjson(user)
        }

        visualization_analyses = @visualization.analyses
        vizjson[:analyses] = if display_named_map?(@visualization, forced_privacy_version)
                               visualization_analyses.map { |a| named_map_analysis_json(a) }
                             else
                               visualization_analyses.map(&:analysis_definition)
                             end

        auth_tokens = @visualization.needed_auth_tokens
        vizjson[:auth_tokens] = auth_tokens unless auth_tokens.empty?

        parent = @visualization.parent
        if parent
          vizjson[:title] = parent.qualified_name(user)
          vizjson[:description] = html_safe(parent.description)
        end

        vizjson
      end

{'fraction_non_alphanumeric': 0.06818620822870389, 'fraction_numerical': 0.0015452965037666602, 'mean_word_length': 2.1871345029239766, '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': '4324658', 'n_tokens_mistral': 2999, 'n_tokens_neox': 2839, 'n_words': 507}