source: trunk/lib/Stats.cc @ 39

Last change on this file since 39 was 39, checked in by Jari Häkkinen, 16 years ago

Gnuplot working.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1//$Id: Stats.cc 39 2006-01-13 15:02:31Z jari $
2
3#include "Stats.h"
4#include "Gnuplot.h"
5#include "utility.h"
6
7#include <cstdio>
8#include <fstream>
9
10#include <map>
11#include <numeric>
12#include <sstream>
13#include <string>
14#include <utility>
15#include <vector>
16
17#include <iostream>
18
19namespace theplu{
20namespace svnstat{
21
22  Gnuplot Stats::gnuplot_pipe_;
23
24  std::vector<double> Stats::accumulated(void) const
25  {
26    if (map_.empty())
27      return std::vector<double>();
28
29    // sum of all users
30    std::vector<u_int> sum;
31    sum=std::accumulate(map_.begin(), map_.end(), sum,
32                        PairValuePlus<std::string,u_int>());
33
34    // calculate accumulated sum
35    std::vector<double> accum(sum.size());
36    std::partial_sum(sum.begin(),sum.end(),accum.begin());   
37    return accum;
38  }
39
40  std::vector<double> Stats::accumulated(const std::string& user) const
41  {
42    if (!map_.count(user))
43      return std::vector<double>();
44    std::vector<u_int> vec=(map_.find(user))->second;
45
46    std::vector<double> accum(vec.size());
47    std::partial_sum(vec.begin(),vec.end(),accum.begin());
48    return accum;
49  }
50
51  void Stats::add(const std::string& user, const u_int& rev)
52  {
53    std::vector<u_int> vec = map_[user];
54    if (vec.size() < rev+1){
55      u_int i=vec.size();
56      vec.resize(rev+1);
57      for (; i<rev; i++)
58        vec[i]=0;
59      vec[rev]=1;
60    }
61    else
62      vec[rev]++;
63    map_[user]=vec;
64  }
65
66  std::string Stats::plot(void) const
67  {
68    char name[]="svnstatXXXXXX";
69    if (!strlen(mktemp(name))) {
70      std::cerr << "Failed to get unique filename: " << name << std::endl;
71      exit(-1);
72    }
73    std::string cmd=std::string("set term png; set output '")+name+".png'";
74    gnuplot_pipe_.command(cmd);
75    gnuplot_pipe_.command("set key left Left reverse");
76    std::vector<double> x=accumulated();
77    std::stringstream sa;
78    sa << x.back() << " total";
79    gnuplot_pipe_.linetitle(sa.str());
80    gnuplot_pipe_.plot(x);
81    for (MapConstIter_ i= map_.begin(); i != map_.end(); i++) {
82      x=accumulated(i->first);
83      std::stringstream s;
84      s << x.back() << " " << i->first;
85      gnuplot_pipe_.linetitle(s.str());
86      gnuplot_pipe_.replot(x);
87    }
88
89    // Jari, must rewrite output once since the replots above are not
90    // added to the plot. Rather, the plot only contains the result
91    // from the first 'plot' call.
92    gnuplot_pipe_.command(cmd);
93    gnuplot_pipe_.command("replot");
94    return std::string(name)+".png";
95  }
96
97  void Stats::print(std::ostream& os) const
98  {
99    os << "<p><img src='" << plot() << "' alt='[svnstat plot]' border=0></p>\n";
100  }
101
102  Stats& Stats::operator+=(const Stats& other)
103  {
104    for (MapConstIter_ o_i= other.map_.begin(); o_i != other.map_.end(); ++o_i)
105    {
106      std::pair<MapIter_,bool> result = map_.insert(*o_i);
107      if (!result.second)
108        map_[(*(result.first)).first] = 
109          VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second );
110 
111    }
112    return *this;
113  }
114
115}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.