Class: Store::Idn

Inherits:
Store
  • Object
show all
Defined in:
lib/store/idn.rb

Constant Summary collapse

METS =
["delta_t","gust_kmh","rain_trace","rel_hum","wind_spd_kmh","air_temp","apparent_t"]

Instance Method Summary collapse

Constructor Details

#initialize(origin, settings) ⇒ Idn


19
20
21
22
# File 'lib/store/idn.rb', line 19

def initialize origin,settings
  super
  @data_folder = mandatory_param :data_folder, "store_settings"
end

Instance Method Details

#data_filesObject


24
25
26
# File 'lib/store/idn.rb', line 24

def data_files
  Dir[@data_folder+"/**/*"].reject{|f| File.directory? f}
end

#get_metric(m, start = nil, stop = nil, step = nil) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/store/idn.rb', line 63

def get_metric m, start=nil, stop=nil, step=nil
  site, met = m.id.split(SEP).last.split(sep)

  unless idn_sitemap(site).length > 1 then
    return {error: "Site #{site} does not exist"}.to_json
  end

  blob = []
  zone = ""

  idn_sitemap(site).each { |f|
    blob += JSON.parse(File.read(f)).observations.data
    zone = JSON.parse(File.read(f)).observations.header.first.time_zone
  }

  offset = "+10" if zone == "EST"
  offset = "+11" if zone == "EDT"

  blob = blob.uniq{|k| k.local_date_time_full}
  data = []

  blob.each{|b|
    date = DateTime.parse("#{b.local_date_time_full}#{offset}").strftime("%s").to_i
    data.push({x: date, y: b[met].to_f}) if date.between?(start,stop)
  }

  return {error: "No data for period specified"} if data.nil? || data == []

  data_sanitize data, start, stop, step
end

#get_metric_url(m, _, _, _) ⇒ Object


56
57
58
59
60
61
# File 'lib/store/idn.rb', line 56

def get_metric_url m,_,_,_
  name = m.id.split(SEP).last.split(sep).first
  id = idn_sitemap(name).first.split("/")[2]
  dir = @data_folder.gsub("public/","")
  return "#{dir}/#{id}"
end

#get_metrics_listObject


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/store/idn.rb', line 28

def get_metrics_list
  list = []
  data_files.each{|f|
    d = JSON.parse(File.read(f))
    site = nameparse(d.observations.header.first.name)
    unless d.observations.data.empty? 
      measures = d.observations.data.first.map{|k,v| k}.select{|m| METS.include? m}
      list.push( measures.map{|m| "#{site}#{sep}#{m}"})
    end
  }

  set_idn_sitemap

  list.flatten.uniq
end

#idn_sitemap(site) ⇒ Object


116
117
118
# File 'lib/store/idn.rb', line 116

def idn_sitemap site
  redis_conn.smembers "#{REDIS_KEY}BOM:IDN_SITEMAP:#{site}"
end

#is_up?Boolean

In lieu of pinging a href, confirm the existance of the file


46
47
48
# File 'lib/store/idn.rb', line 46

def is_up?
        File.exists?(@data_folder)
end

#live?Boolean

Flat files don't auto update, therefore cannot be assumed to be live


51
52
53
# File 'lib/store/idn.rb', line 51

def live?
        false
end

#nameparse(n) ⇒ Object


106
# File 'lib/store/idn.rb', line 106

def nameparse n; n.gsub(" ","").gsub("-",""); end

#sepObject


120
# File 'lib/store/idn.rb', line 120

def sep; "-"; end

#set_idn_sitemapObject


108
109
110
111
112
113
114
# File 'lib/store/idn.rb', line 108

def set_idn_sitemap
  r = redis_conn
  data_files.each{|f|
    site = nameparse(JSON.parse(File.read(f)).observations.header.first.name)
    r.sadd "#{REDIS_KEY}BOM:IDN_SITEMAP:#{site}",f
  }
end