Project

General

Profile

« Previous | Next » 

Revision 51326

Added by Tsampikos Livisianos about 6 years ago

add project count to each result type

View differences:

modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/controllers/StatsController.java
38 38
        return repository.getCommunity(community);
39 39
    }
40 40

  
41
    @RequestMapping(value = "/communities/refresh")
42
    public Result refreshCommunities() {
43
        log.info("refreshing communities");
44
        return repository.refreshCommunities();
45
    }
46

  
41 47
}
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/repos/BaseRepository.java
12 12
import org.apache.log4j.Logger;
13 13
import org.springframework.beans.factory.annotation.Autowired;
14 14
import org.springframework.data.redis.core.HashOperations;
15
import org.springframework.data.redis.core.RedisTemplate;
15 16
import org.springframework.stereotype.Repository;
16 17

  
17 18
import javax.annotation.PostConstruct;
......
23 24
import java.sql.ResultSet;
24 25
import java.util.ArrayList;
25 26
import java.util.List;
27
import java.util.Set;
26 28

  
27

  
28 29
@Repository
29 30
public class BaseRepository {
30 31
    @Autowired
......
112 113
        return new Result("OK", "200", new Funder(pubs, oa, emb, res, proj, proj_pubs));
113 114
    }
114 115

  
115
    public Result getCommunity(String community){
116
    public Result refreshCommunities() {
117
        RedisTemplate<String, String> jd = springRedisConfiguration.redisTemplate();
118
        Set<String> keys = jd.keys("community:*");
119
        for(String key : keys) {
120
            jd.delete(key);
121
            getCommunity(key.substring(key.lastIndexOf(":")+1));
122
        }
123
        return new Result("OK", "200", null);
124
    }
125

  
126
    public Result getCommunity(String community_id){
116 127
        Connection connection = null;
117 128
        PreparedStatement st = null;
118 129
        ResultSet rs = null;
119 130

  
120
        String redisResponse = jedis.get("community:" + community, "result");
131
        String redisKey = "community:" + community_id;
132
        String redisResponse = jedis.get(redisKey, "result");
121 133
        try {
122 134
            if (redisResponse != null) {
123 135
                return new Result("OK","200", new ObjectMapper().readValue(redisResponse, Community.class));
124 136
            } else {
125 137
                connection = statsDB.getConnection();
126 138
                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);
139
                st.setString(1, community_id);
140
                st.setString(2, community_id);
129 141

  
130 142
                StatsByAccessMode publications = new StatsByAccessMode();
131 143
                StatsByAccessMode software = new StatsByAccessMode();
......
133 145

  
134 146
                rs = st.executeQuery();
135 147
                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));
148
                    switch (rs.getString(1)) {
149
                        case "publication":
150
                            publications = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
151
                            break;
152
                        case "software":
153
                            software = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
154
                            break;
155
                        case "dataset":
156
                            datasets = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6));
157
                            break;
142 158
                    }
143 159
                }
144 160
                rs.close();
145 161
                st.close();
146 162

  
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);
163
                st = connection.prepareStatement("SELECT r.type, COUNT(DISTINCT pr.id) FROM result r, result_concepts rc, project_results pr WHERE r.id=rc.id AND r.id=pr.result AND (rc.concept=? OR rc.concept LIKE ? || '::%') GROUP BY r.type;");
164
                st.setString(1, community_id);
165
                st.setString(2, community_id);
150 166

  
151 167
                int projects = 0;
152 168
                rs = st.executeQuery();
153 169
                while (rs.next()) {
154
                    projects = rs.getInt(1);
170
                    projects += rs.getInt(2);
171
                    if(rs.getString(1).equals("publication")) {
172
                        publications.setProjects(rs.getInt(2));
173
                    } else if(rs.getString(1).equals("software")){
174
                        software.setProjects(rs.getInt(2));
175
                    } else if(rs.getString(1).equals("dataset")){
176
                        datasets.setProjects(rs.getInt(2));
177
                    }
155 178
                }
156 179
                rs.close();
157 180
                st.close();
158 181

  
159 182
                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);
183
                st.setString(1, community_id);
161 184

  
162 185
                int virtual_organizations = 0;
163 186
                rs = st.executeQuery();
......
165 188
                     virtual_organizations = rs.getInt(1);
166 189
                }
167 190

  
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));
191
                Community community = new Community(publications, datasets, software, projects, virtual_organizations);
192
                jedis.put(redisKey, "persistent", "false");
193
                jedis.put(redisKey, "result", new ObjectMapper().writeValueAsString(community));
172 194

  
173
                return new Result("OK", "200", com);
195
                return new Result("OK", "200", community);
174 196
            }
175 197
        } catch (Exception e) {
176 198
            log.error(e.getMessage());
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/entity/StatsByAccessMode.java
9 9
    private int embargo = 0;
10 10
    private int restricted = 0;
11 11
    private int closed_access = 0;
12
    private int projects = 0;
12 13

  
13 14
    public StatsByAccessMode() {}
14 15

  
......
59 60
    public void setClosed_access(int closed_access) {
60 61
        this.closed_access = closed_access;
61 62
    }
63

  
64
    public int getProjects() {
65
        return projects;
66
    }
67

  
68
    public void setProjects(int projects) {
69
        this.projects = projects;
70
    }
62 71
}
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 StatsByAccessMode publications;
9
    private StatsByAccessMode datasets;
8
    private StatsByAccessMode publication;
9
    private StatsByAccessMode dataset;
10 10
    private StatsByAccessMode software;
11 11

  
12 12
    private int total_projects = 0;
......
14 14

  
15 15
    public Community() {}
16 16

  
17
    public Community(StatsByAccessMode publications, StatsByAccessMode datasets, StatsByAccessMode software, int total_projects, int virtual_organizations) {
18
        this.publications = publications;
19
        this.datasets = datasets;
17
    public Community(StatsByAccessMode publication, StatsByAccessMode datasets, StatsByAccessMode software, int total_projects, int virtual_organizations) {
18
        this.publication = publication;
19
        this.dataset = datasets;
20 20
        this.software = software;
21 21
        this.total_projects = total_projects;
22 22
        this.virtual_organizations = virtual_organizations;
23 23
    }
24 24

  
25
    public StatsByAccessMode getPublications() {
26
        return publications;
25
    public StatsByAccessMode getPublication() {
26
        return publication;
27 27
    }
28 28

  
29
    public void setPublications(StatsByAccessMode publications) {
30
        this.publications = publications;
29
    public void setPublication(StatsByAccessMode publication) {
30
        this.publication = publication;
31 31
    }
32 32

  
33
    public StatsByAccessMode getDatasets() {
34
        return datasets;
33
    public StatsByAccessMode getDataset() {
34
        return dataset;
35 35
    }
36 36

  
37
    public void setDatasets(StatsByAccessMode datasets) {
38
        this.datasets = datasets;
37
    public void setDataset(StatsByAccessMode dataset) {
38
        this.dataset = dataset;
39 39
    }
40 40

  
41 41
    public StatsByAccessMode getSoftware() {

Also available in: Unified diff