Skip to content

Commit

Permalink
feat : Alert Management : add different severity level
Browse files Browse the repository at this point in the history
  • Loading branch information
annadiplacido committed Oct 14, 2024
1 parent 2bf9fb5 commit 5a7f9b4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.etu.polytech.dto.AlertDTO;
import fr.etu.polytech.entity.Alert;
import fr.etu.polytech.entity.Severity;
import fr.etu.polytech.exception.IncorrectRequestException;
import fr.etu.polytech.exception.ResourceNotFoundException;
import fr.etu.polytech.repository.AlertRepository;
Expand Down Expand Up @@ -81,11 +82,23 @@ public List<Alert> getAllAlerts() {
return alertRepository.listAll();
}

@GET
@Path("/severity/{severity}")
public List<Alert> getAlertsBySeverity(@PathParam("severity") String severity) {
try {
Severity sev = Severity.valueOf(severity.toUpperCase());
return alertRepository.findBySeverity(sev);
} catch (IllegalArgumentException e) {
throw new WebApplicationException("Invalid severity level", 400);
}
}


@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createAlert(@Valid AlertDTO alertDTO) throws IncorrectRequestException {
Alert alert = new Alert(alertDTO.type(), alertDTO.message(), alertDTO.gatewayId(),alertDTO.value());
Alert alert = new Alert(alertDTO.type(), alertDTO.message(), alertDTO.gatewayId(),alertDTO.value(),alertDTO.severity());
alertRepository.persist(alert);
return Response.status(Response.Status.CREATED).entity(alert).build();
}
Expand Down Expand Up @@ -171,4 +184,6 @@ public void validateGatewayId(String gatewayId) throws IncorrectRequestException
}
}



}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package fr.etu.polytech.dto;
import fr.etu.polytech.entity.Severity;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PositiveOrZero;
Expand All @@ -15,6 +16,8 @@ public record AlertDTO(
@NotNull(message = "Gateway ID must not be null")
@NotEmpty(message = "Gateway ID must not be empty")
String gatewayId,
int value
int value,
@NotNull(message = "Severity must not be null")
Severity severity
) {}

Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ public class Alert extends PanacheMongoEntity {
public LocalDateTime timestamp;
public boolean treated;
private int value;
private Severity severity;

public Alert() {}

public Alert(String type, String message, String gatewayId, int value) {
public Alert(String type, String message, String gatewayId, int value,Severity severity) {
this.type = type;
this.message = message;
this.gatewayId = gatewayId;
this.timestamp = LocalDateTime.now();
this.treated = false;
this.value = value;
this.severity = severity;
}

public String getType() {
Expand Down Expand Up @@ -77,4 +79,11 @@ public int getValue() {
public void setValue(int value) {
this.value = value;
}
public Severity getSeverity() {
return severity;
}

public void setSeverity(Severity severity) {
this.severity = severity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.etu.polytech.entity;

public enum Severity {
INFO,
WARNING,
CRITICAL
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.etu.polytech.repository;
import fr.etu.polytech.entity.Alert;

import fr.etu.polytech.entity.Severity;
import io.quarkus.mongodb.panache.PanacheMongoRepository;
import jakarta.enterprise.context.ApplicationScoped;

Expand All @@ -18,4 +19,7 @@ public List<Alert> findByGatewayId(String gatewayId) {
return find("gatewayId", gatewayId).list();
}

public List<Alert> findBySeverity(Severity severity) {
return find("severity", severity).list();
}
}

0 comments on commit 5a7f9b4

Please sign in to comment.