Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
YvannPONCE committed Oct 27, 2024
2 parents d79e585 + f42dcea commit 5c58a8d
Show file tree
Hide file tree
Showing 24 changed files with 343 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ RUN ln -s /opt/apache-maven-3.9.9 /opt/maven
ENV PATH="/opt/maven/bin:${PATH}"
USER quarkus
WORKDIR /code
RUN mvn -B -f /code/__BACKEND__/pom.xml -DskipTests clean package -Dnative
RUN mvn -B -f /code/alert-management/pom.xml -DskipTests clean package -Dnative

## Stage 2 : create the docker final image
FROM quay.io/quarkus/quarkus-micro-image:2.0
WORKDIR /work/
COPY --from=build /code/__BACKEND__/target/*-runner /work/application
COPY --from=build /code/alert-management/target/*-runner /work/application

# set up permissions for user `1001`
RUN chmod 775 /work /work/application \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ quarkus.http.cors=true
quarkus.http.cors.origins=*
quarkus.http.cors.methods=GET,POST,PUT,DELETE,PATCH
# configure the mongoDB client for a single instance on localhost
quarkus.mongodb.connection-string=${DB_URL:mongodb://localhost:27017}
quarkus.mongodb.connection-string=${DB_URL:mongodb://mongodb:27017}
quarkus.mongodb.database=${DB_NAME:poulet}
# Enable keycloak authentication
quarkus.oidc.auth-server-url=${AUTH_SERVER_URL:http://localhost:8081/realms/poulet-realm}
quarkus.oidc.auth-server-url=${AUTH_SERVER_URL:http://keycloak:8080/realms/poulet-realm}
quarkus.oidc.client-id=${CLIENT_ID:backend-service}
quarkus.oidc.credentials.secret=${CLIENT_SECRET:J1jZPePtgzG4Q9ltZTHlBGKEyj93P4hd}
# Docker build properties
Expand All @@ -17,4 +17,4 @@ quarkus.swagger-ui.path=/swagger-ui
quarkus.smallrye-openapi.enable=true
quarkus.smallrye-openapi.format=yaml
quarkus.smallrye-openapi.store-schema-directory=target/generated/
quarkus.http.port=${PORT:8086 }
quarkus.http.port=${PORT:8082 }
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ RUN ln -s /opt/apache-maven-3.9.9 /opt/maven
ENV PATH="/opt/maven/bin:${PATH}"
USER quarkus
WORKDIR /code
RUN mvn -B -f /code/__BACKEND__/pom.xml -DskipTests clean package -Dnative

#RUN mvn -B -f /code/__BACKEND__/pom.xml -DskipTests clean package -Dnative
RUN mvn -B -f /code/analyse-haut-niveau-management/pom.xml -DskipTests clean package -Dnative
## Stage 2 : create the docker final image
FROM quay.io/quarkus/quarkus-micro-image:2.0
WORKDIR /work/
COPY --from=build /code/__BACKEND__/target/*-runner /work/application
#COPY --from=build /code/__BACKEND__/target/*-runner /work/application
COPY --from=build /code/analyse-haut-niveau-management/target/*-runner /work/application

# set up permissions for user `1001`
RUN chmod 775 /work /work/application \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,28 @@ public class KpiService {



public JsonObject calculateKpis(String start, String end) {
public JsonObject calculateKpis(String start, String end, String gatewayId, String step) {
JsonObject kpis = new JsonObject();
log.info("Calculating KPIs for gateway " + gatewayId + " between" + start + " and " + end + " with step " + step);

kpis.put("averageAcceleration",calculateMetricsForRange("acceleration", start, end));
kpis.put("averageHeartRate", calculateMetricsForRange("heartrate", start, end));
kpis.put("averageTemperature", calculateMetricsForRange("temperature", start, end));
kpis.put("averageGlucose", calculateMetricsForRange("glucose", start, end));
kpis.put("averageAcceleration",calculateMetricsForRange("acceleration", start, end, gatewayId, step));
kpis.put("averageHeartRate", calculateMetricsForRange("heartrate", start, end, gatewayId, step));
kpis.put("averageTemperature", calculateMetricsForRange("temperature", start, end, gatewayId, step));
kpis.put("averageGlucose", calculateMetricsForRange("glucose", start, end, gatewayId, step));

return kpis;
}

private JsonObject calculateMetricsForRange(String metricName, String start, String end) {
String step = "120s";
Response response = prometheusService.queryMetric(metricName, start, end, step);
private JsonObject calculateMetricsForRange(String metricName, String start, String end, String gatewayId, String step) {
if (step != null) {
step = "120s";
}

String query = metricName + "{gateway_id=\"" + gatewayId + "\"}";

log.info("Querying Prometheus for metric " + query + " between " + start + " and " + end + " with step " + step);

Response response = prometheusService.queryMetric(query, start, end, step);
JsonObject result = new JsonObject(response.readEntity(String.class));


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneOffset;


@Path("/metrics")
public class MetricsResource {


private static final Logger LOGGER = LoggerFactory.getLogger(MetricsResource.class);
private final Logger LOGGER = LoggerFactory.getLogger(MetricsResource.class);
@Inject
KpiService kpiService;

Expand All @@ -31,8 +34,26 @@ public class MetricsResource {
@Path("/kpis")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject getKpis(@QueryParam("start") String startTime,
@QueryParam("end") String endTime) {
return kpiService.calculateKpis(startTime, endTime);
@QueryParam("end") String endTime,
@QueryParam("gatewayId") String gatewayId) {
return kpiService.calculateKpis(startTime, endTime, gatewayId, null);
}


@GET
@Path("/today")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject getTodayMetrics(@QueryParam("gatewayId") String gatewayId) {

DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;

LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);

String startTime = now.withHour(0).withMinute(0).withSecond(0).withNano(0).format(formatter) + "Z";
String endTime = now.withHour(23).withMinute(59).withSecond(59).withNano(0).format(formatter) + "Z";


return kpiService.calculateKpis(startTime, endTime, gatewayId, "5s");
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# configure the mongoDB client for a single instance on localhost
quarkus.mongodb.connection-string=${DB_URL:mongodb://localhost:27017}
quarkus.mongodb.connection-string=${DB_URL:mongodb://mongodb:27017}
quarkus.mongodb.database=${DB_NAME:poulet}
# Enable keycloak authentication
quarkus.oidc.auth-server-url=${AUTH_SERVER_URL:http://localhost:8081/realms/poulet-realm}
quarkus.oidc.auth-server-url=${AUTH_SERVER_URL:http://keycloak:8080/realms/poulet-realm}
quarkus.oidc.client-id=${CLIENT_ID:backend-service}
quarkus.oidc.credentials.secret=${CLIENT_SECRET:J1jZPePtgzG4Q9ltZTHlBGKEyj93P4hd}
quarkus.oidc.application-type=web-app
Expand All @@ -19,5 +19,5 @@ quarkus.smallrye-openapi.store-schema-directory=target/generated/
quarkus.http.port=${PORT:8084}
quarkus.metric.enabled=true

prometheus-api/mp-rest/url=${PROMETHEUS_URL:http://localhost:9090}
prometheus-api/mp-rest/url=${PROMETHEUS_URL:http://read:9090}

52 changes: 52 additions & 0 deletions cloud/backend/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
services:
alert-management:
build:
context: ./
dockerfile: ./alert-management/src/main/docker/Dockerfile.multistage
container_name: alert-management
ports:
- 0.0.0.0:8082:8082
networks:
- poulet_internal
- internal

analyse-haut-niveau-management:
build:
context: ./
dockerfile: ./analyse-haut-niveau-management/src/main/docker/Dockerfile.multistage
container_name: analyse-haut-niveau-management
ports:
- 0.0.0.0:8084:8084
networks:
- poulet_internal
- internal
environment:
- DB_URL=mongodb://mongodb:27017


patient-management:
build:
context: ./
dockerfile: ./patient-management/src/main/docker/Dockerfile.multistage
container_name: patient-management
ports:
- 0.0.0.0:8083:8083
networks:
- poulet_internal
- internal


mongodb:
image: mongodb/mongodb-community-server:7.0.2-ubi8
volumes:
- /etc/localtime:/etc/localtime:ro
ports:
- 0.0.0.0:27017:27017
networks:
- internal
- poulet_internal

networks:
internal:
poulet_internal:
external: true
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ RUN ln -s /opt/apache-maven-3.9.9 /opt/maven
ENV PATH="/opt/maven/bin:${PATH}"
USER quarkus
WORKDIR /code
RUN mvn -B -f /code/__BACKEND__/pom.xml -DskipTests clean package -Dnative
#RUN mvn -B -f /code/__BACKEND__/pom.xml -DskipTests clean package -Dnative
RUN mvn -B -f /code/patient-management/pom.xml -DskipTests clean package -Dnative

## Stage 2 : create the docker final image
FROM quay.io/quarkus/quarkus-micro-image:2.0
WORKDIR /work/
COPY --from=build /code/__BACKEND__/target/*-runner /work/application
#COPY --from=build /code/__BACKEND__/target/*-runner /work/application
COPY --from=build /code/patient-management/target/*-runner /work/application

# set up permissions for user `1001`
RUN chmod 775 /work /work/application \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ quarkus.http.cors.origins=*
quarkus.http.cors.methods=GET,POST,PUT,DELETE

# configure the mongoDB client for a single instance on localhost
quarkus.mongodb.connection-string=${DB_URL:mongodb://localhost:27017}
quarkus.mongodb.connection-string=${DB_URL:mongodb://mongodb:27017}
quarkus.mongodb.database=${DB_NAME:poulet}

# Enable keycloak authentication
quarkus.oidc.auth-server-url=${AUTH_SERVER_URL:http://localhost:8081/realms/poulet-realm}
quarkus.oidc.auth-server-url=${AUTH_SERVER_URL:http://keycloak:8080/realms/poulet-realm}
quarkus.oidc.client-id=${CLIENT_ID:backend-service}
quarkus.oidc.credentials.secret=${CLIENT_SECRET:J1jZPePtgzG4Q9ltZTHlBGKEyj93P4hd}
quarkus.oidc.application-type=web-app
Expand Down
3 changes: 2 additions & 1 deletion cloud/config/alertmanager/alertmanager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ route:
receivers:
- name: 'webhook_receiver'
webhook_configs:
- url: 'http://host.docker.internal:8086/alert/receiveAlert'
- url: 'http://alert-management:8082/alert/receiveAlert'


2 changes: 1 addition & 1 deletion cloud/config/read/rules/acceleration_alerts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ groups:
labels:
severity: "WARNING"
type: "acceleration"
gatewayId: "6718fcc16adfca0b7f8f6a67"
gatewayId: "671aaecbbf1f103b28e58788"
value: "{{ $value }}"
annotations:
summary: "High Acceleration Alert"
Expand Down
30 changes: 30 additions & 0 deletions cloud/config/read/rules/deriv_rules/glucoseD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
groups:
- name: glucose-deriv-alerts
rules:
- alert: RapidIncreaseGlucose
expr: deriv(glucose[5m]) > 10
for: 1m
labels:
severity: "MEDIUM"
type: "glucose_deriv"
gatewayId: "671aaecbbf1f103b28e58788"
value: "{{ $value }}"

annotations:
summary: "Rapid Increase in Glucose Alert"
description: "The glucose level is increasing rapidly (rate of change > 10 mg/dL per 5 minutes)."
message: "Current rate of glucose change: {{ $value }} mg/dL per 5 minutes"

- alert: RapidDecreaseGlucose
expr: deriv(glucose[5m]) < -10
for: 1m
labels:
severity: "MEDIUM"
type: "glucose_deriv"
gatewayId: "671aaecbbf1f103b28e58788"
value: "{{ $value }}"

annotations:
summary: "Rapid Decrease in Glucose Alert"
description: "The glucose level is decreasing rapidly (rate of change < -10 mg/dL per 5 minutes)."
message: "Current rate of glucose change: {{ $value }} mg/dL per 5 minutes"
30 changes: 30 additions & 0 deletions cloud/config/read/rules/deriv_rules/heartrateD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
groups:
- name: heartrate-deriv-alerts
rules:
- alert: RapidIncreaseHeartRate
expr: deriv(heartrate[5m]) > 15
for: 1m
labels:
severity: "MEDIUM"
type: "heartrate_deriv"
gatewayId: "671aaecbbf1f103b28e58788"
value: "{{ $value }}"

annotations:
summary: "Rapid Increase in Heart Rate Alert"
description: "The heart rate is increasing rapidly (rate of change > 15 bpm per 5 minutes)."
message: "Current rate of heart rate change: {{ $value }} bpm per 5 minutes"

- alert: RapidDecreaseHeartRate
expr: deriv(heartrate[5m]) < -15
for: 1m
labels:
severity: "HIGH"
type: "heartrate_deriv"
gatewayId: "671aaecbbf1f103b28e58788"
value: "{{ $value }}"

annotations:
summary: "Rapid Decrease in Heart Rate Alert"
description: "The heart rate is decreasing rapidly (rate of change < -15 bpm per 5 minutes)."
message: "Current rate of heart rate change: {{ $value }} bpm per 5 minutes"
30 changes: 30 additions & 0 deletions cloud/config/read/rules/deriv_rules/temperatureD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
groups:
- name: temperature-deriv-alerts
rules:
- alert: RapidIncreaseTemperature
expr: deriv(temperature[5m]) > 0.5
for: 1m
labels:
severity: "MEDIUM"
type: "temperature_deriv"
gatewayId: "671aaecbbf1f103b28e58788"
value: "{{ $value }}"

annotations:
summary: "Rapid Increase in Temperature Alert"
description: "The temperature is increasing rapidly (rate of change > 0.5°C per 5 minutes)."
message: "Current rate of temperature change: {{ $value }}°C per 5 minutes"

- alert: RapidDecreaseTemperature
expr: deriv(temperature[5m]) < -0.5
for: 1m
labels:
severity: "MEDIUM"
type: "temperature_deriv"
gatewayId: "671aaecbbf1f103b28e58788"
value: "{{ $value }}"

annotations:
summary: "Rapid Decrease in Temperature Alert"
description: "The temperature is decreasing rapidly (rate of change < -0.5°C per 5 minutes)."
message: "Current rate of temperature change: {{ $value }}°C per 5 minutes"
4 changes: 2 additions & 2 deletions cloud/config/read/rules/glucose_alerts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ groups:
labels:
severity: "WARNING"
type: "glucose"
gatewayId: "example-gateway-id"
gatewayId: "671aaecbbf1f103b28e58788"
value: "{{ $value }}"

annotations:
Expand All @@ -21,7 +21,7 @@ groups:
labels:
severity: "WARNING"
type: "glucose"
gatewayId: "e6718fcc16adfca0b7f8f6a67"
gatewayId: "671aaecbbf1f103b28e58788"
value: "{{ $value }}"
annotations:
summary: "Low Glucose Alert"
Expand Down
4 changes: 2 additions & 2 deletions cloud/config/read/rules/heartrate_alerts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ groups:
labels:
severity: "WARNING"
type: "heartrate"
gatewayId: "example-gateway-id"
gatewayId: "671aaecbbf1f103b28e58788"
value: "{{ $value }}"

annotations:
Expand All @@ -21,7 +21,7 @@ groups:
labels:
severity: "WARNING"
type: "heartrate"
gatewayId: "e6718fcc16adfca0b7f8f6a67"
gatewayId: "671aaecbbf1f103b28e58788"
value: "{{ $value }}"

annotations:
Expand Down
Loading

0 comments on commit 5c58a8d

Please sign in to comment.