|
|
@ -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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|