Project

General

Profile

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

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

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

    
20
@Component
21
public class StatsApiImpl implements StatsApi {
22

    
23
    private RestTemplate restTemplate = null;
24

    
25
    private HttpHeaders httpHeaders;
26

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

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

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

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

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

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

    
46

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

    
50

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

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

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

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

    
73
        String publications = getTotalByType("other",baseAddress,"/publications/count");
74
        String datasets = getTotalByType("other",baseAddress,"/datasets/count");
75
        String software = getTotalByType("other",baseAddress,"/software/count");
76
        JSONObject lastYearUsagestats = getLastYearUsageStatsTotal();
77
        Integer usagestats = getUsageStatsTotal();
78

    
79

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

    
86
        stats.put("publications",publications);
87
        stats.put("datasets",datasets);
88
        stats.put("software",software);
89

    
90
        stats.put("lastYearUsagestats",lastYearUsagestats.toString());
91
        stats.put("usagestats",usagestats.toString());
92

    
93

    
94
        return stats;
95
    }
96

    
97

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

    
106
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
107

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

    
114
    }
115

    
116
    private JSONObject getLastYearUsageStatsTotal() throws JSONException {
117

    
118
        UriComponents uriComponents = UriComponentsBuilder
119
                .fromHttpUrl(usagestatsBaseAddress + "/totals")
120
                .build().encode();
121

    
122
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
123

    
124
        JSONArray resultSet = (JSONArray) new JSONObject(rs).getJSONArray("yearly_stats");
125
        JSONObject lastYear = resultSet.getJSONObject(resultSet.length()-1);
126

    
127
        Integer downloads = lastYear.getInt("downloads");
128
        Integer views = lastYear.getInt("views");
129
        String year = lastYear.getString("year");
130

    
131
        JSONObject usagestats = new JSONObject();
132
        usagestats.put("number",String.valueOf(downloads+views));
133
        usagestats.put("year",year);
134

    
135
        return usagestats;
136

    
137
    }
138

    
139
    private Integer getUsageStatsTotal() throws JSONException {
140
        UriComponents uriComponents = UriComponentsBuilder
141
                .fromHttpUrl(usagestatsBaseAddress + "/totals")
142
                .build().encode();
143

    
144
        String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
145

    
146
        JSONObject resultSet = new JSONObject(rs);
147

    
148
        String downloads = resultSet.getString("downloads");
149
        String views = resultSet.getString("views");
150

    
151

    
152
        return Integer.valueOf(downloads) + Integer.valueOf(views);
153
    }
154
}
(13-13/19)