From d4c6876bc9e3669d1c7202e6d3b967af8aa6344d Mon Sep 17 00:00:00 2001 From: Zed Date: Thu, 6 Jan 2022 00:42:18 +0100 Subject: [PATCH] Add more info to /.tokens endpoint --- src/routes/debug.nim | 10 ++++++++++ src/routes/router_utils.nim | 6 +++--- src/tokens.nim | 30 +++++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 src/routes/debug.nim diff --git a/src/routes/debug.nim b/src/routes/debug.nim new file mode 100644 index 0000000..192786e --- /dev/null +++ b/src/routes/debug.nim @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: AGPL-3.0-only +import jester +import router_utils +import ".."/[tokens, types] + +proc createDebugRouter*(cfg: Config) = + router debug: + get "/.tokens": + cond cfg.enableDebug + respJson getPoolJson() diff --git a/src/routes/router_utils.nim b/src/routes/router_utils.nim index 4ae3f1d..7159890 100644 --- a/src/routes/router_utils.nim +++ b/src/routes/router_utils.nim @@ -1,5 +1,5 @@ # SPDX-License-Identifier: AGPL-3.0-only -import strutils, sequtils, uri, tables +import strutils, sequtils, uri, tables, json from jester import Request, cookies import ../views/general @@ -43,5 +43,5 @@ template getCursor*(req: Request): string = proc getNames*(name: string): seq[string] = name.strip(chars={'/'}).split(",").filterIt(it.len > 0) -template respJson*(body: string) = - resp body, "application/json" +template respJson*(node: JsonNode) = + resp $node, "application/json" diff --git a/src/tokens.nim b/src/tokens.nim index a9a7831..929a984 100644 --- a/src/tokens.nim +++ b/src/tokens.nim @@ -15,9 +15,15 @@ var tokenPool: seq[Token] lastFailed: Time -proc getPoolJson*: string = - let list = newJObject() +proc getPoolJson*(): JsonNode = + var + list = newJObject() + totalReqs = 0 + totalPending = 0 + reqsPerApi: Table[string, int] + for token in tokenPool: + totalPending.inc(token.pending) list[token.tok] = %*{ "apis": newJObject(), "pending": token.pending, @@ -27,7 +33,25 @@ proc getPoolJson*: string = for api in token.apis.keys: list[token.tok]["apis"][$api] = %token.apis[api] - return $list + + let + maxReqs = + case api + of Api.listBySlug, Api.list: 500 + of Api.timeline: 187 + else: 180 + reqs = maxReqs - token.apis[api].remaining + + reqsPerApi[$api] = reqsPerApi.getOrDefault($api, 0) + reqs + totalReqs.inc(reqs) + + return %*{ + "amount": tokenPool.len, + "requests": totalReqs, + "pending": totalPending, + "apis": reqsPerApi, + "tokens": list + } proc rateLimitError*(): ref RateLimitError = newException(RateLimitError, "rate limited")