<commit_before># Required config/settings.yml > backend > settings parameters: 
# #  url - the entrypoint for the descartes backend
# #  origin - the origin for the data (BETA)

#require 'open-uri'
require 'net/http'
class Backend::Descartes < Backend::GenericBackend

        def initialize params={}
		@alias = params[:alias] || self.class.name.split("::").last
                @base_url = params[:url]
                raise Backend::Error, "Must provide a url value" if @base_url.nil?
                @origin = params[:origin]
                raise Backend::Error, "Must provide an origin value" if @origin.nil?
        end

	# Descartes don't need no storage
        def get_metrics_list
		return []
        end

	# Descartes is dynamic, yo
	def search_metric_list q
		uri = "#{@base_url}/simple/search?origin=#{@origin}&q=#{q}"
		result = get_json uri
		result.map{|x| "#{@alias}:#{x}"}
	end

        def get_metric m, start, stop, step
		query = []
		query << "start=#{start - 200}" 
		query << "end=#{stop}"
		query << "interval=#{step}"
		query << "origin=#{@origin}"

		replace = [ ["/", "%2f"], ["_","%5f"]]
		replace.each { |r| m.gsub!(r[0], r[1]) } # TODO make metrics not have to be manhandled back into quasi-encoded status
		
		query_string = "?" + query.join("&")
	
		uri = "#{@base_url}/interpolated/#{m}#{query_string}"

		puts uri
		begin
			data = get_json uri
		rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, EOFError => e
			raise Backend::Error, "Error retreiving descartes metric #{m}: #{e} (full_url: #{@base_url}/interpolated/#{m}#{query_string})"
		end

		if (data.is_a? Hash) then
			if data[:error] then
				raise Backend::Error, "Descartes Exception raised: #{data[:error]}. uri: #{uri}"
			end
		end
			
		metric = []
		data.each do |node|
			metric << {x: node[0], y: node[1]}
		end

		padded = []
		(start..stop).step(step).each do |i|
			points = metric.select{|p| p[:x].between?(i, i+step-1)}
			if points.length == 1 then
				padded << points.first
			elsif points.length > 1 then
				binding.pry
			elsif points.length == 0 then
				padded << {x: i, y: (0.0/0.0)}
			end<commit_msg>remove case statement that shouldn't happen<commit_after># Required config/settings.yml > backend > settings parameters: 
# #  url - the entrypoint for the descartes backend
# #  origin - the origin for the data (BETA)

#require 'open-uri'
require 'net/http'
class Backend::Descartes < Backend::GenericBackend

        def initialize params={}
		@alias = params[:alias] || self.class.name.split("::").last
                @base_url = params[:url]
                raise Backend::Error, "Must provide a url value" if @base_url.nil?
                @origin = params[:origin]
                raise Backend::Error, "Must provide an origin value" if @origin.nil?
        end

	# Descartes don't need no storage
        def get_metrics_list
		return []
        end

	# Descartes is dynamic, yo
	def search_metric_list q
		uri = "#{@base_url}/simple/search?origin=#{@origin}&q=#{q}"
		result = get_json uri
		result.map{|x| "#{@alias}:#{x}"}
	end

        def get_metric m, start, stop, step
		query = []
		query << "start=#{start - 200}" 
		query << "end=#{stop}"
		query << "interval=#{step}"
		query << "origin=#{@origin}"

		replace = [ ["/", "%2f"], ["_","%5f"]]
		replace.each { |r| m.gsub!(r[0], r[1]) } # TODO make metrics not have to be manhandled back into quasi-encoded status
		
		query_string = "?" + query.join("&")
	
		uri = "#{@base_url}/interpolated/#{m}#{query_string}"

		puts uri
		begin
			data = get_json uri
		rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, EOFError => e
			raise Backend::Error, "Error retreiving descartes metric #{m}: #{e} (full_url: #{@base_url}/interpolated/#{m}#{query_string})"
		end

		if (data.is_a? Hash) then
			if data[:error] then
				raise Backend::Error, "Descartes Exception raised: #{data[:error]}. uri: #{uri}"
			end
		end
			
		metric = []
		data.each do |node|
			metric << {x: node[0], y: node[1]}
		end

		padded = []
		(start..stop).step(step).each do |i|
			points = metric.select{|p| p[:x].between?(i, i+step-1)}
			if points.length == 1 then
				padded << points.first
			elsif points.length == 0 then
				padded << {x: i, y: (0.0/0.0)}
			end
{'fraction_non_alphanumeric': 0.16524621212121213, 'fraction_numerical': 0.0078125, 'mean_word_length': 3.1543756145526056, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 10, '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': '7452970', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1464, 'n_words': 393}