From 9292c1116c799b14c58dec9edcc146a348dddb2f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Fri, 22 May 2020 14:31:03 -0400
Subject: [PATCH] proxy: count permanent put values by hash in stats

---
 include/opendht/dht_proxy_server.h |  5 ++++-
 src/dht_proxy_server.cpp           |  8 ++++++--
 tools/dhtproxy_stats.py            | 11 ++++-------
 3 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h
index 5977129a..5dcc4193 100644
--- a/include/opendht/dht_proxy_server.h
+++ b/include/opendht/dht_proxy_server.h
@@ -98,8 +98,10 @@ public:
     struct ServerStats {
         /** Current number of listen operations */
         size_t listenCount {0};
-        /** Current number of permanent put operations */
+        /** Current number of permanent put operations (hash used) */
         size_t putCount {0};
+        /** Current number of permanent put values */
+        size_t totalPermanentPuts {0};
         /** Current number of push tokens with at least one listen operation */
         size_t pushListenersCount {0};
         /** Average requests per second */
@@ -129,6 +131,7 @@ public:
             Json::Value result;
             result["listenCount"] = static_cast<Json::UInt64>(listenCount);
             result["putCount"] = static_cast<Json::UInt64>(putCount);
+            result["totalPermanentPuts"] = static_cast<Json::UInt64>(totalPermanentPuts);
             result["pushListenersCount"] = static_cast<Json::UInt64>(pushListenersCount);
             result["requestRate"] = requestRate;
             if (nodeInfo)
diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp
index 4ef34260..71c48310 100644
--- a/src/dht_proxy_server.cpp
+++ b/src/dht_proxy_server.cpp
@@ -555,6 +555,10 @@ DhtProxyServer::updateStats(std::shared_ptr<NodeInfo> info) const
 #ifdef OPENDHT_PUSH_NOTIFICATIONS
     stats.pushListenersCount = pushListeners_.size();
 #endif
+    stats.totalPermanentPuts = 0;
+    std::for_each(puts_.begin(), puts_.end(), [&stats](const auto& put) {
+        stats.totalPermanentPuts += put.second.puts.size();
+    });
     stats.putCount = puts_.size();
     stats.listenCount = listeners_.size();
     stats.nodeInfo = std::move(info);
@@ -1142,8 +1146,8 @@ DhtProxyServer::put(restinio::request_handle_t request,
                 auto& sPuts = puts_[infoHash];
                 if (value->id == Value::INVALID_ID) {
                     for (auto& pp : sPuts.puts) {
-                        if (pp.second.pushToken == pushToken 
-                            and pp.second.clientId == clientId 
+                        if (pp.second.pushToken == pushToken
+                            and pp.second.clientId == clientId
                             and pp.second.value->contentEquals(*value))
                         {
                             pp.second.expireTimer->expires_at(timeout);
diff --git a/tools/dhtproxy_stats.py b/tools/dhtproxy_stats.py
index 45625b0e..42b57ffe 100644
--- a/tools/dhtproxy_stats.py
+++ b/tools/dhtproxy_stats.py
@@ -7,7 +7,7 @@ stats_total = {"users":0, "pushListenersCount":0, "listenCount":0, "totalListene
 
 for i in range(80,101):
     print("Collecting stats for proxy " + str(i))
-    response = requests.request('STATS', 'http://dhtproxy.jami.net:' + str(i))
+    response = requests.request('STATS', 'http://127.0.0.1:' + str(i))
 
     if response.status_code == 200:
         result = response.json()
@@ -38,14 +38,11 @@ for i in range(80,101):
         except:
             pass
         try:
-            total = 0
-            for h,v in result["puts"].items():
-                total += int(v)
-            stats['totalPermanentPuts'] = total
-            stats_total['totalPermanentPuts'] += total
+            stats['totalPermanentPuts'] = int(result["totalPermanentPuts"])
+            stats_total['totalPermanentPuts'] += int(result["totalPermanentPuts"])
         except:
             pass
-        
+
         stats['timestamp'] = str(ts)
 
         #with open("stats_proxy_" + str(i), "a") as stat_file:
-- 
GitLab