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.
64 lines
1.2 KiB
64 lines
1.2 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() |
|
|
|
-- generate json |
|
jsonString = game.table_to_json(this.metrics) |
|
|
|
-- write to disk |
|
game.write_file(this.filename, jsonString) |
|
|
|
-- clear json |
|
jsonString = nil |
|
|
|
-- reset local cache |
|
this.metrics = {} |
|
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 |