Project

General

Profile

« Previous | Next » 

Revision 51121

Added by Tsampikos Livisianos about 6 years ago

replace old community stats with configurable queries

View differences:

modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/repos/DataSourceBean.java
16 16
@Component
17 17
class DataSourceBean {
18 18

  
19
    @ConfigurationProperties(prefix = "usagestats")
19
    @ConfigurationProperties(prefix = "statsapi")
20 20
    @Bean
21 21
    @Primary
22 22
    public DataSource getDataSource() {
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/repos/BaseRepository.java
6 6
import eu.dnetlib.statsapi.entity.Community;
7 7
import eu.dnetlib.statsapi.entity.Funder;
8 8
import eu.dnetlib.statsapi.entity.Result;
9
import eu.dnetlib.statsapi.entity.StatsByAccessMode;
9 10

  
10 11
import org.apache.commons.dbutils.DbUtils;
11 12
import org.apache.log4j.Logger;
......
14 15
import org.springframework.stereotype.Repository;
15 16

  
16 17
import javax.annotation.PostConstruct;
17
//import javax.sql.DataSource;
18
import javax.sql.DataSource;
18 19

  
19 20
import java.security.MessageDigest;
20
//import java.sql.Connection;
21
//import java.sql.PreparedStatement;
22
//import java.sql.ResultSet;
21
import java.sql.Connection;
22
import java.sql.PreparedStatement;
23
import java.sql.ResultSet;
23 24
import java.util.ArrayList;
24 25
import java.util.List;
25 26

  
26 27

  
27 28
@Repository
28 29
public class BaseRepository {
29
    //@Autowired
30
    //private DataSourceBean dataSourceBean;
30
    @Autowired
31
    private DataSourceBean dataSourceBean;
31 32

  
32
    //private DataSource statsDB;
33
    private DataSource statsDB;
33 34

  
34 35
    @Autowired
35 36
    private SpringRedisConfiguration springRedisConfiguration;
......
42 43

  
43 44
    @PostConstruct
44 45
    public void initDB() {
45
        //statsDB = dataSourceBean.getDataSource();
46
        statsDB = dataSourceBean.getDataSource();
46 47
        jedis = springRedisConfiguration.redisTemplate().opsForHash();
47 48
    }
48 49

  
......
112 113
    }
113 114

  
114 115
    public Result getCommunity(String community){
115
        Boolean not_found = true;
116
        ArrayList<String> items = new ArrayList<>();
117
        items.add(community.toLowerCase() + "pubs");
118
        items.add(community.toLowerCase() + "oa");
119
        items.add(community.toLowerCase() + "emb");
120
        items.add(community.toLowerCase() + "res");
121
        items.add(community.toLowerCase() + "proj");
122
        items.add(community.toLowerCase() + "vo");
123
        items.add(community.toLowerCase() + "datasets");
124
        items.add(community.toLowerCase() + "software");
116
        Connection connection = null;
117
        PreparedStatement st = null;
118
        ResultSet rs = null;
125 119

  
126
        List<String> result = jedis.multiGet("STATS_NUMBERS", items);
127
        int pubs = 0, oa = 0, emb = 0, res = 0, proj = 0, vo = 0, datasets = 0, software =0;
128
        if (result.get(0) != null) {
129
            pubs = Integer.parseInt(result.get(0).replaceAll(",",""));
130
            not_found = false;
131
        }
132
        if (result.get(1) != null) {
133
            oa = Integer.parseInt(result.get(1).replaceAll(",",""));
134
            not_found = false;
135
        }
136
        if (result.get(2) != null) {
137
            emb = Integer.parseInt(result.get(2).replaceAll(",",""));
138
            not_found = false;
139
        }
140
        if (result.get(3) != null) {
141
            res = Integer.parseInt(result.get(3).replaceAll(",",""));
142
            not_found = false;
143
        }
144
        if (result.get(4) != null) {
145
            proj = Integer.parseInt(result.get(4).replaceAll(",",""));
146
            not_found = false;
147
        }
148
        if (result.get(5) != null) {
149
            vo = Integer.parseInt(result.get(5).replaceAll(",",""));
150
            not_found = false;
151
        }
152
        if (result.get(6) != null) {
153
            datasets = Integer.parseInt(result.get(6).replaceAll(",",""));
154
            not_found = false;
155
        }
156
        if (result.get(7) != null) {
157
            software = Integer.parseInt(result.get(7).replaceAll(",",""));
158
            not_found = false;
159
        }
120
        String redisResponse = jedis.get("community:" + community, "result");
121
        try {
122
            if (redisResponse != null) {
123
                return new Result("OK","200", new ObjectMapper().readValue(redisResponse, Community.class));
124
            } else {
125
                connection = statsDB.getConnection();
126
                st = connection.prepareStatement("SELECT r.type, COUNT(DISTINCT r.id) AS total, COUNT(DISTINCT CASE WHEN r.bestlicense='Open Access' THEN r.id END) AS open_access, COUNT(DISTINCT CASE WHEN r.bestlicense='Embargo' THEN r.id END) AS embargo, COUNT(DISTINCT CASE WHEN r.bestlicense='Restricted' THEN r.id END) AS restricted, COUNT(DISTINCT CASE WHEN r.bestlicense='Closed Access' THEN r.id END) AS closed_access FROM result_concepts rc, result r WHERE rc.id=r.id AND (rc.concept=? OR rc.concept LIKE ? || '::%') GROUP BY r.type;");
127
                st.setString(1, community);
128
                st.setString(2, community);
160 129

  
161
        if(not_found){
162
            return new Result("Not Found", "400", null);
130
                StatsByAccessMode publications = new StatsByAccessMode(0,0,0,0,0);
131
                StatsByAccessMode software = new StatsByAccessMode(0,0,0,0,0);
132
                StatsByAccessMode datasets = new StatsByAccessMode(0,0,0,0,0);
133

  
134
                rs = st.executeQuery();
135
                while (rs.next()) {
136
                    if(rs.getString(1).equals("publication")){
137
                        publications = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
138
                    } else if(rs.getString(1).equals("software")){
139
                        software = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
140
                    } else if(rs.getString(1).equals("dataset")){
141
                        datasets = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
142
                    }
143
                }
144
                rs.close();
145
                st.close();
146

  
147
                st = connection.prepareStatement("SELECT COUNT(DISTINCT pr.id) FROM result_concepts rc, project_results pr, project p WHERE p.id=pr.id AND pr.result=rc.id AND (rc.concept=? OR rc.concept LIKE ? || '::%');");
148
                st.setString(1, community);
149
                st.setString(2, community);
150

  
151
                int projects = 0;
152
                rs = st.executeQuery();
153
                while (rs.next()) {
154
                    projects = rs.getInt(1);
155
                }
156
                rs.close();
157
                st.close();
158

  
159
                st = connection.prepareStatement("SELECT COUNT(DISTINCT c.name) FROM result_concepts rc, concept c, category cat WHERE c.id=rc.concept AND cat.id=c.category AND cat.id=? || '::virtual';");
160
                st.setString(1, community);
161

  
162
                int virtual_organizations = 0;
163
                rs = st.executeQuery();
164
                while (rs.next()) {
165
                     virtual_organizations = rs.getInt(1);
166
                }
167

  
168
                Community com = new Community(publications, datasets, software, projects, virtual_organizations);
169
                String redis_key = "community:" + community;
170
                jedis.put(redis_key, "persistent", "false");
171
                jedis.put(redis_key, "result", new ObjectMapper().writeValueAsString(com));
172

  
173
                return new Result("OK", "200", com);
174
            }
175
        } catch (Exception e) {
176
            log.error(e.getMessage());
177
        } finally {
178
            DbUtils.closeQuietly(rs);
179
            DbUtils.closeQuietly(st);
180
            DbUtils.closeQuietly(connection);
163 181
        }
164

  
165
        return new Result("OK", "200", new Community(pubs, oa, emb, res, proj, vo, datasets, software));
182
        return new Result("Not Found", "404", null);
166 183
    }
167 184
}
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/entity/StatsByAccessMode.java
1
package eu.dnetlib.statsapi.entity;
2

  
3
import java.io.Serializable;
4

  
5
public class StatsByAccessMode implements Serializable{
6
    private final static long serialVersionUID = 1;
7
    private int total = 0;
8
    private int open_access = 0;
9
    private int embargo = 0;
10
    private int restricted = 0;
11
    private int closed_access = 0;
12

  
13
    public StatsByAccessMode() {}
14

  
15
    public StatsByAccessMode(int total, int open_access, int embargo, int restricted, int closed_access) {
16
        this.total = total;
17
        this.open_access = open_access;
18
        this.embargo = embargo;
19
        this.restricted = restricted;
20
        this.closed_access = closed_access;
21
    }
22

  
23
    public int getTotal() {
24
        return total;
25
    }
26

  
27
    public void setTotal(int total) {
28
        this.total = total;
29
    }
30

  
31
    public int getOpen_access() {
32
        return open_access;
33
    }
34

  
35
    public void setOpen_access(int open_access) {
36
        this.open_access = open_access;
37
    }
38

  
39
    public int getEmbargo() {
40
        return embargo;
41
    }
42

  
43
    public void setEmbargo(int embargo) {
44
        this.embargo = embargo;
45
    }
46

  
47
    public int getRestricted() {
48
        return restricted;
49
    }
50

  
51
    public void setRestricted(int restricted) {
52
        this.restricted = restricted;
53
    }
54

  
55
    public int getClosed_access() {
56
        return closed_access;
57
    }
58

  
59
    public void setClosed_access(int closed_access) {
60
        this.closed_access = closed_access;
61
    }
62
}
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/entity/Community.java
5 5
public class Community implements Serializable{
6 6
    private final static long serialVersionUID = 1;
7 7

  
8
    private int publications = 0;
9
    private int open_access = 0;
10
    private int embargo = 0;
11
    private int restricted = 0;
8
    private StatsByAccessMode publications;
9
    private StatsByAccessMode datasets;
10
    private StatsByAccessMode software;
12 11

  
13 12
    private int total_projects = 0;
14 13
    private int virtual_organizations = 0;
15 14

  
16
    private int datasets = 0;
17
    private int software = 0;
15
    public Community() {}
18 16

  
19
    public Community(int pubs, int oa, int emb, int res, int proj, int vo, int datasets, int software) {
20
        this.publications = pubs;
21
        this.open_access = oa;
22
        this.embargo = emb;
23
        this.restricted = res;
24
        this.total_projects = proj;
25
        this.virtual_organizations = vo;
17
    public Community(StatsByAccessMode publications, StatsByAccessMode datasets, StatsByAccessMode software, int total_projects, int virtual_organizations) {
18
        this.publications = publications;
26 19
        this.datasets = datasets;
27 20
        this.software = software;
28

  
21
        this.total_projects = total_projects;
22
        this.virtual_organizations = virtual_organizations;
29 23
    }
30 24

  
31
    public int getPublications() {
25
    public StatsByAccessMode getPublications() {
32 26
        return publications;
33 27
    }
34 28

  
35
    public int getOpen_access() {
36
        return open_access;
29
    public void setPublications(StatsByAccessMode publications) {
30
        this.publications = publications;
37 31
    }
38 32

  
39
    public int getEmbargo() {
40
        return embargo;
33
    public StatsByAccessMode getDatasets() {
34
        return datasets;
41 35
    }
42 36

  
43
    public int getRestricted() {
44
        return restricted;
37
    public void setDatasets(StatsByAccessMode datasets) {
38
        this.datasets = datasets;
45 39
    }
46 40

  
41
    public StatsByAccessMode getSoftware() {
42
        return software;
43
    }
44

  
45
    public void setSoftware(StatsByAccessMode software) {
46
        this.software = software;
47
    }
48

  
47 49
    public int getTotal_projects() {
48 50
        return total_projects;
49 51
    }
50 52

  
53
    public void setTotal_projects(int total_projects) {
54
        this.total_projects = total_projects;
55
    }
56

  
51 57
    public int getVirtual_organizations() {
52 58
        return virtual_organizations;
53 59
    }
54 60

  
55
    public int getDatasets() {
56
        return datasets;
61
    public void setVirtual_organizations(int virtual_organizations) {
62
        this.virtual_organizations = virtual_organizations;
57 63
    }
58

  
59
    public int getSoftware() {
60
        return software;
61
    }
62 64
}
modules/dnet-openaire-stats-api/pom.xml
31 31
            </exclusions>
32 32
        </dependency>
33 33

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

  
41 39
        <dependency>
42 40
            <groupId>org.springframework.boot</groupId>
......
60 58
            <version>1.2.17</version>
61 59
        </dependency>
62 60

  
63
        <!--
64 61
        <dependency>
65 62
            <groupId>org.postgresql</groupId>
66 63
            <artifactId>postgresql</artifactId>
67 64
            <scope>runtime</scope>
68 65
        </dependency>
69
        -->
70 66

  
71 67
        <dependency>
72 68
            <groupId>junit</groupId>

Also available in: Unified diff