<commit_before>require 'rubygems'
require 'sinatra'
require 'json'
require 'mysql2'



host = ENV['MYSQL_PORT_3306_TCP_ADDR']

if host.to_s == ''
  client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "fbapp")
else
  # Connect to a MySQL server via a linked docker container 
  client = Mysql2::Client.new(:host => ENV['MYSQL_PORT_3306_TCP_ADDR'], 
                             :port => ENV['MYSQL_PORT_3306_TCP_PORT'], 
                             :password => ENV['MYSQL_ENV_MYSQL_ROOT_PASSWORD'],
                             :username => "root", 
                             :database => "fbapp")
end

before do
  puts '[Params]'
  p params
end

not_found do
	halt 402, {'Content-Type' => 'application/json'}, {'error' => 'Page not found'}
  # 'This is nowhere to be found.'
end

error 404 do
  'Not found'
end

before do
  headers "Content-Type" => "application/json; charset=utf8"
end

get '/' do
	redirect '/heartbeat'
end

get "/heartbeat" do
	return JSON.pretty_generate({
		"paths" => [
			"/heartbeat",
			"/species/:id?<params>",
			"/genera/:id?<params>",
			"/getfaoarea?<params>",
			"/faoareas/:id?<params>",
			"/getfooditems?<params>"
		]
	})
end

get '/species/?:id?/?' do
	id = params[:id]
 	limit = params[:limit] || 10
 	fields = params[:fields] || '*'
 	params.delete("limit")
 	params.delete("fields")

 	fields = check_fields(client, 'species', fields)
 	args = get_args(params)

	if id.nil?
		query = sprintf("SELECT %s FROM species %s limit %d", fields, args, limit)
	else
		query = sprintf("SELECT %s FROM species WHERE SpecCode = '%d' limit %d", fields, id.to_s, limit)
	end
	res = client.query(query, :as => :json)
	out = res.collect{ |row| row }
	err = get_error(out)
	data = { "count" => out.length, "returned" => out.length, "error" => err, "data" => out }
	return JSON.pretty_generate(data)
end

get '/genera/?:id?/?' do
	id = params[:id]
 	limit = params[:limit] || 10
 	fields = params[:fields] || '*'
 	params.delete("limit")
 	params.delete("fields")

 	fields = check_fields(client, 'genera', fields)
 	args = get_args(params)

	if id.nil?
		query = sprintf("SELECT %s FROM genera %s limit %d", fields, args, limit)
	else
		query = sprintf("SELECT %s FROM genera WHERE GenCode = '%d' limit %d", fields, id.to_s, limit)
	end
	res = client.query(query, :as => :json)
	out = res.collect{ |row| row }
	err = get_error(out)
	data = { "count" => out.length, "returned" => out.length, "error" => err, "data" => out }
	return JSON.pretty_generate(data)
end

