Changeset 12909


Ignore:
Timestamp:
02/19/12 17:19:46 (13 years ago)
Author:
ogrimm
Message:
Improved filter function for History server
Location:
fact/Evidence
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • fact/Evidence/History.cc

    r11088 r12909  
    2323#include <math.h>
    2424#include <float.h>
     25#include <limits>
    2526#include <sys/stat.h>
    2627#include <sys/time.h>
     
    4445          double LastValue;
    4546          double MinAbsChange;
     47          pair <unsigned int, unsigned int> Range;
    4648          string Format;
    4749          time_t LastSave;
     
    5153        DimInfo *ServerList;
    5254        char *Directory;
    53         string Change;
    5455
    5556    void infoHandler();
     
    7374
    7475  // Get/initialize configuration
    75   Change = GetConfig("minchange", " ");
     76  GetConfig("minchange", " ");
    7677  GetConfig("maxsize_kb", DEFAULT_MAX_SIZE_KB);
    7778  GetConfig("numentries", DEFAULT_NUM_ENTRIES);
     
    161162  }
    162163
    163   // If data is number of single type, check minumum change before adding to history
    164   if (strcmp(I->getFormat(), "C") != 0) {
    165         // Calculate sum of all number in array
    166         istringstream Text(EvidenceServer::ToString(I->getFormat(), I->getData(), I->getSize()));
    167         double Num, Sum = 0;
    168         while (Text.good()) {
    169           Text >> Num;
    170           Sum += fabs(Num);
    171         }
    172 
    173         // Minimum change?
    174         if (fabs(Sum-Map[Service].LastValue) < fabs(Map[Service].MinAbsChange)) return;
     164  // Check minumum change before adding to history
     165  if (Map[Service].MinAbsChange != 0) {
     166        double Sum = 0;
     167        unsigned int Count = 0;
     168
     169        // Add values within given range       
     170        vector<string> Values = Tokenize(EvidenceServer::ToString(I->getFormat(), I->getData(), I->getSize()));
     171
     172        for (unsigned int i=0; i<Values.size(); i++) {
     173          if ((i < Map[Service].Range.first) || (i > Map[Service].Range.second)) continue;
     174
     175          Sum += atof(Values[i].c_str());
     176          Count++;
     177        }
     178
     179        // Minimum change of average value?     
     180        if (Count>0 && (fabs(Sum-Map[Service].LastValue)/Count < fabs(Map[Service].MinAbsChange))) return;
    175181        Map[Service].LastValue = Sum;
    176182  }
     
    276282  Map[Name].Format = Format;
    277283  Map[Name].MinAbsChange = 0.0;
     284  Map[Name].Range = pair <unsigned int, unsigned int> (0, numeric_limits<unsigned int>::max());
    278285  Map[Name].LastSave = 0.0;
    279286 
    280287  // Set minimum required change if given in configuratrion
     288  string Change = GetConfig("minchange");
    281289  size_t Pos = Change.find(Name+":");
    282   if (Pos != string::npos) Map[Name].MinAbsChange = atof(Change.c_str() + Pos + Name.size() + 1);
    283 
     290 
     291  if (Pos != string::npos) {
     292        vector<string> Parts = Tokenize(Change.substr(Pos, Change.find(' ', Pos)-Pos), ":");
     293
     294        // Check if index range is given as well 
     295    if (Parts.size() == 2) Map[Name].MinAbsChange = atof(Parts[1].c_str());
     296    else if (Parts.size() == 3) {
     297          Map[Name].MinAbsChange = atof(Parts[2].c_str());
     298
     299          vector<string> Range = Tokenize(Parts[1], "-");
     300          if (Range.size() == 1) Map[Name].Range = pair <unsigned int, unsigned int> (atoi(Range[0].c_str()), atoi(Range[0].c_str()));
     301          else if (Range.size() == 2) Map[Name].Range = pair <unsigned int, unsigned int> (atoi(Range[0].c_str()), atoi(Range[1].c_str()));
     302        }       
     303  }
     304
     305  SendToLog("Subscribing to service '%s': MinAbsChange %f, Index range %u-%u\n", Name.c_str(),   Map[Name].MinAbsChange, Map[Name].Range.first, Map[Name].Range.second);
     306 
    284307  // Load history buffer from file if existing
    285308  FILE *File = OpenFile(Name, "rb");
     
    326349  if (Map.count(Name) == 0) return;
    327350
     351  SendToLog("Unsubscribing service '%s'", Name.c_str());
     352
    328353  // Delete subscription first so handler and not called anymore
    329354  delete Map[Name].DataItem;
  • fact/Evidence/readme.txt

    r12892 r12909  
    54546/7/2011        Missing pthread_mutexattr_init() in Evidence constructor caused spurious errors when
    5555                        locking mutex
    56 2/8/2011        Used inCallback() method of DIM version v19r23 to decide between blocking or non-blocking config request.
    57                         Updated EvidenceServer::ToString() for B and V formats.
     562/8/2011        Used inCallback() method of DIM version v19r23 to decide between blocking or
     57                        non-blocking config request. Updated EvidenceServer::ToString() for B and V formats.
     5816/2/2012       EvidenceServer::ToString() handles more complex single-type formats (e.g. "F:1;F:4;F"
     5918/2/2012       History server detection for minimum change takes an index range given in the configuration
     60                        into account (e.g. Service:a-b:0.4 requires an minimum absolute change of 0.4 for the average
     61                        value of indices a to b)
Note: See TracChangeset for help on using the changeset viewer.