7 changed files with 93 additions and 72 deletions
@ -1,29 +0,0 @@ |
|||
class Datastore |
|||
@@client = Mongo::Client.new ENV['MONGO_URL'] |
|||
|
|||
def self.host(type, host, port) |
|||
key = self.key host, port |
|||
@@client[type].find(key).first |
|||
# result = @@index.type(type).get key |
|||
# result.date = Time.parse result.date |
|||
# result |
|||
end |
|||
|
|||
def self.pending(type, host, port) |
|||
self.post type, host, port, { pending: true, date: DateTime.now } |
|||
end |
|||
|
|||
def self.post(type, host, port, data) |
|||
# entry = self.host type, host, port |
|||
# entry.delete if entry |
|||
# |
|||
key = self.key host, port |
|||
data = data.merge key |
|||
@@client[type].update_one key, data, {upsert: true} |
|||
end |
|||
|
|||
private |
|||
def self.key(host, port) |
|||
{ host: host, port: port } |
|||
end |
|||
end |
@ -0,0 +1,56 @@ |
|||
class Analysis |
|||
include Mongoid::Document |
|||
include Mongoid::Timestamps |
|||
|
|||
field :type, type: Symbol |
|||
field :host, type: String |
|||
field :port, type: Numeric |
|||
field :pending, type: Boolean |
|||
field :date, type: Time |
|||
field :result, type: Array |
|||
|
|||
validates_presence_of :type |
|||
validates_presence_of :host |
|||
validates_presence_of :port |
|||
validates_uniqueness_of :type, scope: %i[host port] |
|||
|
|||
index type: 1 |
|||
index({ type: 1, host: 1, port: 1 }, { unique: true }) |
|||
|
|||
def self.[](type, host, port) |
|||
key = self.key type, host, port |
|||
self.where(key).first |
|||
end |
|||
|
|||
def self.pending(type, host, port) |
|||
analysis = self[type, host, port] |
|||
if analysis |
|||
analysis.remove_attribute :result |
|||
analysis.update_attributes pending: true, date: Time.now |
|||
analysis |
|||
else |
|||
self.create! type: type, host: host, port: port, pending: true, date: Time.now |
|||
end |
|||
end |
|||
|
|||
def self.result(type, host, port, result) |
|||
analysis = self[type, host, port] |
|||
if analysis |
|||
analysis.remove_attribute :pending |
|||
analysis.update_attributes result: result, date: Time.now |
|||
analysis |
|||
else |
|||
self.create! type: type, host: host, port: port, result: result, date: Time.now |
|||
end |
|||
end |
|||
|
|||
def publish(result) |
|||
self.remove_attribute :pending |
|||
self.update_attribute :result, result |
|||
end |
|||
|
|||
private |
|||
def self.key(type, host, port) |
|||
{ type: type, host: host, port: port } |
|||
end |
|||
end |
@ -0,0 +1,22 @@ |
|||
development: |
|||
clients: |
|||
default: |
|||
database: <%= ENV['MONGO_DATABASE'] %>_development |
|||
hosts: |
|||
- <%= ENV['MONGO_URL'] %> |
|||
test: |
|||
clients: |
|||
default: |
|||
database: <%= ENV['MONGO_DATABASE'] %>_test |
|||
hosts: |
|||
- <%= ENV['MONGO_URL'] %> |
|||
options: |
|||
read: |
|||
mode: :primary |
|||
max_pool_size: 1 |
|||
production: |
|||
clients: |
|||
default: |
|||
database: <%= ENV['MONGO_DATABASE'] %> |
|||
hosts: |
|||
- <%= ENV['MONGO_URL'] %> |
Loading…
Reference in new issue