60 lines
1.8 KiB
Java
60 lines
1.8 KiB
Java
package dev.dinauer.monitoring;
|
|
|
|
import dev.dinauer.monitoring.indexing.IndexCollection;
|
|
import dev.dinauer.monitoring.indexing.IndexMetric;
|
|
import dev.dinauer.monitoring.indexing.IndexMetricsRepo;
|
|
import dev.dinauer.monitoring.indexing.TimeUnit;
|
|
import dev.dinauer.utils.ClientProvider;
|
|
import io.fabric8.kubernetes.api.model.Node;
|
|
import jakarta.inject.Inject;
|
|
import jakarta.ws.rs.GET;
|
|
import jakarta.ws.rs.Path;
|
|
import jakarta.ws.rs.PathParam;
|
|
import jakarta.ws.rs.QueryParam;
|
|
|
|
import java.io.IOException;
|
|
import java.time.ZonedDateTime;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@Path("/monitorings/nodes/jobs")
|
|
public class NodeMonitoringJobResource
|
|
{
|
|
@Inject
|
|
IndexMetricsRepo indexMetricsRepo;
|
|
|
|
@Inject
|
|
ClientProvider clientProvider;
|
|
|
|
@GET
|
|
public List<MonitoredResource<Node, IndexCollection>> get(@QueryParam("from") ZonedDateTime from, @QueryParam("to") ZonedDateTime to) throws IOException
|
|
{
|
|
List<MonitoredResource<Node, IndexCollection>> result = new ArrayList<>();
|
|
List<Node> nodes = clientProvider.getClient().nodes().list().getItems();
|
|
for (Node node : nodes)
|
|
{
|
|
String resource = String.format("NODE-%s", node.getMetadata().getUid());
|
|
result.add(new MonitoredResource<>(node, indexMetricsRepo.findByResourceAndMetricAndTimeUnit(resource, "NODE_METRICS", determineTimeUnit(from, to))));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private TimeUnit determineTimeUnit(ZonedDateTime from, ZonedDateTime to)
|
|
{
|
|
long day = 60 * 60 * 24;
|
|
long twoDays = day * 2;
|
|
long fifteenDays = day * 15;
|
|
|
|
long dif = to.toEpochSecond() - from.toEpochSecond();
|
|
if (dif < twoDays)
|
|
{
|
|
return TimeUnit.RAW;
|
|
}
|
|
if (dif < fifteenDays)
|
|
{
|
|
return TimeUnit.HOUR;
|
|
}
|
|
return TimeUnit.DAY;
|
|
}
|
|
}
|