Class: AMEE::Data::ItemValueHistory

Inherits:
Object
  • Object
show all
Extended by:
ParseHelper
Defined in:
lib/amee/data_item_value_history.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParseHelper

load_xml_doc, node_value, xmlpathpreamble

Constructor Details

#initialize(data = {}) ⇒ ItemValueHistory

Returns a new instance of ItemValueHistory.



11
12
13
14
15
16
17
# File 'lib/amee/data_item_value_history.rb', line 11

def initialize(data = {})
  @type = data ? data[:type] : nil
  @path = data ? data[:path] : nil
  @connection = data ? data[:connection] : nil
  @values=data&&data[:values] ? data[:values] : []
  self.series=data[:series] if data[:series]
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



68
69
70
# File 'lib/amee/data_item_value_history.rb', line 68

def connection
  @connection
end

#pathObject

the IV path corresponding to the series, without the /data



20
21
22
# File 'lib/amee/data_item_value_history.rb', line 20

def path
  @path
end

#typeObject (readonly)

Returns the value of attribute type.



69
70
71
# File 'lib/amee/data_item_value_history.rb', line 69

def type
  @type
end

#valuesObject

Returns the value of attribute values.



19
20
21
# File 'lib/amee/data_item_value_history.rb', line 19

def values
  @values
end

Class Method Details

.from_json(json, path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/amee/data_item_value_history.rb', line 71

def self.from_json(json, path)
  # Read JSON
  data = {}
  data[:path] = path.gsub(/^\/data/, '')
  doc = JSON.parse(json)['itemValues']
  doc=[JSON.parse(json)['itemValue']] unless doc
  data[:values]=doc.map do |json_item_value|
    ItemValue.from_json(json_item_value,path)
  end
  data[:type]=data[:values][0].type
  # Create object
  ItemValueHistory.new(data)
rescue
  raise AMEE::BadData.new("Couldn't load DataItemValueHistory from JSON. Check that your URL is correct.\n#{json}")
end

.from_xml(xml, path) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/amee/data_item_value_history.rb', line 87

def self.from_xml(xml, path)
  # Read XML
  data = {}
  data[:path] = path.gsub(/^\/data/, '')
  doc = load_xml_doc(xml)
  valuedocs=doc.xpath('//ItemValue')
  raise  if valuedocs.length==0
  data[:values] = valuedocs.map do |xml_item_value|
    ItemValue.from_xml(xml_item_value,path)
  end
  data[:type]=data[:values][0].type
  # Create object
  ItemValueHistory.new(data)
rescue
  raise AMEE::BadData.new("Couldn't load DataItemValueHistory from XML. Check that your URL is correct.\n#{xml}")
end

.get(connection, path) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/amee/data_item_value_history.rb', line 104

def self.get(connection, path)
  # Load data from path
  response = connection.get(path,:valuesPerPage=>2).body
  # Parse data from response
  data = {}
  value = ItemValueHistory.parse(connection, response, path)
  # Done
  return value
rescue
  raise AMEE::BadData.new("Couldn't load DataItemValueHistory. Check that your URL is correct.")
end

.parse(connection, response, path) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/amee/data_item_value_history.rb', line 145

def self.parse(connection, response, path) 
  if response.is_json?
    history = ItemValueHistory.from_json(response, path)
  else
    history = ItemValueHistory.from_xml(response, path)
  end
  # Store connection in object for future use
  history.connection = connection
  # Done
  return history
rescue
  raise AMEE::BadData.new("Couldn't load DataItemValueHistory. Check that your URL is correct.\n#{response}")
end

Instance Method Details

#compare(origin) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/amee/data_item_value_history.rb', line 159

def compare(origin)
  new=Set.new(times)
  old=Set.new(origin.times)
  {
    :insertions=>values_at(new-old),
    :deletions=>origin.values_at(old-new),
    :updates=>values_at(old&new)
  }
end

#create!Object

Raises:



140
141
142
143
# File 'lib/amee/data_item_value_history.rb', line 140

def create!
  # deprecated, as DI cannot exist without at least one point
  raise AMEE::NotSupported.new("Cannot create a Data Item Value History from scratch: at least one data point must exist when the DI is created")
end

#delete!Object

Raises:



135
136
137
138
# File 'lib/amee/data_item_value_history.rb', line 135

def delete!
   # deprecated, as DI cannot exist without at least one point
  raise AMEE::NotSupported.new("Cannot delete all of history: at least one data point must always exist.")
end

#full_pathObject



22
23
24
# File 'lib/amee/data_item_value_history.rb', line 22

def full_path
  "/data#{path}"
end

#save!Object

Raises:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/amee/data_item_value_history.rb', line 116

def save!
  raise AMEE::BadData.new("Can't save without a path") unless @path
  raise AMEE::BadData.new("Can't save without a connection") unless @connection
  origin=ItemValueHistory.get(connection,full_path)
  changes=compare(origin)
  changes[:updates].each do |update|
    # we've decided to identify these, but the version in the thing to be
    # saved is probably home made, so copy over the uid
    update.uid=origin.value_at(update.start_date).uid
    update.save!
  end
  changes[:insertions].each do |insertion|
    insertion.create!
  end
  changes[:deletions].each do |deletion|
    deletion.delete!
  end
end

#seriesObject



26
27
28
29
30
31
32
# File 'lib/amee/data_item_value_history.rb', line 26

def series
  values.map {|x|
    [x.start_date.utc,x.value]
  }.sort {|x,y|
    x[0]<=>y[0]
  }
end

#series=(newseries) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/amee/data_item_value_history.rb', line 40

def series=(newseries)
  raise AMEE::BadData.new("Series must have initial (Epoch) value") unless newseries.any?{|x| x[0]==Epoch}
  @values=newseries.map{|x|
    AMEE::Data::ItemValue.new(:value=>x[1],
      :start_date=>x[0],
      :path=>path,
      :connection=>connection,
      :type=>type
    )
  }
end

#timesObject



34
35
36
37
38
# File 'lib/amee/data_item_value_history.rb', line 34

def times
  values.map {|x|
    x.start_date.utc
  }
end

#value_at(time) ⇒ Object

Raises:



52
53
54
55
56
57
# File 'lib/amee/data_item_value_history.rb', line 52

def value_at(time)
  selected=values.select{|x| x.start_date==time}
  raise AMEE::BadData.new("Multiple data item values matching one time.") if selected.length >1
  raise AMEE::BadData.new("No data item value for that time #{time}.") if selected.length ==0
  selected[0]
end

#values_at(times) ⇒ Object



59
60
61
# File 'lib/amee/data_item_value_history.rb', line 59

def values_at(times)
  times.map{|x| value_at(x)}
end