Project

General

Profile

« Previous | Next » 

Revision 51106

Added by Tsampikos Livisianos about 4 years ago

add initial code

View differences:

modules/dnet-openaire-stats-api/src/main/java/eu/dnetlib/statsapi/controllers/StatsController.java
1
package eu.dnetlib.statsapi.controllers;
2

  
3
import eu.dnetlib.statsapi.entity.Funder;
4
import eu.dnetlib.statsapi.entity.Result;
5
import eu.dnetlib.statsapi.repos.BaseRepository;
6

  
7
import org.apache.log4j.Logger;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.web.bind.annotation.CrossOrigin;
10
import org.springframework.web.bind.annotation.PathVariable;
11
import org.springframework.web.bind.annotation.RequestMapping;
12
import org.springframework.web.bind.annotation.RequestMethod;
13
import org.springframework.web.bind.annotation.RestController;
14

  
15

  
16
@RestController
17
@CrossOrigin(methods = RequestMethod.GET, origins = "*")
18
public class StatsController {
19
    @Autowired
20
    private BaseRepository repository;
21

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

  
24
    @RequestMapping(value = "/numbers/{number}")
25
    public Result getNumber(@PathVariable(value="number") String number) {
26
        log.info("request for number: " + number);
27
        return repository.getNumber(number);
28
    }
29

  
30
    @RequestMapping(value = "/funders/{funder}")
31
    public Result getFunder(@PathVariable(value="funder") String funder) {
32
        log.info("request for funder: " + funder);
33
        return repository.getFunder(funder);
34
    }
35

  
36
    @RequestMapping(value = "/communities/{community}")
37
    public Result getCommunity(@PathVariable(value="community") String community) {
38
        log.info("request for community: " + community);
39
        return repository.getCommunity(community);
40
    }
41

  
42
}
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 = "usagestats")
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
    String hostname;
26

  
27
    @Value("${statsapi.redis.port}")
28
    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/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

  
10
import org.apache.commons.dbutils.DbUtils;
11
import org.apache.log4j.Logger;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.data.redis.core.HashOperations;
14
import org.springframework.stereotype.Repository;
15

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

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

  
26

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

  
32
    //private DataSource statsDB;
33

  
34
    @Autowired
35
    private SpringRedisConfiguration springRedisConfiguration;
36

  
37
    //private RedisTemplate redisTemplate;
38

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

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

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

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

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

  
59
        return sb.toString();
60
    }
61

  
62
    public Result getNumber(String number){
63
        String redis_result = jedis.get("STATS_NUMBERS",number);
64
        if(redis_result != null){
65
            return new Result("OK", "200", Integer.parseInt(redis_result.replaceAll(",","")));
66
        }
67
        return new Result("Not Found", "400", null);
68
    }
69

  
70
    public Result getFunder(String funder){
71
        Boolean not_found = true;
72
        ArrayList<String> items = new ArrayList<>();
73
        items.add(funder.toLowerCase() + "pubstotal");
74
        items.add(funder.toLowerCase() + "oapubs");
75
        items.add(funder.toLowerCase() + "embpubs");
76
        items.add(funder.toLowerCase() + "respubs");
77
        items.add(funder.toLowerCase() + "projtotal");
78
        items.add(funder.toLowerCase() + "projpubs");
79

  
80
        List<String> result = jedis.multiGet("STATS_NUMBERS", items);
81
        int pubs = 0, oa = 0, emb = 0, res = 0, proj = 0, proj_pubs = 0;
82
        if (result.get(0) != null) {
83
            pubs = Integer.parseInt(result.get(0).replaceAll(",",""));
84
            not_found = false;
85
        }
86
        if (result.get(1) != null) {
87
            oa = Integer.parseInt(result.get(1).replaceAll(",",""));
88
            not_found = false;
89
        }
90
        if (result.get(2) != null) {
91
            emb = Integer.parseInt(result.get(2).replaceAll(",",""));
92
            not_found = false;
93
        }
94
        if (result.get(3) != null) {
95
            res = Integer.parseInt(result.get(3).replaceAll(",",""));
96
            not_found = false;
97
        }
98
        if (result.get(4) != null) {
99
            proj = Integer.parseInt(result.get(4).replaceAll(",",""));
100
            not_found = false;
101
        }
102
        if (result.get(5) != null) {
103
            proj_pubs = Integer.parseInt(result.get(5).replaceAll(",",""));
104
            not_found = false;
105
        }
106

  
107
        if(not_found){
108
            return new Result("Not Found", "400", null);
109
        }
110

  
111
        return new Result("OK", "200", new Funder(pubs, oa, emb, res, proj, proj_pubs));
112
    }
113

  
114
    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");
125

  
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
        }
