class Fluent::Plugin::MonitorAgentInput::APIHandler

def build_object(opts)

def build_object(opts)
  qs = opts[:query]
  if tag = qs['tag'.freeze].first
    # ?tag= to search an output plugin by match pattern
    if obj = @agent.plugin_info_by_tag(tag, opts)
      list = [obj]
    else
      list = []
    end
  elsif plugin_id = (qs['@id'.freeze].first || qs['id'.freeze].first)
    # ?@id= to search a plugin by 'id <plugin_id>' config param
    if obj = @agent.plugin_info_by_id(plugin_id, opts)
      list = [obj]
    else
      list = []
    end
  elsif plugin_type = (qs['@type'.freeze].first || qs['type'.freeze].first)
    # ?@type= to search plugins by 'type <type>' config param
    list = @agent.plugins_info_by_type(plugin_type, opts)
  else
    # otherwise show all plugins
    list = @agent.plugins_info_all(opts)
  end
  list
end

def build_option(req)

def build_option(req)
  qs = Hash.new { |_, _| [] }
  # parse ?=query string
  if req.query_string
    qs.merge!(CGI.parse(req.query_string))
  end
  # if ?debug=1 is set, set :with_debug_info for get_monitor_info
  # and :pretty_json for render_json_error
  opts = { query: qs }
  if qs['debug'.freeze].first
    opts[:with_debug_info] = true
    opts[:pretty_json] = true
  end
  if ivars = qs['with_ivars'.freeze].first
    opts[:ivars] = ivars.split(',')
  end
  if with_config = qs['with_config'.freeze].first
    opts[:with_config] = Fluent::Config.bool_value(with_config)
  else
    opts[:with_config] = @agent.include_config
  end
  if with_retry = qs['with_retry'.freeze].first
    opts[:with_retry] = Fluent::Config.bool_value(with_retry)
  else
    opts[:with_retry] = @agent.include_retry
  end
  opts
end

def config_json(req)

def config_json(req)
  obj = {
    'pid' => Process.pid,
    'ppid' => Process.ppid
  }.merge(@agent.fluentd_opts)
  opts = build_option(req)
  render_json(obj, pretty_json: opts[:pretty_json])
end

def config_ltsv(_req)

def config_ltsv(_req)
  obj = {
    'pid' => Process.pid,
    'ppid' => Process.ppid
  }.merge(@agent.fluentd_opts)
  render_ltsv([obj])
end

def initialize(agent)

def initialize(agent)
  @agent = agent
end

def plugins_json(req)

def plugins_json(req)
  opts = build_option(req)
  obj = build_object(opts)
  render_json({ 'plugins' => obj }, pretty_json: opts[:pretty_json])
end

def plugins_ltsv(req)

def plugins_ltsv(req)
  list = build_object(build_option(req))
  render_ltsv(list)
end

def render_error_json(code:, msg:, pretty_json: nil, **additional_params)

def render_error_json(code:, msg:, pretty_json: nil, **additional_params)
  resp = additional_params.merge('message' => msg)
  render_json(resp, code: code, pretty_json: pretty_json)
end

def render_json(obj, code: 200, pretty_json: nil)

def render_json(obj, code: 200, pretty_json: nil)
  body =
    if pretty_json
      JSON.pretty_generate(obj)
    else
      obj.to_json
    end
  [code, { 'Content-Type' => 'application/json' }, body]
end

def render_ltsv(obj, code: 200)

def render_ltsv(obj, code: 200)
  normalized = JSON.parse(obj.to_json)
  text = ''
  normalized.each do |hash|
    row = []
    hash.each do |k, v|
      if v.is_a?(Array)
        row << "#{k}:#{v.join(',')}"
      elsif v.is_a?(Hash)
        next
      else
        row << "#{k}:#{v}"
      end
    end
    text << row.join("\t") << "\n"
  end
  [code, { 'Content-Type' => 'text/plain' }, text]
end