-
Notifications
You must be signed in to change notification settings - Fork 0
/
piwik-openaire-dspace5-sqlite-update.diff
executable file
·392 lines (384 loc) · 15.4 KB
/
piwik-openaire-dspace5-sqlite-update.diff
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
diff --git a/dspace/config/launcher.xml b/dspace/config/launcher.xml
index 931b461fa..3d5eb3320 100644
--- a/dspace/config/launcher.xml
+++ b/dspace/config/launcher.xml
@@ -403,4 +403,28 @@
<class>org.dspace.app.util.Version</class>
</step>
</command>
+ <command>
+ <name>resend-to-matomo</name>
+ <description>Create Matomo DB for Missing Usage Events</description>
+ <arguments>-create</arguments>
+ <step>
+ <class>com.openaire.piwik.tracker.OpenAirePiwikTrackerUnreportedDB</class>
+ </step>
+ </command>
+ <command>
+ <name>resend-to-matomo</name>
+ <description>Retry sending all Missing Usage Events to Matomo</description>
+ <arguments>-retry</arguments>
+ <step>
+ <class>com.openaire.piwik.tracker.OpenAirePiwikTrackerUnreportedDB</class>
+ </step>
+ </command>
+ <command>
+ <name>resend-to-matomo</name>
+ <description>Delete Usage Events Sent to Matomo</description>
+ <arguments>-delete</arguments>
+ <step>
+ <class>com.openaire.piwik.tracker.OpenAirePiwikTrackerUnreportedDB</class>
+ </step>
+ </command>
</commands>
diff --git a/dspace/config/modules/oapiwik.cfg b/dspace/config/modules/oapiwik.cfg
index 42aa24142..350688b27 100755
--- a/dspace/config/modules/oapiwik.cfg
+++ b/dspace/config/modules/oapiwik.cfg
@@ -16,4 +16,11 @@ piwik.tokenAuth=32846584f571be9b57488bf4088f30ea
#piwik.ipanonymizationbytes=2
# Flag to enable/disable tracking. Default is true
-piwik.enabled = true
\ No newline at end of file
+piwik.enabled = true
+
+
+# Flag whether failed reports of views should be stored in a database to resend them later
+piwik.retry = true
+
+# Location of the SQLite DB on the file system is [dspace]/oapiwik.piwik.matomoDbLocation
+piwik.matomoDbLocation = var/Matomo.db
diff --git a/dspace/modules/additions/pom.xml b/dspace/modules/additions/pom.xml
index 108119db4..068f10b14 100644
--- a/dspace/modules/additions/pom.xml
+++ b/dspace/modules/additions/pom.xml
@@ -96,7 +96,12 @@
</exclusion>
</exclusions>
</dependency>
-
+ <dependency>
+ <groupId>com.openaire</groupId>
+ <artifactId>openaire-piwik-api</artifactId>
+ <version>1.0.0</version>
+ <type>jar</type>
+ </dependency>
</dependencies>
</project>
diff --git a/dspace/modules/openaire-piwik/openaire-piwik-api/pom.xml b/dspace/modules/openaire-piwik/openaire-piwik-api/pom.xml
index 132c4afb2..8cfc2e4c1 100755
--- a/dspace/modules/openaire-piwik/openaire-piwik-api/pom.xml
+++ b/dspace/modules/openaire-piwik/openaire-piwik-api/pom.xml
@@ -35,5 +35,11 @@
<artifactId>httpasyncclient</artifactId>
<version>4.0.2</version>
</dependency>
+ <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
+ <dependency>
+ <groupId>org.xerial</groupId>
+ <artifactId>sqlite-jdbc</artifactId>
+ <version>3.21.0.1</version>
+ </dependency>
</dependencies>
</project>
diff --git a/dspace/modules/openaire-piwik/openaire-piwik-api/src/main/java/com/openaire/piwik/tracker/OpenAirePiwikTracker.java b/dspace/modules/openaire-piwik/openaire-piwik-api/src/main/java/com/openaire/piwik/tracker/OpenAirePiwikTracker.java
index a6ed0c064..9150dbe41 100755
--- a/dspace/modules/openaire-piwik/openaire-piwik-api/src/main/java/com/openaire/piwik/tracker/OpenAirePiwikTracker.java
+++ b/dspace/modules/openaire-piwik/openaire-piwik-api/src/main/java/com/openaire/piwik/tracker/OpenAirePiwikTracker.java
@@ -1,9 +1,9 @@
/*
* OpenAirePiwikTracker.java
*
- * Version: 0.1
- * Date: 2017-04-09
- * Initial version @mire. All rights reserved.
+ * Version: 0.2
+ * Date: 2018-05-20
+* Initial version @mire. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -88,6 +88,9 @@ public class OpenAirePiwikTracker extends AbstractUsageEventListener
// test instances e.g..
private boolean piwikEnabled;
+ // Flag if mising requests are stored in local DB for retry
+ private boolean piwikRetry;
+
// Flag if a proxy is in front of the DSpace instance
private boolean useProxies;
@@ -159,6 +162,8 @@ public class OpenAirePiwikTracker extends AbstractUsageEventListener
piwikIPAnonymizationBytes = ConfigurationManager.getIntProperty("oapiwik","piwik.ipanonymizationbytes");
+ piwikRetry = ConfigurationManager.getBooleanProperty("oapiwik", "piwik.retry");
+
// DSpace variables
useProxies = ConfigurationManager.getBooleanProperty("useProxies");
@@ -394,6 +399,19 @@ public class OpenAirePiwikTracker extends AbstractUsageEventListener
public void failed(final Exception ex)
{
log.error("Error sending usage event to Piwik", ex);
+ try
+ {
+ if (piwikRetry)
+ {
+ OpenAirePiwikTrackerUnreported unreportedReq = new OpenAirePiwikTrackerUnreported();
+ unreportedReq.storeRequest(uri.toString());
+ log.info("Missing request stored to local DB");
+ }
+ }
+ catch (Exception e)
+ {
+ log.error("Error storing unreported request");
+ }
}
@Override
diff --git a/dspace/modules/openaire-piwik/openaire-piwik-api/src/main/java/com/openaire/piwik/tracker/OpenAirePiwikTrackerUnreported.java b/dspace/modules/openaire-piwik/openaire-piwik-api/src/main/java/com/openaire/piwik/tracker/OpenAirePiwikTrackerUnreported.java
new file mode 100755
index 000000000..69ce2cba3
--- /dev/null
+++ b/dspace/modules/openaire-piwik/openaire-piwik-api/src/main/java/com/openaire/piwik/tracker/OpenAirePiwikTrackerUnreported.java
@@ -0,0 +1,86 @@
+/*
+ * OpenAirePiwikTrackerUnreported.java
+ *
+ * Version: 0.1
+ * Date: 2018-05-20
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+ * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package com.openaire.piwik.tracker;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Timestamp;
+import java.util.Calendar;
+
+import org.apache.log4j.Logger;
+
+/**
+ * User: dpie (dpierrakos at gmail.com) Date: Time:
+ */
+public class OpenAirePiwikTrackerUnreported
+{
+
+ private static Logger log = Logger
+ .getLogger(OpenAirePiwikTrackerUnreported.class);
+
+ public OpenAirePiwikTrackerUnreported()
+ {
+ try
+ {
+ // The JDBC driver loading doesn't work for webapps without this.
+ // http://tomcat.apache.org/tomcat-8.0-doc/jndi-datasource-examples-howto.html#DriverManager,_the_service_provider_mechanism_and_memory_leaks
+ Class.forName("org.sqlite.JDBC");
+ }
+ catch (ClassNotFoundException e)
+ {
+ log.error("Could not load sqlite JDBC driver");
+ }
+ }
+
+ public void storeRequest(String requestLost) throws Exception
+ {
+
+ try (Connection conn = DriverManager.getConnection(
+ OpenAirePiwikTrackerUnreportedDB.getMatomoDatabaseUrl());
+ Statement stmt = conn.createStatement();)
+ {
+
+ log.info("Connected to Matomo DB...");
+
+ Timestamp currentTimestamp = new java.sql.Timestamp(
+ Calendar.getInstance().getTime().getTime());
+ String sqlInsertMissingRequestsToDB = "INSERT INTO MissingInformation (time_req,url,isSend) "
+ + "VALUES ('" + currentTimestamp + "','" + requestLost
+ + "',0);";
+
+ stmt.execute(sqlInsertMissingRequestsToDB);
+ }
+ catch (SQLException e)
+ {
+ log.error(e.getMessage());
+ }
+ }
+}
\ No newline at end of file
diff --git a/dspace/modules/openaire-piwik/openaire-piwik-api/src/main/java/com/openaire/piwik/tracker/OpenAirePiwikTrackerUnreportedDB.java b/dspace/modules/openaire-piwik/openaire-piwik-api/src/main/java/com/openaire/piwik/tracker/OpenAirePiwikTrackerUnreportedDB.java
new file mode 100755
index 000000000..3f5c1b454
--- /dev/null
+++ b/dspace/modules/openaire-piwik/openaire-piwik-api/src/main/java/com/openaire/piwik/tracker/OpenAirePiwikTrackerUnreportedDB.java
@@ -0,0 +1,151 @@
+/*
+ * OpenAirePiwikTrackerUnreportedDB.java
+ *
+ * Version: 0.1
+ * Date: 2018-05-20
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+ * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package com.openaire.piwik.tracker;
+
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.HashMap;
+import java.util.Map;
+import org.dspace.core.ConfigurationManager;
+
+/**
+ * User: dpie (dpierrakos at gmail.com) Date: Time:
+**/
+public class OpenAirePiwikTrackerUnreportedDB
+{
+
+ public static String getMatomoDatabaseUrl()
+ {
+ String dspaceDir = ConfigurationManager.getProperty("dspace.dir");
+
+ String matomoDbLocation = ConfigurationManager.getProperty("oapiwik","piwik.matomoDbLocation");
+
+ String connectionStringMatomoDB = "jdbc:sqlite:" + dspaceDir + "/"
+ + matomoDbLocation;
+
+ return connectionStringMatomoDB;
+ }
+
+ public static void main(String[] args) throws Exception
+ {
+
+ String argument = args[0];
+ if (!("-create".equals(argument)) && !("-retry".equals(argument))
+ && !("-delete".equals(argument)))
+ {
+ System.out.println("Usage: dspace.dir/bin/dspace -create or -retry or -delete");
+ return;
+ }
+
+ try (Connection conn = DriverManager
+ .getConnection(getMatomoDatabaseUrl());
+ Statement stmt = conn.createStatement();)
+ {
+
+ System.out.println("Connected to Matomo DB...");
+ if (argument.equals("-create"))
+ {
+
+ String sqlCreate = "CREATE TABLE IF NOT EXISTS MissingInformation (time_req TEXT PRIMARY KEY, url TEXT NOT NULL, isSend INTEGER NOT NULL);";
+ stmt.executeUpdate(sqlCreate);
+ }
+ else if (argument.equals("-retry"))
+ {
+
+ try (ResultSet rs = stmt.executeQuery(
+ "SELECT time_req, url FROM MissingInformation where isSend=0;");)
+ {
+ String urlUnreported = "";
+ String time_req = "";
+
+ HashMap<String, String> timestampURLMap = new HashMap<String, String>();
+
+ if (!rs.isBeforeFirst())
+ System.out.println("No pending missing requests");
+
+ else
+ {
+ while (rs.next())
+ {
+ time_req = rs.getString("time_req");
+ urlUnreported = rs.getString("url");
+ timestampURLMap.put(time_req, urlUnreported);
+ }
+ }
+
+ for (Map.Entry<String, String> entry : timestampURLMap
+ .entrySet())
+ {
+ String timeStamp = entry.getKey();
+ String urlForReport = entry.getValue();
+
+ URL obj = new URL(urlForReport + "&cdt="
+ + URLEncoder.encode(timeStamp,
+ StandardCharsets.UTF_8.name()));
+ HttpURLConnection con = (HttpURLConnection) obj
+ .openConnection();
+ int responseCode = con.getResponseCode();
+ if (responseCode == 200)
+ {
+ String sqlUpdate = "";
+
+ sqlUpdate = "UPDATE MissingInformation set isSend=1 where time_req='"
+ + timeStamp + "';";
+ stmt.executeUpdate(sqlUpdate);
+ System.out.println("Unreported Event sent");
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ System.out.println(e);
+ }
+ }
+ else if (argument.equals("-delete"))
+ {
+
+ String sqlPurge = "DELETE FROM MissingInformation where isSend=1;";
+ int deletedRowCount = stmt.executeUpdate(sqlPurge);
+ System.out.println(deletedRowCount
+ + " missing requests have already been sent to Matomo and deleted from local DB");
+
+ }
+ }
+ catch (Exception e)
+ {
+ System.out.println("Error in creating table " + e.getMessage());
+ }
+ }
+}
\ No newline at end of file