Changeset 529 for trunk/lib


Ignore:
Timestamp:
Dec 25, 2007, 2:10:25 PM (16 years ago)
Author:
Peter Johansson
Message:

moving stuff to base class, Stats

Location:
trunk/lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/ClassicStats.cc

    r528 r529  
    5555  }
    5656
     57
    5758  ClassicStats::ClassicStats(const ClassicStats& other)
    5859  : Stats(other)
    5960  {
    6061  }
    61 
    62   /*
    63   std::vector<u_int> ClassicStats::accumulated(const Map_& map) const
    64   {
    65     // sum of all users
    66     std::vector<u_int> sum(revision()+1);
    67     sum=std::accumulate(map.begin(), map.end(), sum,
    68                         PairValuePlus<std::string,u_int>());
    69 
    70     // calculate accumulated sum
    71     std::vector<u_int> accum(sum.size());
    72     std::partial_sum(sum.begin(),sum.end(),accum.begin());
    73     assert(sum.size()==accum.size());
    74     return accum;
    75   }
    76 
    77   std::vector<u_int> ClassicStats::accumulated(const Map_& map,
    78                                         const std::string& user) const
    79   {
    80     if (!map.count(user))
    81       return std::vector<u_int>(last_changed_rev(),0);
    82     std::vector<u_int> vec=(map.find(user))->second;
    83 
    84     // static_cast to remove annoying compiler warning
    85     if (vec.size() < static_cast<size_t>(revision()+1))
    86       vec.insert(vec.end(), revision()+1-vec.size(), 0);
    87 
    88     std::vector<u_int> accum(vec.size());
    89     std::partial_sum(vec.begin(),vec.end(),accum.begin());
    90     return accum;
    91   }
    92   */
    9362
    9463
     
    150119
    151120
    152   bool ClassicStats::do_load_cache(std::istream& is)
    153   {
    154     svn_revnum_t rev;
    155     is >> rev;
    156     if (rev<last_changed_rev()){
    157       return false; // cache is not up to date
    158     }
    159     reset();
    160     size_t a_size=0;
    161     is >> a_size;
    162     std::string str;
    163     while (authors().size()<a_size){
    164       getline(is, str);
    165       assert(str.size());
    166       add_author(str);
    167     }
    168     getline(is, str);
    169     if (str!=code_cache()){
    170       return false;
    171     }
    172     load(is, code_);
    173     getline(is, str);
    174     getline(is, str);
    175     if (str!=comments_cache()){
    176       return false;
    177     }
    178     load(is, comments_);
    179     getline(is, str);
    180     getline(is, str);
    181     if (str!=other_cache()){
    182       return false;
    183     }
    184     load(is, other_);
    185     getline(is, str);
    186     getline(is, str);
    187     if (str!=total_cache()){
    188       return false;
    189     }
    190     load(is, total_);
    191     getline(is,str);
    192     getline(is,str);
    193     return str==end_of_cache();
    194   }
    195 
    196 
    197   void ClassicStats::load(std::istream& is, Author2Vector& m)
    198   {
    199     while (m.size() < authors().size() && is.good()) {
    200       std::string name;
    201       std::getline(is, name);
    202       assert(name.size());
    203       std::vector<u_int>& vec=m[name];
    204       size_t revs=0;
    205       is >> revs;
    206       vec.reserve(revs);
    207       while (vec.size() < revs) {
    208         u_int tmp;
    209         is >> tmp;
    210         vec.push_back(tmp);
    211       }
    212     }
    213   }
    214 
    215 
    216121  void ClassicStats::do_parse(const std::string& path)
    217122  {
     
    228133
    229134
    230   std::string ClassicStats::do_plot(const std::string& filename,
    231                                     const std::string& linetype) const
    232   {
    233     plot_init(filename);
    234     GnuplotFE* gp=GnuplotFE::instance();
    235     const Author2Vector* stat=NULL;
    236     if (linetype=="total")
    237       stat = &total_;
    238     else if (linetype=="code")
    239       stat = &code_;
    240     else if (linetype=="comments")
    241       stat = &comments_;
    242     else if (linetype=="empty")
    243       stat = &other_;
    244     assert(stat);
    245     assert(stat->size());
    246     std::vector<u_int> total=get_vector(*stat, "all");   
    247     double yrange_max=1.03*total.back()+1;
    248     gp->yrange(yrange_max);
    249 
    250     typedef std::vector<std::pair<std::string, std::vector<u_int> > > vec_type;
    251     vec_type author_cont;
    252     author_cont.reserve(stat->size());
    253     for (A2VConstIter i= stat->begin(); i != stat->end(); ++i) {
    254       author_cont.push_back(std::make_pair(i->first,
    255                                            get_vector(*stat,i->first)));
    256     }
    257 
    258     LessReversed<std::vector<u_int> > lr;
    259     PairSecondCompare<std::string, std::vector<u_int>,
    260       LessReversed<std::vector<u_int> > > compare(lr);
    261     std::sort(author_cont.begin(), author_cont.end(), compare);
    262 
    263     size_t plotno=author_cont.size();
    264     std::stringstream ss;
    265     vec_type::iterator end(author_cont.end());
    266     for (vec_type::iterator i(author_cont.begin()); i!=end; ++i) {
    267       ss.str("");
    268       ss << "set key height " << 2*plotno;
    269       gp->command(ss.str());
    270       ss.str("");
    271       ss << i->second.back() << " " << i->first;
    272       gp->yrange(yrange_max);
    273       gp->linetitle(ss.str());
    274       ss.str("");
    275       ss << "steps " << --plotno+2;
    276       gp->linestyle(ss.str());
    277       gp->plot(i->second);
    278     }
    279     ss.str("");
    280     ss << total.back() << " total";
    281     gp->command("set key height 0");
    282     gp->linetitle(ss.str());
    283     gp->linestyle("steps 1");
    284     gp->plot(total);
    285 
    286     gp->command("unset multiplot");
    287     gp->yrange();
    288 
    289     return filename;
    290   }
    291 
    292 
    293   void ClassicStats::do_print(std::ostream& os) const
    294   {
    295     os << last_changed_rev() << " ";
    296     os << authors().size() << " ";
    297 
    298     std::copy(authors().begin(), authors().end(),
    299               std::ostream_iterator<std::string>(os, "\n"));
    300     os << code_cache() << "\n";
    301     do_print(os, code_);
    302     os << "\n" << comments_cache() << "\n";
    303     do_print(os, comments_);
    304     os << "\n" << other_cache() << "\n";
    305     do_print(os, other_);
    306     os << "\n" << total_cache() << "\n";
    307     do_print(os, total_);
    308     os << "\n" << end_of_cache() << "\n";
    309   }
    310 
    311 
    312   void ClassicStats::do_print(std::ostream& os, const Author2Vector& m) const
    313   {
    314     for (A2VConstIter i(m.begin()); i!=m.end(); ++i){
    315       os << i->first << "\n";
    316       os << i->second.size() << " ";
    317       std::copy(i->second.begin(), i->second.end(),
    318                 std::ostream_iterator<u_int>(os, " "));
    319     }
    320   }
    321 
    322  
    323135}} // end of namespace svndigest and namespace theplu
  • trunk/lib/ClassicStats.h

    r528 r529  
    4545    ClassicStats(const ClassicStats& other);
    4646
    47     ///
    48     /// Summary plot for the first page
    49     ///
    50     void plot_summary(const std::string& output) const;
    51 
    5247  private:
    5348    /// using compiler generated copy
     
    7772    void do_print(std::ostream&) const;
    7873
    79     // Change this string if cache format is changed in such a way
    80     // that all old cache files are obsolete.
    81     inline std::string end_of_cache(void) const
    82     {return "END OF OK CACHE FILE VERSION 2";}
    83    
    84 
    8574    std::vector<u_int> vector(std::string type, std::string user) const;
    8675    /**
  • trunk/lib/Stats.cc

    r528 r529  
    5656    revision_=svn_info.rev();
    5757    last_changed_rev_=svn_info.last_changed_rev();
    58     std::vector<u_int> vec(last_changed_rev_+1, 0);
    59     code_["all"] = vec;
    60     comments_["all"] = vec;
    61     other_["all"] = vec;
    62     total_["all"] = vec;
    6358  }
    6459
     
    164159
    165160
     161  svn_revnum_t Stats::last_changed_rev(void) const
     162  {
     163    return last_changed_rev_;
     164  }
     165
     166
     167  u_int Stats::lines(const std::string& user) const
     168  {
     169    return get_back(total_, user);
     170  }
     171
     172
     173  void Stats::load(std::istream& is, Author2Vector& m)
     174  {
     175    while (m.size() < authors().size()+1 && is.good()) {
     176      std::string name;
     177      std::getline(is, name);
     178      if (name.empty())
     179        continue;
     180      std::vector<u_int>& vec=m[name];
     181      size_t revs=0;
     182      is >> revs;
     183      vec.reserve(revs);
     184      while (vec.size() < revs && is.good()) {
     185        u_int tmp;
     186        is >> tmp;
     187        vec.push_back(tmp);
     188      }
     189    }
     190  }
     191
     192
    166193  bool Stats::load_cache(std::istream& is)
    167194  {
    168 
    169195    svn_revnum_t rev;
    170196    is >> rev;
    171     if (rev<last_changed_rev_){
     197    if (rev<last_changed_rev()){
    172198      return false; // cache is not up to date
    173199    }
     200    reset();
    174201    size_t a_size=0;
    175     authors_.clear();
    176202    is >> a_size;
    177203    std::string str;
    178     getline(is, str);
    179     while (authors_.size()<a_size){
     204    while (authors().size()<a_size && is.good()){
    180205      getline(is, str);
    181       assert(str.size());
    182       authors_.insert(str);
    183     }
    184     return do_load_cache(is);
    185   }
    186 
    187 
    188   svn_revnum_t Stats::last_changed_rev(void) const
    189   {
    190     return last_changed_rev_;
    191   }
    192 
    193 
    194   u_int Stats::lines(const std::string& user) const
    195   {
    196     return get_back(total_, user);
     206      if (str.size())
     207        add_author(str);
     208    }
     209    getline(is, str);
     210    if (str!=code_cache()){
     211      return false;
     212    }
     213    load(is, code_);
     214    getline(is, str);
     215    getline(is, str);
     216    if (str!=comments_cache()){
     217      return false;
     218    }
     219    load(is, comments_);
     220    getline(is, str);
     221    getline(is, str);
     222    if (str!=other_cache()){
     223      return false;
     224    }
     225    load(is, other_);
     226    getline(is, str);
     227    getline(is, str);
     228    if (str!=total_cache()){
     229      return false;
     230    }
     231    load(is, total_);
     232    getline(is,str);
     233    getline(is,str);
     234    return str==end_of_cache();
    197235  }
    198236
     
    233271                          const std::string& linetype) const
    234272  {
    235     return do_plot(filename, linetype);
     273    plot_init(filename);
     274    GnuplotFE* gp=GnuplotFE::instance();
     275    const Author2Vector* stat=NULL;
     276    if (linetype=="total")
     277      stat = &total_;
     278    else if (linetype=="code")
     279      stat = &code_;
     280    else if (linetype=="comments")
     281      stat = &comments_;
     282    else if (linetype=="empty")
     283      stat = &other_;
     284    assert(stat);
     285    assert(stat->size());
     286    std::vector<u_int> total=get_vector(*stat, "all");   
     287    double yrange_max=1.03*total.back()+1;
     288    gp->yrange(yrange_max);
     289
     290    typedef std::vector<std::pair<std::string, std::vector<u_int> > > vec_type;
     291    vec_type author_cont;
     292    author_cont.reserve(stat->size());
     293    for (std::set<std::string>::const_iterator i=authors_.begin();
     294         i != authors_.end(); ++i) {
     295      if (lines(*i))
     296        author_cont.push_back(std::make_pair(*i,get_vector(*stat,*i)));
     297    }
     298
     299    LessReversed<std::vector<u_int> > lr;
     300    PairSecondCompare<std::string, std::vector<u_int>,
     301      LessReversed<std::vector<u_int> > > compare(lr);
     302    std::sort(author_cont.begin(), author_cont.end(), compare);
     303
     304    size_t plotno=author_cont.size();
     305    std::stringstream ss;
     306    vec_type::iterator end(author_cont.end());
     307    for (vec_type::iterator i(author_cont.begin()); i!=end; ++i) {
     308      ss.str("");
     309      ss << "set key height " << 2*plotno;
     310      gp->command(ss.str());
     311      ss.str("");
     312      ss << get_back(*stat, i->first) << " " << i->first;
     313      gp->yrange(yrange_max);
     314      gp->linetitle(ss.str());
     315      ss.str("");
     316      ss << "steps " << --plotno+2;
     317      gp->linestyle(ss.str());
     318      gp->plot(i->second);
     319    }
     320    ss.str("");
     321    ss << get_back(*stat, "all") << " total";
     322    gp->command("set key height 0");
     323    gp->linetitle(ss.str());
     324    gp->linestyle("steps 1");
     325    gp->plot(total);
     326
     327    gp->command("unset multiplot");
     328    gp->yrange();
     329
     330    return filename;
    236331  }
    237332
     
    297392  void Stats::print(std::ostream& os) const
    298393  {
    299     os << last_changed_rev_ << " ";
    300     os << authors_.size() << "\n";
    301 
    302     std::copy(authors_.begin(), authors_.end(),
     394    os << last_changed_rev() << " ";
     395    os << authors().size() << " ";
     396
     397    std::copy(authors().begin(), authors().end(),
    303398              std::ostream_iterator<std::string>(os, "\n"));
    304     do_print(os);
    305   }
    306 
     399    os << code_cache() << "\n";
     400    print(os, code_);
     401    os << "\n" << comments_cache() << "\n";
     402    print(os, comments_);
     403    os << "\n" << other_cache() << "\n";
     404    print(os, other_);
     405    os << "\n" << total_cache() << "\n";
     406    print(os, total_);
     407    os << "\n" << end_of_cache() << "\n";
     408  }
     409
     410
     411  void Stats::print(std::ostream& os, const Author2Vector& m) const
     412  {
     413    for (A2VConstIter i(m.begin()); i!=m.end(); ++i){
     414      os << i->first << "\n";
     415      os << i->second.size() << " ";
     416      std::copy(i->second.begin(), i->second.end(),
     417                std::ostream_iterator<u_int>(os, " "));
     418    }
     419  }
    307420
    308421  void Stats::reset(void)
    309422  {
    310     assert("implement me");
     423    total_.clear();
     424    code_.clear();
     425    comments_.clear();
     426    other_.clear();
     427    authors_.clear();
     428    std::vector<u_int> vec;
     429    code_["all"] = vec;
     430    comments_["all"] = vec;
     431    other_["all"] = vec;
     432    total_["all"] = vec;
    311433  }
    312434
  • trunk/lib/Stats.h

    r528 r529  
    133133
    134134  protected:
    135     inline std::string code_cache(void) const {return "CACHE CODE";}
    136     inline std::string comments_cache(void) const {return "CACHE COMMENTS";}
    137     inline std::string other_cache(void) const {return "CACHE EMPTY";}
    138     inline std::string total_cache(void) const {return "CACHE TOTAL";}
    139 
    140135    typedef std::map<std::string, std::vector<u_int> > Author2Vector;
    141136    typedef Author2Vector::iterator A2VIter;
     
    169164    void accumulate(std::vector<u_int>& vec) const;
    170165    void accumulate_stats(void);
    171     virtual bool do_load_cache(std::istream&)=0;
    172166    virtual void do_parse(const std::string&)=0;
    173     virtual std::string do_plot(const std::string&, const std::string&) const=0;
    174     virtual void do_print(std::ostream& ) const=0;
    175167   
     168    // Change this string if cache format is changed in such a way
     169    // that all old cache files are obsolete.
     170    inline std::string end_of_cache(void) const
     171    {return "END OF OK CACHE FILE VERSION 2";}
     172    inline std::string code_cache(void) const {return "CACHE CODE";}
     173    inline std::string comments_cache(void) const {return "CACHE COMMENTS";}
     174    inline std::string other_cache(void) const {return "CACHE EMPTY";}
     175    inline std::string total_cache(void) const {return "CACHE TOTAL";}
     176
     177   
     178
    176179    u_int get_back(const Author2Vector&, std::string user) const;
     180    void load(std::istream& is, Author2Vector& m);
     181    void print(std::ostream& os, const Author2Vector& m) const;
    177182
    178183    svn_revnum_t revision_; // Should be the latest revision for whole project
Note: See TracChangeset for help on using the changeset viewer.