commit 755077b: [Minor] Statistics_dump: Fix formatting

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Apr 27 13:42:06 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-04-27 14:37:11 +0100
URL: https://github.com/rspamd/rspamd/commit/755077b555ee224a1655308cc2b3fa8aa9741bfb (HEAD -> master)

[Minor] Statistics_dump: Fix formatting

---
 lualib/rspamadm/statistics_dump.lua | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/lualib/rspamadm/statistics_dump.lua b/lualib/rspamadm/statistics_dump.lua
index ec8892002..a11c79129 100644
--- a/lualib/rspamadm/statistics_dump.lua
+++ b/lualib/rspamadm/statistics_dump.lua
@@ -142,7 +142,6 @@ local function check_redis_classifier(cls, cfg)
 end
 
 local function dump_handler(opts)
-  io.write('[\n')
 
   for _,cls in ipairs(classifiers) do
     local res,conn = lua_redis.redis_connect_sync(cls.redis_params, false)
@@ -153,6 +152,7 @@ local function dump_handler(opts)
     end
 
     local cursor = 0
+    local has_hdr = false
     local compress_ctx
     if opts.compress then
       compress_ctx = rspamd_zstd.compress_ctx()
@@ -175,6 +175,10 @@ local function dump_handler(opts)
       local tokens = {}
       local out = {}
 
+      if not has_hdr then
+        out[1] = "{\n"
+        has_hdr = true
+      end
       for _,e in ipairs(elts) do
         conn:add_cmd('HGETALL', {e})
       end
@@ -198,18 +202,18 @@ local function dump_handler(opts)
 
       -- Output keeping track of the commas
       for i,d in ipairs(tokens) do
-        if i == #tokens then
-          out[#out + 1] = rspamd_logger.slog('{"%s": %s}\n', d.key,
+        if cursor == 0 and i == #tokens then
+          out[#out + 1] = rspamd_logger.slog('"%s": %s\n', d.key,
               ucl.to_format(d.data, "json-compact"))
         else
-          out[#out + 1] = rspamd_logger.slog('{"%s": %s},\n', d.key,
+          out[#out + 1] = rspamd_logger.slog('"%s": %s,\n', d.key,
               ucl.to_format(d.data, "json-compact"))
         end
 
       end
 
       if cursor == 0 then
-        out[#out + 1] = ']\n'
+        out[#out + 1] = '}\n'
       end
 
       if compress_ctx then


More information about the Commits mailing list