<commit_before>require 'conceptql/query'
require 'conceptql/fake_grapher'
require 'sequelizer'
require 'digest'
require 'json'

def graph_it(statement, file)
  ConceptQL::FakeGrapher.new(suffix: 'png').graph_it(statement, file)
end

def dejson(str)
  parsed = JSON.parse(str)
  # We've got a problem where JavaScripts JSON.stringify seems to turn an array
  # into a hash with keys of "0", "1", etc
  # I want a real array, so we'll test for this situation and return an array
  # if we find it
  if parsed.respond_to?(:keys) && parsed.keys.all? { |k| k =~ /^\d+$/ }
    parsed = parsed.keys.map(&:to_i).sort.map { |k| parsed[k.to_s] }
  end
  parsed
end

include Sequelizer
get '/' do
  @statements = Pathname.new('statements').children.map do |dir|
    next unless dir.directory? && !dir.children.empty?
    dir.children.reject { |f| f.basename.to_s =~ /^\./ }.map do |f|
      description = File.readlines(f).reject { |l| l !~ /^#/ }.map { |l| l.gsub('#', '').chomp.strip }.first
      [f.to_s, description]
    end
  end.flatten(1).compact.sort_by(&:last)
  @statements = Hash[@statements]
  haml :index
end

get '/statements.json' do
  puts params
  file_path = params[:path]
  hash = eval(File.read(file_path))
  { statement: hash }.to_json
end

post '/api/v0/sql' do
  statement = dejson(request.body.read)
  sql = begin
    ConceptQL::Query.new(db, statement).sql
  rescue LoadError
    "One of the nodes in your statement appears to be experimental.  Cannot generate SQL statement."
  rescue
    puts $!.message
    puts $!.backtrace.join("\n")
    "The statement you submitted generated an unexpected error.  Sorry.  Please try modifying your statement."
  end
  { sql: sql }.to_json
end

post '/api/v0/diagram' do
  statement = dejson(request.body.read)
  digest = Digest::SHA256.hexdigest statement.to_s
  output_file = Pathname.new('public') + digest
  graph_it(statement, output_file.to_s)
  { img_src: "#{digest}.png" }.to_json
end

post '/to_yaml' do
  statement = dejson(request.body.read)
  { yaml: statement.to_yaml }.to_json
end
<commit_msg>Go back to using original API for ConceptQL::Query<commit_after>require 'conceptql/query'
require 'conceptql/fake_grapher'
require 'sequelizer'
require 'digest'
require 'json'

def graph_it(statement, file)
  ConceptQL::FakeGrapher.new(suffix: 'png').graph_it(statement, file)
end

def dejson(str)
  parsed = JSON.parse(str)
  # We've got a problem where JavaScripts JSON.stringify seems to turn an array
  # into a hash with keys of "0", "1", etc
  # I want a real array, so we'll test for this situation and return an array
  # if we find it
  if parsed.respond_to?(:keys) && parsed.keys.all? { |k| k =~ /^\d+$/ }
    parsed = parsed.keys.map(&:to_i).sort.map { |k| parsed[k.to_s] }
  end
  parsed
end

include Sequelizer
get '/' do
  @statements = Pathname.new('statements').children.map do |dir|
    next unless dir.directory? && !dir.children.empty?
    dir.children.reject { |f| f.basename.to_s =~ /^\./ }.map do |f|
      description = File.readlines(f).reject { |l| l !~ /^#/ }.map { |l| l.gsub('#', '').chomp.strip }.first
      [f.to_s, description]
    end
  end.flatten(1).compact.sort_by(&:last)
  @statements = Hash[@statements]
  haml :index
end

get '/statements.json' do
  puts params
  file_path = params[:path]
  hash = eval(File.read(file_path))
  { statement: hash }.to_json
end

post '/api/v0/sql' do
  statement = dejson(request.body.read)
  sql = begin
    ConceptQL::Query.new(db, statement).query.sql
  rescue LoadError
    "One of the nodes in your statement appears to be experimental.  Cannot generate SQL statement."
  rescue
    puts $!.message
    puts $!.backtrace.join("\n")
    "The statement you submitted generated an unexpected error.  Sorry.  Please try modifying your statement."
  end
  { sql: sql }.to_json
end

post '/api/v0/diagram' do
  statement = dejson(request.body.read)
  digest = Digest::SHA256.hexdigest statement.to_s
  output_file = Pathname.new('public') + digest
  graph_it(statement, output_file.to_s)
  { img_src: "#{digest}.png" }.to_json
end

post '/to_yaml' do
  statement = dejson(request.body.read)
  { yaml: statement.to_yaml }.to_json
end

{'fraction_non_alphanumeric': 0.13949579831932774, 'fraction_numerical': 0.0038415366146458585, 'mean_word_length': 3.7180067950169877, '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': '1959846', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1466, 'n_words': 407}