|
1 | 1 | package de.l3s.learnweb.dashboard.tracker; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.io.Serial; |
4 | 5 | import java.io.Serializable; |
| 6 | +import java.net.URI; |
| 7 | +import java.net.http.HttpClient; |
| 8 | +import java.net.http.HttpRequest; |
| 9 | +import java.net.http.HttpResponse; |
| 10 | +import java.time.LocalDate; |
5 | 11 | import java.util.List; |
6 | 12 | import java.util.Map; |
7 | 13 |
|
| 14 | +import jakarta.enterprise.inject.spi.DeploymentException; |
8 | 15 | import jakarta.faces.view.ViewScoped; |
| 16 | +import jakarta.inject.Inject; |
9 | 17 | import jakarta.inject.Named; |
10 | 18 |
|
| 19 | +import org.apache.commons.lang3.StringUtils; |
| 20 | +import org.apache.logging.log4j.LogManager; |
| 21 | +import org.apache.logging.log4j.Logger; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | + |
| 26 | +import de.l3s.learnweb.app.ConfigProvider; |
11 | 27 | import de.l3s.learnweb.dashboard.CommonDashboardUserBean; |
12 | 28 |
|
13 | 29 | @Named |
14 | 30 | @ViewScoped |
15 | 31 | public class TrackerDashboardBean extends CommonDashboardUserBean implements Serializable { |
| 32 | + private static final Logger log = LogManager.getLogger(TrackerDashboardBean.class); |
| 33 | + |
16 | 34 | @Serial |
17 | 35 | private static final long serialVersionUID = 3640317272542005280L; |
18 | 36 |
|
19 | | - private static final int TRACKER_CLIENT_ID = 2; |
20 | | - |
21 | | - private TrackerDao trackerDao; |
22 | | - |
23 | 37 | private transient Map<String, Integer> proxySources; |
24 | 38 | private transient List<TrackerUserActivity> statistics; |
25 | 39 |
|
| 40 | + @Inject |
| 41 | + private ConfigProvider config; |
| 42 | + |
| 43 | + @Inject |
| 44 | + private ObjectMapper objectMapper; |
| 45 | + |
26 | 46 | @Override |
27 | 47 | public void onLoad() { |
28 | 48 | super.onLoad(); |
29 | 49 |
|
30 | | - trackerDao = dao().getJdbi().onDemand(TrackerDao.class); |
31 | | - cleanAndUpdateStoredData(); |
| 50 | + if (StringUtils.isAnyEmpty(config.getProperty("integration_tracker_url"), config.getProperty("integration_tracker_apikey"), config.getProperty("integration_tracker_secret"))) { |
| 51 | + throw new DeploymentException("Proxy is not configured. Please contact the administrator."); |
| 52 | + } |
32 | 53 | } |
33 | 54 |
|
34 | 55 | @Override |
35 | 56 | public void cleanAndUpdateStoredData() { |
36 | 57 | statistics = null; |
37 | 58 | proxySources = null; |
38 | | - |
39 | | - fetchDataFromManager(); |
40 | | - } |
41 | | - |
42 | | - private void fetchDataFromManager() { |
43 | | - if (getSelectedUsersIds() != null && !getSelectedUsersIds().isEmpty()) { |
44 | | - List<Integer> selectedUsersIds = getSelectedUsersIds(); |
45 | | - statistics = trackerDao.countTrackerStatistics(TRACKER_CLIENT_ID, selectedUsersIds, startDate, endDate); |
46 | | - proxySources = trackerDao.countUsagePerDomain(TRACKER_CLIENT_ID, selectedUsersIds, startDate, endDate); |
47 | | - } |
48 | 59 | } |
49 | 60 |
|
50 | 61 | public List<TrackerUserActivity> getStatistics() { |
| 62 | + if (statistics == null) { |
| 63 | + try { |
| 64 | + String response = sendApiRequest("/api/v2/statistics/users", getSelectedUsersIds(), startDate, endDate); |
| 65 | + this.statistics = objectMapper.readValue(response, new TypeReference<>() {}); |
| 66 | + } catch (IOException | InterruptedException e) { |
| 67 | + log.error("Error fetching proxy sources: ", e); |
| 68 | + } |
| 69 | + } |
51 | 70 | return statistics; |
52 | 71 | } |
53 | 72 |
|
54 | 73 | public Map<String, Integer> getProxySources() { |
| 74 | + if (proxySources == null) { |
| 75 | + try { |
| 76 | + String response = sendApiRequest("/api/v2/statistics/domains", getSelectedUsersIds(), startDate, endDate); |
| 77 | + proxySources = objectMapper.readValue(response, new TypeReference<>() {}); |
| 78 | + } catch (IOException | InterruptedException e) { |
| 79 | + log.error("Error fetching proxy sources: ", e); |
| 80 | + } |
| 81 | + } |
55 | 82 | return proxySources; |
56 | 83 | } |
| 84 | + |
| 85 | + private String sendApiRequest(String endpoint, List<Integer> userIds, LocalDate startDate, LocalDate endDate) |
| 86 | + throws IOException, InterruptedException { |
| 87 | + String queryParams = buildQueryParameters(userIds, startDate, endDate); |
| 88 | + String url = config.getProperty("integration_tracker_url") + endpoint + queryParams; |
| 89 | + |
| 90 | + HttpRequest request = HttpRequest.newBuilder() |
| 91 | + .uri(URI.create(url)) |
| 92 | + .header("Authorization", "Apikey " + config.getProperty("integration_tracker_apikey")) |
| 93 | + .header("X-Secret-Key", config.getProperty("integration_tracker_secret")) |
| 94 | + .GET().build(); |
| 95 | + |
| 96 | + HttpResponse<String> response = HttpClient.newHttpClient() |
| 97 | + .send(request, HttpResponse.BodyHandlers.ofString()); |
| 98 | + |
| 99 | + if (response.statusCode() >= 400) { |
| 100 | + log.error("API request failed with status code {}: {}", response.statusCode(), response.body()); |
| 101 | + throw new IOException("API request failed with status code: " + response.statusCode()); |
| 102 | + } |
| 103 | + |
| 104 | + return response.body(); |
| 105 | + } |
| 106 | + |
| 107 | + private String buildQueryParameters(List<Integer> userIds, LocalDate startDate, LocalDate endDate) { |
| 108 | + StringBuilder params = new StringBuilder("?"); |
| 109 | + boolean hasParams = false; |
| 110 | + |
| 111 | + if (userIds != null && !userIds.isEmpty()) { |
| 112 | + params.append("user_ids=").append(String.join(",", userIds.stream().map(String::valueOf).toList())); |
| 113 | + hasParams = true; |
| 114 | + } |
| 115 | + |
| 116 | + if (startDate != null) { |
| 117 | + if (hasParams) params.append("&"); |
| 118 | + params.append("start_date=").append(startDate); |
| 119 | + hasParams = true; |
| 120 | + } |
| 121 | + |
| 122 | + if (endDate != null) { |
| 123 | + if (hasParams) params.append("&"); |
| 124 | + params.append("end_date=").append(endDate); |
| 125 | + } |
| 126 | + |
| 127 | + return params.toString(); |
| 128 | + } |
57 | 129 | } |
0 commit comments