Revision 51837
Added by Tsampikos Livisianos about 6 years ago
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/repos/BaseRepository.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.repos; |
|
2 |
|
|
3 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
4 |
|
|
5 |
|
|
6 |
import eu.dnetlib.statsapi.entity.Community; |
|
7 |
import eu.dnetlib.statsapi.entity.Funder; |
|
8 |
import eu.dnetlib.statsapi.entity.Result; |
|
9 |
import eu.dnetlib.statsapi.entity.StatsByAccessMode; |
|
10 |
|
|
11 |
import org.apache.commons.dbutils.DbUtils; |
|
12 |
import org.apache.log4j.Logger; |
|
13 |
import org.springframework.beans.factory.annotation.Autowired; |
|
14 |
import org.springframework.data.redis.core.HashOperations; |
|
15 |
import org.springframework.data.redis.core.RedisTemplate; |
|
16 |
import org.springframework.stereotype.Repository; |
|
17 |
|
|
18 |
import javax.annotation.PostConstruct; |
|
19 |
import javax.sql.DataSource; |
|
20 |
|
|
21 |
import java.security.MessageDigest; |
|
22 |
import java.sql.Connection; |
|
23 |
import java.sql.PreparedStatement; |
|
24 |
import java.sql.ResultSet; |
|
25 |
import java.util.ArrayList; |
|
26 |
import java.util.List; |
|
27 |
import java.util.Set; |
|
28 |
|
|
29 |
@Repository |
|
30 |
public class BaseRepository { |
|
31 |
@Autowired |
|
32 |
private DataSourceBean dataSourceBean; |
|
33 |
|
|
34 |
private DataSource statsDB; |
|
35 |
|
|
36 |
@Autowired |
|
37 |
private SpringRedisConfiguration springRedisConfiguration; |
|
38 |
|
|
39 |
//private RedisTemplate redisTemplate; |
|
40 |
|
|
41 |
private HashOperations<String, String, String> jedis; |
|
42 |
|
|
43 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
44 |
|
|
45 |
@PostConstruct |
|
46 |
public void initDB() { |
|
47 |
statsDB = dataSourceBean.getDataSource(); |
|
48 |
jedis = springRedisConfiguration.redisTemplate().opsForHash(); |
|
49 |
} |
|
50 |
|
|
51 |
private static String MD5(String string) throws java.security.NoSuchAlgorithmException { |
|
52 |
MessageDigest md = MessageDigest.getInstance("MD5"); |
|
53 |
md.update(string.getBytes()); |
|
54 |
|
|
55 |
byte byteData[] = md.digest(); |
|
56 |
StringBuilder sb = new StringBuilder(); |
|
57 |
for (byte aByteData : byteData) { |
|
58 |
sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1)); |
|
59 |
} |
|
60 |
|
|
61 |
return sb.toString(); |
|
62 |
} |
|
63 |
|
|
64 |
public Result getNumber(String number){ |
|
65 |
String redis_result = jedis.get("STATS_NUMBERS",number); |
|
66 |
if(redis_result != null){ |
|
67 |
return new Result("OK", "200", Integer.parseInt(redis_result.replaceAll(",",""))); |
|
68 |
} |
|
69 |
return new Result("Not Found", "400", null); |
|
70 |
} |
|
71 |
|
|
72 |
public Result getFunder(String funder){ |
|
73 |
Boolean not_found = true; |
|
74 |
ArrayList<String> items = new ArrayList<>(); |
|
75 |
items.add(funder.toLowerCase() + "pubstotal"); |
|
76 |
items.add(funder.toLowerCase() + "oapubs"); |
|
77 |
items.add(funder.toLowerCase() + "embpubs"); |
|
78 |
items.add(funder.toLowerCase() + "respubs"); |
|
79 |
items.add(funder.toLowerCase() + "projtotal"); |
|
80 |
items.add(funder.toLowerCase() + "projpubs"); |
|
81 |
|
|
82 |
List<String> result = jedis.multiGet("STATS_NUMBERS", items); |
|
83 |
int pubs = 0, oa = 0, emb = 0, res = 0, proj = 0, proj_pubs = 0; |
|
84 |
if (result.get(0) != null) { |
|
85 |
pubs = Integer.parseInt(result.get(0).replaceAll(",","")); |
|
86 |
not_found = false; |
|
87 |
} |
|
88 |
if (result.get(1) != null) { |
|
89 |
oa = Integer.parseInt(result.get(1).replaceAll(",","")); |
|
90 |
not_found = false; |
|
91 |
} |
|
92 |
if (result.get(2) != null) { |
|
93 |
emb = Integer.parseInt(result.get(2).replaceAll(",","")); |
|
94 |
not_found = false; |
|
95 |
} |
|
96 |
if (result.get(3) != null) { |
|
97 |
res = Integer.parseInt(result.get(3).replaceAll(",","")); |
|
98 |
not_found = false; |
|
99 |
} |
|
100 |
if (result.get(4) != null) { |
|
101 |
proj = Integer.parseInt(result.get(4).replaceAll(",","")); |
|
102 |
not_found = false; |
|
103 |
} |
|
104 |
if (result.get(5) != null) { |
|
105 |
proj_pubs = Integer.parseInt(result.get(5).replaceAll(",","")); |
|
106 |
not_found = false; |
|
107 |
} |
|
108 |
|
|
109 |
if(not_found){ |
|
110 |
return new Result("Not Found", "400", null); |
|
111 |
} |
|
112 |
|
|
113 |
return new Result("OK", "200", new Funder(pubs, oa, emb, res, proj, proj_pubs)); |
|
114 |
} |
|
115 |
|
|
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){ |
|
127 |
Connection connection = null; |
|
128 |
PreparedStatement st = null; |
|
129 |
ResultSet rs = null; |
|
130 |
|
|
131 |
String redisKey = "community:" + community_id; |
|
132 |
String redisResponse = jedis.get(redisKey, "result"); |
|
133 |
try { |
|
134 |
if (redisResponse != null) { |
|
135 |
return new Result("OK","200", new ObjectMapper().readValue(redisResponse, Community.class)); |
|
136 |
} else { |
|
137 |
connection = statsDB.getConnection(); |
|
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;"); |
|
139 |
st.setString(1, community_id); |
|
140 |
st.setString(2, community_id); |
|
141 |
|
|
142 |
StatsByAccessMode publications = new StatsByAccessMode(); |
|
143 |
StatsByAccessMode software = new StatsByAccessMode(); |
|
144 |
StatsByAccessMode datasets = new StatsByAccessMode(); |
|
145 |
|
|
146 |
rs = st.executeQuery(); |
|
147 |
while (rs.next()) { |
|
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; |
|
158 |
} |
|
159 |
} |
|
160 |
rs.close(); |
|
161 |
st.close(); |
|
162 |
|
|
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); |
|
166 |
|
|
167 |
int projects = 0; |
|
168 |
rs = st.executeQuery(); |
|
169 |
while (rs.next()) { |
|
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 |
} |
|
178 |
} |
|
179 |
rs.close(); |
|
180 |
st.close(); |
|
181 |
|
|
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';"); |
|
183 |
st.setString(1, community_id); |
|
184 |
|
|
185 |
int virtual_organizations = 0; |
|
186 |
rs = st.executeQuery(); |
|
187 |
while (rs.next()) { |
|
188 |
virtual_organizations = rs.getInt(1); |
|
189 |
} |
|
190 |
|
|
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)); |
|
194 |
|
|
195 |
return new Result("OK", "200", community); |
|
196 |
} |
|
197 |
} catch (Exception e) { |
|
198 |
log.error(e.getMessage()); |
|
199 |
} finally { |
|
200 |
DbUtils.closeQuietly(rs); |
|
201 |
DbUtils.closeQuietly(st); |
|
202 |
DbUtils.closeQuietly(connection); |
|
203 |
} |
|
204 |
return new Result("Not Found", "404", null); |
|
205 |
} |
|
206 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/repos/DataSourceBean.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.repos; |
|
2 |
|
|
3 |
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; |
|
4 |
import org.springframework.boot.context.properties.ConfigurationProperties; |
|
5 |
import org.springframework.context.annotation.Bean; |
|
6 |
import org.springframework.context.annotation.Configuration; |
|
7 |
import org.springframework.context.annotation.Primary; |
|
8 |
import org.springframework.stereotype.Component; |
|
9 |
|
|
10 |
import javax.sql.DataSource; |
|
11 |
|
|
12 |
/** |
|
13 |
* Created by tsampikos on 8/3/2017. |
|
14 |
*/ |
|
15 |
@Configuration |
|
16 |
@Component |
|
17 |
class DataSourceBean { |
|
18 |
|
|
19 |
@ConfigurationProperties(prefix = "statsapi") |
|
20 |
@Bean |
|
21 |
@Primary |
|
22 |
public DataSource getDataSource() { |
|
23 |
return DataSourceBuilder |
|
24 |
.create() |
|
25 |
.build(); |
|
26 |
} |
|
27 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/repos/SpringRedisConfiguration.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.repos; |
|
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.boot.context.properties.ConfigurationProperties; |
|
7 |
import org.springframework.context.annotation.Bean; |
|
8 |
import org.springframework.context.annotation.Configuration; |
|
9 |
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; |
|
10 |
import org.springframework.data.redis.core.RedisTemplate; |
|
11 |
import org.springframework.data.redis.serializer.StringRedisSerializer; |
|
12 |
import org.springframework.stereotype.Component; |
|
13 |
|
|
14 |
/** |
|
15 |
* Created by tsampikos on 20/4/2017. |
|
16 |
*/ |
|
17 |
@Component |
|
18 |
@Configuration |
|
19 |
@ConfigurationProperties(prefix = "statsapi.redis") |
|
20 |
class SpringRedisConfiguration { |
|
21 |
|
|
22 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
23 |
|
|
24 |
@Value("${statsapi.redis.hostname}") |
|
25 |
private String hostname; |
|
26 |
|
|
27 |
@Value("${statsapi.redis.port}") |
|
28 |
private int port; |
|
29 |
|
|
30 |
@Bean |
|
31 |
public JedisConnectionFactory connectionFactory() { |
|
32 |
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(); |
|
33 |
connectionFactory.setHostName(hostname); |
|
34 |
connectionFactory.setPort(port); |
|
35 |
log.info("Opening redis connection to : " + connectionFactory.getHostName() + ":" + connectionFactory.getPort()); |
|
36 |
return connectionFactory; |
|
37 |
} |
|
38 |
|
|
39 |
@Bean |
|
40 |
@Autowired |
|
41 |
public RedisTemplate<String, String> redisTemplate() { |
|
42 |
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); |
|
43 |
redisTemplate.setConnectionFactory(connectionFactory()); |
|
44 |
redisTemplate.setKeySerializer(new StringRedisSerializer()); |
|
45 |
redisTemplate.setValueSerializer(new StringRedisSerializer()); |
|
46 |
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); |
|
47 |
redisTemplate.setHashValueSerializer(new StringRedisSerializer()); |
|
48 |
return redisTemplate; |
|
49 |
} |
|
50 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/statsApi.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi; |
|
2 |
|
|
3 |
import org.springframework.boot.SpringApplication; |
|
4 |
import org.springframework.boot.autoconfigure.SpringBootApplication; |
|
5 |
import org.springframework.boot.web.support.SpringBootServletInitializer; |
|
6 |
import org.springframework.context.annotation.PropertySource; |
|
7 |
import org.springframework.context.annotation.PropertySources; |
|
8 |
import org.springframework.web.bind.annotation.CrossOrigin; |
|
9 |
import org.springframework.web.bind.annotation.RequestMethod; |
|
10 |
|
|
11 |
@PropertySources({ |
|
12 |
@PropertySource("classpath:statsAPI.properties"), |
|
13 |
@PropertySource("classpath:dnet-override.properties"), |
|
14 |
@PropertySource("classpath:log4j.properties")} |
|
15 |
) |
|
16 |
@SpringBootApplication |
|
17 |
@CrossOrigin(methods = RequestMethod.GET, origins = "*") |
|
18 |
public class statsApi extends SpringBootServletInitializer { |
|
19 |
|
|
20 |
public static void main(String[] args) { |
|
21 |
SpringApplication.run(statsApi.class, args); |
|
22 |
|
|
23 |
} |
|
24 |
} |
|
25 |
|
|
26 |
|
|
27 |
|
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/entity/Result.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.entity; |
|
2 |
|
|
3 |
import com.fasterxml.jackson.annotation.JsonInclude; |
|
4 |
|
|
5 |
import java.io.Serializable; |
|
6 |
|
|
7 |
public class Result implements Serializable{ |
|
8 |
private final static long serialVersionUID = 1; |
|
9 |
|
|
10 |
private final String status; |
|
11 |
private final String code; |
|
12 |
|
|
13 |
private final Object statistics; |
|
14 |
|
|
15 |
public Result(String status, String code, Object statistics){ |
|
16 |
this.status = status; |
|
17 |
this.code = code; |
|
18 |
this.statistics = statistics; |
|
19 |
} |
|
20 |
|
|
21 |
public String getStatus() { |
|
22 |
return status; |
|
23 |
} |
|
24 |
|
|
25 |
public String getCode() { |
|
26 |
return code; |
|
27 |
} |
|
28 |
|
|
29 |
@JsonInclude(JsonInclude.Include.NON_NULL) |
|
30 |
public Object getStatistics() { |
|
31 |
return statistics; |
|
32 |
} |
|
33 |
} |
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 |
private int projects = 0; |
|
13 |
|
|
14 |
public StatsByAccessMode() {} |
|
15 |
|
|
16 |
public StatsByAccessMode(int total, int open_access, int embargo, int restricted, int closed_access) { |
|
17 |
this.total = total; |
|
18 |
this.open_access = open_access; |
|
19 |
this.embargo = embargo; |
|
20 |
this.restricted = restricted; |
|
21 |
this.closed_access = closed_access; |
|
22 |
} |
|
23 |
|
|
24 |
public int getTotal() { |
|
25 |
return total; |
|
26 |
} |
|
27 |
|
|
28 |
public void setTotal(int total) { |
|
29 |
this.total = total; |
|
30 |
} |
|
31 |
|
|
32 |
public int getOpen_access() { |
|
33 |
return open_access; |
|
34 |
} |
|
35 |
|
|
36 |
public void setOpen_access(int open_access) { |
|
37 |
this.open_access = open_access; |
|
38 |
} |
|
39 |
|
|
40 |
public int getEmbargo() { |
|
41 |
return embargo; |
|
42 |
} |
|
43 |
|
|
44 |
public void setEmbargo(int embargo) { |
|
45 |
this.embargo = embargo; |
|
46 |
} |
|
47 |
|
|
48 |
public int getRestricted() { |
|
49 |
return restricted; |
|
50 |
} |
|
51 |
|
|
52 |
public void setRestricted(int restricted) { |
|
53 |
this.restricted = restricted; |
|
54 |
} |
|
55 |
|
|
56 |
public int getClosed_access() { |
|
57 |
return closed_access; |
|
58 |
} |
|
59 |
|
|
60 |
public void setClosed_access(int closed_access) { |
|
61 |
this.closed_access = closed_access; |
|
62 |
} |
|
63 |
|
|
64 |
public int getProjects() { |
|
65 |
return projects; |
|
66 |
} |
|
67 |
|
|
68 |
public void setProjects(int projects) { |
|
69 |
this.projects = projects; |
|
70 |
} |
|
71 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/entity/Funder.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.entity; |
|
2 |
|
|
3 |
import java.io.Serializable; |
|
4 |
|
|
5 |
public class Funder implements Serializable{ |
|
6 |
private final static long serialVersionUID = 1; |
|
7 |
|
|
8 |
private int publications = 0; |
|
9 |
private int open_access = 0; |
|
10 |
private int embargo = 0; |
|
11 |
private int restricted = 0; |
|
12 |
|
|
13 |
private int total_projects = 0; |
|
14 |
private int projects_with_publications = 0; |
|
15 |
|
|
16 |
public Funder() { |
|
17 |
} |
|
18 |
|
|
19 |
public Funder(int pubs, int oa, int emb, int res, int proj, int proj_pubs){ |
|
20 |
this.publications = pubs; |
|
21 |
this.open_access = oa; |
|
22 |
this.embargo = emb; |
|
23 |
this.restricted = res; |
|
24 |
this.total_projects = proj; |
|
25 |
this.projects_with_publications = proj_pubs; |
|
26 |
} |
|
27 |
|
|
28 |
public int getPublications() { |
|
29 |
return publications; |
|
30 |
} |
|
31 |
|
|
32 |
public int getOpen_access() { |
|
33 |
return open_access; |
|
34 |
} |
|
35 |
|
|
36 |
public int getEmbargo() { |
|
37 |
return embargo; |
|
38 |
} |
|
39 |
|
|
40 |
public int getRestricted() { |
|
41 |
return restricted; |
|
42 |
} |
|
43 |
|
|
44 |
public int getTotal_projects() { |
|
45 |
return total_projects; |
|
46 |
} |
|
47 |
|
|
48 |
public int getProjects_with_publications() { |
|
49 |
return projects_with_publications; |
|
50 |
} |
|
51 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/entity/Community.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.entity; |
|
2 |
|
|
3 |
import java.io.Serializable; |
|
4 |
|
|
5 |
public class Community implements Serializable{ |
|
6 |
private final static long serialVersionUID = 1; |
|
7 |
|
|
8 |
private StatsByAccessMode publication; |
|
9 |
private StatsByAccessMode dataset; |
|
10 |
private StatsByAccessMode software; |
|
11 |
|
|
12 |
private int total_projects = 0; |
|
13 |
private int virtual_organizations = 0; |
|
14 |
|
|
15 |
public Community() {} |
|
16 |
|
|
17 |
public Community(StatsByAccessMode publication, StatsByAccessMode datasets, StatsByAccessMode software, int total_projects, int virtual_organizations) { |
|
18 |
this.publication = publication; |
|
19 |
this.dataset = datasets; |
|
20 |
this.software = software; |
|
21 |
this.total_projects = total_projects; |
|
22 |
this.virtual_organizations = virtual_organizations; |
|
23 |
} |
|
24 |
|
|
25 |
public StatsByAccessMode getPublication() { |
|
26 |
return publication; |
|
27 |
} |
|
28 |
|
|
29 |
public void setPublication(StatsByAccessMode publication) { |
|
30 |
this.publication = publication; |
|
31 |
} |
|
32 |
|
|
33 |
public StatsByAccessMode getDataset() { |
|
34 |
return dataset; |
|
35 |
} |
|
36 |
|
|
37 |
public void setDataset(StatsByAccessMode dataset) { |
|
38 |
this.dataset = dataset; |
|
39 |
} |
|
40 |
|
|
41 |
public StatsByAccessMode getSoftware() { |
|
42 |
return software; |
|
43 |
} |
|
44 |
|
|
45 |
public void setSoftware(StatsByAccessMode software) { |
|
46 |
this.software = software; |
|
47 |
} |
|
48 |
|
|
49 |
public int getTotal_projects() { |
|
50 |
return total_projects; |
|
51 |
} |
|
52 |
|
|
53 |
public void setTotal_projects(int total_projects) { |
|
54 |
this.total_projects = total_projects; |
|
55 |
} |
|
56 |
|
|
57 |
public int getVirtual_organizations() { |
|
58 |
return virtual_organizations; |
|
59 |
} |
|
60 |
|
|
61 |
public void setVirtual_organizations(int virtual_organizations) { |
|
62 |
this.virtual_organizations = virtual_organizations; |
|
63 |
} |
|
64 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/Application.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi; |
|
2 |
|
|
3 |
import org.springframework.boot.SpringApplication; |
|
4 |
import org.springframework.boot.autoconfigure.SpringBootApplication; |
|
5 |
import org.springframework.boot.web.support.SpringBootServletInitializer; |
|
6 |
import org.springframework.context.annotation.PropertySource; |
|
7 |
import org.springframework.context.annotation.PropertySources; |
|
8 |
import org.springframework.web.bind.annotation.CrossOrigin; |
|
9 |
import org.springframework.web.bind.annotation.RequestMethod; |
|
10 |
|
|
11 |
@PropertySources({ |
|
12 |
@PropertySource("classpath:dnet-override.properties"), |
|
13 |
@PropertySource("classpath:log4j.properties")} |
|
14 |
) |
|
15 |
@SpringBootApplication |
|
16 |
@CrossOrigin(methods = RequestMethod.GET, origins = "*") |
|
17 |
public class Application extends SpringBootServletInitializer { |
|
18 |
|
|
19 |
public static void main(String[] args) { |
|
20 |
SpringApplication.run(Application.class, args); |
|
21 |
|
|
22 |
} |
|
23 |
} |
|
24 |
|
|
25 |
|
|
26 |
|
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/config/DataSourceConfiguration.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.config; |
|
2 |
|
|
3 |
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; |
|
4 |
import org.springframework.boot.context.properties.ConfigurationProperties; |
|
5 |
import org.springframework.context.annotation.Bean; |
|
6 |
import org.springframework.context.annotation.Configuration; |
|
7 |
import org.springframework.context.annotation.Primary; |
|
8 |
|
|
9 |
import javax.sql.DataSource; |
|
10 |
|
|
11 |
/** |
|
12 |
* Created by tsampikos on 8/3/2017. |
|
13 |
*/ |
|
14 |
@Configuration |
|
15 |
public class DataSourceConfiguration { |
|
16 |
|
|
17 |
@ConfigurationProperties(prefix = "statsapi.db") |
|
18 |
@Bean |
|
19 |
@Primary |
|
20 |
public DataSource getDataSource() { |
|
21 |
return DataSourceBuilder |
|
22 |
.create() |
|
23 |
.build(); |
|
24 |
} |
|
25 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/config/RedisConfiguration.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.config; |
|
2 |
|
|
3 |
import org.apache.log4j.Logger; |
|
4 |
import org.springframework.beans.factory.annotation.Value; |
|
5 |
import org.springframework.context.annotation.Bean; |
|
6 |
import org.springframework.context.annotation.Configuration; |
|
7 |
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; |
|
8 |
import org.springframework.data.redis.core.RedisTemplate; |
|
9 |
import org.springframework.data.redis.serializer.StringRedisSerializer; |
|
10 |
|
|
11 |
/** |
|
12 |
* Created by tsampikos on 20/4/2017. |
|
13 |
*/ |
|
14 |
@Configuration |
|
15 |
public class RedisConfiguration { |
|
16 |
|
|
17 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
18 |
|
|
19 |
@Value("${statsapi.redis.hostname}") |
|
20 |
private String hostname; |
|
21 |
|
|
22 |
@Value("${statsapi.redis.port}") |
|
23 |
private int port; |
|
24 |
|
|
25 |
@Bean |
|
26 |
public JedisConnectionFactory connectionFactory() { |
|
27 |
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(); |
|
28 |
connectionFactory.setHostName(hostname); |
|
29 |
connectionFactory.setPort(port); |
|
30 |
log.info("Opening redis connection to : " + connectionFactory.getHostName() + ":" + connectionFactory.getPort()); |
|
31 |
return connectionFactory; |
|
32 |
} |
|
33 |
|
|
34 |
@Bean |
|
35 |
public RedisTemplate<String, String> redisTemplate() { |
|
36 |
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); |
|
37 |
redisTemplate.setConnectionFactory(connectionFactory()); |
|
38 |
redisTemplate.setKeySerializer(new StringRedisSerializer()); |
|
39 |
redisTemplate.setValueSerializer(new StringRedisSerializer()); |
|
40 |
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); |
|
41 |
redisTemplate.setHashValueSerializer(new StringRedisSerializer()); |
|
42 |
return redisTemplate; |
|
43 |
} |
|
44 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/controllers/StatsController.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.statsapi.controllers; |
2 | 2 |
|
3 |
import eu.dnetlib.statsapi.entity.Result;
|
|
4 |
import eu.dnetlib.statsapi.repos.BaseRepository;
|
|
3 |
import eu.dnetlib.statsapi.domain.Result;
|
|
4 |
import eu.dnetlib.statsapi.repositories.StatsRepository;
|
|
5 | 5 |
|
6 | 6 |
import org.apache.log4j.Logger; |
7 | 7 |
import org.springframework.beans.factory.annotation.Autowired; |
... | ... | |
16 | 16 |
@CrossOrigin(methods = RequestMethod.GET, origins = "*") |
17 | 17 |
public class StatsController { |
18 | 18 |
@Autowired |
19 |
private BaseRepository repository;
|
|
19 |
private StatsRepository repository;
|
|
20 | 20 |
|
21 | 21 |
private final Logger log = Logger.getLogger(this.getClass()); |
22 | 22 |
|
23 | 23 |
@RequestMapping(value = "/numbers/{number}") |
24 |
public Result getNumber(@PathVariable(value="number") String number) {
|
|
24 |
public Result getNumber(@PathVariable(value = "number") String number) {
|
|
25 | 25 |
log.info("request for number: " + number); |
26 | 26 |
return repository.getNumber(number); |
27 | 27 |
} |
28 | 28 |
|
29 |
@RequestMapping(value = "/funders/{funder}") |
|
30 |
public Result getFunder(@PathVariable(value="funder") String funder) { |
|
31 |
log.info("request for funder: " + funder); |
|
32 |
return repository.getFunder(funder); |
|
33 |
} |
|
34 |
|
|
35 |
@RequestMapping(value = "/communities/{community}") |
|
36 |
public Result getCommunity(@PathVariable(value="community") String community) { |
|
37 |
log.info("request for community: " + community); |
|
38 |
return repository.getCommunity(community); |
|
39 |
} |
|
40 |
|
|
41 |
@RequestMapping(value = "/communities/refresh") |
|
42 |
public Result refreshCommunities() { |
|
43 |
log.info("refreshing communities"); |
|
44 |
return repository.refreshCommunities(); |
|
45 |
} |
|
46 |
|
|
47 | 29 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/controllers/FunderController.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.controllers; |
|
2 |
|
|
3 |
import eu.dnetlib.statsapi.domain.Result; |
|
4 |
import eu.dnetlib.statsapi.repositories.FunderRepository; |
|
5 |
|
|
6 |
import org.apache.log4j.Logger; |
|
7 |
import org.springframework.beans.factory.annotation.Autowired; |
|
8 |
import org.springframework.web.bind.annotation.PathVariable; |
|
9 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
10 |
import org.springframework.web.bind.annotation.RestController; |
|
11 |
|
|
12 |
@RestController |
|
13 |
public class FunderController { |
|
14 |
|
|
15 |
@Autowired |
|
16 |
private FunderRepository funderRepository; |
|
17 |
|
|
18 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
19 |
|
|
20 |
@RequestMapping(value = "/funders/{funder}") |
|
21 |
public Result getFunder(@PathVariable(value = "funder") String funder) { |
|
22 |
log.info("request for funder: " + funder); |
|
23 |
return funderRepository.getFunder(funder); |
|
24 |
} |
|
25 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/controllers/CommunityController.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.controllers; |
|
2 |
|
|
3 |
import eu.dnetlib.statsapi.domain.Result; |
|
4 |
import eu.dnetlib.statsapi.repositories.CommunityRepository; |
|
5 |
|
|
6 |
import org.apache.log4j.Logger; |
|
7 |
import org.springframework.beans.factory.annotation.Autowired; |
|
8 |
import org.springframework.web.bind.annotation.PathVariable; |
|
9 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
10 |
import org.springframework.web.bind.annotation.RestController; |
|
11 |
|
|
12 |
@RestController |
|
13 |
public class CommunityController { |
|
14 |
@Autowired |
|
15 |
private CommunityRepository communityRepository; |
|
16 |
|
|
17 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
18 |
|
|
19 |
@RequestMapping(value = "/communities/{community}") |
|
20 |
public Result getCommunity(@PathVariable(value = "community") String community) { |
|
21 |
log.info("request for community: " + community); |
|
22 |
return communityRepository.getCommunity(community); |
|
23 |
} |
|
24 |
|
|
25 |
@RequestMapping(value = "/communities/refresh") |
|
26 |
public Result refreshCommunities() { |
|
27 |
log.info("refreshing communities"); |
|
28 |
return communityRepository.refreshCommunities(); |
|
29 |
} |
|
30 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/domain/StatsByAccessMode.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.domain; |
|
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 |
private int projects = 0; |
|
13 |
|
|
14 |
public StatsByAccessMode() { |
|
15 |
} |
|
16 |
|
|
17 |
public StatsByAccessMode(int total, int open_access, int embargo, int restricted, int closed_access) { |
|
18 |
this.total = total; |
|
19 |
this.open_access = open_access; |
|
20 |
this.embargo = embargo; |
|
21 |
this.restricted = restricted; |
|
22 |
this.closed_access = closed_access; |
|
23 |
} |
|
24 |
|
|
25 |
public int getTotal() { |
|
26 |
return total; |
|
27 |
} |
|
28 |
|
|
29 |
public void setTotal(int total) { |
|
30 |
this.total = total; |
|
31 |
} |
|
32 |
|
|
33 |
public int getOpen_access() { |
|
34 |
return open_access; |
|
35 |
} |
|
36 |
|
|
37 |
public void setOpen_access(int open_access) { |
|
38 |
this.open_access = open_access; |
|
39 |
} |
|
40 |
|
|
41 |
public int getEmbargo() { |
|
42 |
return embargo; |
|
43 |
} |
|
44 |
|
|
45 |
public void setEmbargo(int embargo) { |
|
46 |
this.embargo = embargo; |
|
47 |
} |
|
48 |
|
|
49 |
public int getRestricted() { |
|
50 |
return restricted; |
|
51 |
} |
|
52 |
|
|
53 |
public void setRestricted(int restricted) { |
|
54 |
this.restricted = restricted; |
|
55 |
} |
|
56 |
|
|
57 |
public int getClosed_access() { |
|
58 |
return closed_access; |
|
59 |
} |
|
60 |
|
|
61 |
public void setClosed_access(int closed_access) { |
|
62 |
this.closed_access = closed_access; |
|
63 |
} |
|
64 |
|
|
65 |
public int getProjects() { |
|
66 |
return projects; |
|
67 |
} |
|
68 |
|
|
69 |
public void setProjects(int projects) { |
|
70 |
this.projects = projects; |
|
71 |
} |
|
72 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/domain/Funder.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.domain; |
|
2 |
|
|
3 |
import java.io.Serializable; |
|
4 |
|
|
5 |
public class Funder implements Serializable { |
|
6 |
private final static long serialVersionUID = 1; |
|
7 |
|
|
8 |
private int publications = 0; |
|
9 |
private int open_access = 0; |
|
10 |
private int embargo = 0; |
|
11 |
private int restricted = 0; |
|
12 |
|
|
13 |
private int total_projects = 0; |
|
14 |
private int projects_with_publications = 0; |
|
15 |
|
|
16 |
public Funder() { |
|
17 |
} |
|
18 |
|
|
19 |
public Funder(int pubs, int oa, int emb, int res, int proj, int proj_pubs) { |
|
20 |
this.publications = pubs; |
|
21 |
this.open_access = oa; |
|
22 |
this.embargo = emb; |
|
23 |
this.restricted = res; |
|
24 |
this.total_projects = proj; |
|
25 |
this.projects_with_publications = proj_pubs; |
|
26 |
} |
|
27 |
|
|
28 |
public int getPublications() { |
|
29 |
return publications; |
|
30 |
} |
|
31 |
|
|
32 |
public int getOpen_access() { |
|
33 |
return open_access; |
|
34 |
} |
|
35 |
|
|
36 |
public int getEmbargo() { |
|
37 |
return embargo; |
|
38 |
} |
|
39 |
|
|
40 |
public int getRestricted() { |
|
41 |
return restricted; |
|
42 |
} |
|
43 |
|
|
44 |
public int getTotal_projects() { |
|
45 |
return total_projects; |
|
46 |
} |
|
47 |
|
|
48 |
public int getProjects_with_publications() { |
|
49 |
return projects_with_publications; |
|
50 |
} |
|
51 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/domain/Community.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.domain; |
|
2 |
|
|
3 |
import java.io.Serializable; |
|
4 |
|
|
5 |
public class Community implements Serializable { |
|
6 |
private final static long serialVersionUID = 1; |
|
7 |
|
|
8 |
private StatsByAccessMode publication; |
|
9 |
private StatsByAccessMode dataset; |
|
10 |
private StatsByAccessMode software; |
|
11 |
|
|
12 |
private int total_projects = 0; |
|
13 |
private int virtual_organizations = 0; |
|
14 |
|
|
15 |
public Community() { |
|
16 |
} |
|
17 |
|
|
18 |
public Community(StatsByAccessMode publication, StatsByAccessMode datasets, StatsByAccessMode software, int total_projects, int virtual_organizations) { |
|
19 |
this.publication = publication; |
|
20 |
this.dataset = datasets; |
|
21 |
this.software = software; |
|
22 |
this.total_projects = total_projects; |
|
23 |
this.virtual_organizations = virtual_organizations; |
|
24 |
} |
|
25 |
|
|
26 |
public StatsByAccessMode getPublication() { |
|
27 |
return publication; |
|
28 |
} |
|
29 |
|
|
30 |
public void setPublication(StatsByAccessMode publication) { |
|
31 |
this.publication = publication; |
|
32 |
} |
|
33 |
|
|
34 |
public StatsByAccessMode getDataset() { |
|
35 |
return dataset; |
|
36 |
} |
|
37 |
|
|
38 |
public void setDataset(StatsByAccessMode dataset) { |
|
39 |
this.dataset = dataset; |
|
40 |
} |
|
41 |
|
|
42 |
public StatsByAccessMode getSoftware() { |
|
43 |
return software; |
|
44 |
} |
|
45 |
|
|
46 |
public void setSoftware(StatsByAccessMode software) { |
|
47 |
this.software = software; |
|
48 |
} |
|
49 |
|
|
50 |
public int getTotal_projects() { |
|
51 |
return total_projects; |
|
52 |
} |
|
53 |
|
|
54 |
public void setTotal_projects(int total_projects) { |
|
55 |
this.total_projects = total_projects; |
|
56 |
} |
|
57 |
|
|
58 |
public int getVirtual_organizations() { |
|
59 |
return virtual_organizations; |
|
60 |
} |
|
61 |
|
|
62 |
public void setVirtual_organizations(int virtual_organizations) { |
|
63 |
this.virtual_organizations = virtual_organizations; |
|
64 |
} |
|
65 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/domain/Result.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.domain; |
|
2 |
|
|
3 |
import com.fasterxml.jackson.annotation.JsonInclude; |
|
4 |
|
|
5 |
import java.io.Serializable; |
|
6 |
|
|
7 |
public class Result implements Serializable { |
|
8 |
private final static long serialVersionUID = 1; |
|
9 |
|
|
10 |
private final String status; |
|
11 |
private final String code; |
|
12 |
|
|
13 |
private final Object statistics; |
|
14 |
|
|
15 |
public Result(String status, String code, Object statistics) { |
|
16 |
this.status = status; |
|
17 |
this.code = code; |
|
18 |
this.statistics = statistics; |
|
19 |
} |
|
20 |
|
|
21 |
public String getStatus() { |
|
22 |
return status; |
|
23 |
} |
|
24 |
|
|
25 |
public String getCode() { |
|
26 |
return code; |
|
27 |
} |
|
28 |
|
|
29 |
@JsonInclude(JsonInclude.Include.NON_NULL) |
|
30 |
public Object getStatistics() { |
|
31 |
return statistics; |
|
32 |
} |
|
33 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/repositories/FunderRepository.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.repositories; |
|
2 |
|
|
3 |
import eu.dnetlib.statsapi.domain.Funder; |
|
4 |
import eu.dnetlib.statsapi.domain.Result; |
|
5 |
|
|
6 |
import org.apache.log4j.Logger; |
|
7 |
import org.springframework.data.redis.core.HashOperations; |
|
8 |
import org.springframework.data.redis.core.RedisTemplate; |
|
9 |
import org.springframework.stereotype.Repository; |
|
10 |
|
|
11 |
import java.util.ArrayList; |
|
12 |
import java.util.List; |
|
13 |
|
|
14 |
@Repository |
|
15 |
public class FunderRepository { |
|
16 |
|
|
17 |
private RedisTemplate<String, String> redisTemplate; |
|
18 |
|
|
19 |
private HashOperations<String, String, String> jedis; |
|
20 |
|
|
21 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
22 |
|
|
23 |
public FunderRepository(RedisTemplate<String, String> redisTemplate) { |
|
24 |
this.redisTemplate = redisTemplate; |
|
25 |
this.jedis = this.redisTemplate.opsForHash(); |
|
26 |
} |
|
27 |
|
|
28 |
public Result getFunder(String funder) { |
|
29 |
Boolean not_found = true; |
|
30 |
ArrayList<String> items = new ArrayList<>(); |
|
31 |
items.add(funder.toLowerCase() + "pubstotal"); |
|
32 |
items.add(funder.toLowerCase() + "oapubs"); |
|
33 |
items.add(funder.toLowerCase() + "embpubs"); |
|
34 |
items.add(funder.toLowerCase() + "respubs"); |
|
35 |
items.add(funder.toLowerCase() + "projtotal"); |
|
36 |
items.add(funder.toLowerCase() + "projpubs"); |
|
37 |
|
|
38 |
List<String> result = jedis.multiGet("STATS_NUMBERS", items); |
|
39 |
int pubs = 0, oa = 0, emb = 0, res = 0, proj = 0, proj_pubs = 0; |
|
40 |
if (result.get(0) != null) { |
|
41 |
pubs = Integer.parseInt(result.get(0).replaceAll(",", "")); |
|
42 |
not_found = false; |
|
43 |
} |
|
44 |
if (result.get(1) != null) { |
|
45 |
oa = Integer.parseInt(result.get(1).replaceAll(",", "")); |
|
46 |
not_found = false; |
|
47 |
} |
|
48 |
if (result.get(2) != null) { |
|
49 |
emb = Integer.parseInt(result.get(2).replaceAll(",", "")); |
|
50 |
not_found = false; |
|
51 |
} |
|
52 |
if (result.get(3) != null) { |
|
53 |
res = Integer.parseInt(result.get(3).replaceAll(",", "")); |
|
54 |
not_found = false; |
|
55 |
} |
|
56 |
if (result.get(4) != null) { |
|
57 |
proj = Integer.parseInt(result.get(4).replaceAll(",", "")); |
|
58 |
not_found = false; |
|
59 |
} |
|
60 |
if (result.get(5) != null) { |
|
61 |
proj_pubs = Integer.parseInt(result.get(5).replaceAll(",", "")); |
|
62 |
not_found = false; |
|
63 |
} |
|
64 |
|
|
65 |
if (not_found) { |
|
66 |
return new Result("Not Found", "400", null); |
|
67 |
} |
|
68 |
|
|
69 |
return new Result("OK", "200", new Funder(pubs, oa, emb, res, proj, proj_pubs)); |
|
70 |
} |
|
71 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/repositories/CommunityRepository.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.repositories; |
|
2 |
|
|
3 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
4 |
|
|
5 |
import eu.dnetlib.statsapi.domain.Community; |
|
6 |
import eu.dnetlib.statsapi.domain.Result; |
|
7 |
import eu.dnetlib.statsapi.domain.StatsByAccessMode; |
|
8 |
|
|
9 |
import org.apache.commons.dbutils.DbUtils; |
|
10 |
import org.apache.log4j.Logger; |
|
11 |
import org.springframework.data.redis.core.HashOperations; |
|
12 |
import org.springframework.data.redis.core.RedisTemplate; |
|
13 |
import org.springframework.stereotype.Repository; |
|
14 |
|
|
15 |
import java.sql.Connection; |
|
16 |
import java.sql.PreparedStatement; |
|
17 |
import java.sql.ResultSet; |
|
18 |
import java.util.Set; |
|
19 |
|
|
20 |
import javax.sql.DataSource; |
|
21 |
|
|
22 |
@Repository |
|
23 |
public class CommunityRepository { |
|
24 |
|
|
25 |
private DataSource statsDB; |
|
26 |
|
|
27 |
private RedisTemplate<String, String> redisTemplate; |
|
28 |
|
|
29 |
private HashOperations<String, String, String> jedis; |
|
30 |
|
|
31 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
32 |
|
|
33 |
public CommunityRepository(DataSource statsDB, RedisTemplate<String, String> redisTemplate) { |
|
34 |
this.statsDB = statsDB; |
|
35 |
this.redisTemplate = redisTemplate; |
|
36 |
this.jedis = this.redisTemplate.opsForHash(); |
|
37 |
} |
|
38 |
|
|
39 |
public Result refreshCommunities() { |
|
40 |
RedisTemplate<String, String> jd = redisTemplate; |
|
41 |
Set<String> keys = jd.keys("community:*"); |
|
42 |
for (String key : keys) { |
|
43 |
jd.delete(key); |
|
44 |
getCommunity(key.substring(key.lastIndexOf(":") + 1)); |
|
45 |
} |
|
46 |
return new Result("OK", "200", null); |
|
47 |
} |
|
48 |
|
|
49 |
public Result getCommunity(String community_id) { |
|
50 |
Connection connection = null; |
|
51 |
PreparedStatement st = null; |
|
52 |
ResultSet rs = null; |
|
53 |
|
|
54 |
String redisKey = "community:" + community_id; |
|
55 |
String redisResponse = jedis.get(redisKey, "result"); |
|
56 |
try { |
|
57 |
if (redisResponse != null) { |
|
58 |
return new Result("OK", "200", new ObjectMapper().readValue(redisResponse, Community.class)); |
|
59 |
} else { |
|
60 |
connection = statsDB.getConnection(); |
|
61 |
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;"); |
|
62 |
st.setString(1, community_id); |
|
63 |
st.setString(2, community_id); |
|
64 |
|
|
65 |
StatsByAccessMode publications = new StatsByAccessMode(); |
|
66 |
StatsByAccessMode software = new StatsByAccessMode(); |
|
67 |
StatsByAccessMode datasets = new StatsByAccessMode(); |
|
68 |
|
|
69 |
rs = st.executeQuery(); |
|
70 |
while (rs.next()) { |
|
71 |
switch (rs.getString(1)) { |
|
72 |
case "publication": |
|
73 |
publications = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6)); |
|
74 |
break; |
|
75 |
case "software": |
|
76 |
software = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6)); |
|
77 |
break; |
|
78 |
case "dataset": |
|
79 |
datasets = new StatsByAccessMode(rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6)); |
|
80 |
break; |
|
81 |
} |
|
82 |
} |
|
83 |
rs.close(); |
|
84 |
st.close(); |
|
85 |
|
|
86 |
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;"); |
|
87 |
st.setString(1, community_id); |
|
88 |
st.setString(2, community_id); |
|
89 |
|
|
90 |
int projects = 0; |
|
91 |
rs = st.executeQuery(); |
|
92 |
while (rs.next()) { |
|
93 |
projects += rs.getInt(2); |
|
94 |
switch (rs.getString(1)) { |
|
95 |
case "publication": |
|
96 |
publications.setProjects(rs.getInt(2)); |
|
97 |
break; |
|
98 |
case "software": |
|
99 |
software.setProjects(rs.getInt(2)); |
|
100 |
break; |
|
101 |
case "dataset": |
|
102 |
datasets.setProjects(rs.getInt(2)); |
|
103 |
break; |
|
104 |
} |
|
105 |
} |
|
106 |
rs.close(); |
|
107 |
st.close(); |
|
108 |
|
|
109 |
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';"); |
|
110 |
st.setString(1, community_id); |
|
111 |
|
|
112 |
int virtual_organizations = 0; |
|
113 |
rs = st.executeQuery(); |
|
114 |
while (rs.next()) { |
|
115 |
virtual_organizations = rs.getInt(1); |
|
116 |
} |
|
117 |
|
|
118 |
Community community = new Community(publications, datasets, software, projects, virtual_organizations); |
|
119 |
jedis.put(redisKey, "persistent", "false"); |
|
120 |
jedis.put(redisKey, "result", new ObjectMapper().writeValueAsString(community)); |
|
121 |
|
|
122 |
return new Result("OK", "200", community); |
|
123 |
} |
|
124 |
} catch (Exception e) { |
|
125 |
log.error(e.getMessage()); |
|
126 |
} finally { |
|
127 |
DbUtils.closeQuietly(rs); |
|
128 |
DbUtils.closeQuietly(st); |
|
129 |
DbUtils.closeQuietly(connection); |
|
130 |
} |
|
131 |
return new Result("Not Found", "404", null); |
|
132 |
} |
|
133 |
} |
modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/repositories/StatsRepository.java | ||
---|---|---|
1 |
package eu.dnetlib.statsapi.repositories; |
|
2 |
|
|
3 |
import eu.dnetlib.statsapi.domain.Result; |
|
4 |
|
|
5 |
import org.apache.log4j.Logger; |
|
6 |
import org.springframework.data.redis.core.HashOperations; |
|
7 |
import org.springframework.data.redis.core.RedisTemplate; |
|
8 |
import org.springframework.stereotype.Repository; |
|
9 |
|
|
10 |
import java.security.MessageDigest; |
|
11 |
|
|
12 |
@Repository |
|
13 |
public class StatsRepository { |
|
14 |
|
|
15 |
private RedisTemplate<String, String> redisTemplate; |
|
16 |
|
|
17 |
private HashOperations<String, String, String> jedis; |
|
18 |
|
|
19 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
20 |
|
|
21 |
public StatsRepository(RedisTemplate<String, String> redisTemplate) { |
|
22 |
this.redisTemplate = redisTemplate; |
|
23 |
this.jedis = this.redisTemplate.opsForHash(); |
|
24 |
} |
|
25 |
|
|
26 |
private static String MD5(String string) throws java.security.NoSuchAlgorithmException { |
|
27 |
MessageDigest md = MessageDigest.getInstance("MD5"); |
|
28 |
md.update(string.getBytes()); |
|
29 |
|
|
30 |
byte byteData[] = md.digest(); |
|
31 |
StringBuilder sb = new StringBuilder(); |
|
32 |
for (byte aByteData : byteData) { |
|
33 |
sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1)); |
|
34 |
} |
|
35 |
|
|
36 |
return sb.toString(); |
|
37 |
} |
|
38 |
|
|
39 |
public Result getNumber(String number) { |
|
40 |
String redis_result = jedis.get("STATS_NUMBERS", number); |
|
41 |
if (redis_result != null) { |
|
42 |
return new Result("OK", "200", Integer.parseInt(redis_result.replaceAll(",", ""))); |
|
43 |
} |
|
44 |
return new Result("Not Found", "400", null); |
|
45 |
} |
|
46 |
} |
modules/dnet-openaire-stats-api/src/main/resources/statsAPI.properties | ||
---|---|---|
1 |
#spring.jpa.database=POSTGRESQL |
|
2 |
#spring.datasource.platform=postgres |
|
3 |
#spring.jpa.show-sql=true |
|
4 |
#spring.jpa.hibernate.ddl-auto=validate |
|
5 |
name=statsAPI |
|
6 |
#logging.config=log4j.properties |
|
7 |
#spring.database.driverClassName=org.postgresql.Driver |
|
8 |
#spring.datasource.url=jdbc:postgresql://vatopedi.di.uoa.gr:5432/stats |
|
9 |
#spring.datasource.username=sqoop |
|
10 |
#spring.datasource.password=sqoop |
|
11 |
#statsapi.driverClassName=org.postgresql.Driver |
|
12 |
#statsapi.url=jdbc:postgresql://localhost:5432/stats |
|
13 |
#statsapi.username=sqoop |
|
14 |
#statsapi.password=sqoop |
|
15 |
#server.port=8080 |
|
16 |
|
|
17 |
#spring.jackson.serialization.INDENT_OUTPUT=true |
Also available in: Unified diff
refactor everything