Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Commit

Permalink
topUrl fix
Browse files Browse the repository at this point in the history
  • Loading branch information
flrnull committed Jun 13, 2013
1 parent d37504c commit 6e21edd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Config::Config()
, agentPattern("")
, urlPattern("")
, trafficPattern("")
, topUrlsCount(0)
, topUrlsLimit(10)
{
if (this->debugMode == 1) {
Debug::print("<Config constructor>");
Expand Down
2 changes: 1 addition & 1 deletion Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Config
std::string agentPattern;
std::string urlPattern;
std::string trafficPattern;
int topUrlsCount;
int topUrlsLimit;
};

#endif /* CONFIG_H */
Expand Down
64 changes: 46 additions & 18 deletions Result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,28 @@ void Result::display() {
std::cout << " }," << std::endl;
std::cout << " topUrls: {" << std::endl;
typedef std::map<std::string,int>::const_iterator UrlMapIterator;
int topUrlMapSize = Result::topUrlMap.size();
int iteration = 0;
for (UrlMapIterator it = Result::topUrlMap.begin(); it != Result::topUrlMap.end(); it++) {
iteration++;
std::cout << " " << it->first << ": " << it->second;
if (it != Result::topUrlMap.end()) {
if (iteration < topUrlMapSize) {
std::cout << ",";
}
std::cout << std::endl;
}

std::cout << " allUrls: {" << std::endl;
typedef std::map<std::string,int>::const_iterator UrlIterator;
for (UrlIterator it = Result::urlMap.begin(); it != Result::urlMap.end(); it++) {
std::cout << " " << it->first << ": " << it->second;
if (it != Result::urlMap.end()) {
std::cout << ",";
}
std::cout << std::endl;
}


std::cout << " }" << std::endl;
std::cout << "}" << std::endl;
}
Expand All @@ -51,8 +66,9 @@ void Result::topUrlTryToAdd(std::string urlMapKey, int count, Config * config) {
}
// First item add to top
int topCount = Result::topUrlMap.size();
std::map<std::string,int>::iterator itRes;
bool isAdded, isDeleted;
typedef std::map<std::string,int>::const_iterator mapIterator;
mapIterator itRes;
bool isAdded;
std::string tempUrl;
int tempCount;
if (!topCount) {
Expand All @@ -62,17 +78,19 @@ void Result::topUrlTryToAdd(std::string urlMapKey, int count, Config * config) {
Result::topUrlMap[urlMapKey] = count;
return;
} else {
if (config->debugMode) {
Debug::print("Result::topUrlTryToAdd: try to add element " + urlMapKey);
}
itRes = Result::topUrlMap.begin();
isAdded = false;
isDeleted = false;
// Go through current top from the largest to the smallest
// First element in top should be the largest
for(int j = 0; j < topCount && !isAdded; j++) {
for(int j = 0; (j < topCount) && !isAdded; j++) {
std::advance(itRes, j);
// If current value bigger than in top we add it
if (count > itRes->second) {
if (count >= itRes->second) {
if (config->debugMode) {
Debug::print("Result::calcTopUrls: " + urlMapKey + " count is bigger");
Debug::print("Result::topUrlTryToAdd: " + urlMapKey + " count is bigger");
}
Result::topUrlMap[urlMapKey] = count;
topCount++;
Expand All @@ -82,27 +100,37 @@ void Result::topUrlTryToAdd(std::string urlMapKey, int count, Config * config) {
// If topUrl exceed limit we should remove the smallest
tempUrl = "";
tempCount = 0;
if (isAdded && topCount > config->topUrlsCount) {
for(int z = 0; z < topCount && !isDeleted; z++) {
std::advance(itRes, z);
if (itRes->second < tempCount && tempCount) {
if (isAdded && (topCount > config->topUrlsLimit)) {
if (config->debugMode) {
Debug::print("Result::topUrlTryToAdd: topCount is bigger than topUrlsLimit");
}
for (mapIterator iterTop = Result::topUrlMap.begin(); iterTop != Result::topUrlMap.end(); iterTop++) {
if ((iterTop->second < tempCount) || !tempCount) {
// we save smallest values to temp params
tempUrl = itRes->first;
tempCount = itRes->second;
if (config->debugMode) {
Debug::print("Result::topUrlTryToAdd: (itRes->second < tempCount) && tempCount");
}
tempUrl = iterTop->first;
tempCount = iterTop->second;
} else {
if (config->debugMode) {
Debug::print("Result::topUrlTryToAdd: false return by if (itRes->second < tempCount) && tempCount");
}
}
}
// delete saved temp param
if (tempCount) {
std::map<std::string,int>::iterator removeIt = Result::topUrlMap.find(tempUrl);
if (config->debugMode) {
Debug::print("Result::calcTopUrls: remove the smallest url " + tempUrl);
Debug::print("Result::topUrlTryToAdd: remove the smallest url: " + tempUrl);
}
Result::topUrlMap.erase(tempUrl);
Result::topUrlMap.erase(removeIt);
}
}
}
}

void Result::calcTopUrls(Config * config) {
/*void Result::calcTopUrls(Config * config) {
if (config->debugMode) {
Debug::print("Result::calcTopUrls: calc started");
}
Expand Down Expand Up @@ -144,7 +172,7 @@ void Result::calcTopUrls(Config * config) {
// If topUrl exceed limit we should remove the smallest
tempUrl = "";
tempCount = 0;
if (isAdded && topCount > config->topUrlsCount) {
if (isAdded && topCount > config->topUrlsLimit) {
for(int z = 0; z < topCount && !isDeleted; z++) {
std::advance(itRes, z);
if (itRes->second < tempCount && tempCount) {
Expand All @@ -164,4 +192,4 @@ void Result::calcTopUrls(Config * config) {
}
}
}
}
}*/
1 change: 0 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ int main(int argc, char **argv)
// Calc unique users and urls count
Result::visitors = Result::ipAgentMap.size();
Result::urls = Result::urlMap.size();
//Result::calcTopUrls(config.get());
// Result
Result::display();
return 0;
Expand Down

0 comments on commit 6e21edd

Please sign in to comment.