get '/getfaoareas/?' do
	genus = params[:genus]
	species = params[:species]
	limit = params[:limit] || 10
	query = sprintf("SELECT s.SpecCode, s.Genus, s.Species, k.AreaCode, k.FAO, k.Note, t.status
							FROM species s JOIN faoareas t on s.SpecCode = t.SpecCode
							INNER JOIN faoarref k on t.AreaCode = k.AreaCode
							WHERE Genus = '%s' AND Species = '%s' limit %d", genus, species, limit)
	res = client.query(query, :as => :json)
	out = res.collect{ |row| row }
	err = get_error(out)
	data = { "count" => out.length, "returned" => out.length, "error" => err, "data" => out }
	return JSON.pretty_generate(data)
end

get '/faoareas/?:id?/?' do
	id = params[:id]
 	limit = params[:limit] || 10
 	fields = params[:fields] || '*'
 	params.delete("limit")
 	params.delete("fields")

 	fields = check_fields(client, 'faoareas', fields)
 	args = get_args(params)

	if id.nil?
		query = sprintf("SELECT %s FROM faoareas %s limit %d", fields, args, limit)
		count = get_count(client, 'faoareas', args)
	else
		query = sprintf("SELECT %s FROM faoareas WHERE AreaCode = '%d' limit %d", fields, id.to_s, limit)
		count = get_count(client, 'faoareas', sprintf("WHERE AreaCode = '%d'", id.to_s))
	end
	res = client.query(query, :as => :json)
	out = res.collect{ |row| row }
	err = get_error(out)
	data = { "count" => count, "returned" => out.length, "error" => err, "data" => out }
	return JSON.pretty_generate(data)
end

get '/fooditems/?' do
	genus = params[:genus]
	species = params[:species]
	limit = params[:limit] || 10
	query = sprintf("SELECT s.SpecCode, s.Genus, s.Species, t.FoodI, t.FoodII, t.FoodIII, t.PredatorStage
						FROM species s JOIN fooditems t on s.SpecCode = t.SpecCode
						WHERE Genus = '%s' AND Species = '%s' limit %d", genus, species, limit)
	res = client.query(query, :as => :json)
	out = res.collect{ |row| row }
	err = get_error(out)
	data = { "count" => out.length, "returned" => out.length, "error" => err, "data" => out }
	return JSON.pretty_generate(data)
end


# helpers
def get_error(x)
	if x.length == 0
		return "not found"
	else
		return nil
	end
end

def get_args(x)
	res = x.collect{ |row| "%s = '%s'" % row }
	if res.length == 0
		return ''
	else
		return "WHERE " + res.join(' AND ')
	end
end<commit_msg>changed fooditems path to just query on the fooditems table, #2<commit_after>require 'rubygems'
require 'sinatra'
require 'json'
require 'mysql2'



host = ENV['MYSQL_PORT_3306_TCP_ADDR']

if host.to_s == ''
  client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "fbapp")
else
  # Connect to a MySQL server via a linked docker container
  client = Mysql2::Client.new(:host => ENV['MYSQL_PORT_3306_TCP_ADDR'],
                             :port => ENV['MYSQL_PORT_3306_TCP_PORT'],
                             :password => ENV['MYSQL_ENV_MYSQL_ROOT_PASSWORD'],
                             :username => "root",
                             :database => "fbapp")
end

before do
  puts '[Params]'
  p params
end

not_found do
	halt 402, {'Content-Type' => 'application/json'}, {'error' => 'Page not found'}
  # 'This is nowhere to be found.'
end

error 404 do
  'Not found'
end

before do
  headers "Content-Type" => "application/json; charset=utf8"
end

get '/' do
	redirect '/heartbeat'
end

get "/heartbeat" do
	return JSON.pretty_generate({
		"paths" => [
			"/heartbeat",
			"/species/:id?<params>",
			"/genera/:id?<params>",
			"/faoareas/:id?<params>",
			"/fooditems?<params>"
		]
	})
end

get '/species/?:id?/?' do
	id = params[:id]
 	limit = params[:limit] || 10
 	fields = params[:fields] || '*'
 	params.delete("limit")
 	params.delete("fields")

 	fields = check_fields(client, 'species', fields)
 	args = get_args(params)

	if id.nil?
		query = sprintf("SELECT %s FROM species %s limit %d", fields, args, limit)
	else
		query = sprintf("SELECT %s FROM species WHERE SpecCode = '%d' limit %d", fields, id.to_s, limit)
	end
	res = client.query(query, :as => :json)
	out = res.collect{ |row| row }
	err = get_error(out)
	data = { "count" => out.length, "returned" => out.length, "error" => err, "data" => out }
	return JSON.pretty_generate(data)
end

get '/genera/?:id?/?' do
	id = params[:id]
 	limit = params[:limit] || 10
 	fields = params[:fields] || '*'
 	params.delete("limit")
 	params.delete("fields")

 	fields = check_fields(client, 'genera', fields)
 	args = get_args(params)

	if id.nil?
		query = sprintf("SELECT %s FROM genera %s limit %d", fields, args, limit)
	else
		query = sprintf("SELECT %s FROM genera WHERE GenCode = '%d' limit %d", fields, id.to_s, limit)
	end
	res = client.query(query, :as => :json)
	out = res.collect{ |row| row }
	err = get_error(out)
	data = { "count" => out.length, "returned" => out.length, "error" => err, "data" => out }
	return JSON.pretty_generate(data)
end

get '/faoareas/?:id?/?' do
	id = params[:id]
 	limit = params[:limit] || 10
 	fields = params[:fields] || '*'
 	params.delete("limit")
 	params.delete("fields")

 	fields = check_fields(client, 'faoareas', fields)
 	args = get_args(params)

	if id.nil?
		query = sprintf("SELECT %s FROM faoareas %s limit %d", fields, args, limit)
		count = get_count(client, 'faoareas', args)
	else
		query = sprintf("SELECT %s FROM faoareas WHERE AreaCode = '%d' limit %d", fields, id.to_s, limit)
		count = get_count(client, 'faoareas', sprintf("WHERE AreaCode = '%d'", id.to_s))
	end
	res = client.query(query, :as => :json)
	out = res.collect{ |row| row }
	err = get_error(out)
	data = { "count" => count, "returned" => out.length, "error" => err, "data" => out }
	return JSON.pretty_generate(data)
end

get '/fooditems/?' do
	limit = params[:limit] || 10
	fields = params[:fields] || '*'

	params.delete("limit")
 	params.delete("fields")

 	fields = check_fields(client, 'fooditems', fields)
 	args = get_args(params)

 	query = sprintf("SELECT %s FROM fooditems %s limit %d", fields, args, limit)
	count = get_count(client, 'faoareas', args)

	res = client.query(query, :as => :json)
	out = res.collect{ |row| row }
	err = get_error(out)
	data = { "count" => out.length, "returned" => out.length, "error" => err, "data" => out }
	return JSON.pretty_generate(data)
end

# helpers
def get_error(x)
	if x.length == 0
		return "not found"
	else
		return nil
	end
end

def get_args(x)
	res = x.collect{ |row| "%s = '%s'" % row }
	if res.length == 0
		return ''
	else
		return "WHERE " + res.join(' AND ')
	end
end
{'fraction_non_alphanumeric': 0.1705921938088829, 'fraction_numerical': 0.007514580529385375, 'mean_word_length': 3.594023699124163, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 81, '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': '6727116', 'n_tokens_mistral': 3451, 'n_tokens_neox': 3229, 'n_words': 880}