Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Commit

Permalink
import
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Jun 26, 2020
0 parents commit f0a9c2c
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/luacheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: luacheck

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: apt
run: sudo apt-get install -y luarocks
- name: luacheck install
run: luarocks install --local luacheck
- name: luacheck run
run: $HOME/.luarocks/bin/luacheck ./
18 changes: 18 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
allow_defined_top = true

globals = {
"monitoring",
"minetest",
"advtrains"
}

read_globals = {
-- Stdlib
string = {fields = {"split"}},
table = {fields = {"copy", "getn"}},

-- Minetest
"vector", "ItemStack",
"dump"

}
79 changes: 79 additions & 0 deletions cleanup.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

local metric_train_count = monitoring.gauge("advtrains_active_train_count", "count of active trains")


-- list of active trains
local active_trains = {}

local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime

-- every 10 seconds
if timer < 10 then return end
timer = 0

for _, train in pairs(advtrains.trains) do
if train.velocity > 0 then
-- train moved, mark as active
active_trains[train.id] = true
end
end

-- update metrics
local active_train_count = 0
for _ in pairs(active_trains) do
active_train_count = active_train_count + 1
end
metric_train_count.set(active_train_count)

end)


minetest.register_chatcommand("advtrains_remove", {
description = "cleans up the train with the given id",
privs = { server = true },
func = function(_, param)
advtrains.remove_train(param)
end
})

minetest.register_chatcommand("advtrains_cleanup", {
description = "cleans up all non-automated trains",
privs = { server = true },
func = function()
local count = 0
for _, train in pairs(advtrains.trains) do
if not active_trains[train.id] then
advtrains.remove_train(train.id)
count = count + 1
end
end
return true, "Removed " .. count .. " trains"
end
})

minetest.register_chatcommand("advtrains_stats", {
description = "show advtrains stats",
func = function()
local train_count = 0
local wagon_count = 0
local active_train_count = 0

for _ in pairs(active_trains) do
active_train_count = active_train_count + 1
end

for _, train in pairs(advtrains.trains) do
train_count = train_count + 1
for _, part in pairs(train.trainparts) do
local wagon = advtrains.wagons[part]
if wagon ~= nil then
wagon_count = wagon_count + 1
end
end
end

return true, "Trains: " .. train_count .. " Wagons: " .. wagon_count .. " Active trains: " .. active_train_count
end
})
12 changes: 12 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

if not minetest.get_modpath("advtrains") then
print("[monitoring] advtrains extension not loaded")
return
else
print("[monitoring] advtrains extension loaded")
end

local MP = minetest.get_modpath("monitoring_advtrains")

dofile(MP.."/metrics.lua")
dofile(MP.."/cleanup.lua")
56 changes: 56 additions & 0 deletions license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
License of source code
----------------------

The MIT License (MIT)
Copyright (C) 2018-2019 Thomas Rudin

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

For more details:
https://opensource.org/licenses/MIT


Licenses of media (textures)
----------------------------

Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)
Copyright (C) 2019 Thomas Rudin

You are free to:
Share — copy and redistribute the material in any medium or format.
Adapt — remix, transform, and build upon the material for any purpose, even commercially.
The licensor cannot revoke these freedoms as long as you follow the license terms.

Under the following terms:

Attribution — You must give appropriate credit, provide a link to the license, and
indicate if changes were made. You may do so in any reasonable manner, but not in any way
that suggests the licensor endorses you or your use.

ShareAlike — If you remix, transform, or build upon the material, you must distribute
your contributions under the same license as the original.

No additional restrictions — You may not apply legal terms or technological measures that
legally restrict others from doing anything the license permits.

Notices:

You do not have to comply with the license for elements of the material in the public
domain or where your use is permitted by an applicable exception or limitation.
No warranties are given. The license may not give you all of the permissions necessary
for your intended use. For example, other rights such as publicity, privacy, or moral
rights may limit how you use the material.
96 changes: 96 additions & 0 deletions metrics.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@


monitoring.wrap_global({"advtrains", "mainloop_trainlogic"}, "advtrains_mainloop_trainlogic")
monitoring.wrap_global({"advtrains", "ndb", "save_data"}, "advtrains_ndb_save_data")
monitoring.wrap_global({"advtrains", "avt_save"}, "advtrains_avt_save")
monitoring.wrap_global({"atlatc", "mainloop_stepcode"}, "advtrains_atlatc_mainloop_stepcode")
monitoring.wrap_global({"atlatc", "interrupt", "mainloop"}, "advtrains_atlatc_interrupt_mainloop")
monitoring.wrap_global({"atlatc", "run_stepcode"}, "advtrains_atlatc_run_stepcode")

