Tests for LogService
parent
63a90f8888
commit
a558758c43
|
@ -1,20 +1,6 @@
|
|||
require_relative "./log_service_support"
|
||||
|
||||
# Listen to all logs on the message broker and store them in the database.
|
||||
Transport
|
||||
.log_channel
|
||||
.subscribe(block: true) do |delivery_info, properties, payload|
|
||||
# { "meta"=>{"z"=>0, "y"=>0, "x"=>0, "type"=>"info", "major_version"=>6},
|
||||
# "message"=>"HQ FarmBot TEST 123 Pin 13 is 0",
|
||||
# "created_at"=>1512585641,
|
||||
# "channels"=>[] }
|
||||
log = JSON.parse(payload)
|
||||
|
||||
# Legacy bots will double save logs if we don't do this:
|
||||
major_version = log.dig("meta", "major_version") || 0
|
||||
|
||||
if(major_version >= 6)
|
||||
device_id = delivery_info.routing_key.split(".")[1].gsub("device_", "").to_i
|
||||
log[:device] = Device.find(device_id)
|
||||
Logs::Create.run!(log).save!
|
||||
else
|
||||
puts "Ignoring legacy log."
|
||||
end
|
||||
end
|
||||
.subscribe(block: true) { |info, _, payl| LogService.process(info, payl) }
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
class LogService
|
||||
def self.process(delivery_info, payload)
|
||||
# { "meta"=>{"z"=>0, "y"=>0, "x"=>0, "type"=>"info", "major_version"=>6},
|
||||
# "message"=>"HQ FarmBot TEST 123 Pin 13 is 0",
|
||||
# "created_at"=>1512585641,
|
||||
# "channels"=>[] }
|
||||
log = JSON.parse(payload)
|
||||
|
||||
# Legacy bots will double save logs if we don't do this:
|
||||
major_version = (log.dig("meta", "major_version") || 0).to_i
|
||||
|
||||
if(major_version >= 6)
|
||||
device_id = delivery_info.routing_key.split(".")[1].gsub("device_", "").to_i
|
||||
log[:device] = Device.find(device_id)
|
||||
Logs::Create.run!(log).save!
|
||||
else
|
||||
puts "Ignoring legacy log."
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,29 @@
|
|||
require "spec_helper"
|
||||
require_relative "../../lib/log_service_support"
|
||||
|
||||
describe LogService do
|
||||
normal_payl = '{"meta":{"z":0,"y":0,"x":0,"type":"info","major_version":6},' +
|
||||
'"message":"HQ FarmBot TEST 123 Pin 13 is 0","created_at":'+
|
||||
'1512585641,"channels":[]}'
|
||||
|
||||
legacy_payl = '{"meta":{"z":0,"y":0,"x":0,"type":"info"},' +
|
||||
'"message":"HQ FarmBot TEST 123 Pin 13 is 0","created_at":'+
|
||||
'1512585641,"channels":[]}'
|
||||
FakeDeliveryInfo = Struct.new(:routing_key)
|
||||
device_id = FactoryBot.create(:device).id
|
||||
fake_delivery_info = FakeDeliveryInfo.new("bot.device_#{device_id}.logs")
|
||||
|
||||
it "creates new messages in the DB when called" do
|
||||
Log.destroy_all
|
||||
b4 = Log.count
|
||||
LogService.process(fake_delivery_info, normal_payl)
|
||||
expect(b4).to be < Log.count
|
||||
end
|
||||
|
||||
it "ignores legacy logs" do
|
||||
Log.destroy_all
|
||||
b4 = Log.count
|
||||
LogService.process(fake_delivery_info, legacy_payl)
|
||||
expect(b4).to eq(Log.count)
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue