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 Basalt Label output adapter #25

Merged
merged 2 commits into from
Oct 21, 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
23 changes: 23 additions & 0 deletions src/telem/lib/MetricCollection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,27 @@ function MetricCollection:setContext (ctx)
return self
end

-- return first metric matching name@adapter
function MetricCollection:find (filter)
local split = {}

for sv in (filter .. '@'):gmatch('([^@]*)@') do
table.insert(split, sv)
end

local name = split[1]
local adapter = split[2]

local nameish = name ~= nil and #name > 0
local adapterish = adapter ~= nil and #adapter > 0

for _,v in pairs(self.metrics) do
if (not nameish or v.name == name) and (not adapterish or v.adapter == adapter) then
return v
end
end

return nil
end

return MetricCollection
5 changes: 5 additions & 0 deletions src/telem/lib/output.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ return {

-- HTTP
grafana = require 'telem.lib.output.GrafanaOutputAdapter',

-- Basalt
basalt = {
label = require 'telem.lib.output.basalt.LabelOutputAdapter',
}
}
86 changes: 86 additions & 0 deletions src/telem/lib/output/basalt/LabelOutputAdapter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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 LabelOutputAdapter = o.class(OutputAdapter)
LabelOutputAdapter.type = 'LabelOutputAdapter'

function LabelOutputAdapter:constructor (frame, filter, bg, fg, fontSize)
self:super('constructor')

self.bBaseFrame = assert(frame, 'Frame is required')
self.filter = assert(filter, 'Filter is required')
self.nameScroll = 1
self.nameText = '-----'

self:register(bg, fg, fontSize)
end

function LabelOutputAdapter:register (bg, fg, fontSize)
self.bInnerFrame = self.bBaseFrame

-- TODO idk if this inner frame is necessary
self.bInnerFrame = self.bBaseFrame:addFrame()
:setBackground(bg)
:setSize('{parent.w}', '{parent.h}')

self.bLabelValue = self.bInnerFrame
:addLabel()
:setText("-----")
:setFontSize(fontSize or 2)
:setBackground(bg)
:setForeground(fg)
:setPosition('{parent.w/2-self.w/2}', '{parent.h/2-self.h/2}')

self.bLabelName = self.bInnerFrame
:addLabel()
:setText(self.nameText)
:setBackground(bg)
:setForeground(fg)
:setPosition(1,1)

self.animThread = self.bInnerFrame:addThread()
:start(function ()
while true do
local goslep = 0.2
self.nameScroll = self.nameScroll + 1
if self.nameScroll > self.nameText:len() + 3 then
self.nameScroll = 0
goslep = 3
end
self:refreshLabels()
t.sleep(goslep)
end
end)
end

function LabelOutputAdapter:refreshLabels ()
local width = self.bBaseFrame:getWidth()
local newtext = '-----'

if self.nameText:len() > width then
newtext = (self.nameText .. ' ' .. string.char(183) .. ' ' .. self.nameText):sub(self.nameScroll)
else
newtext = self.nameText
end

self.bLabelName:setText(newtext)
end

function LabelOutputAdapter:write (collection)
assert(o.instanceof(collection, MetricCollection), 'Collection must be a MetricCollection')

local resultMetric = collection:find(self.filter)

assert(resultMetric, 'could not find metric')

self.bLabelValue:setText(t.shortnum(resultMetric.value))
self.nameText = resultMetric.name
self:refreshLabels()

return self
end

return LabelOutputAdapter
33 changes: 31 additions & 2 deletions src/telem/lib/util.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- TODO write my own pretty_print
local pretty = { pretty_print = print }
local pretty = require 'cc.pretty' or { pretty_print = print }

local function tsleep(num)
local sec = tonumber(os.clock() + num)
Expand Down Expand Up @@ -33,10 +33,39 @@ local function skpairs(t, f)
return iter
end

local function shortnum(n)
if n >= 10^11 then
return string.format("%i G", n / 10^9)
elseif n >= 10^10 then
return string.format("%.1fG", n / 10^9)
elseif n >= 10^9 then
return string.format("%.2fG", n / 10^9)
elseif n >= 10^8 then
return string.format("%i M", n / 10^6)
elseif n >= 10^7 then
return string.format("%.1fM", n / 10^6)
elseif n >= 10^6 then
return string.format("%.2fM", n / 10^6)
elseif n >= 10^5 then
return string.format("%i k", n / 10^3)
elseif n >= 10^4 then
return string.format("%.1fk", n / 10^3)
elseif n >= 10^3 then
return string.format("%.2fk", n / 10^3)
elseif n >= 10^2 then
return string.format("%.1f", n)
elseif n >= 10^1 then
return string.format("%.2f", n)
else
return string.format("%.3f", n)
end
end

return {
log = log,
err = err,
pprint = pprint,
skpairs = skpairs,
sleep = os.sleep or tsleep
sleep = os.sleep or tsleep,
shortnum = shortnum,
}
Loading