Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom adapters #23

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/telem/init.lua
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
2 changes: 2 additions & 0 deletions src/telem/lib/Metric.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/telem/lib/input.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
return {
helloWorld = require 'telem.lib.input.HelloWorldInputAdapter',
custom = require 'telem.lib.input.CustomInputAdapter',

-- storage
itemStorage = require 'telem.lib.input.ItemStorageInputAdapter',
Expand Down
21 changes: 21 additions & 0 deletions src/telem/lib/input/CustomInputAdapter.lua
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/telem/lib/output.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
return {
helloWorld = require 'telem.lib.output.HelloWorldOutputAdapter',
custom = require 'telem.lib.output.CustomOutputAdapter',

-- HTTP
grafana = require 'telem.lib.output.GrafanaOutputAdapter',
Expand Down
22 changes: 22 additions & 0 deletions src/telem/lib/output/CustomOutputAdapter.lua
Original file line number Diff line number Diff line change
@@ -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
Loading