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