<commit_before># encoding utf-8

module Carto
  module Tracking
    module Formats
      class Segment
        def initialize(user: nil, visualization: nil, hash: {})
          @user = user
          @visualization = visualization
          @connection = hash[:connection]
          @origin = hash[:origin]
          @page = hash[:page]
          @quota_overage = hash[:quota_overage]
          @mapviews = hash[:mapviews]
          @action = hash[:action]
        end

        def to_hash
          properties = event_properties

          properties.merge!(user_properties) if @user
          properties.merge!(visualization_properties) if @visualization
          properties.merge!(connection_properties) if @connection

          properties[:page] = @page if @page
          properties[:quota_overage] = @quota_overage if @quota_overage
          properties[:mapviews] = @mapviews if @mapviews
          properties[:action] = @action if @action

          properties
        end

        private

        def visualization_properties
          created_at = @visualization.created_at
          lifetime_in_days_with_decimals = days_with_decimals(now - created_at)

          properties = {
            vis_id: @visualization.id,
            privacy: @visualization.privacy,
            type: @visualization.type,
            object_created_at: created_at,
            lifetime: lifetime_in_days_with_decimals
          }

          properties[:origin] = @origin if @origin

          properties
        end

        def user_properties
          user_created_at = @user.created_at
          user_age_in_days_with_decimals = days_with_decimals(now - user_created_at)

          {
            username: @user.username,
            email: @user.email,
            plan: @user.account_type,
            user_active_for: user_age_in_days_with_decimals,
            user_created_at: user_created_at,
            organization: @user.organization_user? ? @user.organization.name : nil
          }
        end

        def connection_properties
          properties = {
            data_from: @connection[:data_from],
            imported_from: @connection[:imported_from],
            sync: @connection[:sync] || false,
            file_type: @connection[:file_type]
          }

          properties
        end

        def trending_map_properties
          {
            map_id: @visualization.id,
            map_name: @visualization.name,
            mapviews: @views
          }
        end

        def map_liking_properties
          visualization_user = @visualization.user

          {
            action: @action,
            vis_id: @visualization.id,
            vis_name: @visualization.name,
            vis_type: @visualization.type == 'derived' ? 'map' : 'dataset',
            vis_author: visualization_user.username,
            vis_author_email: visualization_user.email,
            vis_author_id: visualization_user.id
          }
        end

        def event_properties
          { event_origin: 'Editor', creation_time: now }
        end
<commit_msg>use right global<commit_after># encoding utf-8

module Carto
  module Tracking
    module Formats
      class Segment
        def initialize(user: nil, visualization: nil, hash: {})
          @user = user
          @visualization = visualization
          @connection = hash[:connection]
          @origin = hash[:origin]
          @page = hash[:page]
          @quota_overage = hash[:quota_overage]
          @mapviews = hash[:mapviews]
          @action = hash[:action]
        end

        def to_hash
          properties = event_properties

          properties.merge!(user_properties) if @user
          properties.merge!(visualization_properties) if @visualization
          properties.merge!(connection_properties) if @connection

          properties[:page] = @page if @page
          properties[:quota_overage] = @quota_overage if @quota_overage
          properties[:mapviews] = @mapviews if @mapviews
          properties[:action] = @action if @action

          properties
        end

        private

        def visualization_properties
          created_at = @visualization.created_at
          lifetime_in_days_with_decimals = days_with_decimals(now - created_at)

          properties = {
            vis_id: @visualization.id,
            privacy: @visualization.privacy,
            type: @visualization.type,
            object_created_at: created_at,
            lifetime: lifetime_in_days_with_decimals
          }

          properties[:origin] = @origin if @origin

          properties
        end

        def user_properties
          user_created_at = @user.created_at
          user_age_in_days_with_decimals = days_with_decimals(now - user_created_at)

          {
            username: @user.username,
            email: @user.email,
            plan: @user.account_type,
            user_active_for: user_age_in_days_with_decimals,
            user_created_at: user_created_at,
            organization: @user.organization_user? ? @user.organization.name : nil
          }
        end

        def connection_properties
          properties = {
            data_from: @connection[:data_from],
            imported_from: @connection[:imported_from],
            sync: @connection[:sync] || false,
            file_type: @connection[:file_type]
          }

          properties
        end

        def trending_map_properties
          {
            map_id: @visualization.id,
            map_name: @visualization.name,
            mapviews: @mapviews
          }
        end

        def map_liking_properties
          visualization_user = @visualization.user

          {
            action: @action,
            vis_id: @visualization.id,
            vis_name: @visualization.name,
            vis_type: @visualization.type == 'derived' ? 'map' : 'dataset',
            vis_author: visualization_user.username,
            vis_author_email: visualization_user.email,
            vis_author_id: visualization_user.id
          }
        end

        def event_properties
          { event_origin: 'Editor', creation_time: now }
        end

{'fraction_non_alphanumeric': 0.07742355237475602, 'fraction_numerical': 0.00032530904359141186, 'mean_word_length': 1.901840490797546, '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': '3853093', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1609, 'n_words': 330}