Ticket #18203: 0001-tmp-msg-Base-direct-user-estimates-on-resp-not-reqs.patch

File 0001-tmp-msg-Base-direct-user-estimates-on-resp-not-reqs.patch, 5.6 KB (added by karsten, 3 years ago)
  • modules/clients/init-userstats.sql

    From 3ede7e36d571a1f689ad154ebdb122d6bf0a2264 Mon Sep 17 00:00:00 2001
    From: Karsten Loesing <karsten.loesing@gmx.net>
    Date: Wed, 18 Jan 2017 23:06:10 +0100
    Subject: [PATCH] (tmp msg) Base direct user estimates on resp, not reqs.
    
    Implements #18203.
    ---
     modules/clients/init-userstats.sql                 |  4 ++
     .../src/org/torproject/metrics/clients/Main.java   | 68 +++++++++++++---------
     2 files changed, 46 insertions(+), 26 deletions(-)
    
    diff --git a/modules/clients/init-userstats.sql b/modules/clients/init-userstats.sql
    index 314ff58..7b041a4 100644
    a b CREATE OR REPLACE VIEW combined AS SELECT 
    748748  -- request statistics.
    749749  WHERE a.frac BETWEEN 0.1 AND 1.0
    750750
     751  -- TODO Something went wront in the import of 2016-12 relay data,
     752  -- leading to NaN values in this column.  Should investigate.
     753  AND 'NaN' <> a.rrx
     754
    751755  -- Order results.
    752756  ORDER BY date DESC;
    753757
  • modules/clients/src/org/torproject/metrics/clients/Main.java

    diff --git a/modules/clients/src/org/torproject/metrics/clients/Main.java b/modules/clients/src/org/torproject/metrics/clients/Main.java
    index c9dc004..bbf2ddb 100644
    a b public class Main { 
    9090
    9191  private static void parseRelayExtraInfoDescriptor(
    9292      ExtraInfoDescriptor descriptor) throws IOException {
    93     long publishedMillis = descriptor.getPublishedMillis();
    9493    String fingerprint = descriptor.getFingerprint()
    9594        .toUpperCase();
     95    long publishedMillis = descriptor.getPublishedMillis();
    9696    long dirreqStatsEndMillis = descriptor.getDirreqStatsEndMillis();
    9797    long dirreqStatsIntervalLengthMillis =
    9898        descriptor.getDirreqStatsIntervalLength() * 1000L;
    99     SortedMap<String, Integer> requests = descriptor.getDirreqV3Reqs();
    10099    BandwidthHistory dirreqWriteHistory =
    101100        descriptor.getDirreqWriteHistory();
    102     parseRelayDirreqV3Reqs(fingerprint, publishedMillis,
    103         dirreqStatsEndMillis, dirreqStatsIntervalLengthMillis, requests);
     101    parseRelayDirreqV3Resp(fingerprint, publishedMillis,
     102        dirreqStatsEndMillis, dirreqStatsIntervalLengthMillis,
     103        descriptor.getDirreqV3Resp(), descriptor.getDirreqV3Reqs());
    104104    parseRelayDirreqWriteHistory(fingerprint, publishedMillis,
    105105        dirreqWriteHistory);
    106106  }
    107107
    108   private static void parseRelayDirreqV3Reqs(String fingerprint,
     108  private static void parseRelayDirreqV3Resp(String fingerprint,
    109109      long publishedMillis, long dirreqStatsEndMillis,
    110110      long dirreqStatsIntervalLengthMillis,
     111      SortedMap<String, Integer> responses,
    111112      SortedMap<String, Integer> requests) throws IOException {
    112     if (requests == null
     113    if (responses == null
    113114        || publishedMillis - dirreqStatsEndMillis > ONE_WEEK_MILLIS
    114115        || dirreqStatsIntervalLengthMillis != ONE_DAY_MILLIS) {
    115116      /* Cut off all observations that are one week older than
    public class Main { 
    121122        - dirreqStatsIntervalLengthMillis;
    122123    long utcBreakMillis = (dirreqStatsEndMillis / ONE_DAY_MILLIS)
    123124        * ONE_DAY_MILLIS;
    124     for (int i = 0; i < 2; i++) {
    125       long fromMillis = i == 0 ? statsStartMillis
    126           : utcBreakMillis;
    127       long toMillis = i == 0 ? utcBreakMillis : dirreqStatsEndMillis;
    128       if (fromMillis >= toMillis) {
    129         continue;
    130       }
    131       double intervalFraction =  ((double) (toMillis - fromMillis))
    132           / ((double) dirreqStatsIntervalLengthMillis);
    133       double sum = 0L;
    134       for (Map.Entry<String, Integer> e : requests.entrySet()) {
    135         String country = e.getKey();
    136         double reqs = ((double) e.getValue()) - 4.0;
    137         sum += reqs;
    138         writeOutputLine(fingerprint, "relay", "responses", country,
    139             "", "", fromMillis, toMillis, reqs * intervalFraction,
    140             publishedMillis);
     125    double resp = ((double) responses.get("ok")) - 4.0;
     126    if (resp > 0.0) {
     127      for (int i = 0; i < 2; i++) {
     128        long fromMillis = i == 0 ? statsStartMillis
     129            : utcBreakMillis;
     130        long toMillis = i == 0 ? utcBreakMillis : dirreqStatsEndMillis;
     131        if (fromMillis >= toMillis) {
     132          continue;
     133        }
     134        double intervalFraction =  ((double) (toMillis - fromMillis))
     135            / ((double) dirreqStatsIntervalLengthMillis);
     136        double total = 0L;
     137        SortedMap<String, Double> requestsCopy = new TreeMap<String, Double>();
     138        if (null != requests) {
     139          for (Map.Entry<String, Integer> e : requests.entrySet()) {
     140            if (e.getValue() < 4.0) {
     141              continue;
     142            }
     143            double frequency = ((double) e.getValue()) - 4.0;
     144            requestsCopy.put(e.getKey(), frequency);
     145            total += frequency;
     146          }
     147        }
     148        /* If we're not told any requests, or at least none of them are greater
     149         * than 4, put in a default that we'll attribute all responses to. */
     150        if (total == 0) {
     151          requestsCopy.put("??", 4.0);
     152        }
     153        for (Map.Entry<String, Double> e : requestsCopy.entrySet()) {
     154          double val = resp * intervalFraction * (e.getValue() - 4.0) / total;
     155          writeOutputLine(fingerprint, "relay", "responses", e.getKey(), "", "",
     156              fromMillis, toMillis, val, publishedMillis);
     157        }
     158        writeOutputLine(fingerprint, "relay", "responses", "", "", "",
     159            fromMillis, toMillis, resp * intervalFraction, publishedMillis);
    141160      }
    142       writeOutputLine(fingerprint, "relay", "responses", "", "",
    143           "", fromMillis, toMillis, sum * intervalFraction,
    144           publishedMillis);
    145161    }
    146162  }
    147163