1 | //$Id$ |
---|
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 <string> |
---|
13 | #include <utility> |
---|
14 | #include <vector> |
---|
15 | |
---|
16 | #include <iostream> |
---|
17 | |
---|
18 | namespace theplu{ |
---|
19 | namespace svnstat{ |
---|
20 | |
---|
21 | Gnuplot Stats::gnuplot_pipe_; |
---|
22 | |
---|
23 | std::vector<double> Stats::accumulated(void) const |
---|
24 | { |
---|
25 | if (map_.empty()) |
---|
26 | return std::vector<double>(); |
---|
27 | |
---|
28 | // sum of all users |
---|
29 | std::vector<u_int> sum; |
---|
30 | sum=std::accumulate(map_.begin(), map_.end(), sum, |
---|
31 | PairValuePlus<std::string,u_int>()); |
---|
32 | |
---|
33 | // calculate accumulated sum |
---|
34 | std::vector<double> accum(sum.size()); |
---|
35 | std::partial_sum(sum.begin(),sum.end(),accum.begin()); |
---|
36 | return accum; |
---|
37 | } |
---|
38 | |
---|
39 | std::vector<double> Stats::accumulated(const std::string& user) const |
---|
40 | { |
---|
41 | if (!map_.count(user)) |
---|
42 | return std::vector<double>(); |
---|
43 | std::vector<u_int> vec=(map_.find(user))->second; |
---|
44 | |
---|
45 | std::vector<double> accum(vec.size()); |
---|
46 | std::partial_sum(vec.begin(),vec.end(),accum.begin()); |
---|
47 | return accum; |
---|
48 | } |
---|
49 | |
---|
50 | void Stats::add(const std::string& user, const u_int& rev) |
---|
51 | { |
---|
52 | std::vector<u_int> vec = map_[user]; |
---|
53 | if (vec.size() < rev+1){ |
---|
54 | u_int i=vec.size(); |
---|
55 | vec.resize(rev+1); |
---|
56 | for (; i<rev; i++) |
---|
57 | vec[i]=0; |
---|
58 | vec[rev]=1; |
---|
59 | } |
---|
60 | else |
---|
61 | vec[rev]++; |
---|
62 | map_[user]=vec; |
---|
63 | } |
---|
64 | |
---|
65 | std::string Stats::plot(void) const |
---|
66 | { |
---|
67 | char name[]="svnstatXXXXXX"; |
---|
68 | if (!strlen(mktemp(name))) { |
---|
69 | std::cerr << "Failed to get unique filename: " << name << std::endl; |
---|
70 | exit(-1); |
---|
71 | } |
---|
72 | std::string cmd=std::string("set term png; set output '")+name+".png'"; |
---|
73 | gnuplot_pipe_.command(cmd); |
---|
74 | gnuplot_pipe_.plot(accumulated()); |
---|
75 | for (MapConstIter_ i= map_.begin(); i != map_.end(); i++) |
---|
76 | gnuplot_pipe_.replot(accumulated(i->first)); |
---|
77 | |
---|
78 | // Jari, must rewrite output once since the replots above are not |
---|
79 | // added to the plot. Rather, the plot only contains the result |
---|
80 | // from the first 'plot' call. |
---|
81 | gnuplot_pipe_.command(cmd); |
---|
82 | gnuplot_pipe_.command("replot"); |
---|
83 | return std::string(name)+".png"; |
---|
84 | } |
---|
85 | |
---|
86 | void Stats::print(std::ostream& os) const |
---|
87 | { |
---|
88 | std::vector<std::pair<std::string, std::vector<u_int> > > accum; |
---|
89 | for (MapConstIter_ i= map_.begin(); i != map_.end(); i++){ |
---|
90 | std::pair<std::string, std::vector<u_int> > element(*i); |
---|
91 | std::partial_sum(element.second.begin(),element.second.end(), |
---|
92 | element.second.begin()); |
---|
93 | accum.push_back(element); |
---|
94 | } |
---|
95 | sort(accum.begin(),accum.end(), CodingMore()); |
---|
96 | os << "<table>\n"; |
---|
97 | for (size_t i = 0; i<accum.size(); i++){ |
---|
98 | os << "<tr><td>\n" << accum[i].first << "</td><td>" |
---|
99 | << accum[i].second.back() << "</td></tr>"; |
---|
100 | } |
---|
101 | os << "</table>\n"; |
---|
102 | |
---|
103 | os << "<p><img src='" << plot() << "' alt='[svnstat plot]' border=0></p>\n"; |
---|
104 | } |
---|
105 | |
---|
106 | Stats& Stats::operator+=(const Stats& other) |
---|
107 | { |
---|
108 | for (MapConstIter_ o_i= other.map_.begin(); o_i != other.map_.end(); ++o_i) |
---|
109 | { |
---|
110 | std::pair<MapIter_,bool> result = map_.insert(*o_i); |
---|
111 | if (!result.second) |
---|
112 | map_[(*(result.first)).first] = |
---|
113 | VectorPlus<u_int>()( (*(result.first)).second, (*o_i).second ); |
---|
114 | |
---|
115 | } |
---|
116 | return *this; |
---|
117 | } |
---|
118 | |
---|
119 | }} // end of namespace svnstat and namespace theplu |
---|