Skip to content

Commit

Permalink
Improve deviation model
Browse files Browse the repository at this point in the history
  • Loading branch information
chimp1984 committed Oct 23, 2020
1 parent 0c4eb14 commit f1fdf3c
Show file tree
Hide file tree
Showing 17 changed files with 665 additions and 505 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import bisq.core.dao.state.DaoStateService;
import bisq.core.network.p2p.inventory.messages.GetInventoryRequest;
import bisq.core.network.p2p.inventory.messages.GetInventoryResponse;
import bisq.core.network.p2p.inventory.model.InventoryItem;

import bisq.network.p2p.network.Connection;
import bisq.network.p2p.network.MessageListener;
Expand All @@ -48,11 +49,9 @@
import com.google.common.base.Optional;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import java.lang.management.ManagementFactory;

Expand All @@ -68,7 +67,6 @@ public class GetInventoryRequestHandler implements MessageListener {
private final ProposalStateMonitoringService proposalStateMonitoringService;
private final BlindVoteStateMonitoringService blindVoteStateMonitoringService;
private final int maxConnections;
private final Set<String> permittedRequestersPubKey = new HashSet<>();

@Inject
public GetInventoryRequestHandler(NetworkNode networkNode,
Expand All @@ -94,10 +92,6 @@ public GetInventoryRequestHandler(NetworkNode networkNode,
@Override
public void onMessage(NetworkEnvelope networkEnvelope, Connection connection) {
if (networkEnvelope instanceof GetInventoryRequest) {
if (permittedRequestersPubKey.isEmpty()) {
return;
}

// Data
GetInventoryRequest getInventoryRequest = (GetInventoryRequest) networkEnvelope;
Map<InventoryItem, Integer> dataObjects = new HashMap<>();
Expand Down Expand Up @@ -179,8 +173,4 @@ public void onMessage(NetworkEnvelope networkEnvelope, Connection connection) {
public void shutDown() {
networkNode.removeMessageListener(this);
}

public void addPermittedRequestersPubKey(String pubKey) {
permittedRequestersPubKey.add(pubKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package bisq.core.network.p2p.inventory;

import bisq.core.network.p2p.inventory.model.InventoryItem;

import bisq.network.p2p.NodeAddress;
import bisq.network.p2p.network.NetworkNode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import bisq.core.network.p2p.inventory.messages.GetInventoryRequest;
import bisq.core.network.p2p.inventory.messages.GetInventoryResponse;
import bisq.core.network.p2p.inventory.model.InventoryItem;

import bisq.network.p2p.NodeAddress;
import bisq.network.p2p.network.CloseConnectionReason;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package bisq.core.network.p2p.inventory.messages;

import bisq.core.network.p2p.inventory.InventoryItem;
import bisq.core.network.p2p.inventory.model.InventoryItem;

import bisq.common.app.Version;
import bisq.common.proto.network.NetworkEnvelope;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.core.network.p2p.inventory.model;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class Average {
public static Map<InventoryItem, Double> of(Set<RequestInfo> requestInfoSet) {
Map<InventoryItem, Double> averageValuesPerItem = new HashMap<>();
Arrays.asList(InventoryItem.values()).forEach(inventoryItem -> {
if (inventoryItem.isNumberValue()) {
averageValuesPerItem.put(inventoryItem, getAverage(requestInfoSet, inventoryItem));
}
});
return averageValuesPerItem;
}

public static double getAverage(Set<RequestInfo> requestInfoSet, InventoryItem inventoryItem) {
return requestInfoSet.stream()
.map(RequestInfo::getInventory)
.filter(Objects::nonNull)
.filter(inventory -> inventory.containsKey(inventoryItem))
.mapToDouble(inventory -> Double.parseDouble((inventory.get(inventoryItem))))
.average()
.orElse(0d);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.core.network.p2p.inventory.model;

import bisq.common.util.Tuple2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.jetbrains.annotations.Nullable;

public class DeviationByIntegerDiff implements DeviationType {
private final int warnTrigger;
private final int alertTrigger;

public DeviationByIntegerDiff(int warnTrigger, int alertTrigger) {
this.warnTrigger = warnTrigger;
this.alertTrigger = alertTrigger;
}

public DeviationSeverity getDeviationSeverity(Collection<List<RequestInfo>> collection,
@Nullable String value,
InventoryItem inventoryItem) {
DeviationSeverity deviationSeverity = DeviationSeverity.OK;
if (value == null) {
return deviationSeverity;
}

Map<String, Integer> sameItemsByValue = new HashMap<>();
collection.stream()
.filter(list -> !list.isEmpty())
.map(list -> list.get(list.size() - 1)) // We use last item only
.map(RequestInfo::getInventory)
.filter(Objects::nonNull)
.map(e -> e.get(inventoryItem))
.forEach(e -> {
sameItemsByValue.putIfAbsent(e, 0);
int prev = sameItemsByValue.get(e);
sameItemsByValue.put(e, prev + 1);
});
if (sameItemsByValue.size() > 1) {
List<Tuple2<String, Integer>> sameItems = new ArrayList<>();
sameItemsByValue.forEach((k, v) -> sameItems.add(new Tuple2<>(k, v)));
sameItems.sort(Comparator.comparing(o -> o.second));
Collections.reverse(sameItems);
String majority = sameItems.get(0).first;
if (!majority.equals(value)) {
int majorityAsInt = Integer.parseInt(majority);
int valueAsInt = Integer.parseInt(value);
int diff = Math.abs(majorityAsInt - valueAsInt);
if (diff >= alertTrigger) {
deviationSeverity = DeviationSeverity.ALERT;
} else if (diff >= warnTrigger) {
deviationSeverity = DeviationSeverity.WARN;
} else {
deviationSeverity = DeviationSeverity.OK;
}
}
}
return deviationSeverity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.core.network.p2p.inventory.model;

public class DeviationByPercentage implements DeviationType {
private final double lowerAlertTrigger;
private final double upperAlertTrigger;
private final double lowerWarnTrigger;
private final double upperWarnTrigger;

public DeviationByPercentage(double lowerAlertTrigger,
double upperAlertTrigger,
double lowerWarnTrigger,
double upperWarnTrigger) {
this.lowerAlertTrigger = lowerAlertTrigger;
this.upperAlertTrigger = upperAlertTrigger;
this.lowerWarnTrigger = lowerWarnTrigger;
this.upperWarnTrigger = upperWarnTrigger;
}

public DeviationSeverity getDeviationSeverity(double deviation) {
if (deviation <= lowerAlertTrigger || deviation >= upperAlertTrigger) {
return DeviationSeverity.ALERT;
}

if (deviation <= lowerWarnTrigger || deviation >= upperWarnTrigger) {
return DeviationSeverity.WARN;
}

return DeviationSeverity.OK;
}
}
Loading

0 comments on commit f1fdf3c

Please sign in to comment.