-
Notifications
You must be signed in to change notification settings - Fork 210
/
UnhealthyEndpoint.java
55 lines (46 loc) · 2.13 KB
/
UnhealthyEndpoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package pl.allegro.tech.hermes.management.api;
import org.springframework.beans.factory.annotation.Autowired;
import pl.allegro.tech.hermes.api.OwnerId;
import pl.allegro.tech.hermes.api.UnhealthySubscription;
import pl.allegro.tech.hermes.management.domain.owner.OwnerSources;
import pl.allegro.tech.hermes.management.domain.subscription.SubscriptionService;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Optional;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
@Path("unhealthy")
public class UnhealthyEndpoint {
private final OwnerSources ownerSources;
private final SubscriptionService subscriptionService;
@Autowired
public UnhealthyEndpoint(OwnerSources ownerSources,
SubscriptionService subscriptionService) {
this.ownerSources = ownerSources;
this.subscriptionService = subscriptionService;
}
@GET
@Produces({APPLICATION_JSON, TEXT_PLAIN})
@Path("/")
public Response listUnhealthy(
@QueryParam("ownerSourceName") String ownerSourceName,
@QueryParam("ownerId") String id,
@DefaultValue("true") @QueryParam("respectMonitoringSeverity") boolean respectMonitoringSeverity) {
Optional<OwnerId> ownerId = resolveOwnerId(ownerSourceName, id);
List<UnhealthySubscription> unhealthySubscriptions = ownerId.isPresent()
? subscriptionService.getUnhealthyForOwner(ownerId.get(), respectMonitoringSeverity)
: subscriptionService.getAllUnhealthy(respectMonitoringSeverity);
return Response.ok()
.entity(new GenericEntity<List<UnhealthySubscription>>(unhealthySubscriptions){})
.build();
}
private Optional<OwnerId> resolveOwnerId(String ownerSourceName, String id) {
return ownerSources.getByName(ownerSourceName).map(ownerSource -> new OwnerId(ownerSource.name(), id));
}
}