160

  
161
        if(not_found){
162
            return new Result("Not Found", "400", null);
163
        }
164

  
165
        return new Result("OK", "200", new Community(pubs, oa, emb, res, proj, vo, datasets, software));
166
    }
167
}
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/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(int pubs, int oa, int emb, int res, int proj, int proj_pubs){
17
        this.publications = pubs;
18
        this.open_access = oa;
19
        this.embargo = emb;
20
        this.restricted = res;
21
        this.total_projects = proj;
22
        this.projects_with_publications = proj_pubs;
23
    }
24

  
25
    public int getPublications() {
26
        return publications;
27
    }
28

  
29
    public int getOpen_access() {
30
        return open_access;
31
    }
32

  
33
    public int getEmbargo() {
34
        return embargo;
35
    }
36

  
37
    public int getRestricted() {
38
        return restricted;
39
    }
40

  
41
    public int getTotal_projects() {
42
        return total_projects;
43
    }
44

  
45
    public int getProjects_with_publications() {
46
        return projects_with_publications;
47
    }
48
}
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 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 virtual_organizations = 0;
15

  
16
    private int datasets = 0;
17
    private int software = 0;
18

  
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;
26
        this.datasets = datasets;
27
        this.software = software;
28

  
29
    }
30

  
31
    public int getPublications() {
32
        return publications;
33
    }
34

  
35
    public int getOpen_access() {
36
        return open_access;
37
    }
38

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

  
43
    public int getRestricted() {
44
        return restricted;
45
    }
46

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

  
51
    public int getVirtual_organizations() {
52
        return virtual_organizations;
53
    }
54

  
55
    public int getDatasets() {
56
        return datasets;
57
    }
58

  
59
    public int getSoftware() {
60
        return software;
61
    }
62
}
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 String status;
11
    private String code;
12

  
13
    private 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/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
modules/dnet-openaire-stats-api/src/main/resources/static/error/404.html
1
<html>
2
404
3
</html>
modules/dnet-openaire-stats-api/src/main/resources/log4j.properties
1
log4j.rootLogger = WARN, R
2

  
3
log4j.logger.eu.dnetlib = WARN
4
log4j.logger.org.springframework = INFO, S
5

  
6
log4j.additivity.org.springframework = false
7

  
8
log4j.appender.R=org.apache.log4j.RollingFileAppender
9
log4j.appender.R.File=/var/log/dnet/statsAPI/statsAPI.log
10
log4j.appender.R.MaxFileSize=10MB
11
log4j.appender.R.MaxBackupIndex=10
12
log4j.appender.R.layout=org.apache.log4j.PatternLayout
13
log4j.appender.R.layout.ConversionPattern= %d %p %t [%c] - %m%n
14

  
15
log4j.appender.S=org.apache.log4j.RollingFileAppender
16
log4j.appender.S.File=/var/log/dnet/statsAPI/statsAPI-spring.log
17
log4j.appender.S.MaxFileSize=10MB
18
log4j.appender.S.MaxBackupIndex=10
19
log4j.appender.S.layout=org.apache.log4j.PatternLayout
20
log4j.appender.S.layout.ConversionPattern= %d %p %t [%c] - %m%n
modules/dnet-openaire-stats-api/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-stats-api</artifactId>
8
    <version>1.0.0-SNAPSHOT</version>
9
    <packaging>war</packaging>
10
    <parent>
11
        <!--
12
                <groupId>eu.dnetlib</groupId>
13
                <artifactId>dnet-parent</artifactId>
14
                <version>1.0.0</version>
15
        -->
16
        <groupId>org.springframework.boot</groupId>
17
        <artifactId>spring-boot-starter-parent</artifactId>
18
        <version>1.5.4.RELEASE</version>
19
    </parent>
20

  
21
    <dependencies>
22

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

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

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

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

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

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

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

  
71
        <dependency>
72
            <groupId>junit</groupId>
73
            <artifactId>junit</artifactId>
74
            <version>3.8.1</version>
75
            <scope>test</scope>
76
        </dependency>
77

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

  
84
        <dependency>
85
            <groupId>commons-dbutils</groupId>
86
            <artifactId>commons-dbutils</artifactId>
87
            <version>1.6</version>
88
        </dependency>
89

  
90
    </dependencies>
91

  
92
    <properties>
93
        <java.version>1.8</java.version>
94
        <tomcat.version>7.0.52</tomcat.version>
95
    </properties>
96

  
97

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

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

  
121
    <pluginRepositories>
122
        <pluginRepository>
123
            <id>spring-releases</id>
124
            <url>https://repo.spring.io/libs-release</url>
125
        </pluginRepository>
126
    </pluginRepositories>
127
</project>

Also available in: Unified diff