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