summaryrefslogtreecommitdiff
path: root/src/lib/analytics.cc
blob: cbb8843025e6b62a22cdf41515ac04e5124ae1a0 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
    Copyright (C) 2018 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "analytics.h"
#include "exceptions.h"
#include "job.h"
#include <dcp/raw_convert.h>
#include <dcp/util.h>
#include <libcxml/cxml.h>
#include <libxml++/libxml++.h>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include <iostream>

#include "i18n.h"

using std::string;
using std::map;
using dcp::raw_convert;
using boost::algorithm::trim;
using boost::shared_ptr;

Analytics* Analytics::_instance;
int const Analytics::_current_version = 1;

Event::Event ()
{
	gettimeofday (&_time, 0);
}

Event::Event (cxml::ConstNodePtr node)
{
	_time.tv_sec = node->number_child<int64_t>("Time");
	_time.tv_usec = 0;
	BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children()) {
		set(i->name(), i->content());
	}
}

void
Event::set (string k, string v)
{
	_data[k] = v;
}

string
Event::get (string k) const
{
	map<string, string>::const_iterator i = _data.find (k);
	if (i == _data.end()) {
		return "";
	}
	return i->second;
}

void
Event::as_xml (xmlpp::Element* parent) const
{
	/* It would be nice if this had timezone */
	parent->add_child("Time")->add_child_text(raw_convert<string>(_time.tv_sec));
	for (map<string, string>::const_iterator i = _data.begin(); i != _data.end(); ++i) {
		parent->add_child(i->first)->add_child_text(i->second);
	}
}

string
Event::dump () const
{
	string d;
	d += raw_convert<string>(_time.tv_sec) + "\n";
	for (map<string, string>::const_iterator i = _data.begin(); i != _data.end(); ++i) {
		d += i->first + ": " + i->second + "\n";
	}
	return d;
}

Analytics::Analytics ()
	: _id (dcp::make_uuid())
{

}

int
Analytics::successful_dcp_encodes () const
{
	boost::mutex::scoped_lock lm (_mutex);
	int n = 0;
	BOOST_FOREACH (Event e, _events) {
		if (e.get("type") == "job_state" && e.get("json_name") == "transcode" && e.get("status") == "finished_ok") {
			++n;
		}
	}
	return n;
}

void
Analytics::job_state_changed (shared_ptr<Job> job)
{
	Event ev;
	ev.set ("type", "job_state");
	ev.set ("json_name", job->json_name());
	ev.set ("sub_name", job->sub_name());
	ev.set ("error-summary", job->error_summary());
	ev.set ("error-details", job->error_details());
	ev.set ("status", job->json_status());

	{
		boost::mutex::scoped_lock lm (_mutex);
		_events.push_back (ev);
	}

	write ();

	if (successful_dcp_encodes() == 3) {
		emit (
			boost::bind(
				boost::ref(Message),
				_("Congratulations!"),
				_(
					"<h2>You have made 3 DCPs with DCP-o-matic!</h2>"
					"<img width=\"20%\" src=\"memory:me.jpg\" align=\"center\">"
                                        "<p>Hello. I'm Carl and I'm the "
					"developer of DCP-o-matic. I work on it in my spare time (with the help "
					"of a fine volunteer team of testers and translators) and I release it "
					"as free software."

					"<p>If you find DCP-o-matic useful, please consider a donation to the "
					"project. Financial support will help me to spend more "
					"time developing DCP-o-matic and making it better!"

					"<p><ul>"
					"<li><a href=\"https://dcpomatic.com/donate_amount?amount=40\">Go to Paypal to donate £40</a>"
					"<li><a href=\"https://dcpomatic.com/donate_amount?amount=20\">Go to Paypal to donate £20</a>"
					"<li><a href=\"https://dcpomatic.com/donate_amount?amount=10\">Go to Paypal to donate £10</a>"
					"</ul>"

					"<p>Thank you!"
					)
				)
			);
	}
}

void
Analytics::write () const
{
	xmlpp::Document doc;
	xmlpp::Element* root = doc.create_root_node ("Analytics");

	root->add_child("Version")->add_child_text(raw_convert<string>(_current_version));
	boost::mutex::scoped_lock lm (_mutex);
	root->add_child("Id")->add_child_text(_id);
	BOOST_FOREACH (Event e, _events) {
		e.as_xml (root->add_child("Event"));
	}

	try {
		doc.write_to_file_formatted(path("analytics.xml").string());
	} catch (xmlpp::exception& e) {
		string s = e.what ();
		trim (s);
		throw FileError (s, path("analytics.xml"));
	}
}

void
Analytics::read ()
try
{
	cxml::Document f ("Analytics");
	f.read_file (path("analytics.xml"));
	boost::mutex::scoped_lock lm (_mutex);
	_id = f.string_child("Id");
	BOOST_FOREACH (cxml::ConstNodePtr i, f.node_children("Event")) {
		_events.push_back (Event(i));
	}
} catch (...) {
	/* Never mind */
}

Analytics*
Analytics::instance ()
{
	if (!_instance) {
		_instance = new Analytics();
		_instance->read();
	}

	return _instance;
}