<commit_before>class Api::SettingsController < ApplicationController
  before_action :login_required
  before_action :find_fluentd
  before_action :set_config
  before_action :set_section, only: [:show, :update, :destroy]
  helper_method :element_id

  def index
    respond_to do |format|
      format.json
    end
  end

  def update
    coming = Fluent::Config::V1Parser.parse(params[:content], @fluentd.config_file)
    current = @section
    index = @config.elements.index current
    unless index
      render_404
      return
    end
    @config.elements[index] = coming.elements.first
    @config.write_to_file
    redirect_to api_setting_path(id: element_id(coming.elements.first))
  end

  def destroy
    unless @config.elements.index(@section)
      render_404
      return
    end
    @config.elements.delete @section
    @config.write_to_file
    head :no_content # 204
  end

  private

  def set_config
    @config = Fluentd::Setting::Config.new(@fluentd.config_file)
  end

  def set_section
    @section = @config.elements.find do |elm|
      element_id(elm) == params[:id]
    end
  end

  def element_id(element)
    index = @config.elements.index(element)
    "#{"%06d" % index}#{Digest::MD5.hexdigest(element.to_s)}"
  end

  def render_404
    render nothing: true, status: 404
  end
end<commit_msg>Fix element_id to support label<commit_after>class Api::SettingsController < ApplicationController
  before_action :login_required
  before_action :find_fluentd
  before_action :set_config
  before_action :set_section, only: [:show, :update, :destroy]
  helper_method :element_id

  def index
    respond_to do |format|
      format.json
    end
  end

  def update
    coming = Fluent::Config::V1Parser.parse(params[:content], @fluentd.config_file)
    current = @section
    index = @config.elements.index current
    unless index
      render_404
      return
    end
    @config.elements[index] = coming.elements.first
    @config.write_to_file
    redirect_to api_setting_path(id: element_id(coming.elements.first))
  end

  def destroy
    unless @config.elements.index(@section)
      render_404
      return
    end
    @config.elements.delete @section
    @config.write_to_file
    head :no_content # 204
  end

  private

  def set_config
    @config = Fluentd::Setting::Config.new(@fluentd.config_file)
  end

  def set_section
    id = params[:id]
    return unless id
    label_name = id.slice(/\A(sources|filters|matches):.+/)[1]
    elements = @config.group_by_label.dig(label_name, element_type)
    @section = elements.find do |elm|
      element_id(elm) == id
    end
  end

  def element_id(label_name, element)
    element_type = case element.name
                   when "source"
                     :sources
                   when "filter"
                     :filters
                   when "match"
                     :matches
                   end
    elements = @config.group_by_label.dig(label_name, element_type)
    index = elements.index(element)
    "#{label_name}:#{"%06d" % index}#{Digest::MD5.hexdigest(element.to_s)}"
  end

  def render_404
    render nothing: true, status: 404
  end
end
{'fraction_non_alphanumeric': 0.0942258883248731, 'fraction_numerical': 0.012373096446700508, 'mean_word_length': 2.9511278195488724, 'pattern_counts': {'":': 0, '<': 5, '<?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': '401075', 'n_tokens_mistral': 1032, 'n_tokens_neox': 985, 'n_words': 216}