Project

General

Profile

1
package eu.dnetlib.openaire.rest;
2

    
3

    
4
import com.google.gson.JsonObject;
5
import eu.dnetlib.data.claims.entity.Metrics;
6
import eu.dnetlib.data.claims.handler.MetricsHandler;
7
import eu.dnetlib.data.claims.sql.SQLStoreException;
8
import org.apache.logging.log4j.LogManager;
9
import org.apache.logging.log4j.Logger;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.stereotype.Component;
12
import org.springframework.web.bind.annotation.CrossOrigin;
13

    
14
import javax.ws.rs.CookieParam;
15
import javax.ws.rs.GET;
16
import javax.ws.rs.Path;
17
import javax.ws.rs.Produces;
18
import javax.ws.rs.core.MediaType;
19
import java.sql.SQLException;
20
import java.util.HashMap;
21
import java.util.Map;
22

    
23

    
24
@Component
25
@CrossOrigin(origins = "*")
26
@Path("/report")
27
public class MetricsService {
28
    private static final Logger logger = LogManager.getLogger(MetricsService.class);
29

    
30
    @Autowired
31
    public Authorization authorization = null;
32

    
33
    @Autowired
34
    private MetricsHandler metricsHandler = null;
35

    
36
    public static void main(String[] args) {}
37

    
38

    
39
//    @GET
40
//    @Path("/euprojects")
41
//    @Produces(MediaType.APPLICATION_JSON)
42
//    public Response getNumOfEUProjects() {
43
//        try {
44
//            int count = metricsHandler.countEUProjects();
45
//            return Response.status(200).entity(composeDataResponse(count)).build();
46
//        } catch (SQLStoreException |Exception e) {
47
//            logger.error("Could not fetch number of EU projects", e);
48
//            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
49
//                    compose500Message("Fail to fetch number of EU projects.", e)).
50
//                    type(MediaType.APPLICATION_JSON).build();
51
//        }
52
//    }
53
//
54
//
55
//    @GET
56
//    @Path("/countries")
57
//    @Produces(MediaType.APPLICATION_JSON)
58
//    public Response getNumOfCountriesInClaims() {
59
//        try {
60
//            int count = metricsHandler.countCountriesFromClaimedBy();
61
//            return Response.status(200).entity(composeDataResponse(count)).build();
62
//        } catch (SQLStoreException |Exception e) {
63
//            logger.error("Could not fetch number of countries for people who claimed", e);
64
//            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
65
//                    compose500Message("Fail to fetch number of countries for people who claimed.", e)).
66
//                    type(MediaType.APPLICATION_JSON).build();
67
//        }
68
//    }
69
//
70
//    @GET
71
//    @Path("/research-results")
72
//    @Produces(MediaType.APPLICATION_JSON)
73
//    public Response getNumOfUniqueResearchResults() {
74
//        try {
75
//            int count = metricsHandler.countUniqueResearchResults();
76
//            return Response.status(200).entity(composeDataResponse(count)).build();
77
//        } catch (SQLStoreException |Exception e) {
78
//            logger.error("Could not fetch number of unique research results claimed", e);
79
//            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
80
//                    compose500Message("Fail to fetch number of unique research results claimed.", e)).
81
//                    type(MediaType.APPLICATION_JSON).build();
82
//        }
83
//    }
84

    
85
    @GET
86
    @Path("/calc")
87
    @Produces(MediaType.TEXT_PLAIN)
88
    public String calculateMetrics(
89
//            @HeaderParam("X-XSRF-TOKEN") String token,
90
            @CookieParam("AccessToken") String  cookie
91
    ) throws SQLStoreException, Exception {
92
        if(cookie == null || cookie.isEmpty()){
93
            return "Forbidden: You don't have permission to access. Maybe you are not registered.";
94
        }
95

    
96
        if(authorization.isPortalAdministrator(cookie)) {
97
            metricsHandler.calculateMetrics();
98
            return "metrics calculated!";
99
        }
100
        return "Forbidden: You don't have permission to access.";
101
    }
102

    
103
    @GET
104
    @Path("/metrics")
105
    @Produces(MediaType.TEXT_PLAIN)
106
    public String getMetrics() throws SQLStoreException, SQLException {
107
        Metrics metrics = metricsHandler.getMetrics();
108

    
109
        String response = "";
110

    
111
        if(metrics != null) {
112
//            response += "# TYPE claims gauge\n";
113
//            response += "claims " + metrics.getTotal_claims() + "\n";
114
//
115
//            response += "# TYPE claims_users gauge\n";
116
//            response += "claims_users " + metrics.getTotal_users() + "\n";
117
//
118
//            response += "# TYPE claims_projects gauge\n";
119
//            response += "claims_projects " + metrics.getProjects() + "\n";
120
//
121
//            response += "# TYPE claims_eu_projects gauge\n";
122
//            response += "claims_eu_projects " + metrics.getEu_projects() + "\n";
123
//
124
//            response += "# TYPE claims_countries gauge\n";
125
//            response += "claims_countries " + metrics.getCountries() + "\n";
126
//
127
//            response += "# TYPE claims_unique_research_results gauge\n";
128
//            response += "claims_unique_research_results " + metrics.getResearch_results() + "\n";
129

    
130
            Map<String, Integer> worksPerDashboard = new HashMap<>();
131
            // 1st approach
132
//            response += "# TYPE orcid_works gauge\n";
133

    
134
            response += "# TYPE claims_recorded gauge\n";
135
            for(JsonObject dashboardMetrics : metrics.getMetrics_per_dashboard()) {
136
                if(dashboardMetrics.get("claims") != null) {
137
                    response += "claims_recorded{environment=" + dashboardMetrics.get("environment") + " portal=" + dashboardMetrics.get("dashboard") + "}" + " " + dashboardMetrics.get("claims") + "\n";
138
                }
139
            }
140

    
141
            response += "# TYPE claims_users gauge\n";
142
            for(JsonObject dashboardMetrics : metrics.getMetrics_per_dashboard()) {
143
                if(dashboardMetrics.get("users") != null) {
144
                    response += "claims_users{environment=" + dashboardMetrics.get("environment") + " portal=" + dashboardMetrics.get("dashboard") + "}" + " " + dashboardMetrics.get("users") + "\n";
145
                }
146
            }
147

    
148
            response += "# TYPE claims_projects gauge\n";
149
            for(JsonObject dashboardMetrics : metrics.getMetrics_per_dashboard()) {
150
                if(dashboardMetrics.get("projects") != null) {
151
                    response += "claims_projects{environment=" + dashboardMetrics.get("environment") + " portal=" + dashboardMetrics.get("dashboard") + "}" + " " + dashboardMetrics.get("projects") + "\n";
152
                }
153
            }
154

    
155
            response += "# TYPE claims_eu_projects gauge\n";
156
            for(JsonObject dashboardMetrics : metrics.getMetrics_per_dashboard()) {
157
                if(dashboardMetrics.get("eu_projects") != null) {
158
                    response += "claims_eu_projects{environment=" + dashboardMetrics.get("environment") + " portal=" + dashboardMetrics.get("dashboard") + "}" + " " + dashboardMetrics.get("eu_projects") + "\n";
159
                }
160
            }
161

    
162
            response += "# TYPE claims_countries gauge\n";
163
            for(JsonObject dashboardMetrics : metrics.getMetrics_per_dashboard()) {
164
                if(dashboardMetrics.get("countries") != null) {
165
                    response += "claims_countries{environment=" + dashboardMetrics.get("environment") + " portal=" + dashboardMetrics.get("dashboard") + "}" + " " + dashboardMetrics.get("countries") + "\n";
166
                }
167
            }
168

    
169
            response += "# TYPE claims_unique_research_results gauge\n";
170
            for(JsonObject dashboardMetrics : metrics.getMetrics_per_dashboard()) {
171
//            for(Object dashboardMetricsObj : (List) metrics.getMetrics_per_dashboard()) {
172
//                Map<String, Object> dashboardMetrics = (HashMap<String, Object>) dashboardMetricsObj;
173
                if(dashboardMetrics.get("research_results") != null) {
174
                    response += "claims_unique_research_results{environment=" + dashboardMetrics.get("environment") + " portal=" + dashboardMetrics.get("dashboard") + "}" + " " + dashboardMetrics.get("research_results") + "\n";
175
                }
176
            }
177

    
178
            if(metrics.getDate() != null) {
179
                response += "claims_last_metrics_updater_run_timestamp_seconds " + (metrics.getDate().getTime() / 1000) + "\n";
180
            }
181
        }
182
        return response;
183
    }
184

    
185
    private String composeResults(int total, String label) {
186
        return "\""+label+"\": \""+total+"\"";
187
    }
188

    
189
    private String composeTotalResults(int total) {
190
        return "\"total\": \""+total+"\"";
191
    }
192

    
193
    private String composeDataResponse(int count) {
194
        return " { \"status\" : \"success\", \"code\": \"200\",  "+composeTotalResults(count) + " }";
195
    }
196

    
197
//    private String composeDataResponse(Metrics metrics) {
198
//        return " { \"status\" : \"success\", \"code\": \"200\",  "
199
//                +composeResults(metrics.getCountries(), "countries")
200
//                +composeResults(metrics.getEu_projects(), "eu_projects")
201
//                +composeResults(metrics.getResearch_results(), "research_results")+ " }";
202
//    }
203

    
204
    private String compose500Message(String message, Throwable throwable) {
205
        return  "{ \"status\" : \"fail\", \"code\" : \"500\", \"message\" : \"  " + message + "\", " +
206
                "\"description\" : \""+  throwable.getMessage() +"\" }";
207
    }
208

    
209

    
210
}
(3-3/4)