source: branches/replacing_gnuplot/lib/first_page.cc @ 646

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

Merged trunk changes r608:645 to replacing_gnuplot branch.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1// $Id: first_page.cc 646 2008-06-03 22:05:22Z jari $
2
3/*
4  Copyright (C) 2006 Peter Johansson
5  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
6
7  This file is part of svndigest, http://trac.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "first_page.h"
26
27#include "Commitment.h"
28#include "Configuration.h"
29#include "Date.h"
30#include "HtmlStream.h"
31#include "html_utility.h"
32#include "Stats.h"
33#include "SVNlog.h"
34#include "Trac.h"
35#include "utility.h"
36#include <config.h> // this header file is created by configure
37
38#include <algorithm>
39#include <cassert>
40#include <fstream>
41#include <iostream>
42#include <sstream>
43#include <stdexcept>
44#include <string>
45#include <sys/param.h>
46#include <unistd.h>
47#include <vector>
48
49namespace theplu{
50namespace svndigest{
51
52  void print_main_page(const std::string& dir, const SVNlog& log,
53                       const StatsCollection& stats, std::string url)
54  {
55    std::string filename="index.html";
56    std::ofstream os(filename.c_str());
57    print_header(os, dir, 0, "all", "main", "index.html", "none");
58
59    using namespace std;
60    set<string> authors;
61    std::transform(log.commits().begin(), log.commits().end(),
62                   std::inserter(authors, authors.begin()), GetAuthor());
63    // erase invalid authors
64    authors.erase("");
65    authors.erase("no author");
66
67    vector<Commitment> latest_commit;
68    latest_commit.reserve(authors.size());
69    for (set<string>::const_iterator i(authors.begin()); i!=authors.end(); ++i)
70      latest_commit.push_back(log.latest_commit(*i));
71
72    print_summary_plot(os, stats["classic"]);
73    print_general_information(os, log, authors.size(), url);
74    sort(latest_commit.begin(), latest_commit.end(), GreaterRevision());
75    print_authors(os, latest_commit, stats["classic"]);
76    print_recent_logs(os, log, stats);
77    os << "<hr width=100% />";
78    print_footer(os);
79    os.close();
80  }
81
82  void print_general_information(std::ostream& os, const SVNlog& log, 
83                                 size_t nof_authors, std::string url)
84  { 
85    assert(log.commits().size());
86    Date begin(log.commits()[0].date());
87    Date end(log.commits().back().date());
88    std::string timefmt("%a, %e %b %Y  %H:%M:%S");
89
90    os << "<div class=\"main\">" 
91       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
92       << "General Information"
93       << "</th></tr></thead>\n"
94       << "<tbody>\n"
95       << "<tr><td>URL:</td><td>" 
96       << url << "</td></tr>\n"
97       << "<tr><td>First Revision Date:</td><td>" 
98       << begin(timefmt) << "</td></tr>\n"
99       << "<tr><td>Latest Revision Date:</td><td>" 
100       << end(timefmt) << "</td></tr>\n"
101       << "<tr><td>Age:</td><td>"; 
102    os << end.difftime(begin);
103    os << "</td></tr>\n"
104       << "<tr><td>Smallest Revision:</td><td>" << log.commits()[0].revision() 
105       << "</td></tr>\n"
106       << "<tr><td>Largest Revision:</td><td>" <<log.commits().back().revision() 
107       << "</td></tr>\n"
108       << "<tr><td>Revision Count:</td><td>" << log.commits().size() 
109       << "</td></tr>\n"
110       << "<tr><td>Number of Authors:</td><td>" << nof_authors
111       << "</td></tr>\n"
112       << "</tbody>\n"
113       << "</table></div>\n";
114  }
115
116
117  void print_authors(std::ostream& os, 
118                     const std::vector<Commitment>& lc,
119                     const Stats& stats)
120  {
121    os << "<div class=\"main\">"
122       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
123       << "Authors"
124       << "</th></tr></thead>\n";
125
126    os << "<tr><td>Author</td>" 
127       << "<td>Number of Lines</td>"
128       << "<td>Code Lines</td>"
129       << "<td>Comment Lines</td>"
130       << "<td>Latest Commitment</td>"
131       <<"</tr>";
132
133    std::string timefmt("%Y %b %d %H:%M:%S");
134    using namespace std;
135    for (vector<Commitment>::const_iterator i=lc.begin(); i!=lc.end(); ++i) {
136      os << "<tr><td>"; 
137      if (!stats.lines(i->author()))
138        os << i->author();
139      else
140        os << anchor(std::string("classic/"+i->author()+"/total/index.html"),
141                     i->author());
142      os << "</td><td>" << stats.lines(i->author()) << " (" 
143         << 100*stats.lines(i->author())/(stats.lines()?stats.lines():1)
144         << "%)</td><td>" << stats.code(i->author()) << " (" 
145         << 100*stats.code(i->author())/(stats.code()?stats.code():1)
146         << "%)</td><td>" << stats.comments(i->author()) << " (" 
147         << 100*stats.comments(i->author())/(stats.comments()?stats.comments():1)
148         << "%)</td><td>" << Date(i->date())(timefmt) << "</td>" <<"</tr>";
149    }
150    os << "<tr><td>Total</td>";
151    os << "<td>" << stats.lines() << "</td>"
152       << "<td>" << stats.code() << "</td>"
153       << "<td>" << stats.comments() << "</td>"
154       <<"</tr>";
155
156    os << "</table></div>\n";
157  }
158
159
160  void print_recent_logs(std::ostream& os, const SVNlog& log,
161                         const StatsCollection& stats)
162  {
163    os << "<div class=\"main\">\n"
164       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
165       << "Recent Log"
166       << "</th></tr></thead>\n";
167
168    os << "<tr><td>Author</td><td>Date</td><td>Rev</td><td>Added</td>"
169       << "<td>Removed</td><td>Message</td></tr>\n";
170    HtmlStream hs(os);
171    std::string timefmt("%Y %b %d %H:%M:%S");
172    const size_t maxlength = 80;
173    const Configuration& conf = Configuration::instance();
174    typedef std::vector<Commitment>::const_reverse_iterator iter;
175    for (iter i=log.commits().rbegin(); 
176         i < std::min(i+10,log.commits().rend()); ++i) {
177      os << "<tr><td>" 
178         << anchor(std::string("classic/")+i->author()+"/total/index.html",
179                   i->author()) 
180         << "</td>";
181      Date date(i->date());
182      os << "<td>" << date(timefmt) << "</td>";
183      os << "<td>"; 
184      os << trac_revision(i->revision());
185      os << "</td>";
186      os << "<td>";
187      int added = stats["add"](LineTypeParser::total, "all", i->revision()) -
188        stats["add"](LineTypeParser::total, "all", i->revision() - 1); 
189      os << added;
190      os << "</td>";
191      os << "<td>";
192      os << added-(stats["blame"](LineTypeParser::total, "all", i->revision()) -
193                   stats["blame"](LineTypeParser::total,"all",i->revision()-1)); 
194      os << "</td>";
195      os << "<td>";
196      std::string mess = i->message();
197      // replace newlines with space
198      std::replace(mess.begin(), mess.end(), '\n', ' ');
199      mess = htrim(mess);
200
201      if (conf.trac_root().empty()) {
202        // truncate message if too long
203        if (mess.size()>maxlength)
204          mess = mess.substr(0,maxlength-3) + "...";
205        hs << mess;
206      }
207      else {// make anchors to trac
208        Trac trac(hs);
209        trac.print(mess, maxlength);
210      }
211
212      os << "</td></tr>\n";
213    }
214    os << "</table></div>\n";
215  }
216
217
218  void print_summary_plot(std::ostream& os, const Stats& stats)
219  {
220    std::string name("summary_plot.png");
221    stats.plot_summary(name);
222    os << "<div class=\"main\">\n";
223    os << "<img src='" << name << "' alt='[plot]'/>"; 
224    os << "</div>";
225  }
226
227}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.