From 50769b45008e7da2cce41d918ec2f0fd5204fe46 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 6 Dec 2010 16:03:29 +0200 Subject: [PATCH] =?utf8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=85=D0=BE=D0=B4=20?= =?utf8?q?=D0=BA=20=D0=BD=D0=BE=D0=B2=D0=BE=D0=B9=20=D1=81=D1=85=D0=B5?= =?utf8?q?=D0=BC=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20?= =?utf8?q?=D0=B4=D0=B5=D1=82=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D1=81?= =?utf8?q?=D1=82=D0=B0=D1=82=D0=B8=D1=81=D1=82=D0=B8=D0=BA=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- projects/stargazer/user.cpp | 105 ++++++++++++++++++++++------------- projects/stargazer/user.h | 13 +++-- projects/stargazer/users.cpp | 8 +-- 3 files changed, 79 insertions(+), 47 deletions(-) diff --git a/projects/stargazer/user.cpp b/projects/stargazer/user.cpp index 2ad1e342..2ac865bd 100644 --- a/projects/stargazer/user.cpp +++ b/projects/stargazer/user.cpp @@ -110,8 +110,8 @@ ipIndex = ipIdx; password = "*_EMPTY_PASSWORD_*"; tariffName = NO_TARIFF_NAME; connected = 0; -traffStatInUse = 0; -traffStat = &traffStatInternal[0]; +/*traffStatInUse = 0; +traffStat = &traffStatInternal[0];*/ tariff = tariffs->GetNoTariff(); ips = StrToIPS("*"); deleted = false; @@ -192,13 +192,13 @@ if (&u == this) return; connected = 0; -traffStatInUse = 0; +//traffStatInUse = 0; ipIndex = u.ipIndex; deleted = u.deleted; -traffStat = &traffStatInternal[traffStatInUse % 2]; -traffStatToWrite = &traffStatInternal[(traffStatInUse +1) % 2]; +/*traffStat = &traffStatInternal[traffStatInUse % 2]; +traffStatToWrite = &traffStatInternal[(traffStatInUse +1) % 2];*/ lastWriteStat = u.lastWriteStat; lastWriteDeatiledStat = u.lastWriteDeatiledStat; @@ -855,25 +855,18 @@ IP_DIR_PAIR idp(ip, dir); #endif map::iterator lb; -lb = traffStat->lower_bound(idp); -if (lb == traffStat->end()) +lb = traffStat.lower_bound(idp); +if (lb == traffStat.end() || lb.first != idp) { - traffStat->insert(lb, + traffStat.insert(lb, pair(idp, STAT_NODE(len, 0, cost))); } else - if (lb->first.dir == dir && lb->first.ip == ip) - { - lb->second.cash += cost; - lb->second.up += len; - } - else - { - traffStat->insert(lb, - pair(idp, - STAT_NODE(len, 0, cost))); - } + { + lb->second.cash += cost; + lb->second.up += len; + } } //----------------------------------------------------------------------------- #ifdef TRAFF_STAT_WITH_PORTS @@ -952,25 +945,18 @@ IP_DIR_PAIR idp(ip, dir); #endif map::iterator lb; -lb = traffStat->lower_bound(idp); -if (lb == traffStat->end()) +lb = traffStat.lower_bound(idp); +if (lb == traffStat->end() || lb->first != idp) { traffStat->insert(lb, pair(idp, STAT_NODE(0, len, cost))); } else - if (lb->first.dir == dir && lb->first.ip == ip) - { - lb->second.cash += cost; - lb->second.down += len; - } - else - { - traffStat->insert(lb, - pair(idp, - STAT_NODE(0, len, cost))); - } + { + lb->second.cash += cost; + lb->second.down += len; + } } //----------------------------------------------------------------------------- void USER::AddCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE * n) @@ -1043,16 +1029,16 @@ else Run(); } //----------------------------------------------------------------------------- -void USER::ResetDetailStat() +/*void USER::ResetDetailStat() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); traffStatToWrite->erase(traffStatToWrite->begin(), traffStatToWrite->end()); -} +}*/ //----------------------------------------------------------------------------- -int USER::WriteDetailStat() +int USER::WriteDetailStat(bool hard) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +/*STG_LOCKER lock(&mutex, __FILE__, __LINE__); printfd(__FILE__, "USER::WriteDetailedStat(): size = %d\n", traffStatToWrite->size()); @@ -1065,10 +1051,53 @@ if (traffStatToWrite->size() && !disabledDetailStat) } } lastWriteDeatiledStat = lastSwapDeatiledStat; +return 0;*/ +printfd(__FILE__, "USER::WriteDetailedStat() - queue size = %d\n", traffStatQueue.size()); + +if (!traffStatQueue.empty()) + { + std::list >::iterator it; + for (it = traffStatQueue.begin(); it != traffStatQueue.end(); ++it) + { + if (store->WriteDetailedStat(it->second, it->first, login)) + { + printfd(__FILE__, "USER::WriteDetailStat() - failed to write detail stat from queue\n"); + WriteServLog("Cannot write detail stat from queue (of size %d recs) for user %s.", traffStatQueue.size(), login.c_str()); + WriteServLog("%s", store->GetStrError().c_str()); + return -1; + } + traffStatQueue.erase(it++); + } + } + +TRAFF_STAT ts; + + { + STG_LOCKER lock(&mutex, __FILE__, __LINE__); + ts.swap(traffStat); + } + +printfd(__FILE__, "USER::WriteDetailedStat() - size = %d\n", ts.size()); + +if (ts.size() && !disabledDetailStat) + { + if (store->WriteDetailedStat(ts, lastWriteDeatiledStat, login)) + { + printfd(__FILE__, "USER::WriteDetailStat() - failed to write current detail stat\n"); + WriteServLog("Cannot write detail stat for user %s.", login.c_str()); + WriteServLog("%s", store->GetStrError().c_str()); + if (!hard) + { + printfd(__FILE__, "USER::WriteDetailStat() - pushing detail stat to queue\n"); + traffStatQueue.push_back(std::make_pair(lastWriteDeatiledStat, ts)); + } + } + } +lastWriteDeatiledStat = stgTime; return 0; } //----------------------------------------------------------------------------- -int USER::SwapDetailStat() +/*int USER::SwapDetailStat() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -1077,7 +1106,7 @@ traffStatToWrite = &traffStatInternal[traffStatInUse % 2]; traffStat = &traffStatInternal[++traffStatInUse % 2]; return 0; -} +}*/ //----------------------------------------------------------------------------- double USER::GetPassiveTimePart() const { diff --git a/projects/stargazer/user.h b/projects/stargazer/user.h index 7a9896bf..8b35dbec 100644 --- a/projects/stargazer/user.h +++ b/projects/stargazer/user.h @@ -144,9 +144,9 @@ public: void ResetPassiveTime() { passiveTime = 0; } void SetPassiveTimeAsNewUser(); - void ResetDetailStat(); - int SwapDetailStat(); - int WriteDetailStat(); + /*void ResetDetailStat(); + int SwapDetailStat();*/ + int WriteDetailStat(bool hard = false); const TARIFF * GetTariff() const { return tariff; } void ResetNextTariff() { nextTariff = ""; } @@ -235,10 +235,13 @@ private: const TARIFFS * tariffs; const TARIFF * tariff; - map traffStatInternal[2]; + /*map traffStatInternal[2]; map * traffStat; map * traffStatToWrite; - int traffStatInUse; + int traffStatInUse;*/ + + TRAFF_STAT traffStat; + std::list > traffStatQueue; const SETTINGS * settings; diff --git a/projects/stargazer/users.cpp b/projects/stargazer/users.cpp index 1b710baa..9a219be6 100644 --- a/projects/stargazer/users.cpp +++ b/projects/stargazer/users.cpp @@ -365,7 +365,7 @@ if (t->tm_hour == 23 && t->tm_min == 59) if (TimeToWriteDetailStat(t)) { //printfd(__FILE__, "USER::WriteInetStat\n"); - for_each(users.begin(), users.end(), mem_fun_ref(&USER::SwapDetailStat)); + //for_each(users.begin(), users.end(), mem_fun_ref(&USER::SwapDetailStat)); usersCnt = 0; // ðÉÛÅÍ ÀÚÅÒÏ× ÞÁÓÔÑÍÉ. ÷ ÐÅÒÅÒÙ×ÁÈ ×ÙÚÙ×ÁÅÍ USER::Run @@ -379,7 +379,7 @@ if (TimeToWriteDetailStat(t)) for_each(users.begin(), users.end(), mem_fun_ref(&USER::Run)); } - for_each(users.begin(), users.end(), mem_fun_ref(&USER::ResetDetailStat)); + //for_each(users.begin(), users.end(), mem_fun_ref(&USER::ResetDetailStat)); } RealDelUser(); @@ -495,8 +495,8 @@ if (isRunning) printfd(__FILE__, "Before USERS::Run()\n"); for_each(users.begin(), users.end(), mem_fun_ref(&USER::Run)); -for_each(users.begin(), users.end(), mem_fun_ref(&USER::SwapDetailStat)); -for_each(users.begin(), users.end(), mem_fun_ref(&USER::WriteDetailStat)); +//for_each(users.begin(), users.end(), mem_fun_ref(&USER::SwapDetailStat)); +for_each(users.begin(), users.end(), bind2nd(mem_fun_ref(&USER::WriteDetailStat), true)); for_each(users.begin(), users.end(), mem_fun_ref(&USER::WriteStat)); for_each(users.begin(), users.end(), mem_fun_ref(&USER::WriteConf)); -- 2.43.2