Class: CS::Collection::SensorDataCollection

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/cs/collection/sensor_data_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session = nil) ⇒ SensorDataCollection

Returns a new instance of SensorDataCollection.



9
10
11
12
13
# File 'lib/cs/collection/sensor_data_collection.rb', line 9

def initialize(session=nil)
  self.session = session
  @batch_size = 1000
  super([])
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



7
8
9
# File 'lib/cs/collection/sensor_data_collection.rb', line 7

def batch_size
  @batch_size
end

#sessionObject

Returns the value of attribute session.



6
7
8
# File 'lib/cs/collection/sensor_data_collection.rb', line 6

def session
  @session
end

Instance Method Details

#get_urlObject



52
53
54
# File 'lib/cs/collection/sensor_data_collection.rb', line 52

def get_url
  "/sensors/data.json"
end

#process_batch(batch) ⇒ Object

Given array of sensor data it will group the data by sensor_id and construct payload for multiple sensor upload



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cs/collection/sensor_data_collection.rb', line 28

def process_batch(batch)
  sensors = {}
  batch.each do |point|
    next if point.nil? || point.sensor_id.nil?
    sensor_id = point.sensor_id

    if !sensors[sensor_id]
      sensors[sensor_id] = {
        sensor_id: sensor_id,
        data: []
      }
    end

    sensors[sensor_id][:data].push(point.to_cs_value)
  end

  retval = []
  sensors.each do |k, v|
    retval.push(v)
  end

  {sensors: retval}
end

#save!Object



15
16
17
18
19
20
21
22
23
# File 'lib/cs/collection/sensor_data_collection.rb', line 15

def save!
  check_session!

  # group batch
  self.each_slice(@batch_size) do |batch|
    body = process_batch(batch)
    @session.post(get_url, body)
  end
end