-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
665 additions
and
505 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 0 additions & 97 deletions
97
core/src/main/java/bisq/core/network/p2p/inventory/InventoryItem.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
core/src/main/java/bisq/core/network/p2p/inventory/model/Average.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
core/src/main/java/bisq/core/network/p2p/inventory/model/DeviationByIntegerDiff.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
core/src/main/java/bisq/core/network/p2p/inventory/model/DeviationByPercentage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.