diff --git a/src/telem/init.lua b/src/telem/init.lua index 0e9ed77..411fa4f 100644 --- a/src/telem/init.lua +++ b/src/telem/init.lua @@ -1,9 +1,13 @@ local _Telem = { - _VERSION = '0.0.3', + _VERSION = '0.1.0', util = require 'telem.lib.util', input = require 'telem.lib.input', output = require 'telem.lib.output', - backplane = require 'telem.lib.Backplane' + + -- API + backplane = require 'telem.lib.Backplane', + metric = require 'telem.lib.Metric', + metricCollection = require 'telem.lib.MetricCollection' } -- _Telem.util.log('init') diff --git a/src/telem/lib/Metric.lua b/src/telem/lib/Metric.lua index c3425aa..42dbd51 100644 --- a/src/telem/lib/Metric.lua +++ b/src/telem/lib/Metric.lua @@ -13,6 +13,8 @@ function Metric:constructor (data, data2) datum = { name = data, value = data2 } end + assert(type(datum.value) == 'number', 'Metric value must be a number') + self.name = assert(datum.name, 'Metric must have a name') self.value = assert(datum.value, 'Metric must have a value') self.unit = datum.unit or nil diff --git a/src/telem/lib/input.lua b/src/telem/lib/input.lua index b80329c..f369ea8 100644 --- a/src/telem/lib/input.lua +++ b/src/telem/lib/input.lua @@ -1,5 +1,6 @@ return { helloWorld = require 'telem.lib.input.HelloWorldInputAdapter', + custom = require 'telem.lib.input.CustomInputAdapter', -- storage itemStorage = require 'telem.lib.input.ItemStorageInputAdapter', diff --git a/src/telem/lib/input/CustomInputAdapter.lua b/src/telem/lib/input/CustomInputAdapter.lua new file mode 100644 index 0000000..4290feb --- /dev/null +++ b/src/telem/lib/input/CustomInputAdapter.lua @@ -0,0 +1,21 @@ +local o = require 'telem.lib.ObjectModel' +local t = require 'telem.lib.util' + +local InputAdapter = require 'telem.lib.InputAdapter' +local Metric = require 'telem.lib.Metric' +local MetricCollection = require 'telem.lib.MetricCollection' + +local CustomInputAdapter = o.class(InputAdapter) +CustomInputAdapter.type = 'CustomInputAdapter' + +function CustomInputAdapter:constructor (func) + self.readlambda = func + + self:super('constructor') +end + +function CustomInputAdapter:read () + return MetricCollection(self.readlambda()) +end + +return CustomInputAdapter \ No newline at end of file diff --git a/src/telem/lib/output.lua b/src/telem/lib/output.lua index 6203f11..291a755 100644 --- a/src/telem/lib/output.lua +++ b/src/telem/lib/output.lua @@ -1,5 +1,6 @@ return { helloWorld = require 'telem.lib.output.HelloWorldOutputAdapter', + custom = require 'telem.lib.output.CustomOutputAdapter', -- HTTP grafana = require 'telem.lib.output.GrafanaOutputAdapter', diff --git a/src/telem/lib/output/CustomOutputAdapter.lua b/src/telem/lib/output/CustomOutputAdapter.lua new file mode 100644 index 0000000..e176fca --- /dev/null +++ b/src/telem/lib/output/CustomOutputAdapter.lua @@ -0,0 +1,22 @@ +local o = require 'telem.lib.ObjectModel' +local t = require 'telem.lib.util' + +local OutputAdapter = require 'telem.lib.OutputAdapter' +local MetricCollection = require 'telem.lib.MetricCollection' + +local CustomOutputAdapter = o.class(OutputAdapter) +CustomOutputAdapter.type = 'CustomOutputAdapter' + +function CustomOutputAdapter:constructor (func) + self.writelambda = func + + self:super('constructor') +end + +function CustomOutputAdapter:write (collection) + assert(o.instanceof(collection, MetricCollection), 'Collection must be a MetricCollection') + + self.writelambda(collection.metrics, collection.context) +end + +return CustomOutputAdapter \ No newline at end of file