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.2 2001/04/12 14:55:57 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 DatePattern&  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(d.size(),max_level));
00026     for (unsigned int l=1;(l<=m);++l) 
00027       if (levels.count(l)) {
00028         DatePattern dp(d,l);
00029         ++dates_[dp];
00030         }
00031     }
00032     break;
00033 
00034   case Configuration::PATH: {
00035     const Path& path(r.path());
00036     unsigned int m(min(path.size(),max_level));
00037     for (unsigned int l=1; l<=m; ++l) 
00038       if (levels.count(l)) {
00039         Path pp(path,l);
00040         ++paths_[pp];
00041         }
00042     }
00043     break;
00044 
00045   case Configuration::DOMAIN: {
00046     const Domain& domain(r.domain());
00047     unsigned int sz(domain.size());
00048     unsigned int m(min(sz,max_level));
00049     for (unsigned int l=1; l<=m; ++l) {
00050       if (levels.count(l)) {
00051         Domain dom(domain,l);
00052         ++(domains_[dom]);
00053         }
00054       }
00055     }
00056     break;
00057   }
00058 }
00059 
00060 // Print dates_ map to os.
00061 void
00062 Stats::print_dates(ostream& os) const {
00063 for (DateCount::const_iterator i=dates_.begin(); i!=dates_.end(); ++i) {
00064   const DatePattern& d((*i).first);
00065   os << d <<  "\t" << (*i).second << "\n";
00066   }
00067 }
00068 
00069 // Print paths_ map to os.
00070 void
00071 Stats::print_paths(ostream& os) const {
00072 for (PathCount::const_iterator i=paths_.begin(); i!=paths_.end(); ++i) {
00073   const Path& path((*i).first);
00074   os << path << "\t" << (*i).second << "\n";
00075   }
00076 }
00077 
00078 // Print domains_ map to os.
00079 void
00080 Stats::print_domains(ostream& os) const {
00081 for (DomainCount::const_iterator i=domains_.begin(); i!=domains_.end(); ++i) {
00082   const Domain& domain((*i).first);
00083   os << domain <<  "\t" << (*i).second << "\n";
00084   }
00085 }
00086 
00087 // Print internal database, depending on configuration.
00088 ostream&
00089 operator<<(ostream& os,const Stats& stats) {
00090 switch (stats.configuration_.format()) {
00091   case Configuration::DATE:
00092     stats.print_dates(os);
00093     break;
00094   case Configuration::PATH:
00095     stats.print_paths(os);
00096     break;
00097   case Configuration::DOMAIN:
00098     stats.print_domains(os);
00099     break;
00100   }
00101 return os;
00102 }

httpstats-stage02a [ 7 April, 2001]