Commit 7b39042e authored by Karsten Loesing's avatar Karsten Loesing
Browse files

Break down totalcw numbers by Guard/(Bad)Exit flags.

Requires updating the vote table and the totalcw view in the database.

Implements #28328.
parent 9df35788
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -1562,7 +1562,9 @@ prepare_totalcw <- function(start_p, end_p) {
    filter(if (!is.null(start_p))
        valid_after_date >= as.Date(start_p) else TRUE) %>%
    filter(if (!is.null(end_p))
        valid_after_date <= as.Date(end_p) else TRUE)
        valid_after_date <= as.Date(end_p) else TRUE) %>%
    group_by(valid_after_date, nickname) %>%
    summarize(measured_sum_avg = sum(measured_sum_avg))
}

plot_totalcw <- function(start_p, end_p, path_p) {
+17 −19
Original line number Diff line number Diff line
@@ -66,9 +66,8 @@ class Database implements AutoCloseable {
        "SELECT EXISTS (SELECT 1 FROM vote "
            + "WHERE valid_after = ? AND authority_id = ?)");
    this.psVoteInsert = this.connection.prepareStatement(
        "INSERT INTO vote (valid_after, authority_id, measured_sum) "
            + "VALUES (?, ?, ?)",
        Statement.RETURN_GENERATED_KEYS);
        "INSERT INTO vote (valid_after, authority_id, have_guard_flag, "
        + "have_exit_flag, measured_sum) VALUES (?, ?, ?, ?, ?)");
  }

  /** Insert a parsed vote into the vote table. */
@@ -116,22 +115,17 @@ class Database implements AutoCloseable {
        }
      }
    }
    int voteId = -1;
    for (int measuredSumsIndex = 0; measuredSumsIndex < 4;
         measuredSumsIndex++) {
      this.psVoteInsert.clearParameters();
      this.psVoteInsert.setTimestamp(1,
          Timestamp.from(ZonedDateTime.of(vote.validAfter,
              ZoneId.of("UTC")).toInstant()), calendar);
      this.psVoteInsert.setInt(2, authorityId);
    this.psVoteInsert.setLong(3, vote.measuredSum);
      this.psVoteInsert.setBoolean(3, 1 == (measuredSumsIndex & 1));
      this.psVoteInsert.setBoolean(4, 2 == (measuredSumsIndex & 2));
      this.psVoteInsert.setLong(5, vote.measuredSums[measuredSumsIndex]);
      this.psVoteInsert.execute();
    try (ResultSet rs = this.psVoteInsert.getGeneratedKeys()) {
      if (rs.next()) {
        voteId = rs.getInt(1);
      }
    }
    if (voteId < 0) {
      throw new SQLException("Could not retrieve auto-generated key for new "
          + "vote entry.");
    }
  }

@@ -159,6 +153,10 @@ class Database implements AutoCloseable {
        outputLine.validAfterDate = rs.getDate(
            OutputLine.Column.VALID_AFTER_DATE.name(), calendar).toLocalDate();
        outputLine.nickname = rs.getString(OutputLine.Column.NICKNAME.name());
        outputLine.haveGuardFlag = rs.getBoolean(
            OutputLine.Column.HAVE_GUARD_FLAG.name());
        outputLine.haveExitFlag = rs.getBoolean(
            OutputLine.Column.HAVE_EXIT_FLAG.name());
        outputLine.measuredSumAvg = rs.getLong(
            OutputLine.Column.MEASURED_SUM_AVG.name());
        statistics.add(outputLine);
+10 −2
Original line number Diff line number Diff line
@@ -13,7 +13,8 @@ class OutputLine {
  /** Column names used in the database and in the first line of the output
   * file. */
  enum Column {
    VALID_AFTER_DATE, NICKNAME, MEASURED_SUM_AVG
    VALID_AFTER_DATE, NICKNAME, HAVE_GUARD_FLAG, HAVE_EXIT_FLAG,
    MEASURED_SUM_AVG
  }

  /** Column headers joined together with the given delimiter. */
@@ -28,6 +29,12 @@ class OutputLine {
  /** Server type, which can be "relay" or "bridge". */
  String nickname;

  /** Whether contained relays all have the "Guard" flag. */
  boolean haveGuardFlag;

  /** Whether contained relays all have the "Exit" flag. */
  boolean haveExitFlag;

  /** Mean value of total measured bandwidths of all relays over the day. */
  Long measuredSumAvg;

@@ -35,7 +42,8 @@ class OutputLine {
   * file. */
  @Override
  public String toString() {
    return String.format("%s,%s,%d", validAfterDate, nickname, measuredSumAvg);
    return String.format("%s,%s,%s,%s,%d", validAfterDate, nickname,
        haveGuardFlag ? "t" : "f", haveExitFlag ? "t" : "f", measuredSumAvg);
  }
}
+10 −7
Original line number Diff line number Diff line
@@ -17,18 +17,21 @@ class Parser {
   * contain any bandwidth measurements. */
  TotalcwRelayNetworkStatusVote parseRelayNetworkStatusVote(
      RelayNetworkStatusVote vote) {
    Long measuredSum = null;
    boolean containsMeasuredBandwidths = false;
    long[] measuredSums = new long[4];
    for (NetworkStatusEntry entry : vote.getStatusEntries().values()) {
      if (null == entry.getFlags() || !entry.getFlags().contains("Running")
          || entry.getMeasured() < 0L) {
        continue;
      }
      if (null == measuredSum) {
        measuredSum = 0L;
      containsMeasuredBandwidths = true;
      /* Encode flags as sum of Guard = 1 and (Exit and !BadExit) = 2. */
      int measuredSumsIndex = (entry.getFlags().contains("Guard") ? 1 : 0)
          + (entry.getFlags().contains("Exit")
          && !entry.getFlags().contains("BadExit") ? 2 : 0);
      measuredSums[measuredSumsIndex] += entry.getMeasured();
    }
      measuredSum += entry.getMeasured();
    }
    if (null == measuredSum) {
    if (!containsMeasuredBandwidths) {
      /* Return null, because we wouldn't want to add this vote to the database
       * anyway. */
      return null;
@@ -39,7 +42,7 @@ class Parser {
        .atZone(ZoneId.of("UTC")).toLocalDateTime();
    parsedVote.identityHex = vote.getIdentity();
    parsedVote.nickname = vote.getNickname();
    parsedVote.measuredSum = measuredSum;
    parsedVote.measuredSums = measuredSums;
    return parsedVote;
  }
}
+4 −2
Original line number Diff line number Diff line
@@ -19,7 +19,9 @@ class TotalcwRelayNetworkStatusVote {
   * key. */
  String identityHex;

  /** Sum of bandwidth measurements of all contained status entries. */
  long measuredSum;
  /** Sums of bandwidth measurements of all contained status entries with four
   * entries: 0 = neither Exit nor Guard, 1 = only Guard, 2 = only Exit, and
   * 3 = both Guard and Exit. */
  long[] measuredSums;
}
Loading