2012-02-29 02:39:56 +00:00
|
|
|
require 'csv'
|
|
|
|
require 'redis/connection/hiredis'
|
|
|
|
require 'redis'
|
|
|
|
|
|
|
|
require 'logger'
|
|
|
|
require 'open-uri'
|
|
|
|
|
|
|
|
|
|
|
|
class Earthquakes
|
|
|
|
DATA_URL = 'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt'
|
|
|
|
|
2012-02-29 04:35:55 +00:00
|
|
|
attr_accessor :id_set, :region_set, :magnitude_set
|
2012-02-29 04:35:36 +00:00
|
|
|
|
2012-02-29 02:39:56 +00:00
|
|
|
def initialize
|
|
|
|
@redis = Redis.new
|
2012-02-29 04:35:55 +00:00
|
|
|
@id_set = 'earthquake-ids'
|
2012-02-29 04:35:36 +00:00
|
|
|
@region_set = 'earthquake-regions'
|
2012-02-29 04:35:55 +00:00
|
|
|
@magnitude_set = 'earthquake-magnitudes'
|
2012-02-29 04:35:36 +00:00
|
|
|
|
2012-02-29 02:39:56 +00:00
|
|
|
@logger = Logger.new(STDOUT)
|
|
|
|
@logger.formatter = proc do |severity, datetime, progname, msg|
|
|
|
|
"#{datetime}: #{msg}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-29 04:35:36 +00:00
|
|
|
def get_info(earthquake_id)
|
2012-02-29 04:35:55 +00:00
|
|
|
@redis.hgetall(with_prefix(earthquake_id, 'eqid'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_region(region)
|
|
|
|
@redis.smembers(with_prefix(region, 'eqregion'))
|
|
|
|
end
|
2012-02-29 04:35:36 +00:00
|
|
|
|
2012-02-29 04:35:55 +00:00
|
|
|
def with_magnitude(magnitude)
|
|
|
|
@redis.smembers(with_prefix(magnitude, 'eqmagnitude'))
|
2012-02-29 04:35:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def all_ids
|
2012-02-29 04:35:55 +00:00
|
|
|
@redis.smembers(@id_set).map { |i| sans_prefix(i, 'eqid') }
|
2012-02-29 04:35:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def all_regions
|
2012-02-29 04:35:55 +00:00
|
|
|
@redis.smembers(@region_set).map { |r| sans_prefix(r, 'eqregion') }
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_magnitudes
|
|
|
|
@redis.smembers(@magnitude_set).map { |m| sans_prefix(m, 'eqmagnitude') }
|
2012-02-29 02:39:56 +00:00
|
|
|
end
|
|
|
|
|
2012-02-29 04:35:36 +00:00
|
|
|
def drop_all
|
|
|
|
@logger.info('Removing data for each earthquake')
|
|
|
|
all_ids.each do |i|
|
2012-02-29 04:35:55 +00:00
|
|
|
@logger.info("Removing data for earthquake #{sans_prefix(i, 'eqid')}")
|
2012-02-29 04:35:36 +00:00
|
|
|
@redis.del(i)
|
2012-02-29 02:39:56 +00:00
|
|
|
end
|
2012-02-29 04:35:36 +00:00
|
|
|
|
|
|
|
@logger.info('Removing earthquake id set')
|
|
|
|
@redis.del(@id_set)
|
|
|
|
|
|
|
|
@logger.info('Removing earthquake region set')
|
|
|
|
@redis.del(@region_set)
|
2012-02-29 04:35:55 +00:00
|
|
|
|
|
|
|
@logger.info('Removing earthquake magnitude set')
|
|
|
|
@redis.del(@magnitude_set)
|
2012-02-29 02:39:56 +00:00
|
|
|
end
|
|
|
|
|
2012-02-29 04:35:36 +00:00
|
|
|
def load_data
|
|
|
|
data = CSV.parse(
|
|
|
|
open(DATA_URL).read, :headers => true, :header_converters => [:symbol]
|
|
|
|
)
|
|
|
|
|
|
|
|
data.each do |row|
|
|
|
|
@redis.multi do |r|
|
|
|
|
@logger.info("Adding earthquake #{row[:eqid]}")
|
2012-02-29 04:35:55 +00:00
|
|
|
id_key = with_prefix(row[:eqid].downcase, 'eqid')
|
|
|
|
r.sadd(@id_set, id_key)
|
2012-02-29 04:35:36 +00:00
|
|
|
row.headers.each do |header|
|
2012-02-29 04:35:55 +00:00
|
|
|
r.hset(id_key, header.downcase, row[header].downcase)
|
2012-02-29 04:35:36 +00:00
|
|
|
end
|
|
|
|
|
2012-02-29 04:35:55 +00:00
|
|
|
region_key = with_prefix(row[:region].downcase, 'eqregion')
|
|
|
|
r.sadd(region_key, id_key)
|
2012-02-29 04:35:36 +00:00
|
|
|
r.sadd(@region_set, region_key)
|
2012-02-29 04:35:55 +00:00
|
|
|
|
|
|
|
magnitude_key = with_prefix(row[:magnitude], 'eqmagnitude')
|
|
|
|
r.sadd(magnitude_key, id_key)
|
|
|
|
r.sadd(@magnitude_set, magnitude_key)
|
2012-02-29 04:35:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-02-29 04:35:55 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
def with_prefix(key, prefix)
|
|
|
|
if not /^#{prefix}:/.match(key)
|
|
|
|
return "#{prefix}:#{key}"
|
|
|
|
end
|
|
|
|
|
|
|
|
key
|
|
|
|
end
|
|
|
|
|
|
|
|
def sans_prefix(key, prefix)
|
|
|
|
key.gsub(/^#{prefix}:/, '')
|
|
|
|
end
|
2012-02-29 02:39:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if $0 == __FILE__
|
2012-02-29 04:35:36 +00:00
|
|
|
Earthquakes.new.send(ARGV.first)
|
2012-02-29 02:39:56 +00:00
|
|
|
end
|