geomena / sinatra / lib / geomena / couch.rb

require 'couchrest'

class GCouch
  @@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)
    rows = @@db.view('observations//count', {:key => macaddr, :group => true})["rows"]
    rows.first["value"] if rows.size > 0
  end

  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)
    @@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"]
    rows.length > 0 ? rows.first["value"] : 0
  end

  def self.last
    rows = @@db.view('observations/bycreatedat', :limit => 1, :descending => true)["rows"]
    rows.first["value"] if rows.length > 0
  end

  def self.log(essid, note)
    log = {:essid => essid, :note => note, :created_at => Time.now}
    @@logdb.save_doc(log)
  end

  def self.logcount(date)
    @@logdb.view('reports/count')
  end

  def self.logsetup
  # lame
  r = @@logdb.get("_design/reports")
  @@logdb.delete_doc(r)
  @@logdb.save_doc({
    "_id" => "_design/reports",
    :views => {
      :count => {
        :map => "function(doc){if(doc.created_at) {emit(doc.created_at, doc);}}"
        }
      }
    })
  end

end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.