Trying to play with sorted sets for querying in range of magnitudes
This commit is contained in:
parent
8bf785f48a
commit
77c8c11ec4
@ -7,14 +7,17 @@ require 'open-uri'
|
|||||||
|
|
||||||
|
|
||||||
class Earthquakes
|
class Earthquakes
|
||||||
attr_accessor :id_set, :region_set, :magnitude_set, :data_url
|
attr_accessor :id_set, :region_set, :magnitude_set
|
||||||
|
attr_accessor :magnitude_scores_set, :data_url
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@redis = Redis.new
|
@redis = Redis.new
|
||||||
@id_set = 'earthquake-ids'
|
@id_set = 'earthquake-ids'
|
||||||
@region_set = 'earthquake-regions'
|
@region_set = 'earthquake-regions'
|
||||||
@magnitude_set = 'earthquake-magnitudes'
|
@magnitude_set = 'earthquake-magnitudes'
|
||||||
@data_url = 'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt'
|
@magnitude_scores_set = 'earthquakes-magnitudes-scores'
|
||||||
|
@data_url =
|
||||||
|
'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt'
|
||||||
|
|
||||||
@logger = Logger.new(STDOUT)
|
@logger = Logger.new(STDOUT)
|
||||||
@logger.formatter = proc do |severity, datetime, progname, msg|
|
@logger.formatter = proc do |severity, datetime, progname, msg|
|
||||||
@ -30,8 +33,12 @@ class Earthquakes
|
|||||||
@redis.smembers(with_prefix(region, 'eqregion'))
|
@redis.smembers(with_prefix(region, 'eqregion'))
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_magnitude(magnitude)
|
def with_magnitude_in_range(min_magnitude, max_magnitude)
|
||||||
@redis.smembers(with_prefix(magnitude, 'eqmagnitude'))
|
min_mag, max_mag =
|
||||||
|
magnitude_as_int(min_magnitude), magnitude_as_int(max_magnitude)
|
||||||
|
@logger.debug(
|
||||||
|
"Fetching magnitudes with scores in range #{min_mag}-#{max_mag}")
|
||||||
|
@redis.zrange(@magnitude_scores_set, min_mag, max_mag)
|
||||||
end
|
end
|
||||||
|
|
||||||
def all_ids
|
def all_ids
|
||||||
@ -61,6 +68,9 @@ class Earthquakes
|
|||||||
|
|
||||||
@logger.info('Removing earthquake magnitude set')
|
@logger.info('Removing earthquake magnitude set')
|
||||||
@redis.del(@magnitude_set)
|
@redis.del(@magnitude_set)
|
||||||
|
|
||||||
|
@logger.info('Removing earthquake magnitude scores set')
|
||||||
|
@redis.del(@magnitude_scores_set)
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_data
|
def load_data
|
||||||
@ -101,6 +111,9 @@ class Earthquakes
|
|||||||
magnitude_key = with_prefix(row[:magnitude], 'eqmagnitude')
|
magnitude_key = with_prefix(row[:magnitude], 'eqmagnitude')
|
||||||
redis_multi.sadd(magnitude_key, id_key)
|
redis_multi.sadd(magnitude_key, id_key)
|
||||||
redis_multi.sadd(@magnitude_set, magnitude_key)
|
redis_multi.sadd(@magnitude_set, magnitude_key)
|
||||||
|
score = magnitude_as_int(row[:magnitude])
|
||||||
|
@logger.info("Setting score of #{score} for row #{row.inspect}")
|
||||||
|
redis_multi.zadd(@magnitude_scores_set, score, id_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_prefix(key, prefix)
|
def with_prefix(key, prefix)
|
||||||
@ -114,6 +127,10 @@ class Earthquakes
|
|||||||
def sans_prefix(key, prefix)
|
def sans_prefix(key, prefix)
|
||||||
key.gsub(/^#{prefix}:/, '')
|
key.gsub(/^#{prefix}:/, '')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def magnitude_as_int(magnitude)
|
||||||
|
magnitude.to_s.gsub(/\./, '').to_i
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ def main
|
|||||||
on :R, :listall_regions, 'List available earthquake regions.'
|
on :R, :listall_regions, 'List available earthquake regions.'
|
||||||
on :M, :listall_magnitudes, 'List available earthquakes magnitudes.'
|
on :M, :listall_magnitudes, 'List available earthquakes magnitudes.'
|
||||||
on :r, :region=, 'Show earthquakes for region.'
|
on :r, :region=, 'Show earthquakes for region.'
|
||||||
on :m, :magnitude=, 'Show earthquakes of magnitude.'
|
on :m, :magnitude_range=, 'Show earthquakes of magnitude in range, e.g. "1.1-2.9".'
|
||||||
on :i, :eqid=, 'Show all available info for earthquake with id.'
|
on :i, :eqid=, 'Show all available info for earthquake with id.'
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -51,11 +51,13 @@ def main
|
|||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
if opts[:magnitude]
|
if opts[:magnitude_range]
|
||||||
puts "/* Earthquakes with magnitude #{opts[:magnitude]} */"
|
puts "/* Earthquakes with magnitude #{opts[:magnitude]} */"
|
||||||
|
|
||||||
|
min_mag, max_mag = opts[:magnitude_range].split('-')
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
earthquakes.with_magnitude(opts[:magnitude]).each do |i|
|
earthquakes.with_magnitude_in_range(min_mag, max_mag).each do |i|
|
||||||
results << earthquakes.get_info(i)
|
results << earthquakes.get_info(i)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user