if minetest.settings:get_bool("monitoring.advtrains.verbose") then

-- contained in advtrains.mainloop_trainlogic()
monitoring.wrap_global({"advtrains", "train_ensure_init"}, "advtrains_train_ensure_init")
monitoring.wrap_global({"advtrains", "train_step_b"}, "advtrains_train_step_b")
monitoring.wrap_global({"advtrains", "train_step_c"}, "advtrains_train_step_c")

-- utils, used by advtrains.train_step_c()
monitoring.wrap_global({"advtrains", "path_clear_unused"}, "advtrains_path_clear_unused")
monitoring.wrap_global({"advtrains", "path_setrestore"}, "advtrains_path_setrestore")
monitoring.wrap_global({"advtrains", "spawn_wagons"}, "advtrains_spawn_wagons")
monitoring.wrap_global({"advtrains", "train_check_couples"}, "advtrains_train_check_couples")
monitoring.wrap_global({"advtrains", "path_get_index_by_offset"}, "advtrains_path_get_index_by_offset")
monitoring.wrap_global({"advtrains", "path_get"}, "advtrains_path_get")

monitoring.wrap_global({"advtrains", "get_adjacent_rail"}, "advtrains_get_adjacent_rail")
monitoring.wrap_global({"advtrains", "path_get_adjacent"}, "advtrains_path_get_adjacent")
monitoring.wrap_global({"advtrains", "path_get_interpolated"}, "advtrains_path_get_interpolated")
monitoring.wrap_global({"advtrains", "get_rail_info_at"}, "advtrains_get_rail_info_at")
monitoring.wrap_global({"advtrains", "ndb", "get_node_or_nil"}, "advtrains_ndb_get_node_or_nil")
monitoring.wrap_global({"advtrains", "ndb", "update"}, "advtrains_ndb_update")
-- monitoring.wrap_global({"advtrains", ""}, "advtrains_")

end

local metric_ndb_count = monitoring.gauge("advtrains_ndb_count", "count of advtrains ndb items")
local metric_train_count = monitoring.gauge("advtrains_train_count", "count of trains")
local metric_wagon_count = monitoring.gauge("advtrains_wagon_count", "count of wagons")


local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer < 60 then return end
timer=0

local count = 0
local ndb_nodes = advtrains.ndb.get_nodes()
for _, ny in pairs(ndb_nodes) do
for _, nx in pairs(ny) do
for _ in pairs(nx) do
count = count + 1
end
end
end
metric_ndb_count.set(count)

local traincount = 0
local wagoncount = 0
for _, train in pairs(advtrains.trains) do
traincount = traincount + 1

for _ in pairs(train.trainparts) do
wagoncount = wagoncount + 1
end
end
metric_train_count.set(traincount)
metric_wagon_count.set(wagoncount)

end)


local stepnum = 1

-- advtrains globalstep
for i, globalstep in ipairs(minetest.registered_globalsteps) do
local info = minetest.callback_origins[globalstep]
if not info then
break
end

local modname = info.mod

if modname == "advtrains" then

local metric_globalstep = monitoring.counter(
"advtrains_globalstep_time_" .. stepnum,
"timing or the advtrains globalstep #" .. stepnum
)

local fn = metric_globalstep.wraptime(globalstep)
minetest.callback_origins[fn] = info
minetest.registered_globalsteps[i] = fn

stepnum = stepnum + 1
end
end
3 changes: 3 additions & 0 deletions mod.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = monitoring_advtrains
depends = monitoring
optional_depends = advtrains, advtrains_luaautomation
23 changes: 23 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

# Monitoring extension for advtrains

Base-mod: https://github.com/thomasrudin-mt/monitoring

# Metrics

* advtrains_ndb_count *count of advtrains ndb items*
* advtrains_train_count *count of trains*
* advtrains_wagon_count *count of wagons"*

# Settings

* **monitoring.advtrains.verbose** more metrics (bool)

# Chat commands

* **/advtrains_stats** shows stats about trains, wagons and the activity of them
* **/advtrains_cleanup** removes all non-moving trains

# License

MIT

0 comments on commit f0a9c2c

Please sign in to comment.