]> git.stg.codes - stg.git/commitdiff
Переход к новой схеме работы с детальной статистикой
authorMaxim Mamontov <faust@gts.dp.ua>
Mon, 6 Dec 2010 14:03:29 +0000 (16:03 +0200)
committerMaxim Mamontov <faust@gts.dp.ua>
Mon, 6 Dec 2010 14:03:29 +0000 (16:03 +0200)
projects/stargazer/user.cpp
projects/stargazer/user.h
projects/stargazer/users.cpp

index 2ad1e342f4a7bc571dac6bf4d6522b713c976efa..2ac865bd0b70d18514d9ef0c564bd5f7e9c765ed 100644 (file)
@@ -110,8 +110,8 @@ ipIndex = ipIdx;
 password = "*_EMPTY_PASSWORD_*";
 tariffName = NO_TARIFF_NAME;
 connected = 0;
 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;
 tariff = tariffs->GetNoTariff();
 ips = StrToIPS("*");
 deleted = false;
@@ -192,13 +192,13 @@ if (&u == this)
     return;
 
 connected = 0;
     return;
 
 connected = 0;
-traffStatInUse = 0;
+//traffStatInUse = 0;
 
 ipIndex = u.ipIndex;
 
 deleted = u.deleted;
 
 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;
 
 lastWriteStat = u.lastWriteStat;
 lastWriteDeatiledStat = u.lastWriteDeatiledStat;
@@ -855,25 +855,18 @@ IP_DIR_PAIR idp(ip, dir);
 #endif
 
 map<IP_DIR_PAIR, STAT_NODE>::iterator lb;
 #endif
 
 map<IP_DIR_PAIR, STAT_NODE>::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<IP_DIR_PAIR, STAT_NODE>(idp,
                                                    STAT_NODE(len, 0, cost)));
     }
 else
                       pair<IP_DIR_PAIR, STAT_NODE>(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<IP_DIR_PAIR, STAT_NODE>(idp,
-                                                       STAT_NODE(len, 0, cost)));
-        }
+    {
+    lb->second.cash += cost;
+    lb->second.up += len;
+    }
 }
 //-----------------------------------------------------------------------------
 #ifdef TRAFF_STAT_WITH_PORTS
 }
 //-----------------------------------------------------------------------------
 #ifdef TRAFF_STAT_WITH_PORTS
@@ -952,25 +945,18 @@ IP_DIR_PAIR idp(ip, dir);
 #endif
 
 map<IP_DIR_PAIR, STAT_NODE>::iterator lb;
 #endif
 
 map<IP_DIR_PAIR, STAT_NODE>::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<IP_DIR_PAIR, STAT_NODE>(idp,
                                                    STAT_NODE(0, len, cost)));
     }
 else
     {
     traffStat->insert(lb,
                       pair<IP_DIR_PAIR, STAT_NODE>(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<IP_DIR_PAIR, STAT_NODE>(idp,
-                                                       STAT_NODE(0, len, cost)));
-        }
+    {
+    lb->second.cash += cost;
+    lb->second.down += len;
+    }
 }
 //-----------------------------------------------------------------------------
 void USER::AddCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> * n)
 }
 //-----------------------------------------------------------------------------
 void USER::AddCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> * n)
@@ -1043,16 +1029,16 @@ else
 Run();
 }
 //-----------------------------------------------------------------------------
 Run();
 }
 //-----------------------------------------------------------------------------
-void USER::ResetDetailStat()
+/*void USER::ResetDetailStat()
 {
 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 traffStatToWrite->erase(traffStatToWrite->begin(), traffStatToWrite->end());
 {
 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());
 
 
 printfd(__FILE__, "USER::WriteDetailedStat(): size = %d\n", traffStatToWrite->size());
 
@@ -1065,10 +1051,53 @@ if (traffStatToWrite->size() && !disabledDetailStat)
         }
     }
 lastWriteDeatiledStat = lastSwapDeatiledStat;
         }
     }
 lastWriteDeatiledStat = lastSwapDeatiledStat;
+return 0;*/
+printfd(__FILE__, "USER::WriteDetailedStat() - queue size = %d\n", traffStatQueue.size());
+
+if (!traffStatQueue.empty())
+    {
+    std::list<std::pair<time_t, TRAFF_STAT> >::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;
 }
 //-----------------------------------------------------------------------------
 return 0;
 }
 //-----------------------------------------------------------------------------
-int USER::SwapDetailStat()
+/*int USER::SwapDetailStat()
 {
 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 {
 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
@@ -1077,7 +1106,7 @@ traffStatToWrite = &traffStatInternal[traffStatInUse % 2];
 traffStat = &traffStatInternal[++traffStatInUse % 2];
 
 return 0;
 traffStat = &traffStatInternal[++traffStatInUse % 2];
 
 return 0;
-}
+}*/
 //-----------------------------------------------------------------------------
 double USER::GetPassiveTimePart() const
 {
 //-----------------------------------------------------------------------------
 double USER::GetPassiveTimePart() const
 {
index 7a9896bfcba77caac35677f2b449ff107838db39..8b35dbecdc7b8801a713d1479021c811b54fbc08 100644 (file)
@@ -144,9 +144,9 @@ public:
     void            ResetPassiveTime() { passiveTime = 0; }
     void            SetPassiveTimeAsNewUser();
 
     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 = ""; }
 
     const TARIFF *  GetTariff() const { return tariff; }
     void            ResetNextTariff() { nextTariff = ""; }
@@ -235,10 +235,13 @@ private:
     const TARIFFS * tariffs;
     const TARIFF *  tariff;
 
     const TARIFFS * tariffs;
     const TARIFF *  tariff;
 
-    map<IP_DIR_PAIR, STAT_NODE>     traffStatInternal[2];
+    /*map<IP_DIR_PAIR, STAT_NODE>     traffStatInternal[2];
     map<IP_DIR_PAIR, STAT_NODE> *   traffStat;
     map<IP_DIR_PAIR, STAT_NODE> *   traffStatToWrite;
     map<IP_DIR_PAIR, STAT_NODE> *   traffStat;
     map<IP_DIR_PAIR, STAT_NODE> *   traffStatToWrite;
-    int                             traffStatInUse;
+    int                             traffStatInUse;*/
+
+    TRAFF_STAT      traffStat;
+    std::list<std::pair<time_t, TRAFF_STAT> > traffStatQueue;
 
     const SETTINGS * settings;
 
 
     const SETTINGS * settings;
 
index 1b710baa9773fe16f0c5ff1e293055a026da11f5..9a219be6ca667a9b122833d5d5aac6a00cef374e 100644 (file)
@@ -365,7 +365,7 @@ if (t->tm_hour == 23 && t->tm_min == 59)
 if (TimeToWriteDetailStat(t))
     {
     //printfd(__FILE__, "USER::WriteInetStat\n");
 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
     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::Run));
         }
 
-    for_each(users.begin(), users.end(), mem_fun_ref(&USER::ResetDetailStat));
+    //for_each(users.begin(), users.end(), mem_fun_ref(&USER::ResetDetailStat));
     }
 
 RealDelUser();
     }
 
 RealDelUser();
@@ -495,8 +495,8 @@ if (isRunning)
 
 printfd(__FILE__, "Before USERS::Run()\n");
 for_each(users.begin(), users.end(), mem_fun_ref(&USER::Run));
 
 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));
 
 for_each(users.begin(), users.end(), mem_fun_ref(&USER::WriteStat));
 for_each(users.begin(), users.end(), mem_fun_ref(&USER::WriteConf));