00001
00002 #include "stats.h"
00003 #include <algorithm>
00004
00005
00006 Stats::Stats(const Configuration& c): configuration_(c) {
00007 }
00008
00009
00010 void
00011 Stats::add(const LogRecord& r) {
00012
00013
00014
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
00024
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
00060
00061
00062
00063
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
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
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
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
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
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
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 }