source: trunk/lib/first_page.cc @ 1253

Last change on this file since 1253 was 1253, checked in by Peter Johansson, 12 years ago

prefer including config.h first.

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