1
|
package eu.dnetlib.usagestats.repositories;
|
2
|
|
3
|
import org.apache.log4j.Logger;
|
4
|
import org.springframework.data.redis.core.HashOperations;
|
5
|
import org.springframework.data.redis.core.RedisTemplate;
|
6
|
import org.springframework.stereotype.Repository;
|
7
|
|
8
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
9
|
|
10
|
import eu.dnetlib.usagestats.portal.MonthlyStats;
|
11
|
import eu.dnetlib.usagestats.portal.RepositoryStats;
|
12
|
import eu.dnetlib.usagestats.portal.TotalStats;
|
13
|
import eu.dnetlib.usagestats.portal.UsageStats;
|
14
|
import eu.dnetlib.usagestats.portal.YearlyStats;
|
15
|
import eu.dnetlib.usagestats.sushilite.domain.ItemIdentifier;
|
16
|
import eu.dnetlib.usagestats.sushilite.domain.ItemPerformance;
|
17
|
import eu.dnetlib.usagestats.sushilite.domain.ReportItem;
|
18
|
|
19
|
import org.apache.commons.dbutils.DbUtils;
|
20
|
|
21
|
import javax.sql.DataSource;
|
22
|
|
23
|
import java.security.MessageDigest;
|
24
|
import java.sql.Connection;
|
25
|
import java.sql.PreparedStatement;
|
26
|
import java.sql.ResultSet;
|
27
|
import java.text.SimpleDateFormat;
|
28
|
import java.util.ArrayList;
|
29
|
import java.util.Calendar;
|
30
|
import java.util.Date;
|
31
|
import java.util.HashMap;
|
32
|
import java.util.List;
|
33
|
import javax.persistence.QueryHint;
|
34
|
|
35
|
@Repository
|
36
|
public class UsageStatsRepository {
|
37
|
|
38
|
private final DataSource usageStatsDB;
|
39
|
|
40
|
private final HashOperations<String, String, String> jedis;
|
41
|
|
42
|
private final Logger log = Logger.getLogger(this.getClass());
|
43
|
|
44
|
public UsageStatsRepository(DataSource usageStatsDB, RedisTemplate<String, String> redisTemplate) {
|
45
|
this.usageStatsDB = usageStatsDB;
|
46
|
this.jedis = redisTemplate.opsForHash();
|
47
|
}
|
48
|
|
49
|
private static String MD5(String string) throws java.security.NoSuchAlgorithmException {
|
50
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
51
|
md.update(string.getBytes());
|
52
|
|
53
|
byte byteData[] = md.digest();
|
54
|
StringBuilder sb = new StringBuilder();
|
55
|
for (byte aByteData : byteData) {
|
56
|
sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1));
|
57
|
}
|
58
|
|
59
|
return sb.toString();
|
60
|
}
|
61
|
|
62
|
private static String toJson(Object o) throws com.fasterxml.jackson.core.JsonProcessingException {
|
63
|
ObjectMapper objectMapper = new ObjectMapper();
|
64
|
return objectMapper.writeValueAsString(o);
|
65
|
}
|
66
|
|
67
|
private static UsageStats fromJson(String string) throws java.io.IOException {
|
68
|
ObjectMapper objectMapper = new ObjectMapper();
|
69
|
return objectMapper.readValue(string, UsageStats.class);
|
70
|
}
|
71
|
|
72
|
/*
|
73
|
private static List<ReportItem> reportItemsFromJson(String string) throws Exception {
|
74
|
ObjectMapper objectMapper = new ObjectMapper();
|
75
|
return objectMapper.readValue(string, objectMapper.getTypeFactory().constructCollectionType(List.class, ReportItem.class));
|
76
|
}
|
77
|
*/
|
78
|
public UsageStats executeUsageStats(String query, List<String> values, String type) {
|
79
|
|
80
|
UsageStats usageStats = new UsageStats();
|
81
|
int total_views = 0;
|
82
|
int total_downloads = 0;
|
83
|
int page_views = 0;
|
84
|
int openaire_downloads = 0;
|
85
|
int openaire_views = 0;
|
86
|
Connection connection = null;
|
87
|
PreparedStatement st = null;
|
88
|
ResultSet rs = null;
|
89
|
try {
|
90
|
connection = usageStatsDB.getConnection();
|
91
|
java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis());
|
92
|
//System.out.println("DB start "+timestamp1);
|
93
|
log.info("Query started..." + timestamp1);
|
94
|
st = connection.prepareStatement(query);
|
95
|
int i = 1;
|
96
|
for (String s : values) {
|
97
|
st.setString(i, s);
|
98
|
i++;
|
99
|
}
|
100
|
|
101
|
String redis_key = MD5(st.toString());
|
102
|
|
103
|
String redis_result = jedis.get(redis_key, "result");
|
104
|
if (redis_result != null) {
|
105
|
return fromJson(redis_result);
|
106
|
}
|
107
|
|
108
|
rs = st.executeQuery();
|
109
|
if (type.equals("result")) {
|
110
|
while (rs.next()) {
|
111
|
if (rs.getString(1).equals("views") && rs.getString(4) != null && !rs.getString(4).equals("") && !rs.getString(4).equals("null")) {
|
112
|
usageStats.addViews(new RepositoryStats(rs.getString(3), rs.getString(2), rs.getString(4), rs.getString(5)));
|
113
|
total_views += Integer.parseInt(rs.getString(4));
|
114
|
openaire_views += Integer.parseInt(rs.getString(5));
|
115
|
} else if (rs.getString(1).equals("downloads") && rs.getString(4) != null && !rs.getString(4).equals("") && !rs.getString(4).equals("null")) {
|
116
|
usageStats.addDownloads(new RepositoryStats(rs.getString(3), rs.getString(2), rs.getString(4), "0"));
|
117
|
total_downloads += Integer.parseInt(rs.getString(4));
|
118
|
openaire_downloads += Integer.parseInt(rs.getString(5));
|
119
|
} else if (rs.getString(1).equals("pageviews") && rs.getString(4) != null && !rs.getString(4).equals("") && !rs.getString(4).equals("null")) {
|
120
|
page_views = Integer.parseInt(rs.getString(4));
|
121
|
}
|
122
|
}
|
123
|
usageStats.setTotal_views(Integer.toString(total_views));
|
124
|
usageStats.setTotal_downloads(Integer.toString(total_downloads));
|
125
|
usageStats.setPageViews(Integer.toString(page_views));
|
126
|
usageStats.setTotal_openaire_views(Integer.toString(openaire_views));
|
127
|
usageStats.setTotal_openaire_downloads(Integer.toString(openaire_downloads));
|
128
|
} else if (type.equals("project") || type.equals("datasource")) {
|
129
|
while (rs.next()) {
|
130
|
if (rs.getString(1).equals("views") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) {
|
131
|
total_views += Integer.parseInt(rs.getString(2));
|
132
|
openaire_views += Integer.parseInt(rs.getString(3));
|
133
|
} else if (rs.getString(1).equals("downloads") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) {
|
134
|
total_downloads += Integer.parseInt(rs.getString(2));
|
135
|
openaire_downloads += Integer.parseInt(rs.getString(3));
|
136
|
} else if (rs.getString(1).equals("pageviews") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) {
|
137
|
page_views = Integer.parseInt(rs.getString(2));
|
138
|
}
|
139
|
/*
|
140
|
else if (rs.getString(1).equals("openaire") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) {
|
141
|
openaire = Integer.parseInt(rs.getString(2));
|
142
|
}
|
143
|
*/
|
144
|
|
145
|
}
|
146
|
usageStats.setTotal_views(Integer.toString(total_views));
|
147
|
usageStats.setTotal_downloads(Integer.toString(total_downloads));
|
148
|
usageStats.setPageViews(Integer.toString(page_views));
|
149
|
usageStats.setTotal_openaire_views(Integer.toString(openaire_views));
|
150
|
usageStats.setTotal_openaire_downloads(Integer.toString(openaire_downloads));
|
151
|
}
|
152
|
|
153
|
jedis.put(redis_key, "persistent", "false");
|
154
|
jedis.put(redis_key, "query", st.toString());
|
155
|
//jedis.put(redis_key, "result", toString(usageStats));
|
156
|
jedis.put(redis_key, "result", toJson(usageStats));
|
157
|
jedis.put(redis_key, "fetchMode", "3");
|
158
|
|
159
|
} catch (Exception e) {
|
160
|
log.error("Cannot execute query2 : ", e);
|
161
|
|
162
|
} finally {
|
163
|
DbUtils.closeQuietly(rs);
|
164
|
DbUtils.closeQuietly(st);
|
165
|
DbUtils.closeQuietly(connection);
|
166
|
java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
|
167
|
//System.out.println("DB end "+timestamp);
|
168
|
log.info("Query end..." + timestamp);
|
169
|
|
170
|
}
|
171
|
return usageStats;
|
172
|
}
|
173
|
|
174
|
public TotalStats executeTotalStats() {
|
175
|
TotalStats totalStats = null;
|
176
|
try {
|
177
|
String redis_result = jedis.get("total_stats", "result");
|
178
|
if (redis_result != null) {
|
179
|
totalStats = fromJsonTotalStats(redis_result);
|
180
|
} else {
|
181
|
return updateTotalStats();
|
182
|
}
|
183
|
} catch (Exception e) {
|
184
|
log.error("Cannot execute totalStats : ", e);
|
185
|
}
|
186
|
return totalStats;
|
187
|
}
|
188
|
|
189
|
public TotalStats updateTotalStats() {
|
190
|
TotalStats totalStats = new TotalStats();
|
191
|
Connection connection = null;
|
192
|
PreparedStatement st = null;
|
193
|
ResultSet rs = null;
|
194
|
HashMap<Integer, List<MonthlyStats>> monthlyStatsMap = new HashMap<>();
|
195
|
|
196
|
try {
|
197
|
connection = usageStatsDB.getConnection();
|
198
|
java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis());
|
199
|
//System.out.println("DB start "+timestamp1);
|
200
|
log.info("Query started..." + timestamp1);
|
201
|
|
202
|
//st = connection.prepareStatement("SELECT count(distinct d.repository_id) AS repository, count(distinct d.result_id) AS items, sum(d.count) AS downloads, sum(v.count) AS views from public.downloads_stats d FULL OUTER JOIN public.views_stats v ON d.source=v.source AND d.repository_id=v.repository_id AND d.result_id=v.result_id AND d.date=v.date;");
|
203
|
st = connection.prepareStatement("SELECT count(distinct repository_id) AS repository, count(distinct result_id) AS items, sum(downloads) AS downloads, sum(views) AS views from usage_stats");
|
204
|
rs = st.executeQuery();
|
205
|
rs.next();
|
206
|
totalStats.setRepositories(rs.getInt(1));
|
207
|
totalStats.setItems(rs.getInt(2));
|
208
|
totalStats.setDownloads(rs.getInt(3));
|
209
|
totalStats.setViews(rs.getInt(4));
|
210
|
rs.close();
|
211
|
st.close();
|
212
|
|
213
|
//st = connection.prepareStatement("select coalesce(d.date,v.date) as month, count(distinct d.repository_id) as repository, count(distinct d.result_id) as items, sum(d.count) as downloads, sum(v.count) as views from public.downloads_stats d FULL OUTER JOIN public.views_stats v ON d.source=v.source AND d.repository_id=v.repository_id AND d.result_id=v.result_id AND d.date=v.date group by month order by month;");
|
214
|
st = connection.prepareStatement("SELECT date, count(distinct repository_id) AS repository, count(distinct result_id) AS items, sum(downloads) AS downloads, sum(views) AS views FROM usage_stats GROUP BY date ORDER BY date;");
|
215
|
rs = st.executeQuery();
|
216
|
while (rs.next()) {
|
217
|
int year = Integer.parseInt(rs.getString(1).substring(0, 4));
|
218
|
int month = Integer.parseInt(rs.getString(1).substring(5));
|
219
|
MonthlyStats monthlyStats = new MonthlyStats();
|
220
|
monthlyStats.setMonth(month);
|
221
|
monthlyStats.setRepositories(rs.getInt(2));
|
222
|
monthlyStats.setItems(rs.getInt(3));
|
223
|
monthlyStats.setDownloads(rs.getInt(4));
|
224
|
monthlyStats.setViews(rs.getInt(5));
|
225
|
|
226
|
if (monthlyStatsMap.get(year) != null) {
|
227
|
monthlyStatsMap.get(year).add(monthlyStats);
|
228
|
} else {
|
229
|
List<MonthlyStats> newList = new ArrayList<>();
|
230
|
newList.add(monthlyStats);
|
231
|
monthlyStatsMap.put(year, newList);
|
232
|
|
233
|
}
|
234
|
}
|
235
|
rs.close();
|
236
|
st.close();
|
237
|
|
238
|
//st = connection.prepareStatement("SELECT COALESCE(SUBSTRING(d.date FROM 1 FOR 4), SUBSTRING(v.date FROM 1 FOR 4)) AS year, COUNT(DISTINCT d.repository_id) AS repository, COUNT(DISTINCT d.result_id) AS items, SUM(d.count) AS downloads, SUM(v.count) AS views FROM public.downloads_stats d FULL OUTER JOIN public.views_stats v ON d.source=v.source AND d.repository_id=v.repository_id AND d.result_id=v.result_id AND d.date=v.date GROUP BY year ORDER BY year;");
|
239
|
st = connection.prepareStatement("SELECT SUBSTRING(date FROM 1 FOR 4) AS year, COUNT(DISTINCT repository_id) AS repository, COUNT(DISTINCT result_id) AS items, SUM(downloads) AS downloads, SUM(views) AS views FROM usage_stats GROUP BY year ORDER BY year;");
|
240
|
rs = st.executeQuery();
|
241
|
List<YearlyStats> yearlyStatsList = new ArrayList<>();
|
242
|
while (rs.next()) {
|
243
|
YearlyStats yearlyStats = new YearlyStats();
|
244
|
yearlyStats.setYear(rs.getInt(1));
|
245
|
yearlyStats.setRepositories(rs.getInt(2));
|
246
|
yearlyStats.setItems(rs.getInt(3));
|
247
|
yearlyStats.setDownloads(rs.getInt(4));
|
248
|
yearlyStats.setViews(rs.getInt(5));
|
249
|
yearlyStats.setMonthlyStats(monthlyStatsMap.get(rs.getInt(1)));
|
250
|
yearlyStatsList.add(yearlyStats);
|
251
|
}
|
252
|
totalStats.setYearlyStats(yearlyStatsList);
|
253
|
jedis.put("total_stats", "result", toJson(totalStats));
|
254
|
jedis.put("total_stats", "persistent", "false");
|
255
|
|
256
|
} catch (Exception e) {
|
257
|
log.error("Cannot execute totalStats : ", e);
|
258
|
|
259
|
} finally {
|
260
|
DbUtils.closeQuietly(rs);
|
261
|
DbUtils.closeQuietly(st);
|
262
|
DbUtils.closeQuietly(connection);
|
263
|
java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
|
264
|
//System.out.println("DB end "+timestamp);
|
265
|
log.info("Query end..." + timestamp);
|
266
|
|
267
|
}
|
268
|
return totalStats;
|
269
|
}
|
270
|
|
271
|
private static TotalStats fromJsonTotalStats(String string) throws java.io.IOException {
|
272
|
ObjectMapper objectMapper = new ObjectMapper();
|
273
|
return objectMapper.readValue(string, TotalStats.class);
|
274
|
}
|
275
|
|
276
|
public String executeRepoId(String repositoryIdentifier, String report) {
|
277
|
PreparedStatement st = null;
|
278
|
Connection connection = null;
|
279
|
ResultSet rs = null;
|
280
|
try {
|
281
|
connection = usageStatsDB.getConnection();
|
282
|
java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis());
|
283
|
//System.out.println("DB start "+timestamp1);
|
284
|
log.info("Query started..." + timestamp1);
|
285
|
|
286
|
String[] split = repositoryIdentifier.split(":");
|
287
|
String openaire_id = "-1";
|
288
|
switch (split[0].toLowerCase()) {
|
289
|
case "openaire":
|
290
|
if (!report.equals("jr1")) {
|
291
|
st = connection.prepareStatement("select id from datasource where id=?");
|
292
|
st.setString(1, repositoryIdentifier.replaceFirst(split[0] + ":", ""));
|
293
|
} else {
|
294
|
st = connection.prepareStatement("select id from datasource where id=? AND (type='Journal' OR type='Journal Aggregator/Publisher')");
|
295
|
st.setString(1, repositoryIdentifier.replaceFirst(split[0] + ":", ""));
|
296
|
}
|
297
|
|
298
|
rs = st.executeQuery();
|
299
|
while (rs.next()) {
|
300
|
openaire_id = rs.getString(1);
|
301
|
}
|
302
|
return openaire_id;
|
303
|
|
304
|
case "opendoar":
|
305
|
if (!report.equals("jr1")) {
|
306
|
st = connection.prepareStatement("select id from datasource_oids where orid=?");
|
307
|
st.setString(1, "opendoar____::" + repositoryIdentifier.replaceFirst(split[0] + ":", ""));
|
308
|
} else {
|
309
|
st = connection.prepareStatement("select distinct d.id from datasource d, datasource_oids di where di.orid=? and d.id=di.id and (type='Journal' OR type='Journal Aggregator/Publisher')");
|
310
|
st.setString(1, "opendoar____::" + repositoryIdentifier.replaceFirst(split[0] + ":", ""));
|
311
|
}
|
312
|
|
313
|
rs = st.executeQuery();
|
314
|
while (rs.next()) {
|
315
|
openaire_id = rs.getString(1);
|
316
|
}
|
317
|
return openaire_id;
|
318
|
case "issn":
|
319
|
st = connection.prepareStatement("select distinct d.id from datasource d, datasource_oids di, datasource_results dr where d.id=dr.id and di.orid like ? and d.id=di.id and (type='Journal' OR type='Journal Aggregator/Publisher')");
|
320
|
st.setString(1, "%" + repositoryIdentifier.replaceFirst(split[0] + ":", "") + "%");
|
321
|
|
322
|
rs = st.executeQuery();
|
323
|
while (rs.next()) {
|
324
|
openaire_id = rs.getString(1);
|
325
|
}
|
326
|
return openaire_id;
|
327
|
default:
|
328
|
return "-1";
|
329
|
}
|
330
|
} catch (Exception e) {
|
331
|
log.error("Repository id failed: ", e);
|
332
|
} finally {
|
333
|
DbUtils.closeQuietly(rs);
|
334
|
DbUtils.closeQuietly(st);
|
335
|
DbUtils.closeQuietly(connection);
|
336
|
java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
|
337
|
//System.out.println("DB end "+timestamp);
|
338
|
log.info("Query end..." + timestamp);
|
339
|
|
340
|
}
|
341
|
return "-1";
|
342
|
}
|
343
|
|
344
|
public void executeItem(List<ReportItem> reportItems, String itemIdentifier, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) {
|
345
|
String[] split = itemIdentifier.split(":");
|
346
|
switch (split[0].toLowerCase()) {
|
347
|
case "oid":
|
348
|
executeOid(reportItems, itemIdentifier.replaceFirst(split[0] + ":", ""), repositoryIdentifier, itemDataType, beginDate, endDate, granularity);
|
349
|
break;
|
350
|
case "doi":
|
351
|
executeDoi(reportItems, itemIdentifier.replaceFirst(split[0] + ":", ""), repositoryIdentifier, itemDataType, beginDate, endDate, granularity);
|
352
|
break;
|
353
|
case "openaire":
|
354
|
executeOpenaire(reportItems, itemIdentifier.replaceFirst(split[0] + ":", ""), repositoryIdentifier, itemDataType, beginDate, endDate, granularity);
|
355
|
break;
|
356
|
default:
|
357
|
}
|
358
|
}
|
359
|
|
360
|
private void executeOid(List<ReportItem> reportItems, String oid, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) {
|
361
|
Connection connection = null;
|
362
|
PreparedStatement st = null;
|
363
|
ResultSet rs = null;
|
364
|
try {
|
365
|
connection = usageStatsDB.getConnection();
|
366
|
java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis());
|
367
|
//System.out.println("DB start "+timestamp1);
|
368
|
log.info("Query started..." + timestamp1);
|
369
|
|
370
|
//st = connection.prepareStatement("SELECT DISTINCT roid.id FROM public.result_oids roid, public.downloads_stats s WHERE s.result_id=roid.id AND roid.orid=? UNION SELECT DISTINCT roid.id FROM public.result_oids roid, public.views_stats s WHERE s.result_id=roid.id AND roid.orid=?");
|
371
|
st = connection.prepareStatement("SELECT DISTINCT us.original_result_id FROM result_oids roid, usage_stats us WHERE us.result_id=roid.id AND roid.orid=?");
|
372
|
st.setString(1, oid);
|
373
|
//st.setString(2, oid);
|
374
|
|
375
|
rs = st.executeQuery();
|
376
|
|
377
|
while (rs.next()) {
|
378
|
executeOpenaire(reportItems, rs.getString(1), repositoryIdentifier, itemDataType, beginDate, endDate, granularity);
|
379
|
}
|
380
|
connection.close();
|
381
|
} catch (Exception e) {
|
382
|
log.error("Oid to OpenAIRE id failed: ", e);
|
383
|
} finally {
|
384
|
DbUtils.closeQuietly(rs);
|
385
|
DbUtils.closeQuietly(st);
|
386
|
DbUtils.closeQuietly(connection);
|
387
|
java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
|
388
|
//System.out.println("DB end "+timestamp);
|
389
|
log.info("Query end..." + timestamp);
|
390
|
|
391
|
}
|
392
|
}
|
393
|
|
394
|
private void executeDoi(List<ReportItem> reportItems, String doi, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) {
|
395
|
Connection connection = null;
|
396
|
PreparedStatement st = null;
|
397
|
ResultSet rs = null;
|
398
|
try {
|
399
|
connection = usageStatsDB.getConnection();
|
400
|
java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis());
|
401
|
//System.out.println("DB start "+timestamp1);
|
402
|
log.info("Query started..." + timestamp1);
|
403
|
|
404
|
//st = connection.prepareStatement("SELECT DISTINCT poid.id FROM public.result_pids poid, public.downloads_stats s WHERE s.result_id=poid.id AND poid.type='doi' AND poid.pid=? UNION SELECT DISTINCT poid.id FROM public.result_pids poid, public.views_stats s WHERE s.result_id=poid.id AND poid.type='doi' AND poid.pid=?");
|
405
|
st = connection.prepareStatement("SELECT DISTINCT us.original_result_id FROM result_pids poid, usage_stats us WHERE us.result_id=poid.id AND poid.type='doi' AND poid.pid=?");
|
406
|
st.setString(1, doi);
|
407
|
//st.setString(2, doi);
|
408
|
|
409
|
rs = st.executeQuery();
|
410
|
|
411
|
while (rs.next()) {
|
412
|
executeOpenaire(reportItems, rs.getString(1), repositoryIdentifier, itemDataType, beginDate, endDate, granularity);
|
413
|
}
|
414
|
} catch (Exception e) {
|
415
|
log.error("Doi to OpenAIRE id failed: ", e);
|
416
|
} finally {
|
417
|
DbUtils.closeQuietly(rs);
|
418
|
DbUtils.closeQuietly(st);
|
419
|
DbUtils.closeQuietly(connection);
|
420
|
java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
|
421
|
//System.out.println("DB end "+timestamp);
|
422
|
log.info("Query end..." + timestamp);
|
423
|
|
424
|
}
|
425
|
}
|
426
|
|
427
|
private void executeOpenaire(List<ReportItem> reportItems, String openaire, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) {
|
428
|
SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
429
|
SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM");
|
430
|
String beginDateStr = postgresFormat.format(beginDate);
|
431
|
String endDateStr = postgresFormat.format(endDate);
|
432
|
|
433
|
Connection connection = null;
|
434
|
PreparedStatement st = null;
|
435
|
ResultSet rs = null;
|
436
|
|
437
|
/*
|
438
|
Calendar startCalendar = Calendar.getInstance();
|
439
|
startCalendar.setTime(beginDate);
|
440
|
Calendar endCalendar = Calendar.getInstance();
|
441
|
endCalendar.setTime(endDate);
|
442
|
int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
|
443
|
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
|
444
|
*/
|
445
|
try {
|
446
|
connection = usageStatsDB.getConnection();
|
447
|
java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis());
|
448
|
//System.out.println("DB start "+timestamp1);
|
449
|
log.info("Query started..." + timestamp1);
|
450
|
|
451
|
if (repositoryIdentifier.equals("")) {
|
452
|
if (itemDataType.equals("")) {
|
453
|
//st = connection.prepareStatement("SELECT res.repository_id, r.title, r.publisher, r.source, rc.type, pids.pid, d.name, res.ddate, oids.orid, res.downloads, res.views FROM (SELECT coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.result_id, vs.result_id) AS result_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.downloads_stats s WHERE s.date>=? AND s.date<=? AND s.result_id=? GROUP BY s.repository_id, s.result_id, s.date) AS ds FULL OUTER JOIN (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.views_stats s WHERE s.date>=? AND s.date<=? AND s.result_id=? GROUP BY s.repository_id, s.result_id, s.date) AS vs ON ds.result_id=vs.result_id AND ds.date=vs.date) AS res JOIN public.result r ON res.result_id=r.id JOIN public.datasource d ON d.id=res.repository_id JOIN public.result_classifications rc ON rc.id=r.id LEFT JOIN (SELECT pids.id, string_agg(pids.pid, '#!#') AS pid FROM public.result_pids pids WHERE pids.id=? AND type='doi' GROUP BY pids.id) AS pids ON pids.id=r.id LEFT JOIN (SELECT oids.id, string_agg(oids.orid, '#!#') AS orid FROM public.result_oids oids WHERE oids.id=? GROUP BY oids.id) AS oids ON oids.id=r.id ORDER BY res.repository_id, res.ddate;");
|
454
|
//st = connection.prepareStatement("SELECT res.repository_id, r.title, r.publisher, r.source, rc.type, pids.pid, d.name, res.date, oids.orid, res.downloads, res.views FROM (SELECT us.repository_id, us.result_id, us.date, us.downloads, us.views FROM usage_stats us WHERE us.date>=? AND us.date<=? AND us.result_id=?) AS res JOIN result r ON res.result_id=r.id JOIN datasource d ON d.id=res.repository_id JOIN result_classifications rc ON rc.id=r.id LEFT JOIN (SELECT pids.id, string_agg(pids.pid, '#!#') AS pid FROM result_pids pids WHERE pids.id=? AND type='doi' GROUP BY pids.id) AS pids ON pids.id=r.id LEFT JOIN (SELECT oids.id, string_agg(oids.orid, '#!#') AS orid FROM result_oids oids WHERE oids.id=? GROUP BY oids.id) AS oids ON oids.id=r.id ORDER BY res.repository_id, res.date;");
|
455
|
st = connection.prepareStatement("SELECT us.repository_id, r.title, r.publisher, r.source, rc.type, string_agg(distinct pids.pid, '#!#') AS pid, d.name, us.date, string_agg(distinct oids.orid, '#!#') AS orid, us.downloads, us.views FROM usage_stats us JOIN result r ON us.result_id=r.id JOIN datasource d ON d.id=us.repository_id JOIN result_classifications rc ON rc.id=r.id JOIN result_pids pids ON pids.id=r.id AND pids.type='doi' JOIN result_oids oids ON oids.id=r.id WHERE us.date>=? AND us.date<=? AND us.original_result_id=? GROUP BY us.repository_id, r.title, r.publisher, r.source, rc.type, d.name, us.date, us.downloads, us.views ORDER BY us.repository_id, us.date;");
|
456
|
st.setString(1, beginDateStr);
|
457
|
st.setString(2, endDateStr);
|
458
|
st.setString(3, openaire);
|
459
|
//st.setString(4, beginDateStr);
|
460
|
//st.setString(5, endDateStr);
|
461
|
//st.setString(6, openaire);
|
462
|
//st.setString(4, openaire);
|
463
|
//st.setString(5, openaire);
|
464
|
} else {
|
465
|
//st = connection.prepareStatement("SELECT res.repository_id, r.title, r.publisher, r.source, rc.type, pids.pid, d.name, res.ddate, oids.orid, res.downloads, res.views FROM (SELECT coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.result_id, vs.result_id) AS result_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.downloads_stats s WHERE s.date>=? AND s.date<=? AND s.result_id=? GROUP BY s.repository_id, s.result_id, s.date) AS ds FULL OUTER JOIN (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.views_stats s WHERE s.date>=? AND s.date<=? AND s.result_id=? GROUP BY s.repository_id, s.result_id, s.date) AS vs ON ds.result_id=vs.result_id AND ds.date=vs.date) AS res JOIN public.result r ON res.result_id=r.id JOIN public.datasource d ON d.id=res.repository_id JOIN public.result_classifications rc ON rc.id=r.id AND rc.type=? LEFT JOIN (SELECT pids.id, string_agg(pids.pid, '#!#') AS pid FROM public.result_pids pids WHERE pids.id=? AND type='doi' GROUP BY pids.id) AS pids ON pids.id=r.id LEFT JOIN (SELECT oids.id, string_agg(oids.orid, '#!#') AS orid FROM public.result_oids oids WHERE oids.id=? GROUP BY oids.id) AS oids ON oids.id=r.id ORDER BY res.repository_id, res.ddate;");
|
466
|
//st = connection.prepareStatement("SELECT res.repository_id, r.title, r.publisher, r.source, rc.type, pids.pid, d.name, res.date, oids.orid, res.downloads, res.views FROM (SELECT us.repository_id, us.result_id, us.date, us.downloads, us.views FROM usage_stats us WHERE us.date>=? AND us.date<=? AND us.result_id=?) AS res JOIN result r ON res.result_id=r.id JOIN datasource d ON d.id=res.repository_id JOIN result_classifications rc ON rc.id=r.id AND rc.type=? LEFT JOIN (SELECT pids.id, string_agg(pids.pid, '#!#') AS pid FROM result_pids pids WHERE pids.id=? AND type='doi' GROUP BY pids.id) AS pids ON pids.id=r.id LEFT JOIN (SELECT oids.id, string_agg(oids.orid, '#!#') AS orid FROM result_oids oids WHERE oids.id=? GROUP BY oids.id) AS oids ON oids.id=r.id ORDER BY res.repository_id, res.date;");
|
467
|
st = connection.prepareStatement("SELECT us.repository_id, r.title, r.publisher, r.source, rc.type, string_agg(distinct pids.pid, '#!#') AS pid, d.name, us.date, string_agg(distinct oids.orid, '#!#') AS orid, us.downloads, us.views FROM usage_stats us JOIN result r ON us.result_id=r.id JOIN datasource d ON d.id=us.repository_id JOIN result_classifications rc ON rc.id=r.id JOIN result_pids pids ON pids.id=r.id AND pids.type='doi' JOIN result_oids oids ON oids.id=r.id WHERE us.date>=? AND us.date<=? AND us.original_result_id=? AND rc.type=? GROUP BY us.repository_id, r.title, r.publisher, r.source, rc.type, d.name, us.date, us.downloads, us.views ORDER BY us.repository_id, us.date;");
|
468
|
st.setString(1, beginDateStr);
|
469
|
st.setString(2, endDateStr);
|
470
|
st.setString(3, openaire);
|
471
|
//st.setString(4, beginDateStr);
|
472
|
//st.setString(5, endDateStr);
|
473
|
//st.setString(6, openaire);
|
474
|
st.setString(4, itemDataType);
|
475
|
//st.setString(5, openaire);
|
476
|
//st.setString(6, openaire);
|
477
|
}
|
478
|
} else {
|
479
|
if (itemDataType.equals("")) {
|
480
|
//st = connection.prepareStatement("SELECT res.repository_id, r.title, r.publisher, r.source, rc.type, pids.pid, d.name, res.ddate, oids.orid, res.downloads, res.views FROM (SELECT coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.result_id, vs.result_id) AS result_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.downloads_stats s WHERE s.date>=? AND s.date<=? AND s.result_id=? AND s.repository_id=? GROUP BY s.repository_id, s.result_id, s.date) AS ds FULL OUTER JOIN (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.views_stats s WHERE s.date>=? AND s.date<=? AND s.result_id=? AND s.repository_id=? GROUP BY s.repository_id, s.result_id, s.date) AS vs ON ds.result_id=vs.result_id AND ds.date=vs.date) AS res JOIN public.result r ON res.result_id=r.id JOIN public.datasource d ON d.id=res.repository_id JOIN public.result_classifications rc ON rc.id=r.id LEFT JOIN (SELECT pids.id, string_agg(pids.pid, '#!#') AS pid FROM public.result_pids pids WHERE pids.id=? AND type='doi' GROUP BY pids.id) AS pids ON pids.id=r.id LEFT JOIN (SELECT oids.id, string_agg(oids.orid, '#!#') AS orid FROM public.result_oids oids WHERE oids.id=? GROUP BY oids.id) AS oids ON oids.id=r.id ORDER BY res.repository_id, res.ddate;");
|
481
|
st = connection.prepareStatement("SELECT us.repository_id, r.title, r.publisher, r.source, rc.type, string_agg(distinct pids.pid, '#!#') AS pid, d.name, us.date, string_agg(distinct oids.orid, '#!#') AS orid, us.downloads, us.views FROM usage_stats us JOIN result r ON us.result_id=r.id JOIN datasource d ON d.id=us.repository_id JOIN result_classifications rc ON rc.id=r.id JOIN result_pids pids ON pids.id=r.id AND pids.type='doi' JOIN result_oids oids ON oids.id=r.id WHERE us.date>=? AND us.date<=? AND us.original_result_id=? AND us.repository_id=? GROUP BY us.repository_id, r.title, r.publisher, r.source, rc.type, d.name, us.date, us.downloads, us.views ORDER BY us.repository_id, us.date;");
|
482
|
st.setString(1, beginDateStr);
|
483
|
st.setString(2, endDateStr);
|
484
|
st.setString(3, openaire);
|
485
|
st.setString(4, repositoryIdentifier);
|
486
|
//st.setString(5, beginDateStr);
|
487
|
//st.setString(6, endDateStr);
|
488
|
//st.setString(7, openaire);
|
489
|
//st.setString(8, repositoryIdentifier);
|
490
|
//st.setString(5, openaire);
|
491
|
//st.setString(6, openaire);
|
492
|
} else {
|
493
|
//st = connection.prepareStatement("SELECT res.repository_id, r.title, r.publisher, r.source, rc.type, pids.pid, d.name, res.ddate, oids.orid, res.downloads, res.views FROM (SELECT coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.result_id, vs.result_id) AS result_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.downloads_stats s WHERE s.date>=? AND s.date<=? AND s.result_id=? AND s.repository_id=? GROUP BY s.repository_id, s.result_id, s.date) AS ds FULL OUTER JOIN (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.views_stats s WHERE s.date>=? AND s.date<=? AND s.result_id=? AND s.repository_id=? GROUP BY s.repository_id, s.result_id, s.date) AS vs ON ds.result_id=vs.result_id AND ds.date=vs.date) AS res JOIN public.result r ON res.result_id=r.id JOIN public.datasource d ON d.id=res.repository_id JOIN public.result_classifications rc ON rc.id=r.id AND rc.type=? LEFT JOIN (SELECT pids.id, string_agg(pids.pid, '#!#') AS pid FROM public.result_pids pids WHERE pids.id=? AND type='doi' GROUP BY pids.id) AS pids ON pids.id=r.id LEFT JOIN (SELECT oids.id, string_agg(oids.orid, '#!#') AS orid FROM public.result_oids oids WHERE oids.id=? GROUP BY oids.id) AS oids ON oids.id=r.id ORDER BY res.repository_id, res.ddate;");
|
494
|
st = connection.prepareStatement("SELECT us.repository_id, r.title, r.publisher, r.source, rc.type, string_agg(distinct pids.pid, '#!#') AS pid, d.name, us.date, string_agg(distinct oids.orid, '#!#') AS orid, us.downloads, us.views FROM usage_stats us JOIN result r ON us.result_id=r.id JOIN datasource d ON d.id=us.repository_id JOIN result_classifications rc ON rc.id=r.id JOIN result_pids pids ON pids.id=r.id AND pids.type='doi' JOIN result_oids oids ON oids.id=r.id WHERE us.date>=? AND us.date<=? AND us.original_result_id=? AND us.repository_id=? AND rc.type=? GROUP BY us.repository_id, r.title, r.publisher, r.source, rc.type, d.name, us.date, us.downloads, us.views ORDER BY us.repository_id, us.date;");
|
495
|
st.setString(1, beginDateStr);
|
496
|
st.setString(2, endDateStr);
|
497
|
st.setString(3, openaire);
|
498
|
st.setString(4, repositoryIdentifier);
|
499
|
//st.setString(5, beginDateStr);
|
500
|
//st.setString(6, endDateStr);
|
501
|
//st.setString(7, openaire);
|
502
|
//st.setString(8, repositoryIdentifier);
|
503
|
st.setString(5, itemDataType);
|
504
|
//st.setString(6, openaire);
|
505
|
//st.setString(7, openaire);
|
506
|
}
|
507
|
}
|
508
|
|
509
|
rs = st.executeQuery();
|
510
|
String repository = "";
|
511
|
String lastDate = "";
|
512
|
ReportItem reportItem = null;
|
513
|
int ft_total = 0;
|
514
|
int abstr = 0;
|
515
|
|
516
|
if (granularity.equalsIgnoreCase("totals")) {
|
517
|
while (rs.next()) {
|
518
|
if (!rs.getString(1).equals(repository)) {
|
519
|
if (reportItem != null) {
|
520
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), Integer.toString(ft_total), Integer.toString(abstr)));
|
521
|
reportItems.add(reportItem);
|
522
|
}
|
523
|
repository = rs.getString(1);
|
524
|
reportItem = new ReportItem(rs.getString(3), rs.getString(7), rs.getString(5), rs.getString(2));
|
525
|
reportItem.addIdentifier(new ItemIdentifier("OpenAIRE", openaire));
|
526
|
reportItem.addIdentifier(new ItemIdentifier("URLs", rs.getString(4)));
|
527
|
if (rs.getString(9) != null && !rs.getString(9).equals("")) {
|
528
|
if (rs.getString(9).contains("#!#")) {
|
529
|
reportItem.addIdentifier(new ItemIdentifier("OAI", rs.getString(9).substring(0, rs.getString(9).indexOf("#!#"))));
|
530
|
} else {
|
531
|
reportItem.addIdentifier(new ItemIdentifier("OAI", rs.getString(9)));
|
532
|
}
|
533
|
}
|
534
|
if (rs.getString(6) != null && !rs.getString(6).equals("")) {
|
535
|
if (rs.getString(6).contains("#!#")) {
|
536
|
reportItem.addIdentifier(new ItemIdentifier("DOI", rs.getString(6).substring(0, rs.getString(6).indexOf("#!#"))));
|
537
|
} else {
|
538
|
reportItem.addIdentifier(new ItemIdentifier("DOI", rs.getString(6)));
|
539
|
}
|
540
|
}
|
541
|
ft_total = 0;
|
542
|
abstr = 0;
|
543
|
}
|
544
|
ft_total += rs.getInt(10);
|
545
|
abstr += rs.getInt(11);
|
546
|
}
|
547
|
if (reportItem != null) {
|
548
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), Integer.toString(ft_total), Integer.toString(abstr)));
|
549
|
reportItems.add(reportItem);
|
550
|
}
|
551
|
} else if (granularity.equalsIgnoreCase("monthly")) {
|
552
|
Calendar endCal = Calendar.getInstance();
|
553
|
endCal.setTime(postgresFormat.parse(endDateStr));
|
554
|
endCal.add(Calendar.MONTH, 1);
|
555
|
Date endDateForZeros = endCal.getTime();
|
556
|
while (rs.next()) {
|
557
|
if (!rs.getString(1).equals(repository)) {
|
558
|
if (reportItem != null) {
|
559
|
fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem);
|
560
|
reportItems.add(reportItem);
|
561
|
}
|
562
|
repository = rs.getString(1);
|
563
|
lastDate = beginDateStr;
|
564
|
reportItem = new ReportItem(rs.getString(3), rs.getString(7), rs.getString(5), rs.getString(2));
|
565
|
reportItem.addIdentifier(new ItemIdentifier("OpenAIRE", openaire));
|
566
|
reportItem.addIdentifier(new ItemIdentifier("URLs", rs.getString(4)));
|
567
|
if (rs.getString(9) != null && !rs.getString(9).equals("")) {
|
568
|
if (rs.getString(9).contains("#!#")) {
|
569
|
reportItem.addIdentifier(new ItemIdentifier("OAI", rs.getString(9).substring(0, rs.getString(9).indexOf("#!#"))));
|
570
|
} else {
|
571
|
reportItem.addIdentifier(new ItemIdentifier("OAI", rs.getString(9)));
|
572
|
}
|
573
|
}
|
574
|
if (rs.getString(6) != null && !rs.getString(6).equals("")) {
|
575
|
if (rs.getString(6).contains("#!#")) {
|
576
|
reportItem.addIdentifier(new ItemIdentifier("DOI", rs.getString(6).substring(0, rs.getString(6).indexOf("#!#"))));
|
577
|
} else {
|
578
|
reportItem.addIdentifier(new ItemIdentifier("DOI", rs.getString(6)));
|
579
|
}
|
580
|
}
|
581
|
}
|
582
|
fillWithZeros(postgresFormat.parse(lastDate), postgresFormat.parse(rs.getString(8)), reportItem);
|
583
|
Calendar endC = Calendar.getInstance();
|
584
|
endC.setTime(postgresFormat.parse(rs.getString(8)));
|
585
|
endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE));
|
586
|
if (reportItem != null) {
|
587
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(postgresFormat.parse(rs.getString(8))), report_dateFormat.format(endC.getTime()), rs.getString(10), rs.getString(11)));
|
588
|
}
|
589
|
endC.setTime(postgresFormat.parse(rs.getString(8)));
|
590
|
endC.add(Calendar.MONTH, 1);
|
591
|
lastDate = postgresFormat.format(endC.getTime());
|
592
|
}
|
593
|
if (reportItem != null) {
|
594
|
fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem);
|
595
|
reportItems.add(reportItem);
|
596
|
}
|
597
|
}
|
598
|
} catch (Exception e) {
|
599
|
log.error("Single Item Report failed: ", e);
|
600
|
} finally {
|
601
|
DbUtils.closeQuietly(rs);
|
602
|
DbUtils.closeQuietly(st);
|
603
|
DbUtils.closeQuietly(connection);
|
604
|
java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
|
605
|
//System.out.println("DB end "+timestamp);
|
606
|
log.info("Query end..." + timestamp);
|
607
|
|
608
|
}
|
609
|
}
|
610
|
|
611
|
public void executeRepo(List<ReportItem> reportItems, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) {
|
612
|
SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
613
|
SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM");
|
614
|
String beginDateStr = postgresFormat.format(beginDate);
|
615
|
String endDateStr = postgresFormat.format(endDate);
|
616
|
|
617
|
Connection connection = null;
|
618
|
PreparedStatement st = null;
|
619
|
ResultSet rs = null;
|
620
|
|
621
|
try {
|
622
|
connection = usageStatsDB.getConnection();
|
623
|
java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis());
|
624
|
//System.out.println("DB start "+timestamp1);
|
625
|
log.info("Query started..." + timestamp1);
|
626
|
|
627
|
if (repositoryIdentifier.equals("")) {
|
628
|
if (itemDataType.equals("")) {
|
629
|
//st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.ddate, res.downloads, res.views FROM (SELECT coalesce(ds.source, vs.source), coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.downloads_stats s WHERE s.date>=? AND s.date<=? GROUP BY s.source, s.repository_id, s.date) AS ds FULL OUTER JOIN (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.views_stats s WHERE s.date>=? AND s.date<=? GROUP BY s.source, s.repository_id, s.date) AS vs ON ds.source=vs.source AND ds.repository_id=vs.repository_id AND ds.date=vs.date) AS res JOIN public.datasource d ON d.id=res.repository_id JOIN public.datasource_oids dois ON d.id=dois.id WHERE dois.orid LIKE 'opendoar%' ORDER BY d.id, d.name, res.ddate ASC;");
|
630
|
st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.date, res.downloads, res.views FROM (SELECT us.source, us.repository_id, us.date, sum(us.downloads) AS downloads, sum(us.views) AS views FROM usage_stats us WHERE us.date>=? AND us.date<=? GROUP BY us.source, us.repository_id, us.date) AS res JOIN datasource d ON d.id=res.repository_id JOIN datasource_oids dois ON d.id=dois.id WHERE dois.orid LIKE 'opendoar%' ORDER BY d.id, d.name, res.date ASC;");
|
631
|
st.setString(1, beginDateStr);
|
632
|
st.setString(2, endDateStr);
|
633
|
//st.setString(3, beginDateStr);
|
634
|
//st.setString(4, endDateStr);
|
635
|
} else {
|
636
|
//st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.ddate, res.downloads, res.views FROM (SELECT coalesce(ds.source, vs.source), coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.downloads_stats s, result_classifications rc WHERE rc.id=s.result_id AND s.date>=? AND s.date<=? AND rc.type=? GROUP BY s.source, s.repository_id, s.date) AS ds FULL OUTER JOIN (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.views_stats s, public.result_classifications rc WHERE rc.id=s.result_id AND s.date>=? AND s.date<=? AND rc.type=? GROUP BY s.source, s.repository_id, s.date) AS vs ON ds.source=vs.source AND ds.repository_id=vs.repository_id AND ds.date=vs.date) AS res JOIN public.datasource d ON d.id=res.repository_id JOIN public.datasource_oids dois ON d.id=dois.id WHERE dois.orid LIKE 'opendoar%' ORDER BY d.id, d.name, res.ddate ASC;");
|
637
|
st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.date, res.downloads, res.views FROM (SELECT us.source, us.repository_id, us.date, sum(us.downloads) AS downloads, sum(us.views) AS views FROM usage_stats us, result_classifications rc WHERE rc.id=us.result_id AND us.date>=? AND us.date<=? AND rc.type=? GROUP BY us.source, us.repository_id, us.date) AS res JOIN datasource d ON d.id=res.repository_id JOIN datasource_oids dois ON d.id=dois.id WHERE dois.orid LIKE 'opendoar%' ORDER BY d.id, d.name, res.date ASC;");
|
638
|
st.setString(1, beginDateStr);
|
639
|
st.setString(2, endDateStr);
|
640
|
st.setString(3, itemDataType);
|
641
|
//st.setString(4, beginDateStr);
|
642
|
//st.setString(5, endDateStr);
|
643
|
//st.setString(6, itemDataType);
|
644
|
}
|
645
|
} else {
|
646
|
if (itemDataType.equals("")) {
|
647
|
//st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.ddate, res.downloads, res.views FROM (SELECT coalesce(ds.source, vs.source), coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.downloads_stats s WHERE s.date>=? AND s.date<=? AND s.repository_id=? GROUP BY s.source, s.repository_id, s.date) AS ds FULL OUTER JOIN (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.views_stats s WHERE s.date>=? AND s.date<=? AND s.repository_id=? GROUP BY s.source, s.repository_id, s.date) AS vs ON ds.source=vs.source AND ds.repository_id=vs.repository_id AND ds.date=vs.date) AS res JOIN public.datasource d ON d.id=res.repository_id JOIN public.datasource_oids dois ON d.id=dois.id WHERE dois.orid LIKE 'opendoar%' ORDER BY d.id, d.name, res.ddate ASC;");
|
648
|
st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.date, res.downloads, res.views FROM (SELECT us.source, us.repository_id, us.date, sum(us.downloads) AS downloads, sum(us.views) AS views FROM usage_stats us WHERE us.date>=? AND us.date<=? AND us.repository_id=? GROUP BY us.source, us.repository_id, us.date) AS res JOIN datasource d ON d.id=res.repository_id JOIN datasource_oids dois ON d.id=dois.id WHERE dois.orid LIKE 'opendoar%' ORDER BY d.id, d.name, res.date ASC;");
|
649
|
st.setString(1, beginDateStr);
|
650
|
st.setString(2, endDateStr);
|
651
|
st.setString(3, repositoryIdentifier);
|
652
|
//st.setString(4, beginDateStr);
|
653
|
//st.setString(5, endDateStr);
|
654
|
//st.setString(6, repositoryIdentifier);
|
655
|
} else {
|
656
|
//st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.ddate, res.downloads, res.views FROM (SELECT coalesce(ds.source, vs.source), coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.downloads_stats s, public.result_classifications rc WHERE rc.id=s.result_id AND s.date>=? AND s.date<=? AND rc.type=? AND s.repository_id=? GROUP BY s.source, s.repository_id, s.date) AS ds FULL OUTER JOIN (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.views_stats s, public.result_classifications rc WHERE rc.id=s.result_id AND s.date>=? AND s.date<=? AND rc.type=? AND s.repository_id=? GROUP BY s.source, s.repository_id, s.date) AS vs ON ds.source=vs.source AND ds.repository_id=vs.repository_id AND ds.date=vs.date) AS res JOIN public.datasource d ON d.id=res.repository_id JOIN public.datasource_oids dois ON d.id=dois.id WHERE dois.orid LIKE 'opendoar%' ORDER BY d.id, d.name, res.ddate ASC;");
|
657
|
st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.date, res.downloads, res.views FROM (SELECT us.source, us.repository_id, us.date, sum(us.downloads) AS downloads, sum(us.views) AS views FROM usage_stats us, result_classifications rc WHERE rc.id=us.result_id AND us.date>=? AND us.date<=? AND rc.type=? AND us.repository_id=? GROUP BY us.source, us.repository_id, us.date) AS res JOIN datasource d ON d.id=res.repository_id JOIN datasource_oids dois ON d.id=dois.id WHERE dois.orid LIKE 'opendoar%' ORDER BY d.id, d.name, res.date ASC;");
|
658
|
st.setString(1, beginDateStr);
|
659
|
st.setString(2, endDateStr);
|
660
|
st.setString(3, itemDataType);
|
661
|
st.setString(4, repositoryIdentifier);
|
662
|
//st.setString(5, beginDateStr);
|
663
|
//st.setString(6, endDateStr);
|
664
|
//st.setString(7, itemDataType);
|
665
|
//st.setString(8, repositoryIdentifier);
|
666
|
}
|
667
|
}
|
668
|
//log.error("RR STATEMENT: " + st);
|
669
|
|
670
|
/*
|
671
|
String redis_key = MD5(st.toString());
|
672
|
|
673
|
if (jedis.hasKey(redis_key, "result")) {
|
674
|
reportItems.addAll(reportItemsFromJson((String) jedis.get(redis_key, "result")));
|
675
|
st.close();
|
676
|
connection.close();
|
677
|
return;
|
678
|
}
|
679
|
*/
|
680
|
rs = st.executeQuery();
|
681
|
String repository = "";
|
682
|
String lastDate = "";
|
683
|
ReportItem reportItem = null;
|
684
|
|
685
|
/*
|
686
|
Calendar startCalendar = Calendar.getInstance();
|
687
|
startCalendar.setTime(beginDate);
|
688
|
Calendar endCalendar = Calendar.getInstance();
|
689
|
endCalendar.setTime(endDate);
|
690
|
*/
|
691
|
int ft_total = 0;
|
692
|
int abstr = 0;
|
693
|
if (granularity.equalsIgnoreCase("totals")) {
|
694
|
while (rs.next()) {
|
695
|
if (!rs.getString(1).equals(repository)) {
|
696
|
if (reportItem != null) {
|
697
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), Integer.toString(ft_total), Integer.toString(abstr)));
|
698
|
reportItems.add(reportItem);
|
699
|
}
|
700
|
repository = rs.getString(1);
|
701
|
reportItem = new ReportItem(null, rs.getString(2), "Platform", null);
|
702
|
reportItem.addIdentifier(new ItemIdentifier("OpenAIRE", rs.getString(1)));
|
703
|
reportItem.addIdentifier(new ItemIdentifier("OpenDOAR", rs.getString(4).substring(rs.getString(4).lastIndexOf(":") + 1)));
|
704
|
reportItem.addIdentifier(new ItemIdentifier("URL", rs.getString(3)));
|
705
|
ft_total = 0;
|
706
|
abstr = 0;
|
707
|
}
|
708
|
ft_total += rs.getInt(6);
|
709
|
abstr += rs.getInt(7);
|
710
|
}
|
711
|
if (reportItem != null) {
|
712
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), Integer.toString(ft_total), Integer.toString(abstr)));
|
713
|
reportItems.add(reportItem);
|
714
|
}
|
715
|
} else if (granularity.equalsIgnoreCase("monthly")) {
|
716
|
Calendar endCal = Calendar.getInstance();
|
717
|
endCal.setTime(postgresFormat.parse(endDateStr));
|
718
|
endCal.add(Calendar.MONTH, 1);
|
719
|
Date endDateForZeros = endCal.getTime();
|
720
|
while (rs.next()) {
|
721
|
if (!rs.getString(1).equals(repository)) {
|
722
|
if (reportItem != null) {
|
723
|
fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem);
|
724
|
reportItems.add(reportItem);
|
725
|
}
|
726
|
repository = rs.getString(1);
|
727
|
lastDate = beginDateStr;
|
728
|
reportItem = new ReportItem(null, rs.getString(2), "Platform", null);
|
729
|
reportItem.addIdentifier(new ItemIdentifier("OpenAIRE", rs.getString(1)));
|
730
|
reportItem.addIdentifier(new ItemIdentifier("OpenDOAR", rs.getString(4).substring(rs.getString(4).lastIndexOf(":") + 1)));
|
731
|
reportItem.addIdentifier(new ItemIdentifier("URL", rs.getString(3)));
|
732
|
}
|
733
|
fillWithZeros(postgresFormat.parse(lastDate), postgresFormat.parse(rs.getString(5)), reportItem);
|
734
|
Calendar endC = Calendar.getInstance();
|
735
|
endC.setTime(postgresFormat.parse(rs.getString(5)));
|
736
|
endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE));
|
737
|
if (reportItem != null) {
|
738
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(postgresFormat.parse(rs.getString(5))), report_dateFormat.format(endC.getTime()), rs.getString(6), rs.getString(7)));
|
739
|
}
|
740
|
endC.setTime(postgresFormat.parse(rs.getString(5)));
|
741
|
endC.add(Calendar.MONTH, 1);
|
742
|
lastDate = postgresFormat.format(endC.getTime());
|
743
|
}
|
744
|
if (reportItem != null) {
|
745
|
fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem);
|
746
|
reportItems.add(reportItem);
|
747
|
}
|
748
|
}
|
749
|
|
750
|
/*
|
751
|
jedis.put(redis_key, "persistent", "false");
|
752
|
jedis.put(redis_key, "query", st.toString());
|
753
|
jedis.put(redis_key, "result", toJson(reportItems));
|
754
|
jedis.put(redis_key, "fetchMode", "3");
|
755
|
*/
|
756
|
rs.close();
|
757
|
st.close();
|
758
|
connection.close();
|
759
|
} catch (Exception e) {
|
760
|
log.error("Repository Report failed: ", e);
|
761
|
} finally {
|
762
|
DbUtils.closeQuietly(rs);
|
763
|
DbUtils.closeQuietly(st);
|
764
|
DbUtils.closeQuietly(connection);
|
765
|
java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
|
766
|
//System.out.println("DB end "+timestamp);
|
767
|
log.info("Query end..." + timestamp);
|
768
|
|
769
|
}
|
770
|
}
|
771
|
|
772
|
public void executeJournal(List<ReportItem> reportItems, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) {
|
773
|
SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
774
|
SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM");
|
775
|
String beginDateStr = postgresFormat.format(beginDate);
|
776
|
String endDateStr = postgresFormat.format(endDate);
|
777
|
|
778
|
Connection connection = null;
|
779
|
PreparedStatement st = null;
|
780
|
ResultSet rs = null;
|
781
|
|
782
|
try {
|
783
|
connection = usageStatsDB.getConnection();
|
784
|
java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis());
|
785
|
//System.out.println("DB start "+timestamp1);
|
786
|
log.info("Query started..." + timestamp1);
|
787
|
|
788
|
if (repositoryIdentifier.equals("")) {
|
789
|
if (itemDataType.equals("")) {
|
790
|
//st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.ddate, res.downloads, res.views FROM (SELECT coalesce(ds.source, vs.source), coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.downloads_stats s WHERE s.date>=? AND s.date<=? GROUP BY s.source, s.repository_id, s.date) AS ds FULL OUTER JOIN (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.views_stats s WHERE s.date>=? AND s.date<=? GROUP BY s.source, s.repository_id, s.date) AS vs ON ds.source=vs.source AND ds.repository_id=vs.repository_id AND ds.date=vs.date) AS res JOIN public.datasource d ON d.id=res.repository_id JOIN public.datasource_oids dois ON d.id=dois.id WHERE (d.type='Journal' OR d.type='Journal Aggregator/Publisher') ORDER BY d.id, d.name, res.ddate ASC;");
|
791
|
st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.date, res.downloads, res.views FROM (SELECT us.source, us.repository_id, us.date, sum(us.downloads) as downloads, sum(us.views) as views FROM usage_stats us WHERE us.date>=? AND us.date<=? GROUP BY us.source, us.repository_id, us.date) AS res JOIN datasource d ON d.id=res.repository_id JOIN datasource_oids dois ON d.id=dois.id WHERE (d.type='Journal' OR d.type='Journal Aggregator/Publisher') ORDER BY d.id, d.name, res.date ASC;");
|
792
|
st.setString(1, beginDateStr);
|
793
|
st.setString(2, endDateStr);
|
794
|
//st.setString(3, beginDateStr);
|
795
|
//st.setString(4, endDateStr);
|
796
|
} else {
|
797
|
//st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.ddate, res.downloads, res.views FROM (SELECT coalesce(ds.source, vs.source), coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.downloads_stats s, public.result_classifications rc WHERE rc.id=s.result_id AND s.date>=? AND s.date<=? AND rc.type=? GROUP BY s.source, s.repository_id, s.date) AS ds FULL OUTER JOIN (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.views_stats s, public.result_classifications rc WHERE rc.id=s.result_id AND s.date>=? AND s.date<=? AND rc.type=? GROUP BY s.source, s.repository_id, s.date) AS vs ON ds.source=vs.source AND ds.repository_id=vs.repository_id AND ds.date=vs.date) AS res JOIN public.datasource d ON d.id=res.repository_id JOIN public.datasource_oids dois ON d.id=dois.id WHERE (d.type='Journal' OR d.type='Journal Aggregator/Publisher') ORDER BY d.id, d.name, res.ddate ASC;");
|
798
|
st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.date, res.downloads, res.views FROM (SELECT us.source, us.repository_id, us.date, sum(us.downloads) as downloads, sum(us.views) as views FROM usage_stats us, result_classifications rc WHERE rc.id=us.result_id AND us.date>=? AND us.date<=? AND rc.type=? GROUP BY us.source, us.repository_id, us.date) AS res JOIN datasource d ON d.id=res.repository_id JOIN datasource_oids dois ON d.id=dois.id WHERE (d.type='Journal' OR d.type='Journal Aggregator/Publisher') ORDER BY d.id, d.name, res.date ASC;");
|
799
|
st.setString(1, beginDateStr);
|
800
|
st.setString(2, endDateStr);
|
801
|
st.setString(3, itemDataType);
|
802
|
//st.setString(4, beginDateStr);
|
803
|
//st.setString(5, endDateStr);
|
804
|
//st.setString(6, itemDataType);
|
805
|
}
|
806
|
} else {
|
807
|
if (itemDataType.equals("")) {
|
808
|
//st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.ddate, res.downloads, res.views FROM (SELECT coalesce(ds.source, vs.source), coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.downloads_stats s WHERE s.date>=? AND s.date<=? AND s.repository_id=? GROUP BY s.source, s.repository_id, s.date) AS ds FULL OUTER JOIN (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.views_stats s WHERE s.date>=? AND s.date<=? AND s.repository_id=? GROUP BY s.source, s.repository_id, s.date) AS vs ON ds.source=vs.source AND ds.repository_id=vs.repository_id AND ds.date=vs.date) AS res JOIN public.datasource d ON d.id=res.repository_id JOIN public.datasource_oids dois ON d.id=dois.id WHERE (d.type='Journal' OR d.type='Journal Aggregator/Publisher') ORDER BY d.id, d.name, res.ddate ASC;");
|
809
|
st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.date, res.downloads, res.views FROM (SELECT us.source, us.repository_id, us.date, sum(us.downloads) as downloads, sum(us.views) as views FROM usage_stats us WHERE us.date>=? AND us.date<=? AND us.repository_id=? GROUP BY us.source, us.repository_id, us.date) AS res JOIN datasource d ON d.id=res.repository_id JOIN datasource_oids dois ON d.id=dois.id WHERE (d.type='Journal' OR d.type='Journal Aggregator/Publisher') ORDER BY d.id, d.name, res.date ASC;");
|
810
|
st.setString(1, beginDateStr);
|
811
|
st.setString(2, endDateStr);
|
812
|
st.setString(3, repositoryIdentifier);
|
813
|
//st.setString(4, beginDateStr);
|
814
|
//st.setString(5, endDateStr);
|
815
|
//st.setString(6, repositoryIdentifier);
|
816
|
} else {
|
817
|
//st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.ddate, res.downloads, res.views FROM (SELECT coalesce(ds.source, vs.source), coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.downloads_stats s, public.result_classifications rc WHERE rc.id=s.result_id AND s.date>=? AND s.date<=? AND rc.type=? AND s.repository_id=? GROUP BY s.source, s.repository_id, s.date) AS ds FULL OUTER JOIN (SELECT s.source, s.repository_id, s.date, sum(s.count) FROM public.views_stats s, public.result_classifications rc WHERE rc.id=s.result_id AND s.date>=? AND s.date<=? AND rc.type=? AND s.repository_id=? GROUP BY s.source, s.repository_id, s.date) AS vs ON ds.source=vs.source AND ds.repository_id=vs.repository_id AND ds.date=vs.date) AS res JOIN public.datasource d ON d.id=res.repository_id JOIN public.datasource_oids dois ON d.id=dois.id WHERE (d.type='Journal' OR d.type='Journal Aggregator/Publisher') ORDER BY d.id, d.name, res.ddate ASC;");
|
818
|
st = connection.prepareStatement("SELECT d.id, d.name, d.websiteurl, dois.orid, res.date, res.downloads, res.views FROM (SELECT us.source, us.repository_id, us.date, sum(us.downloads) as downloads, sum(us.views) as views FROM usage_stats us, result_classifications rc WHERE rc.id=us.result_id AND us.date>=? AND us.date<=? AND rc.type=? AND us.repository_id=? GROUP BY us.source, us.repository_id, us.date) AS res JOIN datasource d ON d.id=res.repository_id JOIN datasource_oids dois ON d.id=dois.id WHERE (d.type='Journal' OR d.type='Journal Aggregator/Publisher') ORDER BY d.id, d.name, res.date ASC;");
|
819
|
st.setString(1, beginDateStr);
|
820
|
st.setString(2, endDateStr);
|
821
|
st.setString(3, itemDataType);
|
822
|
st.setString(4, repositoryIdentifier);
|
823
|
//st.setString(5, beginDateStr);
|
824
|
//st.setString(6, endDateStr);
|
825
|
//st.setString(7, itemDataType);
|
826
|
//st.setString(8, repositoryIdentifier);
|
827
|
}
|
828
|
}
|
829
|
//log.error("RR STATEMENT: " + st);
|
830
|
|
831
|
/*
|
832
|
String redis_key = MD5(st.toString());
|
833
|
|
834
|
if (jedis.hasKey(redis_key, "result")) {
|
835
|
reportItems.addAll(reportItemsFromJson((String) jedis.get(redis_key, "result")));
|
836
|
st.close();
|
837
|
connection.close();
|
838
|
return;
|
839
|
}
|
840
|
*/
|
841
|
rs = st.executeQuery();
|
842
|
String repository = "";
|
843
|
String lastDate = "";
|
844
|
ReportItem reportItem = null;
|
845
|
|
846
|
/*
|
847
|
Calendar startCalendar = Calendar.getInstance();
|
848
|
startCalendar.setTime(beginDate);
|
849
|
Calendar endCalendar = Calendar.getInstance();
|
850
|
endCalendar.setTime(endDate);
|
851
|
*/
|
852
|
int ft_total = 0;
|
853
|
int abstr = 0;
|
854
|
if (granularity.equalsIgnoreCase("totals")) {
|
855
|
while (rs.next()) {
|
856
|
if (!rs.getString(1).equals(repository)) {
|
857
|
if (reportItem != null) {
|
858
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), Integer.toString(ft_total), Integer.toString(abstr)));
|
859
|
reportItems.add(reportItem);
|
860
|
}
|
861
|
repository = rs.getString(1);
|
862
|
reportItem = new ReportItem(null, rs.getString(2), "Platform", null);
|
863
|
reportItem.addIdentifier(new ItemIdentifier("OpenAIRE", rs.getString(1)));
|
864
|
reportItem.addIdentifier(new ItemIdentifier("ISSN", rs.getString(4).substring(rs.getString(4).lastIndexOf(":") + 1)));
|
865
|
if (rs.getString(3) != null) {
|
866
|
reportItem.addIdentifier(new ItemIdentifier("URL", rs.getString(3)));
|
867
|
}
|
868
|
ft_total = 0;
|
869
|
abstr = 0;
|
870
|
}
|
871
|
ft_total += rs.getInt(6);
|
872
|
abstr += rs.getInt(7);
|
873
|
}
|
874
|
if (reportItem != null) {
|
875
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), Integer.toString(ft_total), Integer.toString(abstr)));
|
876
|
reportItems.add(reportItem);
|
877
|
}
|
878
|
} else if (granularity.equalsIgnoreCase("monthly")) {
|
879
|
Calendar endCal = Calendar.getInstance();
|
880
|
endCal.setTime(postgresFormat.parse(endDateStr));
|
881
|
endCal.add(Calendar.MONTH, 1);
|
882
|
Date endDateForZeros = endCal.getTime();
|
883
|
while (rs.next()) {
|
884
|
if (!rs.getString(1).equals(repository)) {
|
885
|
if (reportItem != null) {
|
886
|
fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem);
|
887
|
reportItems.add(reportItem);
|
888
|
}
|
889
|
repository = rs.getString(1);
|
890
|
lastDate = beginDateStr;
|
891
|
reportItem = new ReportItem(null, rs.getString(2), "Platform", null);
|
892
|
reportItem.addIdentifier(new ItemIdentifier("OpenAIRE", rs.getString(1)));
|
893
|
reportItem.addIdentifier(new ItemIdentifier("ISSN", rs.getString(4).substring(rs.getString(4).lastIndexOf(":") + 1)));
|
894
|
if (rs.getString(3) != null) {
|
895
|
reportItem.addIdentifier(new ItemIdentifier("URL", rs.getString(3)));
|
896
|
}
|
897
|
}
|
898
|
fillWithZeros(postgresFormat.parse(lastDate), postgresFormat.parse(rs.getString(5)), reportItem);
|
899
|
Calendar endC = Calendar.getInstance();
|
900
|
endC.setTime(postgresFormat.parse(rs.getString(5)));
|
901
|
endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE));
|
902
|
if (reportItem != null) {
|
903
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(postgresFormat.parse(rs.getString(5))), report_dateFormat.format(endC.getTime()), rs.getString(6), rs.getString(7)));
|
904
|
}
|
905
|
endC.setTime(postgresFormat.parse(rs.getString(5)));
|
906
|
endC.add(Calendar.MONTH, 1);
|
907
|
lastDate = postgresFormat.format(endC.getTime());
|
908
|
}
|
909
|
if (reportItem != null) {
|
910
|
fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem);
|
911
|
reportItems.add(reportItem);
|
912
|
}
|
913
|
}
|
914
|
|
915
|
/*
|
916
|
jedis.put(redis_key, "persistent", "false");
|
917
|
jedis.put(redis_key, "query", st.toString());
|
918
|
jedis.put(redis_key, "result", toJson(reportItems));
|
919
|
jedis.put(redis_key, "fetchMode", "3");
|
920
|
*/
|
921
|
rs.close();
|
922
|
st.close();
|
923
|
connection.close();
|
924
|
} catch (Exception e) {
|
925
|
log.error("Repository Report failed: ", e);
|
926
|
} finally {
|
927
|
DbUtils.closeQuietly(rs);
|
928
|
DbUtils.closeQuietly(st);
|
929
|
DbUtils.closeQuietly(connection);
|
930
|
java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
|
931
|
//System.out.println("DB end "+timestamp);
|
932
|
log.info("Query end..." + timestamp);
|
933
|
|
934
|
}
|
935
|
}
|
936
|
|
937
|
public void executeBatchItems(List<ReportItem> reportItems, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) {
|
938
|
SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
939
|
SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM");
|
940
|
String beginDateStr = postgresFormat.format(beginDate);
|
941
|
String endDateStr = postgresFormat.format(endDate);
|
942
|
|
943
|
Connection connection = null;
|
944
|
PreparedStatement st = null;
|
945
|
ResultSet rs = null;
|
946
|
|
947
|
try {
|
948
|
connection = usageStatsDB.getConnection();
|
949
|
java.sql.Timestamp timestamp1 = new java.sql.Timestamp(System.currentTimeMillis());
|
950
|
//System.out.println("DB start "+timestamp1);
|
951
|
log.info("Query started..." + timestamp1);
|
952
|
if (itemDataType.equals("")) {
|
953
|
//st = connection.prepareStatement("SELECT res.result_id, r.title, r.publisher, r.source, rc.type, pids.pid, d.name, res.ddate, oids.orid, res.downloads, res.views FROM (SELECT coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.result_id, vs.result_id) AS result_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.downloads_stats s WHERE s.date>=? AND s.date<=? AND s.repository_id=? GROUP BY s.repository_id, s.result_id, s.date) AS ds FULL OUTER JOIN (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.views_stats s WHERE s.date>=? AND s.date<=? AND s.repository_id=? GROUP BY s.repository_id, s.result_id, s.date) AS vs ON ds.result_id=vs.result_id AND ds.date=vs.date) AS res JOIN public.result r ON res.result_id=r.id JOIN public.datasource d ON d.id=res.repository_id JOIN public.result_classifications rc ON rc.id=r.id LEFT JOIN (SELECT pids.id, string_agg(pids.pid, '#!#') AS pid FROM public.result_pids pids, public.result_datasources rd WHERE rd.id=pids.id AND type='doi' AND rd.datasource=? GROUP BY pids.id) AS pids ON pids.id=r.id LEFT JOIN (SELECT oids.id, string_agg(oids.orid, '#!#') AS orid FROM public.result_oids oids, public.result_datasources rd WHERE rd.id=oids.id AND rd.datasource=? GROUP BY oids.id) AS oids ON oids.id=r.id ORDER BY res.result_id, res.ddate;");
|
954
|
//st = connection.prepareStatement("SELECT res.result_id, r.title, r.publisher, r.source, rc.type, pids.pid, d.name, res.date, oids.orid, res.downloads, res.views FROM (SELECT us.repository_id, us.result_id, us.date, sum(us.downloads) as downloads, sum(us.views) as views FROM usage_stats us WHERE us.date>=? AND us.date<=? AND us.repository_id=? GROUP BY us.repository_id, us.result_id, us.date) AS res JOIN result r ON res.result_id=r.id JOIN datasource d ON d.id=res.repository_id JOIN result_classifications rc ON rc.id=r.id LEFT JOIN (SELECT pids.id, string_agg(pids.pid, '#!#') AS pid FROM result_pids pids, result_datasources rd WHERE rd.id=pids.id AND type='doi' AND rd.datasource=? GROUP BY pids.id) AS pids ON pids.id=r.id LEFT JOIN (SELECT oids.id, string_agg(oids.orid, '#!#') AS orid FROM result_oids oids, result_datasources rd WHERE rd.id=oids.id AND rd.datasource=? GROUP BY oids.id) AS oids ON oids.id=r.id ORDER BY res.result_id, res.date;");
|
955
|
st = connection.prepareStatement("SELECT us.original_result_id, r.title, r.publisher, r.source, rc.type, string_agg(distinct pids.pid, '#!#') as pid, d.name, us.date, string_agg(distinct oids.orid, '#!#') AS orid , sum(us.downloads) as downloads, sum(us.views) as views FROM usage_stats us JOIN result r ON us.result_id=r.id JOIN datasource d ON d.id=us.repository_id LEFT OUTER JOIN result_classifications rc ON rc.id=r.id LEFT OUTER JOIN result_pids pids on pids.id=r.id LEFT OUTER JOIN result_oids oids on oids.id=r.id WHERE us.date>=? AND us.date<=? AND us.repository_id=? GROUP BY us.original_result_id, r.title, r.publisher, r.source, rc.type, d.name, us.date;");
|
956
|
st.setString(1, beginDateStr);
|
957
|
st.setString(2, endDateStr);
|
958
|
st.setString(3, repositoryIdentifier);
|
959
|
//st.setString(4, beginDateStr);
|
960
|
//st.setString(5, endDateStr);
|
961
|
//st.setString(6, repositoryIdentifier);
|
962
|
//st.setString(4, repositoryIdentifier);
|
963
|
//st.setString(5, repositoryIdentifier);
|
964
|
} else {
|
965
|
//st = connection.prepareStatement("SELECT res.result_id, r.title, r.publisher, r.source, rc.type, pids.pid, d.name, res.ddate, oids.orid, res.downloads, res.views FROM (SELECT coalesce(ds.repository_id, vs.repository_id) AS repository_id, coalesce(ds.result_id, vs.result_id) AS result_id, coalesce(ds.date, vs.date) AS ddate, coalesce(ds.sum, 0) AS downloads, coalesce(vs.sum,0) AS views FROM (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.downloads_stats s WHERE s.date>=? AND s.date<=? AND s.repository_id=? GROUP BY s.repository_id, s.result_id, s.date) AS ds FULL OUTER JOIN (SELECT s.repository_id, s.result_id, s.date, sum(s.count) FROM public.views_stats s WHERE s.date>=? AND s.date<=? AND s.repository_id=? GROUP BY s.repository_id, s.result_id, s.date) AS vs ON ds.result_id=vs.result_id AND ds.date=vs.date) AS res JOIN public.result r ON res.result_id=r.id JOIN public.datasource d ON d.id=res.repository_id JOIN public.result_classifications rc ON rc.id=r.id LEFT JOIN (SELECT pids.id, string_agg(pids.pid, '#!#') AS pid FROM public.result_pids pids, result_datasources rd WHERE rd.id=pids.id AND type='doi' AND rd.datasource=? GROUP BY pids.id) AS pids ON pids.id=r.id LEFT JOIN (SELECT oids.id, string_agg(oids.orid, '#!#') AS orid FROM public.result_oids oids, public.result_datasources rd WHERE rd.id=oids.id AND rd.datasource=? GROUP BY oids.id) AS oids ON oids.id=r.id WHERE rc.type=? ORDER BY res.result_id, res.ddate;");
|
966
|
//st = connection.prepareStatement("SELECT res.result_id, r.title, r.publisher, r.source, rc.type, pids.pid, d.name, res.date, oids.orid, res.downloads, res.views FROM (SELECT us.repository_id, us.result_id, us.date, sum(us.downloads) as downloads, sum(us.views) as views FROM public.usage_stats us WHERE us.date>=? AND us.date<=? AND us.repository_id=? GROUP BY us.repository_id, us.result_id, us.date) AS res JOIN public.result r ON res.result_id=r.id JOIN public.datasource d ON d.id=res.repository_id JOIN public.result_classifications rc ON rc.id=r.id LEFT JOIN (SELECT pids.id, string_agg(pids.pid, '#!#') AS pid FROM public.result_pids pids, result_datasources rd WHERE rd.id=pids.id AND type='doi' AND rd.datasource=? GROUP BY pids.id) AS pids ON pids.id=r.id LEFT JOIN (SELECT oids.id, string_agg(oids.orid, '#!#') AS orid FROM public.result_oids oids, public.result_datasources rd WHERE rd.id=oids.id AND rd.datasource=? GROUP BY oids.id) AS oids ON oids.id=r.id WHERE rc.type=? ORDER BY res.result_id, res.date;");
|
967
|
//st = connection.prepareStatement("SELECT res.result_id, r.title, r.publisher, r.source, rc.type, pids.pid, d.name, res.date, oids.orid, res.downloads, res.views FROM (SELECT us.repository_id, us.result_id, us.date, sum(us.downloads) as downloads, sum(us.views) as views FROM usage_stats us WHERE us.date>=? AND us.date<=? AND us.repository_id=? GROUP BY us.repository_id, us.result_id, us.date) AS res JOIN result r ON res.result_id=r.id JOIN datasource d ON d.id=res.repository_id JOIN result_classifications rc ON rc.id=r.id LEFT JOIN (SELECT pids.id, string_agg(pids.pid, '#!#') AS pid FROM result_pids pids, result_datasources rd WHERE rd.id=pids.id AND type='doi' AND rd.datasource=? GROUP BY pids.id) AS pids ON pids.id=r.id LEFT JOIN (SELECT oids.id, string_agg(oids.orid, '#!#') AS orid FROM result_oids oids, result_datasources rd WHERE rd.id=oids.id AND rd.datasource=? GROUP BY oids.id) AS oids ON oids.id=r.id WHERE rc.type=? ORDER BY res.result_id, res.date;");
|
968
|
st = connection.prepareStatement("SELECT us.original_result_id, r.title, r.publisher, r.source, rc.type, string_agg(distinct pids.pid, '#!#') as pid, d.name, us.date, string_agg(distinct oids.orid, '#!#') AS orid , sum(us.downloads) as downloads, sum(us.views) as views FROM usage_stats us JOIN result r ON us.result_id=r.id JOIN datasource d ON d.id=us.repository_id JOIN result_classifications rc ON rc.id=r.id LEFT OUTER JOIN result_pids pids on pids.id=r.id LEFT OUTER JOIN result_oids oids on oids.id=r.id WHERE us.date>=? AND us.date<=? AND us.repository_id=? AND rc.type=? GROUP BY us.original_result_id, r.title, r.publisher, r.source, rc.type, d.name, us.date;");
|
969
|
|
970
|
st.setString(1, beginDateStr);
|
971
|
st.setString(2, endDateStr);
|
972
|
st.setString(3, repositoryIdentifier);
|
973
|
//st.setString(4, beginDateStr);
|
974
|
//st.setString(5, endDateStr);
|
975
|
//st.setString(6, repositoryIdentifier);
|
976
|
st.setString(4, itemDataType);
|
977
|
}
|
978
|
//log.error("IR STATEMENT: " + st);
|
979
|
|
980
|
/*
|
981
|
String redis_key = MD5(st.toString());
|
982
|
|
983
|
if (jedis.hasKey(redis_key, "result")) {
|
984
|
reportItems.addAll(reportItemsFromJson((String) jedis.get(redis_key, "result")));
|
985
|
st.close();
|
986
|
connection.close();
|
987
|
return;
|
988
|
}
|
989
|
*/
|
990
|
//connection.setAutoCommit(false);
|
991
|
//st.setFetchSize(5000);
|
992
|
rs = st.executeQuery();
|
993
|
String result = "";
|
994
|
String lastDate = "";
|
995
|
ReportItem reportItem = null;
|
996
|
|
997
|
int ft_total = 0;
|
998
|
int abstr = 0;
|
999
|
if (granularity.equalsIgnoreCase("totals")) {
|
1000
|
while (rs.next()) {
|
1001
|
if (!rs.getString(1).equals(result)) {
|
1002
|
if (reportItem != null) {
|
1003
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), Integer.toString(ft_total), Integer.toString(abstr)));
|
1004
|
reportItems.add(reportItem);
|
1005
|
}
|
1006
|
result = rs.getString(1);
|
1007
|
reportItem = new ReportItem(rs.getString(3), rs.getString(7), rs.getString(5), rs.getString(2));
|
1008
|
reportItem.addIdentifier(new ItemIdentifier("OpenAIRE", rs.getString(1)));
|
1009
|
reportItem.addIdentifier(new ItemIdentifier("URLs", rs.getString(4)));
|
1010
|
if (rs.getString(9) != null && !rs.getString(9).equals("")) {
|
1011
|
if (rs.getString(9).contains("#!#")) {
|
1012
|
reportItem.addIdentifier(new ItemIdentifier("OAI", rs.getString(9).substring(0, rs.getString(9).indexOf("#!#"))));
|
1013
|
} else {
|
1014
|
reportItem.addIdentifier(new ItemIdentifier("OAI", rs.getString(9)));
|
1015
|
}
|
1016
|
}
|
1017
|
if (rs.getString(6) != null && !rs.getString(6).equals("")) {
|
1018
|
if (rs.getString(6).contains("#!#")) {
|
1019
|
reportItem.addIdentifier(new ItemIdentifier("DOI", rs.getString(6).substring(0, rs.getString(6).indexOf("#!#"))));
|
1020
|
} else {
|
1021
|
reportItem.addIdentifier(new ItemIdentifier("DOI", rs.getString(6)));
|
1022
|
}
|
1023
|
}
|
1024
|
ft_total = 0;
|
1025
|
abstr = 0;
|
1026
|
}
|
1027
|
ft_total += rs.getInt(10);
|
1028
|
abstr += rs.getInt(11);
|
1029
|
}
|
1030
|
if (reportItem != null) {
|
1031
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), Integer.toString(ft_total), Integer.toString(abstr)));
|
1032
|
reportItems.add(reportItem);
|
1033
|
}
|
1034
|
} else if (granularity.equalsIgnoreCase("monthly")) {
|
1035
|
Calendar endCal = Calendar.getInstance();
|
1036
|
endCal.setTime(postgresFormat.parse(endDateStr));
|
1037
|
endCal.add(Calendar.MONTH, 1);
|
1038
|
Date endDateForZeros = endCal.getTime();
|
1039
|
while (rs.next()) {
|
1040
|
if (!rs.getString(1).equals(result)) {
|
1041
|
if (reportItem != null) {
|
1042
|
fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem);
|
1043
|
reportItems.add(reportItem);
|
1044
|
}
|
1045
|
result = rs.getString(1);
|
1046
|
lastDate = beginDateStr;
|
1047
|
reportItem = new ReportItem(rs.getString(3), rs.getString(7), rs.getString(5), rs.getString(2));
|
1048
|
reportItem.addIdentifier(new ItemIdentifier("OpenAIRE", rs.getString(1)));
|
1049
|
reportItem.addIdentifier(new ItemIdentifier("URLs", rs.getString(4)));
|
1050
|
if (rs.getString(9) != null && !rs.getString(9).equals("")) {
|
1051
|
if (rs.getString(9).contains("#!#")) {
|
1052
|
reportItem.addIdentifier(new ItemIdentifier("OAI", rs.getString(9).substring(0, rs.getString(9).indexOf("#!#"))));
|
1053
|
} else {
|
1054
|
reportItem.addIdentifier(new ItemIdentifier("OAI", rs.getString(9)));
|
1055
|
}
|
1056
|
}
|
1057
|
if (rs.getString(6) != null && !rs.getString(6).equals("")) {
|
1058
|
if (rs.getString(6).contains("#!#")) {
|
1059
|
reportItem.addIdentifier(new ItemIdentifier("DOI", rs.getString(6).substring(0, rs.getString(6).indexOf("#!#"))));
|
1060
|
} else {
|
1061
|
reportItem.addIdentifier(new ItemIdentifier("DOI", rs.getString(6)));
|
1062
|
}
|
1063
|
}
|
1064
|
}
|
1065
|
fillWithZeros(postgresFormat.parse(lastDate), postgresFormat.parse(rs.getString(8)), reportItem);
|
1066
|
Calendar endC = Calendar.getInstance();
|
1067
|
endC.setTime(postgresFormat.parse(rs.getString(8)));
|
1068
|
endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE));
|
1069
|
if (reportItem != null) {
|
1070
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(postgresFormat.parse(rs.getString(8))), report_dateFormat.format(endC.getTime()), rs.getString(10), rs.getString(11)));
|
1071
|
}
|
1072
|
endC.setTime(postgresFormat.parse(rs.getString(8)));
|
1073
|
endC.add(Calendar.MONTH, 1);
|
1074
|
lastDate = postgresFormat.format(endC.getTime());
|
1075
|
}
|
1076
|
if (reportItem != null) {
|
1077
|
fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem);
|
1078
|
reportItems.add(reportItem);
|
1079
|
}
|
1080
|
|
1081
|
}
|
1082
|
|
1083
|
/*
|
1084
|
jedis.put(redis_key, "persistent", "false");
|
1085
|
jedis.put(redis_key, "query", st.toString());
|
1086
|
jedis.put(redis_key, "result", toJson(reportItems));
|
1087
|
jedis.put(redis_key, "fetchMode", "3");
|
1088
|
*/
|
1089
|
} catch (Exception e) {
|
1090
|
log.error("Batch Item Report failed: ", e);
|
1091
|
} finally {
|
1092
|
DbUtils.closeQuietly(rs);
|
1093
|
DbUtils.closeQuietly(st);
|
1094
|
DbUtils.closeQuietly(connection);
|
1095
|
java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
|
1096
|
//System.out.println("DB end "+timestamp);
|
1097
|
log.info("Query end..." + timestamp);
|
1098
|
|
1099
|
}
|
1100
|
}
|
1101
|
|
1102
|
private void fillWithZeros(Date from, Date to, ReportItem reportItem) {
|
1103
|
SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
1104
|
|
1105
|
Calendar fromCalendar = Calendar.getInstance();
|
1106
|
fromCalendar.setTime(from);
|
1107
|
|
1108
|
Calendar toCalendar = Calendar.getInstance();
|
1109
|
toCalendar.setTime(to);
|
1110
|
while (from.before(to)) {
|
1111
|
Calendar temp_c = Calendar.getInstance();
|
1112
|
temp_c.setTime(from);
|
1113
|
temp_c.set(Calendar.DAY_OF_MONTH, temp_c.getActualMaximum(Calendar.DAY_OF_MONTH));
|
1114
|
Date temp_endDate = temp_c.getTime();
|
1115
|
|
1116
|
reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(from), report_dateFormat.format(temp_endDate), "0", "0"));
|
1117
|
fromCalendar.add(Calendar.MONTH, 1);
|
1118
|
from = fromCalendar.getTime();
|
1119
|
}
|
1120
|
}
|
1121
|
}
|