Allowing for query by region and magnitude, too
This commit is contained in:
parent
e84f11dfba
commit
4c2a9ce0b2
@ -9,12 +9,13 @@ require 'open-uri'
|
|||||||
class Earthquakes
|
class Earthquakes
|
||||||
DATA_URL = 'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt'
|
DATA_URL = 'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt'
|
||||||
|
|
||||||
attr_accessor :id_set, :region_set
|
attr_accessor :id_set, :region_set, :magnitude_set
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@redis = Redis.new
|
@redis = Redis.new
|
||||||
@id_set = 'earthquakes'
|
@id_set = 'earthquake-ids'
|
||||||
@region_set = 'earthquake-regions'
|
@region_set = 'earthquake-regions'
|
||||||
|
@magnitude_set = 'earthquake-magnitudes'
|
||||||
|
|
||||||
@logger = Logger.new(STDOUT)
|
@logger = Logger.new(STDOUT)
|
||||||
@logger.formatter = proc do |severity, datetime, progname, msg|
|
@logger.formatter = proc do |severity, datetime, progname, msg|
|
||||||
@ -23,25 +24,33 @@ class Earthquakes
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get_info(earthquake_id)
|
def get_info(earthquake_id)
|
||||||
if not /^eqid:/.match(earthquake_id)
|
@redis.hgetall(with_prefix(earthquake_id, 'eqid'))
|
||||||
earthquake_id = "eqid:#{earthquake_id}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@redis.hgetall(earthquake_id)
|
def in_region(region)
|
||||||
|
@redis.smembers(with_prefix(region, 'eqregion'))
|
||||||
|
end
|
||||||
|
|
||||||
|
def with_magnitude(magnitude)
|
||||||
|
@redis.smembers(with_prefix(magnitude, 'eqmagnitude'))
|
||||||
end
|
end
|
||||||
|
|
||||||
def all_ids
|
def all_ids
|
||||||
@redis.smembers(@id_set).map { |i| i.gsub(/^eqid:/, '') }
|
@redis.smembers(@id_set).map { |i| sans_prefix(i, 'eqid') }
|
||||||
end
|
end
|
||||||
|
|
||||||
def all_regions
|
def all_regions
|
||||||
@redis.smembers(@region_set).map { |r| r.gsub(/^eqregion:/, '') }
|
@redis.smembers(@region_set).map { |r| sans_prefix(r, 'eqregion') }
|
||||||
|
end
|
||||||
|
|
||||||
|
def all_magnitudes
|
||||||
|
@redis.smembers(@magnitude_set).map { |m| sans_prefix(m, 'eqmagnitude') }
|
||||||
end
|
end
|
||||||
|
|
||||||
def drop_all
|
def drop_all
|
||||||
@logger.info('Removing data for each earthquake')
|
@logger.info('Removing data for each earthquake')
|
||||||
all_ids.each do |i|
|
all_ids.each do |i|
|
||||||
@logger.info("Removing data for earthquake #{i.gsub(/^eqid:/, '')}")
|
@logger.info("Removing data for earthquake #{sans_prefix(i, 'eqid')}")
|
||||||
@redis.del(i)
|
@redis.del(i)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -50,6 +59,9 @@ class Earthquakes
|
|||||||
|
|
||||||
@logger.info('Removing earthquake region set')
|
@logger.info('Removing earthquake region set')
|
||||||
@redis.del(@region_set)
|
@redis.del(@region_set)
|
||||||
|
|
||||||
|
@logger.info('Removing earthquake magnitude set')
|
||||||
|
@redis.del(@magnitude_set)
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_data
|
def load_data
|
||||||
@ -60,19 +72,35 @@ class Earthquakes
|
|||||||
data.each do |row|
|
data.each do |row|
|
||||||
@redis.multi do |r|
|
@redis.multi do |r|
|
||||||
@logger.info("Adding earthquake #{row[:eqid]}")
|
@logger.info("Adding earthquake #{row[:eqid]}")
|
||||||
key = "eqid:#{row[:eqid].downcase}"
|
id_key = with_prefix(row[:eqid].downcase, 'eqid')
|
||||||
r.sadd(@id_set, key)
|
r.sadd(@id_set, id_key)
|
||||||
row.headers.each do |header|
|
row.headers.each do |header|
|
||||||
r.hset(key, header.downcase, row[header])
|
r.hset(id_key, header.downcase, row[header].downcase)
|
||||||
end
|
end
|
||||||
|
|
||||||
region_key = "eqregion:#{row[:region].downcase}"
|
region_key = with_prefix(row[:region].downcase, 'eqregion')
|
||||||
|
r.sadd(region_key, id_key)
|
||||||
r.sadd(region_key, key)
|
|
||||||
r.sadd(@region_set, region_key)
|
r.sadd(@region_set, region_key)
|
||||||
|
|
||||||
|
magnitude_key = with_prefix(row[:magnitude], 'eqmagnitude')
|
||||||
|
r.sadd(magnitude_key, id_key)
|
||||||
|
r.sadd(@magnitude_set, magnitude_key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require 'pp'
|
||||||
require 'slop'
|
require 'slop'
|
||||||
|
|
||||||
require File.expand_path('../earthquakes', __FILE__)
|
require File.expand_path('../earthquakes', __FILE__)
|
||||||
@ -8,12 +9,11 @@ require File.expand_path('../earthquakes', __FILE__)
|
|||||||
def main
|
def main
|
||||||
opts = Slop.parse(:help => true, :strict => true) do
|
opts = Slop.parse(:help => true, :strict => true) do
|
||||||
banner "#{File.basename(__FILE__)} [options]"
|
banner "#{File.basename(__FILE__)} [options]"
|
||||||
on :l, :listall, 'List all available earthquake ids.'
|
on :L, :listall, 'List all available earthquake ids.'
|
||||||
on :r, :listall_by_region, 'List available earthquakes by region'
|
on :R, :listall_regions, 'List available earthquake regions.'
|
||||||
#on :l, :'list-by-region', 'List available earthquakes by region'
|
on :M, :listall_magnitudes, 'List available earthquakes magnitudes.'
|
||||||
#on :L, :'list-by-magnitude', 'List available earthquakes by magnitude'
|
on :r, :region=, 'Show earthquakes for region.'
|
||||||
#on :r, :region=, 'Show earthquakes for region'
|
on :m, :magnitude=, 'Show earthquakes of magnitude.'
|
||||||
#on :m, :magnitude=, 'Show earthquakes of magnitude'
|
|
||||||
on :i, :eqid=, 'Show all available info for earthquake with id.'
|
on :i, :eqid=, 'Show all available info for earthquake with id.'
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -24,19 +24,51 @@ def main
|
|||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
if opts.listall_by_region?
|
if opts.listall_regions?
|
||||||
earthquakes.all_regions.each { |r| puts r }
|
earthquakes.all_regions.sort.each { |r| puts r }
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if opts.listall_magnitudes?
|
||||||
|
earthquakes.all_magnitudes.sort.each { |m| puts m }
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
if opts[:region]
|
||||||
|
puts "Earthquakes in region #{opts[:region]}"
|
||||||
|
|
||||||
|
results = []
|
||||||
|
earthquakes.in_region(opts[:region]).each do |i|
|
||||||
|
results << earthquakes.get_info(i)
|
||||||
|
end
|
||||||
|
|
||||||
|
puts results.to_yaml
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
if opts[:magnitude]
|
||||||
|
puts "Earthquakes with magnitude #{opts[:magnitude]}"
|
||||||
|
|
||||||
|
results = []
|
||||||
|
earthquakes.with_magnitude(opts[:magnitude]).each do |i|
|
||||||
|
results << earthquakes.get_info(i)
|
||||||
|
end
|
||||||
|
|
||||||
|
puts results.to_yaml
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
if opts[:eqid]
|
||||||
puts "Earthquake with id of #{opts[:eqid]}"
|
puts "Earthquake with id of #{opts[:eqid]}"
|
||||||
earthquakes.get_info(opts[:eqid]).each do |key, value|
|
earthquakes.get_info(opts[:eqid]).each do |key, value|
|
||||||
puts " #{key}: #{value}"
|
puts " #{key}: #{value}"
|
||||||
end
|
end
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return 1
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
if $0 == __FILE__
|
if $0 == __FILE__
|
||||||
exit(main)
|
exit(main)
|
||||||
|
Loading…
Reference in New Issue
Block a user