From 267930ad7aba1c3c98066df7b4266e1a0841a242 Mon Sep 17 00:00:00 2001 From: cyberbit Date: Wed, 11 Oct 2023 17:41:31 +0000 Subject: [PATCH 1/2] stash basalt progress, still incomplete --- src/telem/lib/MetricCollection.lua | 23 +++ src/telem/lib/output.lua | 6 + .../lib/output/basalt/GraphOutputAdapter.lua | 187 ++++++++++++++++++ .../lib/output/basalt/LabelOutputAdapter.lua | 77 ++++++++ src/telem/lib/util.lua | 33 +++- 5 files changed, 324 insertions(+), 2 deletions(-) create mode 100644 src/telem/lib/output/basalt/GraphOutputAdapter.lua create mode 100644 src/telem/lib/output/basalt/LabelOutputAdapter.lua diff --git a/src/telem/lib/MetricCollection.lua b/src/telem/lib/MetricCollection.lua index 10a68ef..c12b97a 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[3] or 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..ab3291e 100644 --- a/src/telem/lib/output.lua +++ b/src/telem/lib/output.lua @@ -4,4 +4,10 @@ return { -- HTTP grafana = require 'telem.lib.output.GrafanaOutputAdapter', + + -- Basalt + basalt = { + label = require 'telem.lib.output.basalt.LabelOutputAdapter', + -- graph = require 'telem.lib.output.basalt.GraphOutputAdapter', + } } \ No newline at end of file diff --git a/src/telem/lib/output/basalt/GraphOutputAdapter.lua b/src/telem/lib/output/basalt/GraphOutputAdapter.lua new file mode 100644 index 0000000..8e62ef9 --- /dev/null +++ b/src/telem/lib/output/basalt/GraphOutputAdapter.lua @@ -0,0 +1,187 @@ +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 GraphOutputAdapter = o.class(OutputAdapter) +GraphOutputAdapter.type = 'GraphOutputAdapter' + +function GraphOutputAdapter:constructor (endpoint, apiKey) + self:super('constructor') + + self.endpoint = assert(endpoint, 'Endpoint is required') + self.apiKey = assert(apiKey, 'API key is required') +end + +function GraphOutputAdapter:write (collection) + assert(o.instanceof(collection, MetricCollection), 'Collection must be a MetricCollection') + + local outf = {} + + for _,v in pairs(collection.metrics) do + local unitreal = (v.unit and v.unit ~= '' and ',unit=' .. v.unit) or '' + local sourcereal = (v.source and v.source ~= '' and ',source=' .. v.source) or '' + local adapterreal = (v.adapter and v.adapter ~= '' and ',adapter=' .. v.adapter) or '' + + table.insert(outf, v.name .. unitreal .. sourcereal .. adapterreal .. (' metric=%f'):format(v.value)) + end + + -- t.pprint(collection) + + local res = http.post({ + url = self.endpoint, + body = table.concat(outf, '\n'), + headers = { Authorization = ('Bearer %s'):format(self.apiKey) } + }) +end + +return GraphOutputAdapter + + + + + +-------------------- +-------------------- + + +-- local pprint = require('cc.pretty').pretty_print +-- local shortnum = require('shortnum') +-- local basalt = require 'basalt' + +-- local MAX_ENTRIES = 50 +-- local SCALE_TICK = 10 + +-- local base = basalt.createFrame('base'):setBackground(colors.black) +-- local frame = base:addFrame('frame') +-- :setSize('{parent.w}', '{parent.h}') +-- :setBackground(colors.black) + +-- local fGraph = frame:addFrame('fGraph'):setBackground(colors.black) +-- :setPosition(1,1) +-- :setSize('{parent.w - 2}', '{parent.h - 6}') +-- -- :setFlexGrow(0) +-- -- :setFlexShrink(0) + +-- local fLabel = frame:addFrame('fLabel'):setBackground(colors.black) +-- :setSize('{parent.w - 2}', 4) +-- :setPosition(1,'{parent.h - 5}') +-- :setBorder(colors.green) +-- -- :setFlexGrow(1) + +-- local fLabelMax = frame:addFrame('fLabelMax'):setBackground(colors.black) +-- :setSize(6, 1) +-- :setPosition('{parent.w - 7}',1) + +-- local fLabelMin = frame:addFrame('fLabelMin'):setBackground(colors.black) +-- :setSize(6, 1) +-- :setPosition('{parent.w - 7}','{fLabel.y - 2}') + +-- local label = fLabel:addLabel() +-- :setText("-----") +-- :setFontSize(2) +-- :setPosition('{parent.w/2-self.w/2}', 2) +-- :setForeground(colors.white) +-- :setBackground(colors.black) +-- local graph = fGraph:addGraph() +-- :setPosition(1,1) +-- :setSize('{parent.w - 1}', '{parent.h - 1}') +-- :setMaxEntries(MAX_ENTRIES) +-- :setBackground(colors.black) +-- :setGraphColor(colors.red) +-- :setGraphSymbol('\127') + +-- local graphscale = fGraph:addGraph() +-- :setGraphType('scatter') +-- :setPosition(1,'{parent.h - 1}') +-- :setSize('{parent.w - 1}', 2) +-- :setMaxEntries(MAX_ENTRIES) +-- :setBackground(colors.transparent) +-- -- :setGraphColor(colors.lightGray) +-- :setGraphSymbol('|') + +-- local labelmax = fLabelMax:addLabel() +-- :setPosition(1,1) +-- -- :setSize(5,1) +-- :setText('-----') +-- :setForeground(colors.white) +-- :setBackground(colors.black) + +-- local labelmin = fLabelMin:addLabel() +-- :setPosition(1,1) +-- -- :setSize(5,1) +-- :setText('-----') +-- :setForeground(colors.white) +-- :setBackground(colors.black) + +-- local graphdata = {} + +-- -- this breaks it really bad +-- -- graph:setMinValue(-100000):setMaxValue(-100000) +-- -- for i = 1,MAX_ENTRIES,1 do +-- -- graph:addDataPoint(-100000) +-- -- end +-- -- graph:setMinValue(0) + +-- local function graphtrack (value) +-- -- print('graphtrack') +-- table.insert(graphdata, value) +-- while #graphdata>MAX_ENTRIES do +-- table.remove(graphdata,1) +-- end +-- end + +-- local function graphtrackrange () +-- -- print('graphtrackrange') +-- local min = graphdata[1] +-- local max = graphdata[1] +-- for k,v in ipairs(graphdata) do +-- if v < min then min = v end +-- if v > max then max = v end +-- end +-- -- print('min '..min..' max '..max) +-- return min,max +-- end + +-- local currentmin = 0 +-- local currentmax = 1000 + +-- local metric = 0 +-- local tick = 0 + +-- graph:setMinValue(currentmin):setMaxValue(currentmax) + +-- parallel.waitForAll( +-- basalt.autoUpdate, + +-- function () +-- while true do +-- local newnum = math.random(-90,100) +-- -- local newnum = math.random(90,100) +-- metric = metric + newnum + +-- graphtrack(metric) + +-- local newmin, newmax = graphtrackrange() +-- graph:setMinValue(newmin):setMaxValue(newmax) + +-- graph:addDataPoint(metric) + +-- label:setText(shortnum(metric)) + +-- if tick == SCALE_TICK then +-- graphscale:addDataPoint(100) +-- tick = 1 +-- else +-- graphscale:addDataPoint(50) +-- tick = tick + 1 +-- end + +-- labelmax:setText(shortnum(newmax)) +-- labelmin:setText(shortnum(newmin)) + +-- sleep(0.1) +-- end +-- end +-- ) \ 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..396c4d3 --- /dev/null +++ b/src/telem/lib/output/basalt/LabelOutputAdapter.lua @@ -0,0 +1,77 @@ +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) + 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) +end + +function LabelOutputAdapter:register (bg, fg) + self.bInnerframe = self.bBaseframe + :addFrame() + :setBackground(bg) + :setSize('{parent.w}', '{parent.h}') + + self.bLabelValue = self.bInnerframe + :addLabel() + :setText("-----") + :setFontSize(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() + self.animThread:start(function () + while true do + local goslep = 0.2 + print (self.nameScroll) + print(self.nameText) + self.nameScroll = self.nameScroll + 1 + if self.nameScroll > self.nameText:len() + 1 then + self.nameScroll = 0 + goslep = 3 + end + self:refreshLabels() + sleep(goslep) + end + end) +end + +function LabelOutputAdapter:refreshLabels () + self.bLabelName:setText(self.nameText:sub(self.nameScroll)) +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 From cb146523741e942c25e761864e220af2c28e07ea Mon Sep 17 00:00:00 2001 From: cyberbit Date: Sat, 21 Oct 2023 01:26:31 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=A6=20update=20find=20method=20on?= =?UTF-8?q?=20collection=20and=20finalize=20label=20output?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/telem/lib/MetricCollection.lua | 4 +- src/telem/lib/output.lua | 1 - .../lib/output/basalt/GraphOutputAdapter.lua | 187 ------------------ .../lib/output/basalt/LabelOutputAdapter.lua | 57 +++--- 4 files changed, 35 insertions(+), 214 deletions(-) delete mode 100644 src/telem/lib/output/basalt/GraphOutputAdapter.lua diff --git a/src/telem/lib/MetricCollection.lua b/src/telem/lib/MetricCollection.lua index c12b97a..943add4 100644 --- a/src/telem/lib/MetricCollection.lua +++ b/src/telem/lib/MetricCollection.lua @@ -40,12 +40,12 @@ end function MetricCollection:find (filter) local split = {} - for sv in filter:gmatch('[^@]*') do + for sv in (filter .. '@'):gmatch('([^@]*)@') do table.insert(split, sv) end local name = split[1] - local adapter = split[3] or split[2] + local adapter = split[2] local nameish = name ~= nil and #name > 0 local adapterish = adapter ~= nil and #adapter > 0 diff --git a/src/telem/lib/output.lua b/src/telem/lib/output.lua index ab3291e..796597e 100644 --- a/src/telem/lib/output.lua +++ b/src/telem/lib/output.lua @@ -8,6 +8,5 @@ return { -- Basalt basalt = { label = require 'telem.lib.output.basalt.LabelOutputAdapter', - -- graph = require 'telem.lib.output.basalt.GraphOutputAdapter', } } \ No newline at end of file diff --git a/src/telem/lib/output/basalt/GraphOutputAdapter.lua b/src/telem/lib/output/basalt/GraphOutputAdapter.lua deleted file mode 100644 index 8e62ef9..0000000 --- a/src/telem/lib/output/basalt/GraphOutputAdapter.lua +++ /dev/null @@ -1,187 +0,0 @@ -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 GraphOutputAdapter = o.class(OutputAdapter) -GraphOutputAdapter.type = 'GraphOutputAdapter' - -function GraphOutputAdapter:constructor (endpoint, apiKey) - self:super('constructor') - - self.endpoint = assert(endpoint, 'Endpoint is required') - self.apiKey = assert(apiKey, 'API key is required') -end - -function GraphOutputAdapter:write (collection) - assert(o.instanceof(collection, MetricCollection), 'Collection must be a MetricCollection') - - local outf = {} - - for _,v in pairs(collection.metrics) do - local unitreal = (v.unit and v.unit ~= '' and ',unit=' .. v.unit) or '' - local sourcereal = (v.source and v.source ~= '' and ',source=' .. v.source) or '' - local adapterreal = (v.adapter and v.adapter ~= '' and ',adapter=' .. v.adapter) or '' - - table.insert(outf, v.name .. unitreal .. sourcereal .. adapterreal .. (' metric=%f'):format(v.value)) - end - - -- t.pprint(collection) - - local res = http.post({ - url = self.endpoint, - body = table.concat(outf, '\n'), - headers = { Authorization = ('Bearer %s'):format(self.apiKey) } - }) -end - -return GraphOutputAdapter - - - - - --------------------- --------------------- - - --- local pprint = require('cc.pretty').pretty_print --- local shortnum = require('shortnum') --- local basalt = require 'basalt' - --- local MAX_ENTRIES = 50 --- local SCALE_TICK = 10 - --- local base = basalt.createFrame('base'):setBackground(colors.black) --- local frame = base:addFrame('frame') --- :setSize('{parent.w}', '{parent.h}') --- :setBackground(colors.black) - --- local fGraph = frame:addFrame('fGraph'):setBackground(colors.black) --- :setPosition(1,1) --- :setSize('{parent.w - 2}', '{parent.h - 6}') --- -- :setFlexGrow(0) --- -- :setFlexShrink(0) - --- local fLabel = frame:addFrame('fLabel'):setBackground(colors.black) --- :setSize('{parent.w - 2}', 4) --- :setPosition(1,'{parent.h - 5}') --- :setBorder(colors.green) --- -- :setFlexGrow(1) - --- local fLabelMax = frame:addFrame('fLabelMax'):setBackground(colors.black) --- :setSize(6, 1) --- :setPosition('{parent.w - 7}',1) - --- local fLabelMin = frame:addFrame('fLabelMin'):setBackground(colors.black) --- :setSize(6, 1) --- :setPosition('{parent.w - 7}','{fLabel.y - 2}') - --- local label = fLabel:addLabel() --- :setText("-----") --- :setFontSize(2) --- :setPosition('{parent.w/2-self.w/2}', 2) --- :setForeground(colors.white) --- :setBackground(colors.black) --- local graph = fGraph:addGraph() --- :setPosition(1,1) --- :setSize('{parent.w - 1}', '{parent.h - 1}') --- :setMaxEntries(MAX_ENTRIES) --- :setBackground(colors.black) --- :setGraphColor(colors.red) --- :setGraphSymbol('\127') - --- local graphscale = fGraph:addGraph() --- :setGraphType('scatter') --- :setPosition(1,'{parent.h - 1}') --- :setSize('{parent.w - 1}', 2) --- :setMaxEntries(MAX_ENTRIES) --- :setBackground(colors.transparent) --- -- :setGraphColor(colors.lightGray) --- :setGraphSymbol('|') - --- local labelmax = fLabelMax:addLabel() --- :setPosition(1,1) --- -- :setSize(5,1) --- :setText('-----') --- :setForeground(colors.white) --- :setBackground(colors.black) - --- local labelmin = fLabelMin:addLabel() --- :setPosition(1,1) --- -- :setSize(5,1) --- :setText('-----') --- :setForeground(colors.white) --- :setBackground(colors.black) - --- local graphdata = {} - --- -- this breaks it really bad --- -- graph:setMinValue(-100000):setMaxValue(-100000) --- -- for i = 1,MAX_ENTRIES,1 do --- -- graph:addDataPoint(-100000) --- -- end --- -- graph:setMinValue(0) - --- local function graphtrack (value) --- -- print('graphtrack') --- table.insert(graphdata, value) --- while #graphdata>MAX_ENTRIES do --- table.remove(graphdata,1) --- end --- end - --- local function graphtrackrange () --- -- print('graphtrackrange') --- local min = graphdata[1] --- local max = graphdata[1] --- for k,v in ipairs(graphdata) do --- if v < min then min = v end --- if v > max then max = v end --- end --- -- print('min '..min..' max '..max) --- return min,max --- end - --- local currentmin = 0 --- local currentmax = 1000 - --- local metric = 0 --- local tick = 0 - --- graph:setMinValue(currentmin):setMaxValue(currentmax) - --- parallel.waitForAll( --- basalt.autoUpdate, - --- function () --- while true do --- local newnum = math.random(-90,100) --- -- local newnum = math.random(90,100) --- metric = metric + newnum - --- graphtrack(metric) - --- local newmin, newmax = graphtrackrange() --- graph:setMinValue(newmin):setMaxValue(newmax) - --- graph:addDataPoint(metric) - --- label:setText(shortnum(metric)) - --- if tick == SCALE_TICK then --- graphscale:addDataPoint(100) --- tick = 1 --- else --- graphscale:addDataPoint(50) --- tick = tick + 1 --- end - --- labelmax:setText(shortnum(newmax)) --- labelmin:setText(shortnum(newmin)) - --- sleep(0.1) --- end --- end --- ) \ No newline at end of file diff --git a/src/telem/lib/output/basalt/LabelOutputAdapter.lua b/src/telem/lib/output/basalt/LabelOutputAdapter.lua index 396c4d3..bf9d12b 100644 --- a/src/telem/lib/output/basalt/LabelOutputAdapter.lua +++ b/src/telem/lib/output/basalt/LabelOutputAdapter.lua @@ -7,57 +7,66 @@ local MetricCollection = require 'telem.lib.MetricCollection' local LabelOutputAdapter = o.class(OutputAdapter) LabelOutputAdapter.type = 'LabelOutputAdapter' -function LabelOutputAdapter:constructor (frame, filter, bg, fg) +function LabelOutputAdapter:constructor (frame, filter, bg, fg, fontSize) self:super('constructor') - self.bBaseframe = assert(frame, 'Frame is required') + self.bBaseFrame = assert(frame, 'Frame is required') self.filter = assert(filter, 'Filter is required') self.nameScroll = 1 self.nameText = '-----' - self:register(bg, fg) + self:register(bg, fg, fontSize) end -function LabelOutputAdapter:register (bg, fg) - self.bInnerframe = self.bBaseframe - :addFrame() +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 + self.bLabelValue = self.bInnerFrame :addLabel() :setText("-----") - :setFontSize(2) + :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 + self.bLabelName = self.bInnerFrame :addLabel() :setText(self.nameText) :setBackground(bg) :setForeground(fg) :setPosition(1,1) - self.animThread = self.bInnerframe:addThread() - self.animThread:start(function () - while true do - local goslep = 0.2 - print (self.nameScroll) - print(self.nameText) - self.nameScroll = self.nameScroll + 1 - if self.nameScroll > self.nameText:len() + 1 then - self.nameScroll = 0 - goslep = 3 + 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 - self:refreshLabels() - sleep(goslep) - end - end) + end) end function LabelOutputAdapter:refreshLabels () - self.bLabelName:setText(self.nameText:sub(self.nameScroll)) + 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)