You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.7 KiB
81 lines
1.7 KiB
-- ___ _ _ _ |
|
-- / __| |_ __ _| |_ ___ _ _(_)___ |
|
-- \__ \ _/ _` | _/ _ \ '_| / _ \ |
|
-- |___/\__\__,_|\__\___/_| |_\___/ |
|
-- |
|
-- Caches the latest metrics during |
|
-- collection and writes data files |
|
-- on demand. |
|
|
|
|
|
local this = {} |
|
|
|
-- keep filename in case it needs to be deleted |
|
-- after the setting is changed |
|
this.filename = nil |
|
|
|
-- local cache of metrics before publication |
|
this.metrics = {} |
|
|
|
|
|
this.init = function() |
|
this.setFilename(Settings.getRuntimeGlobal("statorio-filename")) |
|
end |
|
|
|
-- queue a metric to be published |
|
this.addMetric = function(name, val) |
|
-- TODO |
|
-- option to swap bool to 1|0 |
|
-- option to swap nil to 0 |
|
|
|
this.metrics[name] = val |
|
end |
|
|
|
-- publish stored metrics |
|
this.publish = function() |
|
if get_file_extension(this.filename) == "statsd" then |
|
this.publishStatsd() |
|
else |
|
this.publishJson() |
|
end |
|
|
|
-- reset local cache |
|
this.metrics = {} |
|
end |
|
|
|
this.publishStatsd = function() |
|
-- generate lines of "metric_name:value|g" |
|
statsdString = "" |
|
for key, val in pairs(this.metrics) do |
|
if type(val) == "string" then |
|
statsdString = statsdString..key..":"..val.."|s\n" |
|
else |
|
statsdString = statsdString..key..":"..val.."|g\n" |
|
end |
|
end |
|
|
|
game.write_file(this.filename, statsdString) |
|
end |
|
|
|
this.publishJson = function() |
|
-- generate json |
|
jsonString = game.table_to_json(this.metrics) |
|
|
|
-- write to disk |
|
game.write_file(this.filename, jsonString) |
|
end |
|
|
|
this.setFilename = function(filename) |
|
|
|
if this.filename ~= nil then |
|
-- remove old file first |
|
log("Deleting old stats file "..this.filename) |
|
game.remove_path(this.filename) |
|
end |
|
|
|
-- set as filename for subsequent writes |
|
this.filename = filename |
|
end |
|
|
|
|
|
return this |