Ping grouping for graphing purposes to PingMutator

This commit is contained in:
Rsl1122 2018-07-16 12:00:44 +03:00
parent ab768c3f4b
commit ead8024ca5
2 changed files with 58 additions and 11 deletions

View File

@ -15,22 +15,31 @@ public class DateHoldersMutator<T extends DateHolder> {
this.dateHolders = dateHolders;
}
public TreeMap<Long, List<T>> groupByStartOfDay() {
public TreeMap<Long, List<T>> groupByStartOfMinute() {
return groupByStartOfSections(TimeAmount.MINUTE.ms());
}
private TreeMap<Long, List<T>> groupByStartOfSections(long sectionLength) {
TreeMap<Long, List<T>> map = new TreeMap<>();
if (dateHolders.isEmpty()) {
return map;
}
long twentyFourHours = 24L * TimeAmount.HOUR.ms();
for (T holder : dateHolders) {
long date = holder.getDate();
long startOfDate = date - (date % twentyFourHours);
long startOfMinute = date - (date % sectionLength);
List<T> list = map.getOrDefault(startOfDate, new ArrayList<>());
List<T> list = map.getOrDefault(startOfMinute, new ArrayList<>());
list.add(holder);
map.put(startOfDate, list);
map.put(startOfMinute, list);
}
return map;
}
public TreeMap<Long, List<T>> groupByStartOfDay() {
long twentyFourHours = 24L * TimeAmount.HOUR.ms();
TreeMap<Long, List<T>> map = groupByStartOfSections(twentyFourHours);
// Empty map firstKey attempt causes NPE if not checked.
if (!map.isEmpty()) {

View File

@ -6,7 +6,10 @@ import com.djrapitops.plan.data.store.keys.CommonKeys;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.TreeMap;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class PingMutator {
@ -20,18 +23,53 @@ public class PingMutator {
return new PingMutator(container.getValue(CommonKeys.PING).orElse(new ArrayList<>()));
}
public Optional<Ping> max() {
Ping maxPing = null;
int max = 0;
public PingMutator filterBy(Predicate<Ping> predicate) {
return new PingMutator(pings.stream().filter(predicate).collect(Collectors.toList()));
}
public PingMutator filterByServer(UUID serverUUID) {
return filterBy(ping -> serverUUID.equals(ping.getServerUUID()));
}
public PingMutator mutateToByMinutePings() {
DateHoldersMutator<Ping> dateMutator = new DateHoldersMutator<>(pings);
TreeMap<Long, List<Ping>> byStartOfMinute = dateMutator.groupByStartOfMinute();
return new PingMutator(byStartOfMinute.entrySet().stream()
.map(entry -> {
PingMutator mutator = new PingMutator(entry.getValue());
return new Ping(entry.getKey(), null,
mutator.min(), mutator.max(), mutator.average());
}).collect(Collectors.toList()));
}
public List<Ping> all() {
return pings;
}
public int max() {
int max = -1;
for (Ping ping : pings) {
Integer value = ping.getMax();
if (value > max) {
max = value;
maxPing = ping;
}
}
return Optional.ofNullable(maxPing);
return max;
}
public int min() {
int min = -1;
for (Ping ping : pings) {
Integer value = ping.getMin();
if (value < min || min == -1) {
min = value;
}
}
return min;
}
public double average() {