Project

General

Profile

« Previous | Next » 

Revision 57022

Added by Dimitris Pierrakos about 5 years ago

View differences:

modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0"
3
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
    <modelVersion>4.0.0</modelVersion>
6
    <groupId>eu.dnetlib</groupId>
7
    <artifactId>dnet-openaire-usage-stats-api</artifactId>
8
    <version>3.0.0-SNAPSHOT</version>
9
    <packaging>war</packaging>
10
    <parent>
11
        <groupId>org.springframework.boot</groupId>
12
        <artifactId>spring-boot-starter-parent</artifactId>
13
        <version>1.5.15.RELEASE</version>
14
    </parent>
15

  
16
    <dependencies>
17

  
18
        <dependency>
19
            <groupId>eu.dnetlib</groupId>
20
            <artifactId>dnet-openaire-usage-stats-sushilite</artifactId>
21
            <version>1.0.0-SNAPSHOT</version>
22
        </dependency>
23

  
24
        <dependency>
25
            <groupId>org.springframework.boot</groupId>
26
            <artifactId>spring-boot-starter-web</artifactId>
27
            <exclusions>
28
                <exclusion>
29
                    <groupId>org.springframework.boot</groupId>
30
                    <artifactId>spring-boot-starter-logging</artifactId>
31
                </exclusion>
32
            </exclusions>
33
        </dependency>
34

  
35
        <dependency>
36
            <groupId>org.springframework.boot</groupId>
37
            <artifactId>spring-boot-starter-data-jpa</artifactId>
38
        </dependency>
39

  
40
        <dependency>
41
            <groupId>org.springframework.boot</groupId>
42
            <artifactId>spring-boot-starter-data-redis</artifactId>
43
        </dependency>
44

  
45
        <dependency>
46
            <groupId>org.springframework.boot</groupId>
47
            <artifactId>spring-boot-starter-test</artifactId>
48
            <scope>test</scope>
49
        </dependency>
50

  
51
        <!--
52
        <dependency>
53
            <groupId>org.springframework.boot</groupId>
54
            <artifactId>spring-boot-starter-log4j2</artifactId>
55
        </dependency>
56
        -->
57

  
58
        <dependency>
59
            <groupId>log4j</groupId>
60
            <artifactId>log4j</artifactId>
61
            <version>1.2.17</version>
62
        </dependency>
63

  
64
        <dependency>
65
            <groupId>org.postgresql</groupId>
66
            <artifactId>postgresql</artifactId>
67
            <scope>runtime</scope>
68
        </dependency>
69

  
70
        <dependency>
71
            <groupId>junit</groupId>
72
            <artifactId>junit</artifactId>
73
            <scope>test</scope>
74
        </dependency>
75

  
76
        <dependency>
77
            <groupId>com.googlecode.json-simple</groupId>
78
            <artifactId>json-simple</artifactId>
79
            <version>1.1.1</version>
80
        </dependency>
81

  
82
        <dependency>
83
            <groupId>commons-dbutils</groupId>
84
            <artifactId>commons-dbutils</artifactId>
85
            <version>1.7</version>
86
        </dependency>
87

  
88
    </dependencies>
89

  
90
    <properties>
91
        <java.version>1.8</java.version>
92
        <tomcat.version>7.0.52</tomcat.version>
93
    </properties>
94

  
95

  
96
    <build>
97
        <plugins>
98
            <plugin>
99
                <groupId>org.springframework.boot</groupId>
100
                <artifactId>spring-boot-maven-plugin</artifactId>
101
            </plugin>
102
            <plugin>
103
                <artifactId>maven-war-plugin</artifactId>
104
                <configuration>
105
                    <failOnMissingWebXml>false</failOnMissingWebXml>
106
                </configuration>
107
            </plugin>
108
        </plugins>
109
        <finalName>usagestats</finalName>
110
    </build>
111

  
112
    <repositories>
113
        <repository>
114
            <id>spring-releases</id>
115
            <url>https://repo.spring.io/libs-release</url>
116
        </repository>
117
    </repositories>
118

  
119
    <pluginRepositories>
120
        <pluginRepository>
121
            <id>spring-releases</id>
122
            <url>https://repo.spring.io/libs-release</url>
123
        </pluginRepository>
124
    </pluginRepositories>
125
</project>
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/portal/RepositoryStats.java
1
package eu.dnetlib.usagestats.portal;
2

  
3
import com.fasterxml.jackson.annotation.JsonProperty;
4

  
5
import java.io.Serializable;
6

  
7
/**
8
 * Created by tsampikos on 8/11/2016.
9
 */
