source: trunk/lib/first_page.cc @ 1254

Last change on this file since 1254 was 1254, checked in by Peter Johansson, 11 years ago

closes #228. Added count number of files and directories on first page. No count for individual authors.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1// $Id: first_page.cc 1254 2010-10-31 23:20:34Z 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 "NodeCounter.h"
34#include "Stats.h"
35#include "StatsCollection.h"
36#include "SVNlog.h"
37#include "Trac.h"
38#include "utility.h"
39
40#include <algorithm>
41#include <cassert>
42#include <fstream>
43#include <iostream>
44#include <sstream>
45#include <stdexcept>
46#include <string>
47#include <sys/param.h>
48#include <unistd.h>
49#include <vector>
50
51namespace theplu{
52namespace svndigest{
53
54  void print_main_page(const std::string& dir, const SVNlog& log,
55                       const StatsCollection& stats, std::string url, 
56                       const NodeCounter& node_counter)
57  {
58    std::string filename="index.html";
59    std::ofstream os(filename.c_str());
60    print_header(os, dir, 0, "all", "main", "index.html", "none");
61
62    using namespace std;
63    set<string> authors;
64    std::transform(log.commits().begin(), log.commits().end(),
65                   std::inserter(authors, authors.begin()), 
66                   std::mem_fun_ref(&Commitment::author));
67    // erase invalid authors
68    authors.erase("");
69    authors.erase("no author");
70
71    vector<Commitment> latest_commit;
72    latest_commit.reserve(authors.size());
73    for (set<string>::const_iterator i(authors.begin()); i!=authors.end(); ++i)
74      latest_commit.push_back(log.latest_commit(*i));
75
76    print_summary_plot(os, stats["classic"]);
77    print_general_information(os, log, authors.size(), url, node_counter);
78    sort(latest_commit.begin(), latest_commit.end(), GreaterRevision());
79    print_authors(os, latest_commit, stats["classic"]);
80    print_recent_logs(os, log, stats);
81    os << "<hr width=100% />";
82    print_footer(os);
83    os.close();
84  }
85
86  void print_general_information(std::ostream& os, const SVNlog& log, 
87                                 size_t nof_authors, std::string url,
88                                 const NodeCounter& node_counter)
89  { 
90    assert(log.commits().size());
91    Date begin(log.commits().begin()->date());   
92    Date end(log.latest_commit().date());
93    std::string timefmt("%a %b %e %H:%M:%S %Y");
94
95    os << "<div class=\"main\">" 
96       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
97       << "General Information"
98       << "</th></tr></thead>\n"
99       << "<tbody>\n"
100       << "<tr><td>URL:</td><td>"; 
101    if (url.size()>=4 && url.substr(0,4)=="http")
102      os << anchor(url, url);
103    else
104      os << url;
105    os << "</td></tr>\n"
106       << "<tr><td>First Revision Date:</td><td>" 
107       << begin(timefmt) << "</td></tr>\n"
108       << "<tr><td>Latest Revision Date:</td><td>" 
109       << end(timefmt) << "</td></tr>\n"
110       << "<tr><td>Age:</td><td>"; 
111    os << end.difftime(begin);
112    os << "</td></tr>\n"
113       << "<tr><td>Smallest Revision:</td><td>" 
114       << log.commits().begin()->revision() 
115       << "</td></tr>\n"
116       << "<tr><td>Largest Revision:</td><td>" << log.latest_commit().revision() 
117       << "</td></tr>\n"
118       << "<tr><td>Revision Count:</td><td>" << log.commits().size() 
119       << "</td></tr>\n"
120       << "<tr><td>Number of Authors:</td><td>" << nof_authors
121       << "</td></tr>\n"
122       << "<tr><td>Number of Directories:</td><td>" 
123       << node_counter.directories()
124       << "</td></tr>\n"
125       << "<tr><td>Number of Files:</td><td>" 
126       << node_counter.files()
127       << "</td></tr>\n"
128       << "</tbody>\n"
129       << "</table></div>\n";
130  }
131
132
133  void print_authors(std::ostream& os, 
134                     const std::vector<Commitment>& lc,
135                     const Stats& stats)
136  {
137    HtmlStream hs(os);
138    os << "<div class=\"main\">"
139       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
140       << "Authors"
141       << "</th></tr></thead>\n";
142
143    os << "<tr><td>Author</td>" 
144       << "<td>Number of Lines</td>"
145       << "<td>Code Lines</td>"
146       << "<td>Comment Lines</td>"
147       << "<td>Latest Commitment</td>"
148       <<"</tr>";
149
150    std::string timefmt("%Y-%m-%d  %H:%M");
151    using namespace std;
152    for (vector<Commitment>::const_iterator i=lc.begin(); i!=lc.end(); ++i) {
153      os << "<tr><td>"; 
154      if (!stats.lines(i->author()))
155        os << i->author();
156      else
157        os << anchor(std::string("classic/"+i->author()+"/total/index.html"),
158                     i->author());
159      os << "</td><td>" << stats.lines(i->author()) << " (" 
160         << 100*stats.lines(i->author())/(stats.lines()?stats.lines():1)
161         << "%)</td><td>" << stats.code(i->author()) << " (" 
162         << 100*stats.code(i->author())/(stats.code()?stats.code():1)
163         << "%)</td><td>" << stats.comments(i->author()) << " (" 
164         << 100*stats.comments(i->author())/(stats.comments()?stats.comments():1)
165         << "%)</td><td>";
166      std::string date = Date(i->date())(timefmt);
167      const Configuration& conf = Configuration::instance();
168      if (conf.trac_root().empty())
169        hs << date;
170      else {
171        std::stringstream url;
172        url << conf.trac_root() << "changeset/" << i->revision();
173        os << anchor(url.str(), date);
174      }
175      os << "</td>" <<"</tr>";
176    }
177    os << "<tr><td>Total</td>";
178    os << "<td>" << stats.lines() << "</td>"
179       << "<td>" << stats.code() << "</td>"
180       << "<td>" << stats.comments() << "</td>"
181       <<"</tr>";
182
183    os << "</table></div>\n";
184  }
185
186
187  void print_recent_logs(std::ostream& os, const SVNlog& log,
188                         const StatsCollection& stats)
189  {
190    os << "<div class=\"main\">\n"
191       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
192       << "Recent Log"
193       << "</th></tr></thead>\n";
194
195    os << "<tr><td>Author</td><td>Date</td><td>Rev</td><td>Added</td>"
196       << "<td>Removed</td><td>Message</td></tr>\n";
197    HtmlStream hs(os);
198    std::string timefmt("%Y-%m-%d  %H:%M");
199    const size_t maxlength = 80;
200    const Configuration& conf = Configuration::instance();
201    typedef SVNlog::container::const_reverse_iterator iter;
202    size_t count = 0;
203    for (iter i=log.commits().rbegin(); 
204         i != log.commits().rend() && count<10; ++i) {
205      os << "<tr><td>" 
206         << anchor(std::string("classic/")+i->author()+"/total/index.html",
207                   i->author()) 
208         << "</td>";
209      Date date(i->date());
210      os << "<td>";
211      hs << date(timefmt); 
212      os << "</td>";
213      os << "<td>"; 
214      os << trac_revision(i->revision());
215      os << "</td>";
216      os << "<td>";
217      int added = stats["add"](LineTypeParser::total, "all", i->revision()) -
218        stats["add"](LineTypeParser::total, "all", i->revision() - 1); 
219      os << added;
220      os << "</td>";
221      os << "<td>";
222      os << added-(stats["blame"](LineTypeParser::total, "all", i->revision()) -
223                   stats["blame"](LineTypeParser::total,"all",i->revision()-1)); 
224      os << "</td>";
225      os << "<td>";
226      std::string mess = i->message();
227      // replace newlines with space
228      std::replace(mess.begin(), mess.end(), '\n', ' ');
229      mess = htrim(mess);
230
231      if (conf.trac_root().empty()) {
232        // truncate message if too long
233        if (mess.size()>maxlength)
234          mess = mess.substr(0,maxlength-3) + "...";
235        hs << mess;
236      }
237      else {// make anchors to trac
238        Trac trac(hs);
239        trac.print(mess, maxlength);
240      }
241
242      os << "</td></tr>\n";
243      ++count;
244    }
245    os << "</table></div>\n";
246  }
247
248
249  void print_summary_plot(std::ostream& os, const Stats& stats)
250  {
251    std::string name("summary_plot");
252    stats.plot_summary(name);
253    os << "<div class=\"main\">\n";
254    os << image(name);
255    os << "</div>";
256
257
258  }
259
260}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.