Skip to content
This repository was archived by the owner on Nov 7, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/data_magic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ def self.s3
# Public Class Methods
#========================================================================

# BigDecimal turns floats to strings; we undo that by casting BigDecimal down to Float.
def self.downgrade_big_decimal(hash)
hash.each do |k, v|
if v.is_a? BigDecimal
hash[k] = v.to_f
elsif v.is_a? Hash
downgrade_big_decimal(hash[k])
end
end

hash
end

# thin layer on elasticsearch query
def self.search(terms, options = {})
terms = IndifferentHash.new(terms)
Expand Down Expand Up @@ -107,8 +120,9 @@ def self.search(terms, options = {})

found.keys.each { |key| found[key] = found[key][0] }
# now it should look like this:
# {"city"=>"Springfield", "address"=>"742 Evergreen Terrace}

# {"city"=>"Springfield", "address"=>"742 Evergreen Terrace"}
downgrade_big_decimal found

# re-insert null fields that didn't get returned by ES
query_body[:fields].each do |field|
if !found.has_key?(field)
Expand Down Expand Up @@ -143,9 +157,10 @@ def self.search(terms, options = {})
if options[:metrics] && options[:metrics].size > 0
aggregations[f_name] = values.reject { |k, v| !(options[:metrics].include? k) }
else
# Keep everything is no metric list is provided
# Keep all metrics returned by ES, if no metric list is provided as an API parameter
aggregations[f_name] = values
end
downgrade_big_decimal aggregations[f_name]
end

simple_result.merge!({"aggregations" => aggregations})
Expand Down
4 changes: 2 additions & 2 deletions spec/features/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,15 @@
let(:all_aggregs) do
{ "aggregations" => {
"age"=>{"count"=>2, "min"=>14.0, "max"=>70.0, "avg"=>42.0, "sum"=>84.0, "sum_of_squares"=>5096.0, "variance"=>784.0, "std_deviation"=>28.0, "std_deviation_bounds"=>{"upper"=>98.0, "lower"=>-14.0}},
"height"=>{"count"=>2, "min"=>2.0, "max"=>142.0, "avg"=>72.0, "sum"=>144.0, "sum_of_squares"=>20168.0, "variance"=>4900.0, "std_deviation"=>70.0, "std_deviation_bounds"=>{"upper"=>212.0, "lower"=>-68.0}}
"height"=>{"count"=>2, "min"=>2.0, "max"=>142.0, "avg"=>72.0, "sum"=>144.0, "sum_of_squares"=>20168.0, "variance"=>4900.0, "std_deviation"=>7104091085045.845, "std_deviation_bounds"=>{"upper"=>212.0, "lower"=>-68.0}}
}
}
end

let(:max_avg_aggregs) do
{ "aggregations" => {
"age" => { "max" => 70.0, "avg" => 42.0},
"height" => { "max" => 142.0, "avg" => 72.0}
"height" => { "max"=>14210984098501.5, "avg"=>7106893013455.655}
}
}
end
Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ def address_data
name,address,city,age,height
Paul,15 Penny Lane,Liverpool,10,142
Michelle,600 Pennsylvania Avenue,Washington,12,1
Marilyn,1313 Mockingbird Lane,Springfield,14,2
Marilyn,1313 Mockingbird Lane,Springfield,14,2801928409.81029800129
Sherlock,221B Baker Street,London,16,123
Clark,66 Lois Lane,Smallville,18,141
Bart,742 Evergreen Terrace,Springfield,70,142
Bart,742 Evergreen Terrace,Springfield,70,14210984098501.507012980419280
Paul,19 N Square,Boston,70,55.2
Peter,66 Parker Lane,New York,74,11.5123
eos
Expand Down
9 changes: 5 additions & 4 deletions spec/lib/data_magic/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
it "can find document with one attribute" do
result = DataMagic.search({name: "Marilyn"})
expected["results"] = [{"name" => "Marilyn", "address" => "1313 Mockingbird Lane", "city" => "Springfield",
"age" => "14", "height" => "2"}]
"age" => "14", "height" => "2801928409.81029800129"}]
expect(result).to eq(expected)
end

Expand All @@ -44,8 +44,9 @@
it "can find a document with a set of values delimited by commas" do
result = DataMagic.search({name: "Paul,Marilyn"})
expected['metadata']["total"] = 3

expect(result["results"]).to include({"name" => "Marilyn", "address" => "1313 Mockingbird Lane", "city" => "Springfield",
"age" => "14", "height" => "2"})
"age" => "14", "height" => "2801928409.81029800129"})
expect(result["results"]).to include({"name" => "Paul", "address" => "15 Penny Lane", "city" => "Liverpool",
"age" => "10", "height" => "142"})
expect(result["results"]).to include({"name" => "Paul", "address" => "19 N Square", "city" => "Boston",
Expand Down Expand Up @@ -131,13 +132,13 @@
it "can correctly compute filtered statistics" do
expected["metadata"]["total"] = 2
result = DataMagic.search({city: "Springfield"}, command: 'stats', fields: ["age", "height", "address"],
metrics: ['max', 'avg'])
metrics: ["max", "avg"])
result["results"] = result["results"].sort_by { |k| k["age"] }

expected["results"] = []
expected["aggregations"] = {
"age" => { "max" => 70.0, "avg" => 42.0},
"height" => {"max"=>142.0, "avg"=>72.0}
"height" => {"max"=>14210984098501.5, "avg"=>7106893013455.655}
}

expect(result).to eq(expected)
Expand Down