<commit_before>module Carto
  module Api
    class WidgetsController < ::Api::ApplicationController
      include Carto::ControllerHelper

      ssl_required :show, :create, :update, :destroy

      before_filter :load_parameters
      before_filter :load_widget, only: [:show, :update, :destroy]

      rescue_from Carto::LoadError, with: :rescue_from_carto_error
      rescue_from Carto::UnauthorizedError, with: :rescue_from_carto_error
      rescue_from Carto::UnprocesableEntityError, with: :rescue_from_carto_error

      def show
        render_jsonp(WidgetPresenter.new(@widget).to_poro)
      end

      def create
        order_param = params[:order]
        order = if order_param.present?
                  order_param
                else
                  @map.visualization.widgets.count
                end
        widget = Carto::Widget.new(
          layer_id: @layer_id,
          order: order,
          type: params[:type],
          title: params[:title],
          options: params[:options],
          style: params[:style],
          source_id: source_id_from_params)
        widget.save!

        Carto::Tracking::Events::CreatedWidget.new(user_id: current_viewer.id,
                                                   visualization_id: @layer.visualization.id,
                                                   widget_id: widget.id).report

        render_jsonp(WidgetPresenter.new(widget).to_poro, 201)
      rescue ActiveRecord::RecordInvalid
        render json: { errors: widget.errors }, status: 422
      rescue => e
        CartoDB::Logger.error(exception: e, message: "Error creating widget", widget: (widget ? widget : 'not created'))
        render json: { errors: e.message }, status: 500
      end

      def update
        update_params = params.slice(:order, :type, :title)
        update_params[:source_id] = source_id_from_params if source_id_from_params
        @widget.update_attributes(update_params)
        @widget.options = params[:options]
        @widget.style = params[:style]
        @widget.save!

        Carto::Tracking::Events::ModifiedWidget.new(user_id: current_viewer.id,
                                                    visualization_id: @layer.visualization.id,
                                                    widget_id: @widget.id).report

        render_jsonp(WidgetPresenter.new(@widget).to_poro)
      rescue => e
        CartoDB::Logger.error(exception: e, message: "Error updating widget", widget: @widget)
        render json: { errors: e.message }, status: 500
      end

      def destroy
        @widget.destroy

        Carto::Tracking::Events::DeletedWidget.new(user_id: current_viewer.id,
                                                   visualization_id: @layer.visualization.id,
                                                   widget_id: @widget.id).report

        render_jsonp(WidgetPresenter.new(@widget).to_poro)
      rescue => e
        CartoDB::Logger.error(exception: e, message: "Error destroying widget", widget: @widget)
        render json: { errors: e.message }, status: 500
      end

      private

      def load_parameters
        @map_id = params[:map_id]
        @map = Carto::Map.where(id: @map_id).first
        raise LoadError.new("Map not found: #{@map_id}") unless @map
        raise Carto::UnauthorizedError.new("Not authorized for map #{@map.id}") unless @map.writable_by_user?(current_user)

        @layer_id = params[:map_layer_id]
        payload_layer_id = params['layer_id']

        if [payload_layer_id, @layer_id].compact.uniq.length >= 2
          raise UnprocesableEntityError.new("URL layer id (#{@layer_id}) and payload layer id (#{payload_layer_id}) don't match")
        end

        @layer = Carto::Layer.where(id: @layer_id).first
        raise LoadError.new("Layer not found: #{@layer_id}") unless @layer

        raise UnprocesableEntityError.new("Layer #{@layer_id} doesn't belong to map #{@map_id}") unless @map.contains_layer?(@layer)

        @widget_id = params[:id]<commit_msg>leave only creation tracking<commit_after>module Carto
  module Api
    class WidgetsController < ::Api::ApplicationController
      include Carto::ControllerHelper

      ssl_required :show, :create, :update, :destroy

      before_filter :load_parameters
      before_filter :load_widget, only: [:show, :update, :destroy]

      rescue_from Carto::LoadError, with: :rescue_from_carto_error
      rescue_from Carto::UnauthorizedError, with: :rescue_from_carto_error
      rescue_from Carto::UnprocesableEntityError, with: :rescue_from_carto_error

      def show
        render_jsonp(WidgetPresenter.new(@widget).to_poro)
      end

      def create
        order_param = params[:order]
        order = if order_param.present?
                  order_param
                else
                  @map.visualization.widgets.count
                end
        widget = Carto::Widget.new(
          layer_id: @layer_id,
          order: order,
          type: params[:type],
          title: params[:title],
          options: params[:options],
          style: params[:style],
          source_id: source_id_from_params)
        widget.save!

        Carto::Tracking::Events::CreatedWidget.new(current_viewer.id,
                                                   user_id: current_viewer.id,
                                                   visualization_id: @layer.visualization.id,
                                                   widget_id: widget.id).report

        render_jsonp(WidgetPresenter.new(widget).to_poro, 201)
      rescue ActiveRecord::RecordInvalid
        render json: { errors: widget.errors }, status: 422
      rescue => e
        CartoDB::Logger.error(exception: e, message: "Error creating widget", widget: (widget ? widget : 'not created'))
        render json: { errors: e.message }, status: 500
      end

      def update
        update_params = params.slice(:order, :type, :title)
        update_params[:source_id] = source_id_from_params if source_id_from_params
        @widget.update_attributes(update_params)
        @widget.options = params[:options]
        @widget.style = params[:style]
        @widget.save!

        render_jsonp(WidgetPresenter.new(@widget).to_poro)
      rescue => e
        CartoDB::Logger.error(exception: e, message: "Error updating widget", widget: @widget)
        render json: { errors: e.message }, status: 500
      end

      def destroy
        @widget.destroy

        render_jsonp(WidgetPresenter.new(@widget).to_poro)
      rescue => e
        CartoDB::Logger.error(exception: e, message: "Error destroying widget", widget: @widget)
        render json: { errors: e.message }, status: 500
      end

      private

      def load_parameters
        @map_id = params[:map_id]
        @map = Carto::Map.where(id: @map_id).first
        raise LoadError.new("Map not found: #{@map_id}") unless @map
        raise Carto::UnauthorizedError.new("Not authorized for map #{@map.id}") unless @map.writable_by_user?(current_user)

        @layer_id = params[:map_layer_id]
        payload_layer_id = params['layer_id']

        if [payload_layer_id, @layer_id].compact.uniq.length >= 2
          raise UnprocesableEntityError.new("URL layer id (#{@layer_id}) and payload layer id (#{payload_layer_id}) don't match")
        end

        @layer = Carto::Layer.where(id: @layer_id).first
        raise LoadError.new("Layer not found: #{@layer_id}") unless @layer

        raise UnprocesableEntityError.new("Layer #{@layer_id} doesn't belong to map #{@map_id}") unless @map.contains_layer?(@layer)

        @widget_id = params[:id]
{'fraction_non_alphanumeric': 0.10439127004996056, 'fraction_numerical': 0.004207204838285564, 'mean_word_length': 2.3644405130473243, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6807727', 'n_tokens_mistral': 2175, 'n_tokens_neox': 2105, 'n_words': 461}