Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

stats.C

Go to the documentation of this file.
00001 // $Id: stats.C,v 1.3 2001/04/12 14:55:55 dvermeir Exp $
00002 #include "stats.h"
00003 #include <algorithm> // min()
00004 
00005 // We copy the configuration into configuration_.
00006 Stats::Stats(const Configuration& c): configuration_(c) {
00007 }
00008 
00009 // Update the internal database with information from a LogRecord.
00010 void
00011 Stats::add(const LogRecord& r) {
00012 // What we do depends on the requested configuration format.
00013 /* It would be more efficient to make these actual data members
00014    of Stat. That way, they would only be ``computed'' once.
00015 */
00016 set<unsigned int> levels(configuration_.levels());
00017 unsigned int      max_level(configuration_.max_level());
00018 
00019 switch (configuration_.format()) {
00020 
00021   case Configuration::DATE: {
00022     const Date& d(r.date());
00023     // We will have at most 4 components in a date vector.
00024     // This should become a symbolic constant in Configuration!
00025     unsigned int m(min(4u,max_level));
00026     for (unsigned int l=1;(l<=m);++l) 
00027       if (levels.count(l)) {
00028         vector<int>     dv(l);
00029         switch (l) {
00030           case 4: dv[3] = d.hours();
00031           case 3: dv[2] = d.day();
00032           case 2: dv[1] = d.month();
00033           case 1: dv[0] = d.year();
00034           }
00035         ++dates_[dv];
00036         }
00037     }
00038     break;
00039 
00040   case Configuration::PATH: {
00041     const vector<string>& path(r.path());
00042     unsigned int m(min(path.size(),max_level));
00043     for (unsigned int l=1; l<=m; ++l) 
00044       if (levels.count(l)) {
00045         vector<string>  pv(l);
00046         for (unsigned int j=0;(j<l);++j)
00047           pv[j] = path[j];
00048         ++paths_[pv];
00049         }
00050     }
00051     break;
00052 
00053   case Configuration::DOMAIN: {
00054     const vector<string>& domain(r.domain());
00055     unsigned int sz(domain.size());
00056     unsigned int m(min(sz,max_level));
00057     for (unsigned int l=1; l<=m; ++l) {
00058       if (levels.count(l)) {
00059         // we should store the last l components of domain:
00060         // dom[0] <- domain[sz-0-1]
00061         // dom[1] <- domain[sz-1-1]
00062         // dom[2] <- domain[sz-2-1]
00063         // etc.
00064         vector<string>  dom(l);
00065         for (unsigned int j=0;(j<l);++j)
00066           dom[j] = domain[sz-j-1];
00067         ++(domains_[dom]);
00068         }
00069       }
00070     }
00071     break;
00072   }
00073 }
00074 
00075 // Print dates_ map to os.
00076 void
00077 Stats::print_dates(ostream& os) const {
00078 for (DateCount::const_iterator i=dates_.begin(); i!=dates_.end(); ++i) {
00079   const vector<int>& dv((*i).first);
00080   unsigned int sz(dv.size());
00081   // Print in format year-month-day hours:00.
00082   if (sz>0) {
00083     os << dv[0] ;
00084     if (sz>1) {
00085       os << "-" << dv[1];
00086       if (sz>2) {
00087         os << "-" << dv[2];
00088         if (sz>3)
00089           os << " " << dv[3] << ":00";
00090         }
00091       }
00092     os << "\t" << (*i).second << "\n";
00093     }
00094   }
00095 }
00096 
00097 // Print paths_ map to os.
00098 void
00099 Stats::print_paths(ostream& os) const {
00100 for (PathCount::const_iterator i=paths_.begin(); i!=paths_.end(); ++i) {
00101   const vector<string>& pv((*i).first);
00102   // E.g. <"","a","b"> is printed as "/a/b".
00103   for (unsigned int j=0; j<pv.size() ; ++j) {
00104     if ((j>0)||(pv.size()==1))
00105       os << "/";
00106     os << pv[j];
00107     }
00108   os << "\t" << (*i).second << "\n";
00109   }
00110 }
00111 
00112 // Print domains_ map to os.
00113 void
00114 Stats::print_domains(ostream& os) const {
00115 for (DomainCount::const_iterator i=domains_.begin(); i!=domains_.end(); ++i) {
00116   const vector<string>& dv((*i).first);
00117   for (unsigned int j=0; j<dv.size() ; ++j) {
00118     if (j>0)
00119       os << ".";
00120     os << dv[j];
00121     }
00122   os << "\t" << (*i).second << "\n";
00123   }
00124 }
00125 
00126 // Print internal database, depending on configuration.
00127 ostream&
00128 operator<<(ostream& os,const Stats& stats) {
00129 switch (stats.configuration_.format()) {
00130   case Configuration::DATE:
00131     stats.print_dates(os);
00132     break;
00133   case Configuration::PATH:
00134     stats.print_paths(os);
00135     break;
00136   case Configuration::DOMAIN:
00137     stats.print_domains(os);
00138     break;
00139   }
00140 return os;
00141 }

httpstats-stage01 [ 7 April, 2001]