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