diff --git a/src/controller/controller_statistics.cpp b/src/controller/controller_statistics.cpp index b1bdcd8a..98fb86a1 100644 --- a/src/controller/controller_statistics.cpp +++ b/src/controller/controller_statistics.cpp @@ -327,6 +327,7 @@ void Controller::SharedMemStats(void * config){ std::set inactiveStreams; Controller::initState(); bool shiftWrites = true; + bool firstRun = true; while(((Util::Config*)config)->is_active){ { tthread::lock_guard guard(Controller::configMutex); @@ -334,6 +335,17 @@ void Controller::SharedMemStats(void * config){ cacheLock->wait(); /*LTS*/ //parse current users statServer.parseEach(parseStatistics); + if (firstRun){ + firstRun = false; + servUpOtherBytes = 0; + servDownOtherBytes = 0; + servUpBytes = 0; + servDownBytes = 0; + for (std::map::iterator it = streamStats.begin(); it != streamStats.end(); ++it){ + it->second.upBytes = 0; + it->second.downBytes = 0; + } + } //wipe old statistics if (sessions.size()){ std::list mustWipe; @@ -580,9 +592,9 @@ void Controller::statSession::update(unsigned long index, IPC::statExchange & da servDownBytes += currDown - prevDown; } } - if (currDown + currUp > COUNTABLE_BYTES){ + if (currDown + currUp >= COUNTABLE_BYTES){ std::string streamName = data.streamName(); - if (prevUp + prevDown < COUNTABLE_BYTES){ + if (sessionType == SESS_UNSET){ if (data.connector() == "INPUT"){ ++servInputs; streamStats[streamName].inputs++; @@ -599,6 +611,10 @@ void Controller::statSession::update(unsigned long index, IPC::statExchange & da streamStats[streamName].currViews++; sessionType = SESS_VIEWER; } + } + //If previous < COUNTABLE_BYTES, we haven't counted any data so far. + //We need to count all the data in that case, otherwise we only count the difference. + if (prevUp + prevDown < COUNTABLE_BYTES){ if (!streamName.size() || streamName[0] == 0){ if (streamStats.count(streamName)){streamStats.erase(streamName);} }else{ @@ -612,15 +628,6 @@ void Controller::statSession::update(unsigned long index, IPC::statExchange & da streamStats[streamName].upBytes += currUp - prevUp; streamStats[streamName].downBytes += currDown - prevDown; } - if (sessionType == SESS_UNSET){ - if (data.connector() == "INPUT"){ - sessionType = SESS_INPUT; - }else if (data.connector() == "OUTPUT"){ - sessionType = SESS_OUTPUT; - }else{ - sessionType = SESS_VIEWER; - } - } } } } @@ -730,6 +737,7 @@ void Controller::statSession::ping(const Controller::sessIndex & index, unsigned wipedUp = 0; wipedDown = 0; oldConns.clear(); + sessionType = SESS_UNSET; } } @@ -1615,6 +1623,7 @@ void Controller::handlePrometheus(HTTP::Parser & H, Socket::Connection & conn, i for (std::map::iterator it = sessions.begin(); it != sessions.end(); it++){ switch (it->second.getSessType()){ case SESS_UNSET: + break; case SESS_VIEWER: if (it->second.hasDataFor(tOut) && it->second.isViewerOn(tOut)){ outputs[it->first.connector]++; @@ -1702,6 +1711,7 @@ void Controller::handlePrometheus(HTTP::Parser & H, Socket::Connection & conn, i for (std::map::iterator it = sessions.begin(); it != sessions.end(); it++){ switch (it->second.getSessType()){ case SESS_UNSET: + break; case SESS_VIEWER: if (it->second.hasDataFor(tOut) && it->second.isViewerOn(tOut)){ outputs[it->first.connector]++;