aboutsummaryrefslogtreecommitdiffstats
path: root/main.cpp
blob: 2e44f8b23cfd8ba4caeba816e5d10bcb9cd19f22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
** Copyright 2012 Kamil Rytarowski < kamil AT mageia DOT org >
**
** This Source Code Form is subject to the terms of the Mozilla Public
** License, v. 2.0. If a copy of the MPL was not distributed with this
** file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <chrono>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

#include <boost/date_time/posix_time/posix_time.hpp>

#include "pugixml.hpp"

typedef std::tuple<unsigned long /* revision */,
		   std::string /* author */,
		   boost::posix_time::ptime /* date */> tuple;

int main(int argc, char **argv)
{
  /*
  ** Read text stream from stdin
  */

  std::cin >> std::noskipws; /* Don't skip whitespaces */

  /*
  ** Use iterators to slurp data from the standard input
  */

  std::istream_iterator<char> it(std::cin);
  std::istream_iterator<char> end;
  const std::string input(it, end);

  pugi::xml_document doc;
  pugi::xml_parse_result result = doc.load_buffer(input.c_str(), input.size());

#ifndef NDEBUG
  std::cerr << "Load result: " << result.description() << "\n";
#endif
  
  /*
  ** <?xml version="1.0" encoding="UTF-8"?>
  ** <log>
  ** <logentry
  **   revision="1234">
  ** <author>author</author>
  ** <date>2012-01-21T10:00:00.000000Z</date>
  ** </logentry>
  ** </log>
  */
  pugi::xml_node log = doc.child("log");

  std::vector<tuple> table;

  for (pugi::xml_node logentry: log.children())
    {
      unsigned long revision = std::stoul(logentry.attribute("revision").value());
      std::string author = logentry.child("author").text().get();

#ifndef NDEBUG
      std::cerr << revision << "\t" << author << "\t";
#endif

      std::string datetime = logentry.child("date").text().get();
      boost::posix_time::time_input_facet * facet =
	new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%sZ"); /* 'Z' stands for UTC */

      std::stringstream ss;
      ss.imbue(std::locale(std::locale(), facet));
      ss << datetime;
      boost::posix_time::ptime date;
      ss >> date;

#ifndef NDEBUG
      std::cerr << datetime << "\t" << date << "\n";
#endif

      table.push_back(std::make_tuple(revision, author, date));
    }

  /*
  ** Statistics:
  ** 
  **  1. Overall number of the commiters
  **  2. Number of commits per a commiter
  **  3. 
  */

  /*
  ** 1. Overall number of the commiters
  */

  unsigned int overall_commiters = 0;
  {
    std::set<std::string> tmp_set;
    overall_commiters =
      (unsigned int) count_if(table.begin(), table.end(), [&tmp_set](tuple & t)
			      {
				std::string author = std::get<1>(t);
			      
				if (tmp_set.empty())
				  {
				    tmp_set.insert(author);
				    return true;
				  }
				else
				  {
				    if (tmp_set.find(author) == tmp_set.end())
				      {
					tmp_set.insert(author);
					return true;
				      }
				    else
				      return false;
				  }  
			      }
			      );
  }

#ifndef NDEBUG
  std::cerr << "Overall commiters: " << overall_commiters << "\n";
#endif

  /*
  ** 2. Number of commits per a commiter
  */
  
  std::map<std::string, unsigned int> commits_per_a_commiter;
  {
    for(auto t : table)
      {
	std::string author = std::get<1>(t);
	if (commits_per_a_commiter.empty())
	  commits_per_a_commiter.insert(std::pair<std::string, unsigned int>(author, 1));
	else
	  {
	    auto a = commits_per_a_commiter.find(author);
	    if (a == commits_per_a_commiter.end())
	      commits_per_a_commiter.insert(std::pair<std::string, unsigned int>(author, 1));
	    else
	      commits_per_a_commiter.at(author) = (commits_per_a_commiter.at(author) + 1);
	  }
      }
  }

#ifndef NDEBUG
  std::cerr << "Commits per a commiter:\n";
  for(auto c : commits_per_a_commiter)
    {
      std::cerr << "\t" << (c.first) << "\t" << (c.second) << "\n";
    }
#endif

  return EXIT_SUCCESS;
}