10
public class RepositoryStats implements Serializable {
11
    private final static long serialVersionUID = 1;
12
    private String datasource_name = "";
13
    private String datasource_id = "";
14
    private String value = "";
15
    private String openaire = "";
16

  
17
    public RepositoryStats() {
18
    }
19

  
20
    public RepositoryStats(String datasource_name, String datasource_id, String value, String openaire) {
21
        this.datasource_name = datasource_name;
22
        this.datasource_id = datasource_id;
23
        this.value = value;
24
        this.openaire = openaire;
25
    }
26

  
27
    @JsonProperty("datasource_name")
28
    public String getDatasource_name() {
29
        return datasource_name;
30
    }
31

  
32
    @JsonProperty("datasource_id")
33
    public String getDatasource_id() {
34
        return datasource_id;
35
    }
36

  
37
    @JsonProperty("value")
38
    public String getValue() {
39
        return value;
40
    }
41

  
42
    @JsonProperty("openaire")
43
    public String getOpenaire() {
44
        return openaire;
45
    }
46

  
47
    /*
48
    public String toString(){
49
        return datasource_name + " " + datasource_id + " " + value + " " + openaire;
50
    }
51
    */
52

  
53
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/portal/TotalStats.java
1
package eu.dnetlib.usagestats.portal;
2

  
3
import com.fasterxml.jackson.annotation.JsonProperty;
4

  
5
import java.util.List;
6

  
7
public class TotalStats {
8
    private int repositories;
9
    private int items;
10
    private int downloads;
11
    private int views;
12
    private List<YearlyStats> yearlyStats;
13

  
14
    @JsonProperty("yearly_stats")
15
    public List<YearlyStats> getYearlyStats() {
16
        return yearlyStats;
17
    }
18

  
19
    public void setYearlyStats(List<YearlyStats> yearlyStats) {
20
        this.yearlyStats = yearlyStats;
21
    }
22

  
23

  
24
    public TotalStats() {}
25

  
26
    public int getRepositories() {
27
        return repositories;
28
    }
29

  
30
    public void setRepositories(int repositories) {
31
        this.repositories = repositories;
32
    }
33

  
34
    public int getItems() {
35
        return items;
36
    }
37

  
38
    public void setItems(int items) {
39
        this.items = items;
40
    }
41

  
42
    public int getDownloads() {
43
        return downloads;
44
    }
45

  
46
    public void setDownloads(int downloads) {
47
        this.downloads = downloads;
48
    }
49

  
50
    public int getViews() {
51
        return views;
52
    }
53

  
54
    public void setViews(int views) {
55
        this.views = views;
56
    }
57
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/portal/UsageStats.java
1
package eu.dnetlib.usagestats.portal;
2

  
3
import com.fasterxml.jackson.annotation.JsonProperty;
4

  
5
import java.io.Serializable;
6
import java.util.ArrayList;
7
import java.util.List;
8

  
9

  
10
public class UsageStats implements Serializable {
11

  
12
    private final static long serialVersionUID = 1;
13

  
14
    private final List<RepositoryStats> downloads = new ArrayList<>();
15
    private final List<RepositoryStats> views = new ArrayList<>();
16

  
17
    private String total_downloads = "0";
18
    private String total_views = "0";
19
    private String pageviews = "0";
20
    private String total_openaire_views = "0";
21
    private String total_openaire_downloads = "0";
22

  
23
    public UsageStats() {
24
    }
25

  
26
    @JsonProperty("downloads")
27
    public List<RepositoryStats> getDownloads() {
28
        return downloads;
29
    }
30

  
31
    @JsonProperty("views")
32
    public List<RepositoryStats> getViews() {
33
        return views;
34
    }
35

  
36
    public void addViews(RepositoryStats view) {
37
        views.add(view);
38
    }
39

  
40
    public void addDownloads(RepositoryStats download) {
41
        downloads.add(download);
42
    }
43

  
44
    @JsonProperty("total_downloads")
45
    public String getTotal_downloads() {
46
        return total_downloads;
47
    }
48

  
49
    public void setTotal_downloads(String total_downloads) {
50
        this.total_downloads = total_downloads;
51
    }
52

  
53
    @JsonProperty("total_views")
54
    public String getTotal_views() {
55
        return total_views;
56
    }
57

  
58
    public void setTotal_views(String total_views) {
59
        this.total_views = total_views;
60
    }
61

  
62
    @JsonProperty("pageviews")
63
    public String getPageViews() {
64
        return pageviews;
65
    }
66

  
67
    public void setPageViews(String pageviews) {
68
        this.pageviews = pageviews;
69
    }
70

  
71
    @JsonProperty("total_openaire_views")
72
    public String getTotal_openaire_views() {
73
        return total_openaire_views;
74
    }
75

  
76
    public void setTotal_openaire_views(String total_openaire_views) {
77
        this.total_openaire_views = total_openaire_views;
78
    }
79

  
80
    @JsonProperty("total_openaire_downloads")
81
    public String getTotal_openaire_downloads() {
82
        return total_openaire_downloads;
83
    }
84

  
85
    public void setTotal_openaire_downloads(String total_openaire_downloads) {
86
        this.total_openaire_downloads = total_openaire_downloads;
87
    }
88

  
89

  
90
    /*
91
    public String toString(){
92
        String string;
93
        string = total_downloads + " ";
94
        string += total_views + " ";
95
        string += pageviews + " ";
96
        string += total_openaire + " ";
97

  
98
        for(RepositoryStats repositoryStats : downloads){
99
            string += repositoryStats.toString() + " ";
100
        }
101

  
102
        for(RepositoryStats repositoryStats : views){
103
            string += repositoryStats.toString() + " ";
104
        }
105
        return string;
106
    }
107
    */
108
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/portal/YearlyStats.java
1
package eu.dnetlib.usagestats.portal;
2

  
3
import com.fasterxml.jackson.annotation.JsonProperty;
4

  
5
import java.util.List;
6

  
7
public class YearlyStats {
8
    private int year;
9
    private int repositories;
10
    private int items;
11
    private int downloads;
12
    private int views;
13
    private List<MonthlyStats> monthlyStats;
14

  
15
    @JsonProperty("monthly_stats")
16
    public List<MonthlyStats> getMonthlyStats() {
17
        return monthlyStats;
18
    }
19

  
20
    public void setMonthlyStats(List<MonthlyStats> monthlyStats) {
21
        this.monthlyStats = monthlyStats;
22
    }
23

  
24
    public YearlyStats() {}
25

  
26
    public int getYear() {
27
        return year;
28
    }
29

  
30
    public void setYear(int year) {
31
        this.year = year;
32
    }
33

  
34
    public int getRepositories() {
35
        return repositories;
36
    }
37

  
38
    public void setRepositories(int repositories) {
39
        this.repositories = repositories;
40
    }
41

  
42
    public int getItems() {
43
        return items;
44
    }
45

  
46
    public void setItems(int items) {
47
        this.items = items;
48
    }
49

  
50
    public int getDownloads() {
51
        return downloads;
52
    }
53

  
54
    public void setDownloads(int downloads) {
55
        this.downloads = downloads;
56
    }
57

  
58
    public int getViews() {
59
        return views;
60
    }
61

  
62
    public void setViews(int views) {
63
        this.views = views;
64
    }
65
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/portal/MonthlyStats.java
1
package eu.dnetlib.usagestats.portal;
2

  
3
public class MonthlyStats {
4
    private int month;
5
    private int repositories;
6
    private int items;
7
    private int downloads;
8
    private int views;
9

  
10
    public int getMonth() {
11
        return month;
12
    }
13

  
14
    public void setMonth(int month) {
15
        this.month = month;
16
    }
17

  
18
    public int getRepositories() {
19
        return repositories;
20
    }
21

  
22
    public void setRepositories(int repositories) {
23
        this.repositories = repositories;
24
    }
25

  
26
    public int getItems() {
27
        return items;
28
    }
29

  
30
    public void setItems(int items) {
31
        this.items = items;
32
    }
33

  
34
    public int getDownloads() {
35
        return downloads;
36
    }
37

  
38
    public void setDownloads(int downloads) {
39
        this.downloads = downloads;
40
    }
41

  
42
    public int getViews() {
43
        return views;
44
    }
45

  
46
    public void setViews(int views) {
47
        this.views = views;
48
    }
49
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/services/SushiLiteService.java
1
package eu.dnetlib.usagestats.services;
2

  
3
import eu.dnetlib.usagestats.sushilite.domain.ReportResponseWrapper;
4

  
5
public interface SushiLiteService {
6
    ReportResponseWrapper buildReport(String reportName, String release, String requestorId, String beginDate,
7
                                             String endDate, String repositoryIdentifier, String itemIdentifier,
8
                                             String itemDataType, String hasDoi, String granularity, String callback);
9

  
10
    String displayReport(String reportName, String release, String requestorId, String beginDate,
11
                                      String endDate, String repositoryIdentifier, String itemIdentifier,
12
                                      String itemDataType, String hasDoi, String granularity, String callback, String pretty);
13
}
14

  
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImpl.java
1
package eu.dnetlib.usagestats.services;
2

  
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import eu.dnetlib.usagestats.repositories.UsageStatsRepository;
5
import eu.dnetlib.usagestats.sushilite.domain.ReportItem;
6
import eu.dnetlib.usagestats.sushilite.domain.ReportException;
7
import eu.dnetlib.usagestats.sushilite.domain.ReportResponse;
8
import eu.dnetlib.usagestats.sushilite.domain.ReportResponseWrapper;
9
import org.springframework.stereotype.Service;
10

  
11
import java.text.SimpleDateFormat;
12
import java.util.ArrayList;
13
import java.util.Calendar;
14
import java.util.Date;
15
import java.util.List;
16
import org.apache.log4j.Logger;
17

  
18
@Service
19
public class SushiLiteServiceImpl implements SushiLiteService {
20

  
21

  
22
    private final UsageStatsRepository usageStatsRepository;
23

  
24
    private final Logger log = Logger.getLogger(this.getClass());
25

  
26
    public SushiLiteServiceImpl(UsageStatsRepository usageStatsRepository) {
27
        this.usageStatsRepository = usageStatsRepository;
28
    }
29

  
30
    @Override
31
    public ReportResponseWrapper buildReport(String reportName, String release, String requestorId, String beginDate,
32
                                             String endDate, String repositoryIdentifier, String itemIdentifier,
33
                                             String itemDataType, String hasDoi, String granularity, String callback) {
34

  
35
        List<ReportItem> reportItems = new ArrayList<>();
36
        List<ReportException> reportExceptions = new ArrayList<>();
37

  
38
        if (!granularity.equalsIgnoreCase("totals") && !granularity.equalsIgnoreCase("monthly")) {
39
            reportExceptions.add(new ReportException("3062", "Warning", "Invalid ReportAttribute Value", "Granularity: \'" + granularity + "\' unknown. Defaulting to Monthly"));
40
            granularity = "Monthly";
41
        }
42

  
43
        Date beginDateParsed;
44
        if (!beginDate.equals("")) {
45
            beginDateParsed = tryParse(beginDate);
46
            if (beginDateParsed != null && (granularity.toLowerCase().equals("monthly") || beginDate.length() == 7)) {
47
                Calendar temp = Calendar.getInstance();
48
                temp.setTime(beginDateParsed);
49
                temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH));
50
                beginDateParsed = temp.getTime();
51
            }
52
        } else {
53
            Calendar temp = Calendar.getInstance();
54
            temp.add(Calendar.MONTH, -1);
55
            temp.set(Calendar.DAY_OF_MONTH, temp.getActualMinimum(Calendar.DAY_OF_MONTH));
56
            beginDateParsed = temp.getTime();
57
            reportExceptions.add(new ReportException("3021", "Warning", "Unspecified Date Arguments", "Begin Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed)));
58
        }
59

  
60
        Date endDateParsed;
61
        if (!endDate.equals("")) {
62
            endDateParsed = tryParse(endDate);
63
            if (endDateParsed != null && (granularity.toLowerCase().equals("monthly") || endDate.length() == 7)) {
64
                Calendar temp = Calendar.getInstance();
65
                temp.setTime(endDateParsed);
66
                temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH));
67
                endDateParsed = temp.getTime();
68
            }
69
        } else {
70
            Calendar temp = Calendar.getInstance();
71
            temp.add(Calendar.MONTH, -1);
72
            temp.set(Calendar.DAY_OF_MONTH, temp.getActualMaximum(Calendar.DAY_OF_MONTH));
73
            endDateParsed = temp.getTime();
74
            reportExceptions.add(new ReportException("3021", "Warning", "Unspecified Date Arguments", "End Date set to default: " + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed)));
75
        }
76
        //log.error("dates: " + beginDateParsed.toString() + " - " + endDateParsed.toString());
77

  
78
        if (beginDateParsed == null) {
79
            reportExceptions.add(new ReportException("3020", "Error", "Invalid Date Arguments", "Begin Date: " + beginDate + " is not a valid date"));
80
        }
81
        if (endDateParsed == null) {
82
            reportExceptions.add(new ReportException("3020", "Error", "Invalid Date Arguments", "End Date: " + endDate + " is not a valid date"));
83
        }
84
        if (beginDateParsed != null && endDateParsed != null && !beginDateParsed.before(endDateParsed)) {
85
            reportExceptions.add(new ReportException("3020", "Error", "Invalid Date Arguments", "BeginDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(beginDateParsed) + "\' is greater than EndDate \'" + new SimpleDateFormat("yyyy-MM-dd").format(endDateParsed) + "\'"));
86
        }
87

  
88
        String repoid = "";
89
        if (!repositoryIdentifier.equals("")) {
90
            repoid = usageStatsRepository.executeRepoId(repositoryIdentifier, reportName.toLowerCase());
91
            if (repoid.equals("-1")) {
92
                reportExceptions.add(new ReportException("3060", "Error", "Invalid Filter Value", "RepositoryIdentifier: " + repositoryIdentifier + " is not valid"));
93
            }
94
        }
95
        String itemid = "";
96
        if (!itemIdentifier.equals("")) {
97
            String[] split = itemIdentifier.split(":");
98
            switch (split[0].toLowerCase()) {
99
                case "oid":
100
                    itemid = itemIdentifier;
101
                    break;
102
                case "doi":
103
                    itemid = itemIdentifier;
104
                    break;
105
                case "openaire":
106
                    itemid = itemIdentifier;
107
                    break;
108
                default:
109
                    reportExceptions.add(new ReportException("3060", "Error", "Invalid Filter Value", "ItemIdentifier: " + itemIdentifier + " is not valid"));
110
                    itemid = "-1";
111
            }
112
        }
113
        if (itemid.equals("") && repoid.equals("") && !reportName.equalsIgnoreCase("rr1") && !reportName.equalsIgnoreCase("jr1")) {
114
            reportExceptions.add(new ReportException("3070", "Error", "Required Filter Missing", "ItemIdentifier or RepositoryIdentifier must be supplied"));
115
        }
116
        if (reportName.equalsIgnoreCase("ar1")) {
117
            if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) {
118
                if (!itemid.equals("")) {
119
                    if (itemDataType.equalsIgnoreCase("") || itemDataType.equalsIgnoreCase("article")) {
120
                        usageStatsRepository.executeItem(reportItems, itemIdentifier, repoid, "Article", beginDateParsed, endDateParsed, granularity);
121
                        if (reportItems.isEmpty()) {
122
                            reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
123
                        }
124
                    } else {
125
                        reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
126
                    }
127
                } else if (!repoid.equals("")) {
128
                    usageStatsRepository.executeBatchItems(reportItems, repoid, "Article", beginDateParsed, endDateParsed, granularity);
129
                    if (reportItems.isEmpty()) {
130
                        reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
131
                    }
132
                }
133
            }
134
        } else if (reportName.equalsIgnoreCase("br1")) {
135
            if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) {
136
                if (!itemid.equals("")) {
137
                    if (itemDataType.equalsIgnoreCase("") || itemDataType.equalsIgnoreCase("book")) {
138
                        usageStatsRepository.executeItem(reportItems, itemIdentifier, repoid, "Book", beginDateParsed, endDateParsed, granularity);
139
                        if (reportItems.isEmpty()) {
140
                            reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
141
                        }
142
                    } else {
143
                        reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
144
                    }
145
                } else if (!repoid.equals("")) {
146
                    usageStatsRepository.executeBatchItems(reportItems, repoid, "Book", beginDateParsed, endDateParsed, granularity);
147
                    if (reportItems.isEmpty()) {
148
                        reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
149
                    }
150
                }
151
            }
152
        } else if (reportName.equalsIgnoreCase("br2")) {
153
            if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) {
154
                if (!itemid.equals("")) {
155
                    if (itemDataType.equalsIgnoreCase("") || itemDataType.equalsIgnoreCase("part of book or chapter of book")) {
156
                        usageStatsRepository.executeItem(reportItems, itemIdentifier, repoid, "Part of book or chapter of book", beginDateParsed, endDateParsed, granularity);
157
                        if (reportItems.isEmpty()) {
158
                            reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
159
                        }
160
                    } else {
161
                        reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
162
                    }
163
                } else if (!repoid.equals("")) {
164
                    usageStatsRepository.executeBatchItems(reportItems, repoid, "Part of book or chapter of book", beginDateParsed, endDateParsed, granularity);
165
                    if (reportItems.isEmpty()) {
166
                        reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
167
                    }
168
                }
169
            }
170
        } else if (reportName.equalsIgnoreCase("ir1")) {
171
            if (!itemid.equals("-1") && !repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) {
172
                if (!itemid.equals("")) {
173
                    usageStatsRepository.executeItem(reportItems, itemIdentifier, repoid, itemDataType, beginDateParsed, endDateParsed, granularity);
174
                } else if (!repoid.equals("")) {
175
                    usageStatsRepository.executeBatchItems(reportItems, repoid, itemDataType, beginDateParsed, endDateParsed, granularity);
176
                }
177
                if (reportItems.isEmpty()) {
178
                    reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
179
                }
180
            }
181
        } else if (reportName.equalsIgnoreCase("rr1")) {
182
            if (!repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) {
183
                usageStatsRepository.executeRepo(reportItems, repoid, itemDataType, beginDateParsed, endDateParsed, granularity);
184
            }
185
            if (reportItems.isEmpty()) {
186
                reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
187
            }
188
        } else if (reportName.equalsIgnoreCase("jr1")) {
189
            if (!repoid.equals("-1") && beginDateParsed != null && endDateParsed != null && beginDateParsed.before(endDateParsed)) {
190
                usageStatsRepository.executeJournal(reportItems, repoid, itemDataType, beginDateParsed, endDateParsed, granularity);
191
            }
192
            if (reportItems.isEmpty()) {
193
                reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data"));
194
            }
195
        }else if (reportName.equals("")) {
196
            reportExceptions.add(new ReportException("3050", "Error", "Report argument is missing", "You must supply a Report argument"));
197
        } else {
198
            reportExceptions.add(new ReportException("3000", "Error", "Report " + reportName + " not supported", "Supported reports: AR1, IR1, RR1, BR1, BR2"));
199
        }
200

  
201

  
202
        ReportResponse reportResponse = new ReportResponse(reportName, release, requestorId, beginDate, endDate,
203
                repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback, reportItems, reportExceptions);
204

  
205
        return new ReportResponseWrapper(reportResponse);
206
    }
207

  
208
    @Override
209
    public String displayReport(String reportName, String release, String requestorId, String beginDate, String endDate, String repositoryIdentifier, String itemIdentifier, String itemDataType, String hasDoi, String granularity, String callback, String pretty) {
210
        ObjectMapper objectMapper = new ObjectMapper();
211
        try {
212
            if (pretty.equalsIgnoreCase("pretty")) {
213
                return "<pre>" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReport(reportName, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback)).replaceAll("/", "\\\\/") + "</pre>";
214
            }
215
            return objectMapper.writeValueAsString(buildReport(reportName, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback)).replaceAll("/", "\\\\/");
216
        } catch (Exception e) {
217
            e.printStackTrace();
218
            return null;
219
        }
220
        //return report.getReport(reportP, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback, pretty);
221
    }
222

  
223
    private Date tryParse(String dateString) {
224
        try {
225
            if (dateString.length() == 7) {
226
                return new SimpleDateFormat("yyyy-MM").parse(dateString);
227
            } else if (dateString.length() == 10) {
228
                return new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
229
            }
230
        } catch (Exception e) {
231
            log.error("ParseError: ", e);
232
        }
233
        return null;
234
    }
235
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/services/UsageStatsService.java
1
package eu.dnetlib.usagestats.services;
2

  
3
import eu.dnetlib.usagestats.portal.TotalStats;
4
import eu.dnetlib.usagestats.portal.UsageStats;
5

  
6
public interface UsageStatsService {
7
    UsageStats getDatasourceClicks(String id);
8
    UsageStats getProjectClicks(String id);
9
    UsageStats getResultClicks(String id);
10
    //UsageStats getOrganizationClicks(String id);
11
    TotalStats getTotalStats();
12
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/services/UsageStatsServiceImpl.java
1
package eu.dnetlib.usagestats.services;
2

  
3
import eu.dnetlib.usagestats.portal.TotalStats;
4
import eu.dnetlib.usagestats.portal.UsageStats;
5
import eu.dnetlib.usagestats.repositories.UsageStatsRepository;
6
import org.springframework.stereotype.Service;
7

  
8
import java.util.ArrayList;
9
import java.util.List;
10

  
11
@Service
12
public class UsageStatsServiceImpl implements UsageStatsService{
13

  
14
    private final UsageStatsRepository usageStatsRepository;
15

  
16
    public UsageStatsServiceImpl(UsageStatsRepository usageStatsRepository) {
17
        this.usageStatsRepository = usageStatsRepository;
18
    }
19

  
20
    @Override
21
    public UsageStats getDatasourceClicks(String id) {
22
        String query = "SELECT 'views', sum(s.count), sum(s.openaire) FROM views_stats s where s.repository_id=? " +
23
                "UNION ALL SELECT 'downloads', sum(s.count), sum(s.openaire) FROM downloads_stats s where s.repository_id=? " +
24
                "UNION ALL SELECT 'pageviews', sum(s.count), '0' FROM pageviews_stats s, result_datasources rd where rd.id=s.result_id and rd.datasource=? ";
25

  
26
        List<String> values = new ArrayList<>();
27
        values.add(id);
28
        values.add(id);
29
        values.add(id);
30

  
31
        return usageStatsRepository.executeUsageStats(query, values, "datasource");
32
    }
33

  
34
    /*
35
    @Override
36
    public UsageStats getOrganizationClicks(String organizationId) {
37

  
38
        String query = "select sum(number_of_views) from organization_stats where id=?";
39

  
40
        List<String> values = new ArrayList<>();
41
        values.add(organizationId);
42

  
43
        return usageStatsRepository.executeUsageStats(query, values, "organization");
44

  
45
    }
46
    */
47

  
48
    @Override
49
    public UsageStats getProjectClicks(String projectId) {
50
        String query = "SELECT 'views', sum(s.count), sum(s.openaire) FROM views_stats s, project_results pr where pr.result=s.result_id and pr.id=? " +
51
                "UNION ALL SELECT 'downloads', sum(s.count), sum(s.openaire) FROM downloads_stats s, project_results pr where pr.result=s.result_id and pr.id=? " +
52
                "UNION ALL SELECT 'pageviews', sum(s.count), '0' FROM pageviews_stats s, project_results pr where pr.result=s.result_id and pr.id=?;";
53

  
54
        List<String> values = new ArrayList<>();
55
        values.add(projectId);
56
        values.add(projectId);
57
        values.add(projectId);
58

  
59
        return usageStatsRepository.executeUsageStats(query, values, "project");
60
    }
61

  
62
    @Override
63
    public UsageStats getResultClicks(String id) {
64
        String query = "SELECT 'views', s.repository_id, CASE WHEN s.source='OpenAIRE' THEN d.name ELSE d.name ||' - '|| s.source END, sum(count), sum(openaire) FROM views_stats s, datasource d WHERE s.repository_id=d.id AND s.result_id=? GROUP BY s.source, s.repository_id, d.name " +
65
                "UNION ALL SELECT 'downloads', s.repository_id, CASE WHEN s.source='OpenAIRE' THEN d.name ELSE d.name ||' - '|| s.source END, sum(count), sum(s.openaire) FROM downloads_stats s, datasource d WHERE s.repository_id=d.id AND s.result_id=? GROUP BY s.source, s.repository_id, d.name " +
66
                "UNION ALL SELECT 'pageviews', 'OpenAIRE id', 'OpenAIRE', sum(count), '0' FROM pageviews_stats s WHERE result_id=?;";
67

  
68
        List<String> values = new ArrayList<>();
69
        values.add(id);
70
        values.add(id);
71
        values.add(id);
72

  
73
        return usageStatsRepository.executeUsageStats(query, values, "result");
74
    }
75

  
76
    @Override
77
    public TotalStats getTotalStats() {
78
        return usageStatsRepository.executeTotalStats();
79
    }
80
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/config/SpringRedisConfiguration.java
1
package eu.dnetlib.usagestats.config;
2

  
3
import org.apache.log4j.Logger;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.beans.factory.annotation.Value;
6
import org.springframework.context.annotation.Bean;
7
import org.springframework.context.annotation.Configuration;
8
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
9
import org.springframework.data.redis.core.RedisTemplate;
10
import org.springframework.data.redis.serializer.StringRedisSerializer;
11

  
12
/**
13
 * Created by tsampikos on 20/4/2017.
14
 */
15
@Configuration
16
public class SpringRedisConfiguration {
17

  
18
    private final Logger log = Logger.getLogger(this.getClass());
19

  
20
    @Value("${usagestats.redis.hostname}")
21
    private String hostname;
22

  
23
    @Value("${usagestats.redis.port}")
24
    private int port;
25

  
26
    @Bean
27
    public JedisConnectionFactory connectionFactory() {
28
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
29
        connectionFactory.setHostName(hostname);
30
        connectionFactory.setPort(port);
31
        connectionFactory.setUsePool(false);
32
        log.info("Opening redis connection to : " + connectionFactory.getHostName() + ":" + connectionFactory.getPort());
33
        return connectionFactory;
34
    }
35

  
36
    @Bean
37
    @Autowired
38
    public RedisTemplate<String, String> redisTemplate() {
39
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
40
        redisTemplate.setConnectionFactory(connectionFactory());
41
        redisTemplate.setKeySerializer(new StringRedisSerializer());
42
        redisTemplate.setValueSerializer(new StringRedisSerializer());
43
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
44
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
45
        return redisTemplate;
46
    }
47
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/config/DataSourceConfiguration.java
1
package eu.dnetlib.usagestats.config;
2

  
3
//import org.springframework.boot.jdbc.DataSourceBuilder;
4
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
5
import org.springframework.boot.context.properties.ConfigurationProperties;
6
import org.springframework.context.annotation.Bean;
7
import org.springframework.context.annotation.Configuration;
8
import org.springframework.context.annotation.Primary;
9

  
10
import javax.sql.DataSource;
11

  
12
/**
13
 * Created by tsampikos on 8/3/2017.
14
 */
15
@Configuration
16
public class DataSourceConfiguration {
17

  
18
    @ConfigurationProperties(prefix = "usagestats")
19
    @Bean
20
    @Primary
21
    public DataSource getDataSource() {
22
        return DataSourceBuilder
23
                .create()
24
                .build();
25
    }
26
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/config/WebMvcConfiguration.java
1
package eu.dnetlib.usagestats.config;
2

  
3
import org.springframework.context.annotation.Configuration;
4
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
5
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7

  
8
/**
9
 * Created by tsampikos on 12/4/2017.
10
 */
11
@Configuration
12
//public class WebMvcConfiguration implements WebMvcConfigurer {
13
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
14

  
15
    @Override
16
    public void addViewControllers(ViewControllerRegistry registry) {
17
        registry.addViewController("/sushilite").setViewName("redirect:/sushilite/");
18
        registry.addViewController("/sushilite/").setViewName("forward:/sushilite/index.html");
19

  
20
        registry.addViewController("/sushilite/AR1").setViewName("redirect:/sushilite/AR1/");
21
        registry.addViewController("/sushilite/AR1/").setViewName("forward:/sushilite/AR1/index.html");
22

  
23
        registry.addViewController("/sushilite/IR1").setViewName("redirect:/sushilite/IR1/");
24
        registry.addViewController("/sushilite/IR1/").setViewName("forward:/sushilite/IR1/index.html");
25

  
26
        registry.addViewController("/sushilite/RR1").setViewName("redirect:/sushilite/RR1/");
27
        registry.addViewController("/sushilite/RR1/").setViewName("forward:/sushilite/RR1/index.html");
28

  
29
        registry.addViewController("/sushilite/JR1").setViewName("redirect:/sushilite/JR1/");
30
        registry.addViewController("/sushilite/JR1/").setViewName("forward:/sushilite/JR1/index.html");
31

  
32
        registry.addViewController("/sushilite/BR1").setViewName("redirect:/sushilite/BR1/");
33
        registry.addViewController("/sushilite/BR1/").setViewName("forward:/sushilite/BR1/index.html");
34

  
35
        registry.addViewController("/sushilite/BR2").setViewName("redirect:/sushilite/BR2/");
36
        registry.addViewController("/sushilite/BR2/").setViewName("forward:/sushilite/BR2/index.html");
37

  
38
        super.addViewControllers(registry);
39
    }
40
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/controllers/UsageStatsController.java
1
package eu.dnetlib.usagestats.controllers;
2

  
3
import eu.dnetlib.usagestats.portal.TotalStats;
4
import eu.dnetlib.usagestats.portal.UsageStats;
5

  
6
import eu.dnetlib.usagestats.services.UsageStatsService;
7
import org.apache.log4j.Logger;
8
import org.springframework.web.bind.annotation.CrossOrigin;
9
import org.springframework.web.bind.annotation.PathVariable;
10
import org.springframework.web.bind.annotation.RequestMapping;
11
import org.springframework.web.bind.annotation.RequestMethod;
12
import org.springframework.web.bind.annotation.RestController;
13

  
14

  
15
@RestController
16
@CrossOrigin(methods = RequestMethod.GET, origins = "*")
17
public class UsageStatsController {
18

  
19
    private final UsageStatsService usageStatsService;
20

  
21
    private final Logger log = Logger.getLogger(this.getClass());
22

  
23
    public UsageStatsController(UsageStatsService usageStatsService) {
24
        this.usageStatsService = usageStatsService;
25
    }
26

  
27
    @RequestMapping(value = "/datasources/{datasourceId}/clicks")
28
    public UsageStats getDatasourceClicks(@PathVariable(value = "datasourceId") String datasourceId) {
29
        log.info("stats request for datasource: " + datasourceId);
30
        return usageStatsService.getDatasourceClicks(datasourceId);
31
    }
32

  
33
    @RequestMapping(value = "/projects/{projectId}/clicks")
34
    public UsageStats getProjectClicks(@PathVariable(value = "projectId") String projectId) {
35
        log.info("stats request for project: " + projectId);
36
        return usageStatsService.getProjectClicks(projectId);
37
    }
38

  
39
    /*
40
    @RequestMapping(value = "/organizations/{organizationId}/clicks")
41
    public UsageStats getOrganizationClicks(@PathVariable(value = "organizationId") String organizationId) {
42
        log.info("stats request for organization: " + organizationId);
43
        return usageStatsService.getOrganizationClicks(organizationId);
44
    }
45
    */
46

  
47
    @RequestMapping(value = "/results/{resultId}/clicks")
48
    public UsageStats getResultClicks(@PathVariable(value = "resultId") String resultId) {
49
        log.info("stats request for result: " + resultId);
50
        return usageStatsService.getResultClicks(resultId);
51
    }
52
    @RequestMapping(value = "/totals")
53
    public TotalStats getTotalStats() {
54
        log.info("total stats request");
55
        return usageStatsService.getTotalStats();
56
    }
57
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteController.java
1
package eu.dnetlib.usagestats.controllers;
2

  
3
import eu.dnetlib.usagestats.services.SushiLiteService;
4

  
5
import org.apache.log4j.Logger;
6
import org.springframework.web.bind.annotation.RequestMapping;
7
import org.springframework.web.bind.annotation.RequestMethod;
8
import org.springframework.web.bind.annotation.RequestParam;
9
import org.springframework.web.bind.annotation.RestController;
10

  
11
/**
12
 * Created by tsampikos on 10/4/2017.
13
 */
14

  
15
@RestController
16
class SushiLiteController {
17

  
18
    private final Logger log = Logger.getLogger(this.getClass());
19

  
20
    private final SushiLiteService sushiLiteService;
21

  
22
    public SushiLiteController(SushiLiteService sushiLiteService) {
23
        this.sushiLiteService = sushiLiteService;
24
    }
25

  
26
    @RequestMapping(value = "/sushilite/GetReport/", method = RequestMethod.GET)
27
    public String getReport(@RequestParam(value = "Report", defaultValue = "") String reportP, @RequestParam(value = "Release", defaultValue = "4") String release, @RequestParam(value = "RequestorID", defaultValue = "anonymous") String requestorId,
28
                            @RequestParam(value = "BeginDate", defaultValue = "") String beginDate, @RequestParam(value = "EndDate", defaultValue = "") String endDate, @RequestParam(value = "RepositoryIdentifier", defaultValue = "") String repositoryIdentifier,
29
                            @RequestParam(value = "ItemIdentifier", defaultValue = "") String itemIdentifier, @RequestParam(value = "ItemDataType", defaultValue = "") String itemDataType,
30
                            @RequestParam(value = "hasDOI", defaultValue = "") String hasDoi, @RequestParam(value = "Granularity", defaultValue = "Monthly") String granularity, @RequestParam(value = "Callback", defaultValue = "") String callback,
31
                            @RequestParam(value = "Pretty", defaultValue = "") String pretty) {
32
        log.info("Sushi Report request: " + reportP + " from " + requestorId);
33
        log.info("repository identifier: " + repositoryIdentifier + " - item identifier: " + itemIdentifier);
34

  
35
        return sushiLiteService.displayReport(reportP, release, requestorId, beginDate, endDate, repositoryIdentifier, itemIdentifier, itemDataType, hasDoi, granularity, callback, pretty);
36
    }
37
}
modules/dnet-openaire-usage-stats-api/branches/usage-stats-api-compression/trunk/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepository.java
1
package eu.dnetlib.usagestats.repositories;
2

  
3
import org.apache.log4j.Logger;
4
import org.springframework.data.redis.core.HashOperations;
5
import org.springframework.data.redis.core.RedisTemplate;
6
import org.springframework.stereotype.Repository;
7

  
8
import com.fasterxml.jackson.databind.ObjectMapper;
9

  
10
import eu.dnetlib.usagestats.portal.MonthlyStats;
11
import eu.dnetlib.usagestats.portal.RepositoryStats;
12
import eu.dnetlib.usagestats.portal.TotalStats;
13
import eu.dnetlib.usagestats.portal.UsageStats;
14
import eu.dnetlib.usagestats.portal.YearlyStats;
15
import eu.dnetlib.usagestats.sushilite.domain.ItemIdentifier;
16
import eu.dnetlib.usagestats.sushilite.domain.ItemPerformance;
17
import eu.dnetlib.usagestats.sushilite.domain.ReportItem;
18

  
19
import org.apache.commons.dbutils.DbUtils;
20

  
21
import javax.sql.DataSource;
22

  
23
import java.security.MessageDigest;
24
import java.sql.Connection;
25
import java.sql.PreparedStatement;
26
import java.sql.ResultSet;
27
import java.text.SimpleDateFormat;
28
import java.util.ArrayList;
29
import java.util.Calendar;
30
import java.util.Date;
31
import java.util.HashMap;
32
import java.util.List;
33

  
34
@Repository
35
public class UsageStatsRepository {
36

  
37
    private final DataSource usageStatsDB;
38

  
39
    private final HashOperations<String, String, String> jedis;
40

  
41
    private final Logger log = Logger.getLogger(this.getClass());
42

  
43
    public UsageStatsRepository(DataSource usageStatsDB, RedisTemplate<String, String> redisTemplate) {
44
        this.usageStatsDB = usageStatsDB;
45
        this.jedis = redisTemplate.opsForHash();
46
    }
47

  
48
    private static String MD5(String string) throws java.security.NoSuchAlgorithmException {
49
        MessageDigest md = MessageDigest.getInstance("MD5");
50
        md.update(string.getBytes());
51

  
52
        byte byteData[] = md.digest();
53
        StringBuilder sb = new StringBuilder();
54
        for (byte aByteData : byteData) {
55
            sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1));
56
        }
57

  
58
        return sb.toString();
59
    }
60

  
61
    private static String toJson(Object o) throws com.fasterxml.jackson.core.JsonProcessingException {
62
        ObjectMapper objectMapper = new ObjectMapper();
63
        return objectMapper.writeValueAsString(o);
64
    }
65

  
66
    private static UsageStats fromJson(String string) throws java.io.IOException {
67
        ObjectMapper objectMapper = new ObjectMapper();
68
        return objectMapper.readValue(string, UsageStats.class);
69
    }
70

  
71
    /*
72
    private static List<ReportItem> reportItemsFromJson(String string) throws Exception {
73
        ObjectMapper objectMapper = new ObjectMapper();
74
        return objectMapper.readValue(string, objectMapper.getTypeFactory().constructCollectionType(List.class, ReportItem.class));
75
    }
76
    */
77

  
78
    public UsageStats executeUsageStats(String query, List<String> values, String type) {
79

  
80
        UsageStats usageStats = new UsageStats();
81
        int total_views = 0;
82
        int total_downloads = 0;
83
        int page_views = 0;
84
        int openaire_downloads = 0;
85
        int openaire_views = 0;
86
        Connection connection = null;
87
        PreparedStatement st = null;
88
        ResultSet rs = null;
89
        try {
90
            connection = usageStatsDB.getConnection();
91
            st = connection.prepareStatement(query);
92
            int i = 1;
93
            for (String s : values) {
94
                st.setString(i, s);
95
                i++;
96
            }
97

  
98
            String redis_key = MD5(st.toString());
99

  
100
            String redis_result = jedis.get(redis_key, "result");
101
            if (redis_result != null) {
102
                return fromJson(redis_result);
103
            }
104

  
105
            rs = st.executeQuery();
106
            if (type.equals("result")) {
107
                while (rs.next()) {
108
                    if (rs.getString(1).equals("views") && rs.getString(4) != null && !rs.getString(4).equals("") && !rs.getString(4).equals("null")) {
109
                        usageStats.addViews(new RepositoryStats(rs.getString(3), rs.getString(2), rs.getString(4), rs.getString(5)));
110
                        total_views += Integer.parseInt(rs.getString(4));
111
                        openaire_views += Integer.parseInt(rs.getString(5));
112
                    } else if (rs.getString(1).equals("downloads") && rs.getString(4) != null && !rs.getString(4).equals("") && !rs.getString(4).equals("null")) {
113
                        usageStats.addDownloads(new RepositoryStats(rs.getString(3), rs.getString(2), rs.getString(4), "0"));
114
                        total_downloads += Integer.parseInt(rs.getString(4));
115
                        openaire_downloads += Integer.parseInt(rs.getString(5));
116
                    } else if (rs.getString(1).equals("pageviews") && rs.getString(4) != null && !rs.getString(4).equals("") && !rs.getString(4).equals("null")) {
117
                        page_views = Integer.parseInt(rs.getString(4));
118
                    }
119
                }
120
                usageStats.setTotal_views(Integer.toString(total_views));
121
                usageStats.setTotal_downloads(Integer.toString(total_downloads));
122
                usageStats.setPageViews(Integer.toString(page_views));
123
                usageStats.setTotal_openaire_views(Integer.toString(openaire_views));
124
                usageStats.setTotal_openaire_downloads(Integer.toString(openaire_downloads));
125
            } else if (type.equals("project") || type.equals("datasource")) {
126
                while (rs.next()) {
127
                    if (rs.getString(1).equals("views") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) {
128
                        total_views += Integer.parseInt(rs.getString(2));
129
                        openaire_views += Integer.parseInt(rs.getString(3));
130
                    } else if (rs.getString(1).equals("downloads") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) {
131
                        total_downloads += Integer.parseInt(rs.getString(2));
132
                        openaire_downloads += Integer.parseInt(rs.getString(3));
133
                    } else if (rs.getString(1).equals("pageviews") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) {
134
                        page_views = Integer.parseInt(rs.getString(2));
135
                    }
136
                    /*
137
                    else if (rs.getString(1).equals("openaire") && rs.getString(2) != null && !rs.getString(2).equals("") && !rs.getString(2).equals("null")) {
138
                        openaire = Integer.parseInt(rs.getString(2));
139
                    }
140
                    */
141

  
142
                }
143
                usageStats.setTotal_views(Integer.toString(total_views));
144
                usageStats.setTotal_downloads(Integer.toString(total_downloads));
145
                usageStats.setPageViews(Integer.toString(page_views));
146
                usageStats.setTotal_openaire_views(Integer.toString(openaire_views));
147
                usageStats.setTotal_openaire_downloads(Integer.toString(openaire_downloads));
148
            }
149

  
150
            jedis.put(redis_key, "persistent", "false");
151
            jedis.put(redis_key, "query", st.toString());
152
            //jedis.put(redis_key, "result", toString(usageStats));
153
            jedis.put(redis_key, "result", toJson(usageStats));
154
            jedis.put(redis_key, "fetchMode", "3");
155

  
156
        } catch (Exception e) {
157
            log.error("Cannot execute query2 : ", e);
158

  
159
        } finally {
160
            DbUtils.closeQuietly(rs);
161
            DbUtils.closeQuietly(st);
162
            DbUtils.closeQuietly(connection);
163
        }
164
        return usageStats;
165
    }
166

  
167
    public TotalStats executeTotalStats() {
168
        TotalStats totalStats = null;
169
        try {
170
            String redis_result = jedis.get("total_stats", "result");
171
            if (redis_result != null) {
172
                totalStats =  fromJsonTotalStats(redis_result);
173
            } else {
174
                return updateTotalStats();
175
            }
176
        } catch (Exception e) {
177
            log.error("Cannot execute totalStats : ", e);
178
        }
179
        return totalStats;
180
    }
181

  
182
    public TotalStats updateTotalStats() {
183
        TotalStats totalStats = new TotalStats();
184
        Connection connection = null;
185
        PreparedStatement st = null;
186
        ResultSet rs = null;
187
        HashMap<Integer, List<MonthlyStats>> monthlyStatsMap = new HashMap<>();
188

  
189
        try {
190
            connection = usageStatsDB.getConnection();
191
            //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;");
192
            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");
193
            rs = st.executeQuery();
194
            rs.next();
195
            totalStats.setRepositories(rs.getInt(1));
196
            totalStats.setItems(rs.getInt(2));
197
            totalStats.setDownloads(rs.getInt(3));
198
            totalStats.setViews(rs.getInt(4));
199
            rs.close();
200
            st.close();
201

  
202
            //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;");
203
            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;");
204
            rs = st.executeQuery();
205
            while (rs.next()) {
206
                int year = Integer.parseInt(rs.getString(1).substring(0, 4));
207
                int month = Integer.parseInt(rs.getString(1).substring(5));
208
                MonthlyStats monthlyStats = new MonthlyStats();
209
                monthlyStats.setMonth(month);
210
                monthlyStats.setRepositories(rs.getInt(2));
211
                monthlyStats.setItems(rs.getInt(3));
212
                monthlyStats.setDownloads(rs.getInt(4));
213
                monthlyStats.setViews(rs.getInt(5));
214

  
215
                if (monthlyStatsMap.get(year) != null) {
216
                    monthlyStatsMap.get(year).add(monthlyStats);
217
                } else {
218
                    List<MonthlyStats> newList = new ArrayList<>();
219
                    newList.add(monthlyStats);
220
                    monthlyStatsMap.put(year, newList);
221

  
222
                }
223
            }
224
            rs.close();
225
            st.close();
226

  
227
            //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;");
228
            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;");
229
            rs = st.executeQuery();
230
            List<YearlyStats> yearlyStatsList = new ArrayList<>();
231
            while (rs.next()) {
232
                YearlyStats yearlyStats = new YearlyStats();
233
                yearlyStats.setYear(rs.getInt(1));
234
                yearlyStats.setRepositories(rs.getInt(2));
235
                yearlyStats.setItems(rs.getInt(3));
236
                yearlyStats.setDownloads(rs.getInt(4));
237
                yearlyStats.setViews(rs.getInt(5));
238
                yearlyStats.setMonthlyStats(monthlyStatsMap.get(rs.getInt(1)));
239
                yearlyStatsList.add(yearlyStats);
240
            }
241
            totalStats.setYearlyStats(yearlyStatsList);
242
            jedis.put("total_stats", "result", toJson(totalStats));
243
            jedis.put("total_stats", "persistent", "false");
244

  
245
        } catch (Exception e) {
246
            log.error("Cannot execute totalStats : ", e);
247

  
248
        } finally {
249
            DbUtils.closeQuietly(rs);
250
            DbUtils.closeQuietly(st);
251
            DbUtils.closeQuietly(connection);
252
        }
253
        return totalStats;
254
    }
255

  
256
    private static TotalStats fromJsonTotalStats(String string) throws java.io.IOException {
257
        ObjectMapper objectMapper = new ObjectMapper();
258
        return objectMapper.readValue(string, TotalStats.class);
259
    }
260

  
261

  
262
    public String executeRepoId(String repositoryIdentifier, String report) {
263
        PreparedStatement st = null;
264
        Connection connection = null;
265
        ResultSet rs = null;
266
        try {
267
            connection = usageStatsDB.getConnection();
268
            String[] split = repositoryIdentifier.split(":");
269
            String openaire_id = "-1";
270
            switch (split[0].toLowerCase()) {
271
                case "openaire":
272
                    if(!report.equals("jr1")) {
273
                        st = connection.prepareStatement("select id from public.datasource where id=?");
274
                        st.setString(1, repositoryIdentifier.replaceFirst(split[0] + ":", ""));
275
                    } else {
276
                        st = connection.prepareStatement("select id from public.datasource where id=? AND (type='Journal' OR type='Journal Aggregator/Publisher')");
277
                        st.setString(1, repositoryIdentifier.replaceFirst(split[0] + ":", ""));
278
                    }
279

  
280
                    rs = st.executeQuery();
281
                    while (rs.next()) {
282
                        openaire_id = rs.getString(1);
283
                    }
284
                    return openaire_id;
285

  
286
                case "opendoar":
287
                    if(!report.equals("jr1")) {
288
                        st = connection.prepareStatement("select id from public.datasource_oids where orid=?");
289
                        st.setString(1, "opendoar____::" + repositoryIdentifier.replaceFirst(split[0] + ":", ""));
290
                    } else {
291
                        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')");
292
                        st.setString(1, "opendoar____::" + repositoryIdentifier.replaceFirst(split[0] + ":", ""));
293
                    }
294

  
295
                    rs = st.executeQuery();
296
                    while (rs.next()) {
297
                        openaire_id = rs.getString(1);
298
                    }
299
                    return openaire_id;
300
                case "issn":
301
                    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')");
302
                    st.setString(1, "%" + repositoryIdentifier.replaceFirst(split[0] + ":", "") + "%");
303

  
304
                    rs = st.executeQuery();
305
                    while (rs.next()) {
306
                        openaire_id = rs.getString(1);
307
                    }
308
                    return openaire_id;
309
                default:
310
                    return "-1";
311
            }
312
        } catch (Exception e) {
313
            log.error("Repository id failed: ", e);
314
        } finally {
315
            DbUtils.closeQuietly(rs);
316
            DbUtils.closeQuietly(st);
317
            DbUtils.closeQuietly(connection);
318
        }
319
        return "-1";
320
    }
321

  
322
    public void executeItem(List<ReportItem> reportItems, String itemIdentifier, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) {
323
        String[] split = itemIdentifier.split(":");
324
        switch (split[0].toLowerCase()) {
325
            case "oid":
326
                executeOid(reportItems, itemIdentifier.replaceFirst(split[0] + ":", ""), repositoryIdentifier, itemDataType, beginDate, endDate, granularity);
327
                break;
328
            case "doi":
329
                executeDoi(reportItems, itemIdentifier.replaceFirst(split[0] + ":", ""), repositoryIdentifier, itemDataType, beginDate, endDate, granularity);
330
                break;
331
            case "openaire":
332
                executeOpenaire(reportItems, itemIdentifier.replaceFirst(split[0] + ":", ""), repositoryIdentifier, itemDataType, beginDate, endDate, granularity);
333
                break;
334
            default:
335
        }
336
    }
337

  
338
    private void executeOid(List<ReportItem> reportItems, String oid, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) {
339
        Connection connection = null;
340
        PreparedStatement st = null;
341
        ResultSet rs = null;
342
        try {
343
            connection = usageStatsDB.getConnection();
344
            //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=?");
345
            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=?");
346
            st.setString(1, oid);
347
            st.setString(2, oid);
348

  
349
            rs = st.executeQuery();
350

  
351
            while (rs.next()) {
352
                executeOpenaire(reportItems, rs.getString(1), repositoryIdentifier, itemDataType, beginDate, endDate, granularity);
353
            }
354
            connection.close();
355
        } catch (Exception e) {
356
            log.error("Oid to OpenAIRE id failed: ", e);
357
        } finally {
358
            DbUtils.closeQuietly(rs);
359
            DbUtils.closeQuietly(st);
360
            DbUtils.closeQuietly(connection);
361
        }
362
    }
363

  
364
    private void executeDoi(List<ReportItem> reportItems, String doi, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) {
365
        Connection connection = null;
366
        PreparedStatement st = null;
367
        ResultSet rs = null;
368
        try {
369
            connection = usageStatsDB.getConnection();
370
            //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=?");
371
            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=?");
372
            st.setString(1, doi);
373
            st.setString(2, doi);
374

  
375
            rs = st.executeQuery();
376

  
377
            while (rs.next()) {
378
                executeOpenaire(reportItems, rs.getString(1), repositoryIdentifier, itemDataType, beginDate, endDate, granularity);
379
            }
380
        } catch (Exception e) {
381
            log.error("Doi to OpenAIRE id failed: ", e);
382
        } finally {
383
            DbUtils.closeQuietly(rs);
384
            DbUtils.closeQuietly(st);
385
            DbUtils.closeQuietly(connection);
386
        }
387
    }
388

  
389
    private void executeOpenaire(List<ReportItem> reportItems, String openaire, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) {
390
        SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd");
391
        SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM");
392
        String beginDateStr = postgresFormat.format(beginDate);
393
        String endDateStr = postgresFormat.format(endDate);
394

  
395
        Connection connection = null;
396
        PreparedStatement st = null;
397
        ResultSet rs = null;
398

  
399
        /*
400
        Calendar startCalendar = Calendar.getInstance();
401
        startCalendar.setTime(beginDate);
402
        Calendar endCalendar = Calendar.getInstance();
403
        endCalendar.setTime(endDate);
404
        int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
405
        int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
406
        */
407

  
408
        try {
409
            connection = usageStatsDB.getConnection();
410
            if (repositoryIdentifier.equals("")) {
411
                if (itemDataType.equals("")) {
412
                    //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;");
413
                    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;");
414
                    st.setString(1, beginDateStr);
415
                    st.setString(2, endDateStr);
416
                    st.setString(3, openaire);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff