diff --git a/src/telem/lib/MetricCollection.lua b/src/telem/lib/MetricCollection.lua index 10a68ef..943add4 100644 --- a/src/telem/lib/MetricCollection.lua +++ b/src/telem/lib/MetricCollection.lua @@ -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 \ No newline at end of file diff --git a/src/telem/lib/output.lua b/src/telem/lib/output.lua index 291a755..796597e 100644 --- a/src/telem/lib/output.lua +++ b/src/telem/lib/output.lua @@ -4,4 +4,9 @@ return { -- HTTP grafana = require 'telem.lib.output.GrafanaOutputAdapter', + + -- Basalt + basalt = { + label = require 'telem.lib.output.basalt.LabelOutputAdapter', + } } \ No newline at end of file diff --git a/src/telem/lib/output/basalt/LabelOutputAdapter.lua b/src/telem/lib/output/basalt/LabelOutputAdapter.lua new file mode 100644 index 0000000..bf9d12b --- /dev/null +++ b/src/telem/lib/output/basalt/LabelOutputAdapter.lua @@ -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 \ No newline at end of file diff --git a/src/telem/lib/util.lua b/src/telem/lib/util.lua index bbe68ae..310833c 100644 --- a/src/telem/lib/util.lua +++ b/src/telem/lib/util.lua @@ -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) @@ -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, } \ No newline at end of file