Skip to content

Instantly share code, notes, and snippets.

@jdkealy
Created January 6, 2014 15:18
Show Gist options
  • Save jdkealy/8284279 to your computer and use it in GitHub Desktop.
Save jdkealy/8284279 to your computer and use it in GitHub Desktop.
aggregates = d.map {|a| a['_id'] || 0}
# get min/max and range
min = aggregates.min.to_f
max = aggregates.max.to_f
range = (max - min) / 8
index = 0
copied = d.clone
bins = (min..max).step(range).inject([]){|memo, num| memo << {"_id"=> num, metric_name => []}}
new_range = bins.map do |item|
more = item['_id']
less = bins[index + 1]['_id'] rescue nil
matches = copied.reject{|d| !d['_id']}.select do |i|
if(less)
i['_id'].to_i >= more && i['_id'].to_i <= less
else
i['_id'] >= more
end
end
matches.each do |match|
item[metric_name] << match[metric_name]
item['last_id'] = match['last_id']
item[metric_name] = item[metric_name].flatten
end
index = index + 1
puts item.inspect
item
end
new_range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment