Partial work on gathering details of job events.
[dcpomatic.git] / src / lib / analytics.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "analytics.h"
22 #include "exceptions.h"
23 #include <dcp/raw_convert.h>
24 #include <libcxml/cxml.h>
25 #include <libxml++/libxml++.h>
26 #include <boost/filesystem.hpp>
27 #include <boost/algorithm/string.hpp>
28
29 #include "i18n.h"
30
31 using std::string;
32 using dcp::raw_convert;
33 using boost::algorithm::trim;
34
35 Analytics* Analytics::_instance;
36 int const Analytics::_current_version = 1;
37
38 Event::Event (cxml::ConstNodePtr node)
39 {
40         _time.tv_sec = node->number_child<int64_t>("Time");
41         _time.tv_usec = 0;
42         BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children()) {
43                 set(i->name(), i->content());
44         }
45 }
46
47 void
48 Event::set (string k, string v)
49 {
50         _data[k] = v;
51 }
52
53 void
54 Event::as_xml (cxml::NodePtr parent)
55 {
56         /* It would be nice if this had timezone */
57         parent->add_child("Time")->add_child_text(raw_convert<int64_t>(_time.tv_sec));
58         for (map<string, string>::const_iterator i = _data.begin(); i != _data.end(); ++i) {
59                 parent->add_child(i->first)->add_child_text(i->second);
60         }
61 }
62
63 Analytics::Analytics ()
64 {
65
66 }
67
68 int
69 Analytics::successful_dcp_encodes () const
70 {
71         boost::mutex::scoped_lock lm (_mutex);
72         BOOST_FOREACH (Event e, _events) {
73                 cout << e.dump() << "\n";
74         }
75 }
76
77 void
78 Analytics::job_state_changed (shared_ptr<Job> job)
79 {
80         Event ev;
81         ev.set ("type", "job-state");
82         ev.set ("json_name", job->json_name());
83         ev.set ("sub_name", job->sub_name());
84         ev.set ("error-summary", job->error_summary());
85         ev.set ("error-details", job->error_details());
86         ev.set ("status", job->json_status());
87
88         {
89                 boost::mutex::scoped_lock lm (_mutex);
90                 _events.push_back (ev);
91         }
92
93         write ();
94
95         if (successful_dcp_encodes() == 3) {
96                 emit (
97                         boost::bind(
98                                 boost::ref(Message),
99                                 _("Congratulations!"),
100                                 _(
101                                         "<h2>You have made 3 DCPs with DCP-o-matic!</h2>"
102                                         "<img width=\"20%\" src=\"memory:me.jpg\" align=\"center\">"
103                                         "<p>Hello. I'm Carl and I'm the "
104                                         "developer of DCP-o-matic. I work on it in my spare time (with the help "
105                                         "of a fine volunteer team of testers and translators) and I release it "
106                                         "as free software."
107
108                                         "<p>If you find DCP-o-matic useful, please consider a donation to the "
109                                         "project. Financial support will help me to spend more "
110                                         "time developing DCP-o-matic and making it better!"
111
112                                         "<p><ul>"
113                                         "<li><a href=\"https://dcpomatic.com/donate_amount?amount=40\">Go to Paypal to donate £40</a>"
114                                         "<li><a href=\"https://dcpomatic.com/donate_amount?amount=20\">Go to Paypal to donate £20</a>"
115                                         "<li><a href=\"https://dcpomatic.com/donate_amount?amount=10\">Go to Paypal to donate £10</a>"
116                                         "</ul>"
117
118                                         "<p>Thank you!"
119                                         )
120                                 )
121                         );
122         }
123 }
124
125 void
126 Analytics::write () const
127 {
128         xmlpp::Document doc;
129         xmlpp::Element* root = doc.create_root_node ("Analytics");
130
131         root->add_child("Version")->add_child_text(raw_convert<string>(_current_version));
132         boost::mutex::scoped_lock lm (_mutex);
133         BOOST_FOREACH (Event e, _events) {
134                 e.as_xml (root->add_child("Event"));
135         }
136
137         try {
138                 doc.write_to_file_formatted(path("analytics.xml").string());
139         } catch (xmlpp::exception& e) {
140                 string s = e.what ();
141                 trim (s);
142                 throw FileError (s, path("analytics.xml"));
143         }
144 }
145
146 void
147 Analytics::read ()
148 try
149 {
150         cxml::Document f ("Analytics");
151         f.read_file (path("analytics.xml"));
152         boost::mutex::scoped_lock lm (_mutex);
153         BOOST_FOREACH (cxml::ConstNodePtr i, f.node_children("Event")) {
154                 _events.push_back (Event(i));
155         }
156 } catch (...) {
157         /* Never mind */
158 }
159
160 Analytics*
161 Analytics::instance ()
162 {
163         if (!_instance) {
164                 _instance = new Analytics();
165                 _instance->read();
166         }
167
168         return _instance;
169 }