Skip to content

Commit

Permalink
Sybil: Keep track of points alongside the reasons so that we can refe…
Browse files Browse the repository at this point in the history
…rence points-per-reason
  • Loading branch information
eyedeekay committed Jun 18, 2024
1 parent b656193 commit bfcfaf5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ private void renderOverview(Writer out, StringBuilder buf, String nonce, Analysi
first = false;
}
buf.append('>').append(DataHelper.formatTime(date.longValue())).append("</option>\n");
}
}
buf.append("</select>\n" +
"<input type=\"submit\" name=\"action\" class=\"go\" value=\"Review analysis\" />" +
"</form>\n");
}
}
writeBuf(out, buf);
}

Expand Down Expand Up @@ -543,10 +543,10 @@ private void renderThreatsHTML(Writer out, StringBuilder buf, long date, Map<Has
if (p < minDisplay)
break; // sorted
buf.append("<p class=\"threatpoints\"><b>Threat Points: " + fmt.format(p) + "</b></p><ul>");
List<String> reasons = pp.getReasons();
Map<String, Double> reasons = pp.getReasons();
if (reasons.size() > 1)
Collections.sort(reasons, rcomp);
for (String s : reasons) {
//Collections.sort(reasons, rcomp);
for (String s : reasons.keySet()) {
int c = s.indexOf(':');
if (c <= 0)
continue;
Expand Down
39 changes: 26 additions & 13 deletions router/java/src/net/i2p/router/sybil/Points.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import net.i2p.data.DataHelper;

Expand All @@ -12,14 +14,12 @@
* @since 0.9.38 moved from SybilRenderer
*/
public class Points implements Comparable<Points> {
private double points;
private final List<String> reasons;

private final Map<String, Double> reasons;
/**
* @since 0.9.38
*/
private Points() {
reasons = new ArrayList<String>(4);
reasons = new ConcurrentHashMap<String, Double>(4);
}

/**
Expand All @@ -30,17 +30,25 @@ public Points(double d, String reason) {
addPoints(d, reason);
}

private double points() {
double rv = 0;
for (String reason: reasons.keySet()){
rv += reasons.get(reason);
}
return rv;
}

/**
* @since 0.9.38
*/
public double getPoints() {
return points;
return points();
}

/**
* @since 0.9.38
*/
public List<String> getReasons() {
public Map<String, Double> getReasons() {
return reasons;
}

Expand All @@ -49,14 +57,20 @@ public List<String> getReasons() {
* @since 0.9.38
*/
public void addPoints(double d, String reason) {
points += d;
DecimalFormat format = new DecimalFormat("#0.00");
String rsn = format.format(d) + ": " + reason;
reasons.add(rsn);
Double rp = reasons.get(rsn);
if (rp == null) {
// reason was not yet present in the map, create a new entry for it.
reasons.put(rsn, d);
}else{
// reason was present in the map, add the points to it.
rp += d;
}
}

public int compareTo(Points r) {
return Double.compare(points, r.points);
return Double.compare(points(), r.points());
}

/**
Expand All @@ -79,8 +93,8 @@ public String toString() {
* @since 0.9.38
*/
public void toString(StringBuilder buf) {
buf.append(points);
for (String r : reasons) {
buf.append(points());
for (String r : reasons.keySet()) {
buf.append('%').append(r.replace("%", "&#x25;"));
}
}
Expand All @@ -102,9 +116,8 @@ public static Points fromString(String s) {
}
Points rv = new Points();
for (int i = 1; i < ss.length; i++) {
rv.reasons.add(ss[i]);
rv.reasons.put(ss[i], d);
}
rv.points = d;
return rv;
}
}
Expand Down

0 comments on commit bfcfaf5

Please sign in to comment.