Browse Source

fix a bug in entropy calculation

aidmar.wainakh 6 years ago
parent
commit
1fe28ac43d
2 changed files with 19 additions and 22 deletions
  1. 19 8
      code_boost/src/cxx/statistics.cpp
  2. 0 14
      code_boost/src/cxx/utilities.cpp

+ 19 - 8
code_boost/src/cxx/statistics.cpp

@@ -56,14 +56,25 @@ std::vector<float> statistics::calculateLastIntervalIPsEntropy(std::chrono::micr
         int pktsSent = 0, pktsReceived = 0;
 
         for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
-            int indexStartSent = getClosestIndex(i->second.pkts_sent_timestamp, intervalStartTimestamp);
-            int IPsSrcPktsCount = i->second.pkts_sent_timestamp.size() - indexStartSent;
-            IPsSrcPktsCounts.push_back(IPsSrcPktsCount);
-            pktsSent += IPsSrcPktsCount;
-            int indexStartReceived = getClosestIndex(i->second.pkts_received_timestamp, intervalStartTimestamp);
-            int IPsDstPktsCount = i->second.pkts_received_timestamp.size() - indexStartReceived;
-            IPsDstPktsCounts.push_back(IPsDstPktsCount);
-            pktsReceived += IPsDstPktsCount;
+            int IPsSrcPktsCount = 0;
+            for (auto j = i->second.pkts_sent_timestamp.begin(); j != i->second.pkts_sent_timestamp.end(); j++) {
+                if(*j >= intervalStartTimestamp)
+                    IPsSrcPktsCount++;
+            }
+            if(IPsSrcPktsCount != 0) {
+                IPsSrcPktsCounts.push_back(IPsSrcPktsCount);
+                pktsSent += IPsSrcPktsCount;
+            }
+
+            int IPsDstPktsCount = 0;
+            for (auto j = i->second.pkts_received_timestamp.begin(); j != i->second.pkts_received_timestamp.end(); j++) {
+                if(*j >= intervalStartTimestamp)
+                    IPsDstPktsCount++;
+            }
+            if(IPsDstPktsCount != 0) {
+                IPsDstPktsCounts.push_back(IPsDstPktsCount);
+                pktsReceived += IPsDstPktsCount;
+            }
         }
 
         for (auto i = IPsSrcPktsCounts.begin(); i != IPsSrcPktsCounts.end(); i++) {

+ 0 - 14
code_boost/src/cxx/utilities.cpp

@@ -74,20 +74,6 @@ std::string getIPv4Class(std::string ipAddress){
     return ipClass;
 }
 
-/**
- * Get closest index for element in a vector.
- * @param v vector.
- * @param refElem element that we search for or for closest element.
- */
-int getClosestIndex(std::vector<std::chrono::microseconds> v, std::chrono::microseconds refElem)
-{
-    auto i = std::min_element(begin(v), end(v), [=] (std::chrono::microseconds x, std::chrono::microseconds y)
-    {
-        return std::abs((x - refElem).count()) < std::abs((y - refElem).count());
-    });
-    return std::distance(begin(v), i);
-}
-
 /**
  * Advance iterator by 10 steps.
  * @param iterator to advance.