-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #612 from NUM-Forschungsdatenplattform/feature/use…
…rs-metrics Feature/users metrics
- Loading branch information
Showing
19 changed files
with
537 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/org/highmed/numportal/service/metric/UsersMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.highmed.numportal.service.metric; | ||
|
||
import io.micrometer.core.instrument.Gauge; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import org.highmed.numportal.domain.model.admin.UserDetails; | ||
import org.highmed.numportal.domain.repository.UserDetailsRepository; | ||
import org.highmed.numportal.web.feign.KeycloakFeign; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Custom prometheus metric, to detect number of user in different states, active, inactive or unapproved. | ||
*/ | ||
@Getter | ||
@Component | ||
public class UsersMetrics { | ||
|
||
private double unapprovedUsers; | ||
private double activeUsers; | ||
private double inactiveUsers; | ||
|
||
public UsersMetrics(MeterRegistry registry, UserDetailsRepository userDetailsRepository, KeycloakFeign keycloakFeign) { | ||
Gauge.builder("custom.metric.user.unapproved.counter", this::getUnapprovedUsers) | ||
.description("Unapproved users") | ||
.register(registry); | ||
Gauge.builder("custom.metric.user.active.counter", this::getActiveUsers) | ||
.description("Active users") | ||
.register(registry); | ||
Gauge.builder("custom.metric.user.inactive.counter", this::getInactiveUsers) | ||
.description("Inactive users") | ||
.register(registry); | ||
|
||
Optional<List<UserDetails>> unapproved = userDetailsRepository.findAllByApproved(false); | ||
unapproved.ifPresent(userDetails -> this.unapprovedUsers = userDetails.size()); | ||
this.inactiveUsers = keycloakFeign.countUsers(false); | ||
// decrease because of service account | ||
this.activeUsers = keycloakFeign.countUsers(true) - 1; | ||
|
||
} | ||
|
||
public void addNewUserAsUnapproved() { | ||
this.unapprovedUsers++; | ||
} | ||
|
||
public void approveUser() { | ||
this.unapprovedUsers--; | ||
} | ||
|
||
public void updateCountStatus(@NotNull boolean active) { | ||
if(active) { | ||
this.incrementActiveUsers(); | ||
} else { | ||
this.incrementInactiveUsers(); | ||
} | ||
} | ||
|
||
private void incrementActiveUsers() { | ||
this.activeUsers++; | ||
this.inactiveUsers--; | ||
} | ||
|
||
private void incrementInactiveUsers() { | ||
this.inactiveUsers++; | ||
this.activeUsers--; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/test/java/org/highmed/numportal/TestNumPortalApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/test/java/org/highmed/numportal/integrationtesting/config/EhrBaseMockContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.highmed.numportal.integrationtesting.config; | ||
|
||
import org.mockserver.client.MockServerClient; | ||
import org.mockserver.model.*; | ||
import org.testcontainers.containers.MockServerContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import static org.highmed.numportal.integrationtesting.tests.IntegrationTest.IDENTITY_PROVIDER_TOKEN_ENDPOINT; | ||
|
||
public class EhrBaseMockContainer extends MockServerContainer { | ||
private static final String IMAGE_VERSION = "mockserver/mockserver:5.15.0"; | ||
private static final String EHR_BASE_URL = "/ehrbase/rest/openehr/v1/definition/template/adl1.4/"; | ||
private static EhrBaseMockContainer container; | ||
|
||
private EhrBaseMockContainer() { | ||
super(DockerImageName.parse(IMAGE_VERSION)); | ||
} | ||
|
||
public static EhrBaseMockContainer getInstance() { | ||
if (container == null) { | ||
container = new EhrBaseMockContainer(); | ||
} | ||
return container; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
super.start(); | ||
System.setProperty("EHRBASE_URL", "http://localhost:" + container.getServerPort()); | ||
|
||
MockServerClient client = new MockServerClient("localhost", container.getServerPort()); | ||
client | ||
.when(HttpRequest.request().withMethod("GET").withPath(EHR_BASE_URL)) | ||
.respond(HttpResponse.response().withStatusCode(HttpStatusCode.OK_200.code()).withBody("[{\"template_id\": \"IDCR - Immunisation summary.v0\",\"concept\": \"IDCR - Immunisation summary.v0\",\"archetype_id\": \"openEHR-EHR-COMPOSITION.health_summary.v1\",\"created_timestamp\": \"2020-11-25T16:19:37.812Z\"}]", MediaType.JSON_UTF_8)); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
// do nothing, JVM handles shut down | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/test/java/org/highmed/numportal/integrationtesting/config/KeycloakMockContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.highmed.numportal.integrationtesting.config; | ||
|
||
import org.mockserver.client.MockServerClient; | ||
import org.mockserver.model.*; | ||
import org.testcontainers.containers.MockServerContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import static org.highmed.numportal.integrationtesting.tests.IntegrationTest.IDENTITY_PROVIDER_TOKEN_ENDPOINT; | ||
|
||
public class KeycloakMockContainer extends MockServerContainer { | ||
private static final String IMAGE_VERSION = "mockserver/mockserver:5.15.0"; | ||
public static final String USERS_COUNT = "/admin/realms/Num/users/count"; | ||
private static KeycloakMockContainer container; | ||
|
||
private KeycloakMockContainer() { | ||
super(DockerImageName.parse(IMAGE_VERSION)); | ||
} | ||
|
||
public static KeycloakMockContainer getInstance() { | ||
if (container == null) { | ||
container = new KeycloakMockContainer(); | ||
} | ||
return container; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
super.start(); | ||
System.setProperty("KEYCLOAK_URL", "http://localhost:" + container.getServerPort()); | ||
|
||
MockServerClient client = new MockServerClient("localhost", container.getServerPort()); | ||
Header header = new Header("Content-Type", "application/json; charset=utf-8"); | ||
Header authHeader = new Header("Authorization", "Bearer {{randomValue length=20 type='ALPHANUMERIC'}}"); | ||
|
||
client | ||
.when(HttpRequest.request().withMethod("GET").withQueryStringParameter("enabled", "true").withPath(USERS_COUNT)) | ||
.respond(HttpResponse.response().withStatusCode(HttpStatusCode.OK_200.code()).withHeaders(authHeader).withBody("2", MediaType.JSON_UTF_8)); | ||
client | ||
.when(HttpRequest.request().withMethod("GET").withQueryStringParameter("enabled", "false").withPath(USERS_COUNT)) | ||
.respond(HttpResponse.response().withStatusCode(HttpStatusCode.OK_200.code()).withHeaders(authHeader).withBody("0", MediaType.JSON_UTF_8)); | ||
client | ||
.when(HttpRequest.request().withMethod("POST").withPath(IDENTITY_PROVIDER_TOKEN_ENDPOINT)) | ||
.respond(HttpResponse.response().withStatusCode(HttpStatusCode.OK_200.code()).withHeaders(header).withBody("{\"token_type\": \"Bearer\",\"access_token\":\"{{randomValue length=20 type='ALPHANUMERIC'}}\"}")); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
// do nothing, JVM handles shut down | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.