From c302cd25da3efcaee22b6eb443efbaf346d130c4 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 25 Jul 2022 18:05:53 +0100 Subject: [PATCH] Implement API endpoint for network listing --- api/views.py | 33 +++++++++++++++++++++++++++++++++ modules/monitor.py | 2 +- modules/userinfo.py | 8 ++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/api/views.py b/api/views.py index 84a5356..7912a62 100644 --- a/api/views.py +++ b/api/views.py @@ -157,3 +157,36 @@ class API(object): results = userinfo.getChanNum(net, data["query"]) return dumps(results) + + @app.route("/irc/stats/", methods=["POST"]) + @login_required + def irc_stats(self, request): + stats = {} + numChannels = 0 + numWhoEntries = 0 + for i in main.IRCPool.keys(): + numChannels += len(main.IRCPool[i].channels) + numWhoEntries += userinfo.getNumTotalWhoEntries() + numRelays = 0 + for net in main.network.keys(): + numRelays += len(main.network[net].relays) + stats["servers_total_total"] = numRelays + stats["servers_total_unique"] = len(main.network.keys()) + stats["servers_online_total"] = len(main.IRCPool.keys()) + stats["servers_online_unique"] = len(main.liveNets()) + stats["channels"] = numChannels + stats["records"] = numWhoEntries + stats["eventrate"] = main.lastMinuteSample + return dumps(stats) + + @app.route("/irc/networks/", methods=["POST"]) + @login_required + def irc_networks(self, request): + networks = {} + for net in main.network.keys(): + networks[net] = { + "relays": len(main.network[net].relays), + "channels": userinfo.getTotalChanNum(net), + "records": userinfo.getNumWhoEntries(net), + } + return dumps(networks) diff --git a/modules/monitor.py b/modules/monitor.py index 1ada7e5..ae4905c 100644 --- a/modules/monitor.py +++ b/modules/monitor.py @@ -1,8 +1,8 @@ +import main from core.logstash import sendLogstashNotification from core.relay import sendRelayNotification from modules import userinfo from utils.dedup import dedup -import main order = [ "type", diff --git a/modules/userinfo.py b/modules/userinfo.py index 38246e3..d281674 100644 --- a/modules/userinfo.py +++ b/modules/userinfo.py @@ -38,6 +38,14 @@ def getChanList(name, nick): return (i.decode() for i in result) +def getTotalChanNum(net): + """ + Get the number of channels a network has. + """ + chans = main.r.keys(f"live.who.{net}.*") + return len(chans) + + def getUserNum(name, channels): """ Get the number of users on a list of channels.