Project

General

Profile

« Previous | Next » 

Revision 61559

[Trunk | Claims API]: MetricsService.java: Added API call methods to calculate and return metrics for claim KPIs (only /report/metrics is needed for Prometheus).

View differences:

modules/uoa-claims-api/trunk/src/main/java/eu/dnetlib/openaire/rest/MetricsService.java
1
package eu.dnetlib.openaire.rest;
2

  
3

  
4
import com.google.gson.JsonObject;
5
import eu.dnetlib.data.claims.sql.SQLStoreException;
6
import org.apache.log4j.Logger;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.stereotype.Component;
9
import org.springframework.web.bind.annotation.CrossOrigin;
10

  
11
import javax.ws.rs.*;
12
import javax.ws.rs.core.MediaType;
13

  
14
import eu.dnetlib.data.claims.handler.MetricsHandler;
15
import eu.dnetlib.data.claims.entity.Metrics;
16

  
17
import java.sql.SQLException;
18
import java.util.HashMap;
19
import java.util.Map;
20

  
21

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

  
28
    @Autowired
29
    private MetricsHandler metricsHandler = null;
30

  
31
    public static void main(String[] args) {}
32

  
33

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

  
80
    @GET
81
    @Path("/calc")
82
    @Produces(MediaType.TEXT_PLAIN)
83
    public String calculateMetrics() throws SQLStoreException, Exception {
84
        metricsHandler.calculateMetrics();
85
        return "metrics calculated!";
86
    }
87

  
88
    @GET
89
    @Path("/metrics")
90
    @Produces(MediaType.TEXT_PLAIN)
91
    public String getMetrics() throws SQLStoreException, SQLException {
92
        Metrics metrics = metricsHandler.getMetrics();
93

  
94
        String response = "";
95

  
96
        if(metrics != null) {
97
//            response += "# TYPE claims gauge\n";
98
//            response += "claims " + metrics.getTotal_claims() + "\n";
99
//
100
//            response += "# TYPE claims_users gauge\n";
101
//            response += "claims_users " + metrics.getTotal_users() + "\n";
102
//
103
//            response += "# TYPE claims_projects gauge\n";
104
//            response += "claims_projects " + metrics.getProjects() + "\n";
105
//
106
//            response += "# TYPE claims_eu_projects gauge\n";
107
//            response += "claims_eu_projects " + metrics.getEu_projects() + "\n";
108
//
109
//            response += "# TYPE claims_countries gauge\n";
110
//            response += "claims_countries " + metrics.getCountries() + "\n";
111
//
112
//            response += "# TYPE claims_unique_research_results gauge\n";
113
//            response += "claims_unique_research_results " + metrics.getResearch_results() + "\n";
114

  
115
            Map<String, Integer> worksPerDashboard = new HashMap<>();
116
            // 1st approach
117
//            response += "# TYPE orcid_works gauge\n";
118

  
119
            response += "# TYPE claims gauge\n";
120
            for(JsonObject dashboardMetrics : metrics.getMetrics_per_dashboard()) {
121
                if(dashboardMetrics.get("claims") != null) {
122
                    response += "claims{environment=" + dashboardMetrics.get("environment") + " portal=" + dashboardMetrics.get("dashboard") + "}" + " " + dashboardMetrics.get("claims") + "\n";
123
                }
124
            }
125

  
126
            response += "# TYPE claims_users gauge\n";
127
            for(JsonObject dashboardMetrics : metrics.getMetrics_per_dashboard()) {
128
                if(dashboardMetrics.get("users") != null) {
129
                    response += "claims_users{environment=" + dashboardMetrics.get("environment") + " portal=" + dashboardMetrics.get("dashboard") + "}" + " " + dashboardMetrics.get("users") + "\n";
130
                }
131
            }
132

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

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

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

  
154
            response += "# TYPE claims_unique_research_results gauge\n";
155
            for(JsonObject dashboardMetrics : metrics.getMetrics_per_dashboard()) {
156
//            for(Object dashboardMetricsObj : (List) metrics.getMetrics_per_dashboard()) {
157
//                Map<String, Object> dashboardMetrics = (HashMap<String, Object>) dashboardMetricsObj;
158
                if(dashboardMetrics.get("research_results") != null) {
159
                    response += "claims_unique_research_results{environment=" + dashboardMetrics.get("environment") + " portal=" + dashboardMetrics.get("dashboard") + "}" + " " + dashboardMetrics.get("research_results") + "\n";
160
                }
161
            }
162

  
163
            if(metrics.getDate() != null) {
164
                response += "claims_last_metrics_updater_run_timestamp_seconds " + (metrics.getDate().getTime() / 1000) + "\n";
165
            }
166
        }
167
        return response;
168
    }
169

  
170
    private String composeResults(int total, String label) {
171
        return "\""+label+"\": \""+total+"\"";
172
    }
173

  
174
    private String composeTotalResults(int total) {
175
        return "\"total\": \""+total+"\"";
176
    }
177

  
178
    private String composeDataResponse(int count) {
179
        return " { \"status\" : \"success\", \"code\": \"200\",  "+composeTotalResults(count) + " }";
180
    }
181

  
182
//    private String composeDataResponse(Metrics metrics) {
183
//        return " { \"status\" : \"success\", \"code\": \"200\",  "
184
//                +composeResults(metrics.getCountries(), "countries")
185
//                +composeResults(metrics.getEu_projects(), "eu_projects")
186
//                +composeResults(metrics.getResearch_results(), "research_results")+ " }";
187
//    }
188

  
189
    private String compose500Message(String message, Throwable throwable) {
190
        return  "{ \"status\" : \"fail\", \"code\" : \"500\", \"message\" : \"  " + message + "\", " +
191
                "\"description\" : \""+  throwable.getMessage() +"\" }";
192
    }
193

  
194

  
195
}

Also available in: Unified diff