Commits

Don Park  committed 74a07bb

better handling of 0 observations

  • Participants
  • Parent commits 68fab4e

Comments (0)

Files changed (5)

File sinatra/Gemfile

 
 gem 'sinatra'
 gem 'couchrest'
+gem 'shotgun'

File sinatra/lib/geomena/couch.rb

   @@db = CouchRest.database!("http://127.0.0.1:5984/geomena")
   @@logdb = CouchRest.database!("http://127.0.0.1:5984/logs")
 
-  def self.get_observation_count_for_mac(macaddr) 
+  def self.get_observation_count_for_mac(macaddr)
     rows = @@db.view('observations//count', {:key => macaddr, :group => true})["rows"]
     rows.first["value"] if rows.size > 0
   end
 
-  def self.get_by_mac(macaddr) 
+  def self.get_by_mac(macaddr)
     startkey=[macaddr, "2038"]
     endkey=[macaddr, ""]
     ret = @@db.view('observations/bymacdate', {:startkey => startkey, :endkey => endkey, :descending => true})
     return ret
   end
-  
-  def self.get_by_essid(essid) 
+
+  def self.get_by_essid(essid)
     @@db.view('observations/byessid', {:key => essid})
   end
-  
+
   def self.put(json)
     @@db.save_doc(json)
   end
-  
+
   def self.count
     rows = @@db.view('observations/count')["rows"]
-    if rows.size > 0
-      rows.first["value"]
-    else
-      0
-    end
+    rows.length > 0 ? rows.first["value"] : 0
   end
 
   def self.last
-    @@db.view('observations/bycreatedat', :limit => 1, :descending => true)["rows"].first["value"]
+    rows = @@db.view('observations/bycreatedat', :limit => 1, :descending => true)["rows"]
+    rows.first["value"] if rows.length > 0
   end
 
   def self.log(essid, note)
   r = @@logdb.get("_design/reports")
   @@logdb.delete_doc(r)
   @@logdb.save_doc({
-    "_id" => "_design/reports", 
+    "_id" => "_design/reports",
     :views => {
       :count => {
         :map => "function(doc){if(doc.created_at) {emit(doc.created_at, doc);}}"

File sinatra/lib/geomena/web.rb

 module Geomena
   class Web < Sinatra::Base
-    configure do 
+    set :views, settings.root + '/../../views'
+    configure do
       set :sessions, true
       @@log = Logger.new(STDOUT)
       Version = File.open(File.dirname(__FILE__)+"/../../public/version") {|f| f.read } rescue Errno::ENOTDIR
     end
-    
+
     get '/' do
       erb :index, :locals => {:ocount => GCouch.count, :aplast => GCouch.last}
     end
-    
+
     get '/api' do
       redirect "http://bitbucket.org/donpdonp/geomena/wiki/APIReference"
     end
-    
+
     get '/search' do
       if mac_valid?(params[:q])
         mac = mac_clean(params[:q])
         redirect "/essid/#{params[:q]}"
       end
     end
-    
+
     get '/locate' do
       erb :locate
     end
-    
+
     post '/' do
       geodata = JSON.parse(request.body.read)
       @@log.info("W3C Lookup: #{geodata}")
         response = GCouch.get_by_mac(macaddr)
         ap = first_ap(response)
         if ap
-          answer = {"location" => {"latitude" => ap["latitude"], 
+          answer = {"location" => {"latitude" => ap["latitude"],
                                    "longitude" => ap["longitude"],
                                    "accuracy" => 150},
                                    "access_token" => "abc123"}
       end
       return ""
     end
-    
+
     get '/ap/:mac' do |mac|
       response = GCouch.get_by_mac(mac)
 puts "response #{response.inspect}"
 puts ":locals => {:ap => #{ap}, :mac => #{mac}, :ocount => #{ocount}}"
       erb :apshow, :locals => {:ap => ap, :mac => mac, :ocount => ocount}
     end
-    
+
     post '/ap/:mac' do |mac|
       payload = {:mac => mac_clean(mac),
                  :essid => params[:essid],
       end
       redirect "/ap/#{params[:mac]}"
     end
-    
+
     get '/essid/:essid' do |essid|
       essids = GCouch.get_by_essid(essid)["rows"]
       if essids.size > 1
         session[:searchmsg] = "Network name not found. Use the mac address to create a new record."
         redirect "/"
       end
-    
+
     end
-    
+
     post '/bitbucket' do
       system("./stop.sh; hg pull; hg update; hg log -l 1|head -1|cut -d' ' -f4 > public/version; ./start.sh")
     end
-    
+
     get '/about' do
       erb :about
     end
-    
+
     get '/clients' do
       erb :clients
     end
-    
+
     get '/data' do
       erb :data
     end
-    
+
     get '/data/heatmap' do
       erb :heatmap
     end
-    
+
     private
-    
+
     def first_ap(aps)
       if aps["rows"].size > 0
         aps["rows"].first["value"]
       end
     end
-    
+
     def mac_clean(mac)
       # take off leading and trailing space
       mac = mac.strip
       # lowercase
       mac = mac.downcase
     end
-    
+
     def mac_valid?(mac)
       !!mac.match(/^\s*..:..:..:..:..:..\s*$/)
     end
-    
+
   end
 end

File sinatra/views/index.erb

 
   <p id="ocount">
    There are <%= ocount %> observations in the database. <br/>
-   Mostly recently "<a href="/ap/<%=aplast["mac"]%>"><%=aplast["essid"]%></a>" was observed  at 
+   <% puts aplast.inspect %>
+   <% if aplast %>
+   Mostly recently "<a href="/ap/<%=aplast["mac"]%>"><%=aplast["essid"]%></a>" was observed  at
     <script>
       var localtime = new Date();
       <% time = Time.parse(aplast["created_at"]) %>
                        localtime.getDate()+" "+
                        hours + ":" + minutes + ampm );
     </script>
+    <% end %>
   </p>
 
   <div id="browsersetupbox">
-   To configure firefox, go to about:config and edit <i>geo.wifi.url</i> to be 
+   To configure firefox, go to about:config and edit <i>geo.wifi.url</i> to be
    "http://geomena.org"
   </div>
 
   <div id="browsersetupbox">
-   To configure opera, go to opera:config and edit <i>Location Provider URL</i> to be 
+   To configure opera, go to opera:config and edit <i>Location Provider URL</i> to be
    "http://geomena.org"
   </div>
 

File tools/geomena.rb

 # Geomena::post(:mac => "1234560a0b0c", :latitude => 45.1, :longitude => -122.5)
 
 class Geomena
-  GeomenaHost = "geomena.org"
+  #GeomenaHost = "geomena.org"
+  GeomenaHost = "localhost"
 
   def self.conn(&blk)
-    Net::HTTP.start(GeomenaHost, 80) do |http|
+    Net::HTTP.start(GeomenaHost, 9393) do |http|
       yield http
     end
   end