<commit_before>require 'sinatra/base'
require 'json'
require 'cgi'

class Optica < Sinatra::Base
  configure :production, :development do
    enable :logging
  end

  get '/' do
    return get_nodes(request, true)
  end

  # endpoint for fab usage
  get '/roles' do
    return get_nodes(request, false)
  end

  def get_nodes(request, is_full_node)
    params = CGI::parse(request.query_string)

    # include only those nodes that match passed-in parameters
    examined = 0
    to_return = {}
    begin
      nodes = settings.store.nodes
    rescue
      halt(503)
    end

    nodes.each do |node, properties|
      examined += 1
      included = true

      params.each do |param, values|
        values.each do |value|
          if not properties.include? param
            included = false
          elsif properties[param].nil?
            included = false
          elsif properties[param].is_a? String
            included = false unless properties[param].match value
          elsif properties[param].is_a? Array
            included = false unless properties[param].include? value
          elsif properties[param].class == TrueClass
            included = false unless ['true', 'True', '1'].include? value
          elsif properties[param].class == FalseClass
            included = false unless ['false', 'False', '0'].include? value
          end
        end
      end

      if included
        to_return[node] = is_full_node ? properties : properties.slice(:role, :id, :hostname) 
      end
    end

    content_type 'application/json', :charset => 'utf-8'
    result = {'examined'=>examined, 'returned'=>to_return.count, 'nodes'=>to_return}
    return result.to_json
  end

  post '/' do
    begin
      data = JSON.parse request.body.read
    rescue JSON::ParserError
      data = {}
    end

    ip = data['ip']

    # check the node ip? disabled by false and nil
    case settings.ip_check
    when :direct
      halt(403) unless ip == request.ip<commit_msg>[fab] refactor to use fields_to_include<commit_after>require 'sinatra/base'
require 'json'
require 'cgi'

class Optica < Sinatra::Base
  configure :production, :development do
    enable :logging
  end

  get '/' do
    return get_nodes(request)
  end

  # endpoint for fab usage
  get '/roles' do
    # keys must be strings
    return get_nodes(request, ['role', 'id', 'hostname'])
  end

  def get_nodes(request, fields_to_include=nil)
    params = CGI::parse(request.query_string)

    # include only those nodes that match passed-in parameters
    examined = 0
    to_return = {}
    begin
      nodes = settings.store.nodes
    rescue
      halt(503)
    end

    nodes.each do |node, properties|
      examined += 1
      included = true

      params.each do |param, values|
        values.each do |value|
          if not properties.include? param
            included = false
          elsif properties[param].nil?
            included = false
          elsif properties[param].is_a? String
            included = false unless properties[param].match value
          elsif properties[param].is_a? Array
            included = false unless properties[param].include? value
          elsif properties[param].class == TrueClass
            included = false unless ['true', 'True', '1'].include? value
          elsif properties[param].class == FalseClass
            included = false unless ['false', 'False', '0'].include? value
          end
        end
      end

      if included
        # return full list if fields_to_include is nil. Otherwise return only keys
        # listed in fields_to_include. Not using slice because not a rails app. 
        to_return[node] = fields_to_include.nil? ? properties : properties
          .select { |key, _value| fields_to_include.include? key }
      end
    end

    content_type 'application/json', :charset => 'utf-8'
    result = {'examined'=>examined, 'returned'=>to_return.count, 'nodes'=>to_return}
    return result.to_json
  end

  post '/' do
    begin
      data = JSON.parse request.body.read
    rescue JSON::ParserError
      data = {}
    end

    ip = data['ip']

    # check the node ip? disabled by false and nil
    case settings.ip_check
    when :direct
      halt(403) unless ip == request.ip
{'fraction_non_alphanumeric': 0.09566446748350613, 'fraction_numerical': 0.0051837888784165885, 'mean_word_length': 2.4206285253827557, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '697863', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1182, 'n_words': 371}