Project

General

Profile

1
package eu.dnetlib.repo.manager.service.controllers;
2

    
3
import org.apache.log4j.Logger;
4
import org.json.JSONArray;
5
import org.json.JSONException;
6
import org.json.JSONObject;
7
import org.springframework.beans.factory.annotation.Value;
8
import org.springframework.http.HttpHeaders;
9
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
10
import org.springframework.stereotype.Component;
11
import org.springframework.web.client.RestTemplate;
12
import org.springframework.web.util.UriComponents;
13
import org.springframework.web.util.UriComponentsBuilder;
14

    
15
import javax.annotation.PostConstruct;
16
import java.util.HashMap;
17
import java.util.Map;
18

    
19
@Component
20
public class StatsApiImpl implements StatsApi {
21

    
22
    private RestTemplate restTemplate = null;
23

    
24
    private HttpHeaders httpHeaders;
25

    
26
    private static final Logger LOGGER = Logger.getLogger(RepositoryApiImpl.class);
27

    
28
    @Value("${search.api.baseAddress}")
29
    private String baseAddress;
30

    
31
    @Value("${search.api.usagestats}")
32
    private String usagestatsBaseAddress;
33

    
34
    @PostConstruct
35
    private void init() {
36
        LOGGER.debug("Initialization method of statistics api!");
37

    
38
        restTemplate = new RestTemplate();
39
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
40

    
41
        httpHeaders = new HttpHeaders();
42
        httpHeaders.set("Content-Type", "application/json");
43
    }
44

    
45

    
46
    @Override
47
    public Map<String, Object> getStatistics() throws JSONException {
48

    
49

    
50
        String aggregators = getTotalByType("datasource",baseAddress+"/resources",
51
                "?query= " +
52
                " oaftype exact datasource and " +
53
                " ( datasourcetypename exact Institutional Repository Aggregator " +
54
                "     or datasourcetypename exact Publication Repository Aggregator )");
55

    
56
        String dataRepositories = getTotalByType("datasource",baseAddress+"/resources",
57
                "?query= " +
58
                " oaftype exact datasource and " +
59
                " datasourcetypename exact Data Repository " );
60

    
61
        String literature = getTotalByType("datasource",baseAddress+"/resources",
62
                "?query= " +
63
                " oaftype exact datasource and " +
64
                " ( datasourcetypename exact Institutional Repository  " +
65
                "     or datasourcetypename exact Publication Repository )");
66

    
67
        String journal = getTotalByType("datasource",baseAddress+"/resources",
68
                "?query= " +
69
                " oaftype exact datasource and " +
70
                " datasourcetypename exact Journal");
71

    
72
        String publications = getTotalByType("other",baseAddress,"/publications/count");
73
        String datasets = getTotalByType("other",baseAddress,"/datasets/count");
74
        String software = getTotalByType("other",baseAddress,"/software/count");
75
        JSONObject usagestats = getUsageStatsTotal();
76

    
77

    
78
        HashMap<String,Object> stats = new HashMap<>();
79
        stats.put("aggregators",aggregators);
80
        stats.put("dataRepositories",dataRepositories);
81
        stats.put("literature",literature);
82
        stats.put("journal",journal);
83

    
84
        stats.put("publications",publications);
85
        stats.put("datasets",datasets);
86
        stats.put("software",software);
87

    
88
        stats.put("usagestats",usagestats.toString());
89

    
90

    
91
        return stats;
92
    }
93

    
94

    
95
    private String getTotalByType(String type,String url,String query) throws JSONException {
96
        UriComponents uriComponents = UriComponentsBuilder
97
                .fromHttpUrl(url + query)
98
                .queryParam("page",0)
99
                .queryParam("size",0)
100
                .queryParam("format","json")
101
                .build().encode();
102

    
103
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
104

    
105
        if(type.equalsIgnoreCase("datasource")){
106
            JSONObject metadata = (JSONObject) new JSONObject(rs).get("meta");
107
            return String.valueOf(metadata.get("total"));
108
        }else
109
            return String.valueOf(new JSONObject(rs).get("total"));
110

    
111
    }
112

    
113
    private JSONObject getUsageStatsTotal() throws JSONException {
114
        UriComponents uriComponents = UriComponentsBuilder
115
                .fromHttpUrl(usagestatsBaseAddress + "/totals")
116
                .build().encode();
117

    
118
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
119

    
120
        JSONArray resultSet = (JSONArray) new JSONObject(rs).getJSONArray("yearly_stats");
121
        JSONObject lastYear = resultSet.getJSONObject(resultSet.length()-1);
122

    
123
        Integer downloads = lastYear.getInt("downloads");
124
        Integer views = lastYear.getInt("views");
125
        String year = lastYear.getString("year");
126

    
127
        JSONObject usagestats = new JSONObject();
128
        usagestats.put("number",String.valueOf(downloads+views));
129
        usagestats.put("year",year);
130

    
131
        return usagestats;
132
    }
133
}
(13-13/19)