diff options
author | Jordan <me@jordan.im> | 2020-09-04 12:19:43 -0700 |
---|---|---|
committer | Jordan <me@jordan.im> | 2020-09-04 12:19:43 -0700 |
commit | 9a33d379d8fa7fc5e4f59b122d9212a07fadb37a (patch) | |
tree | bf33257c1226e74e99ec1fda2a83fc87693a6975 /tor-metrics/relays.py | |
parent | 194dca5d1d6031249a79d1d44174a31d19163679 (diff) | |
download | allium-9a33d379d8fa7fc5e4f59b122d9212a07fadb37a.tar.gz allium-9a33d379d8fa7fc5e4f59b122d9212a07fadb37a.zip |
add support for sorting by contact info
Diffstat (limited to 'tor-metrics/relays.py')
-rw-r--r-- | tor-metrics/relays.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tor-metrics/relays.py b/tor-metrics/relays.py index a64f097..2b79af5 100644 --- a/tor-metrics/relays.py +++ b/tor-metrics/relays.py @@ -5,6 +5,7 @@ Relays class object consisting of relays (list of dict) and onionoo fetch timestamp ''' +import hashlib import json import os import time @@ -18,6 +19,27 @@ ABS_PATH = os.path.dirname(os.path.abspath(__file__)) ENV = Environment(loader=FileSystemLoader(os.path.join(ABS_PATH, 'templates')), trim_blocks=True, lstrip_blocks=True) +def hash_filter(value, hash_type='md5'): + ''' + Custom hash filter for jinja; defaults to "md5" if no type specified + + :param value: value to be hashed + :param hash_type: valid hash type + :return: computed hash as a hexadecimal string + ''' + hash_func = getattr(hashlib, hash_type, None) + + if hash_func: + computed_hash = hash_func(value.encode("utf-8")).hexdigest() + else: + raise AttributeError( + "No hashing function named {hname}".format(hname=hash_type) + ) + + return computed_hash + +ENV.filters['hash'] = hash_filter + class Relays: ''' Relay class consisting of relays (list of dict) and onionoo fetch timestamp @@ -174,6 +196,26 @@ class Relays: else: self.json['sorted']['family'][member]['middle_count'] += 1 + c_str = relay.get('contact', '').encode('utf-8') + c_hash = hashlib.md5(c_str).hexdigest() + if 'contact' not in self.json['sorted']: + self.json['sorted']['contact'] = dict() + if not c_hash in self.json['sorted']['contact']: + self.json['sorted']['contact'][c_hash] = { + 'relays': list(), + 'contact': c_str, + 'bw': 0, + 'exit_count': 0, + 'middle_count': 0 + } + bw = relay['observed_bandwidth'] + self.json['sorted']['contact'][c_hash]['relays'].append(idx) + self.json['sorted']['contact'][c_hash]['bw'] += bw + if 'Exit' in relay['flags']: + self.json['sorted']['contact'][c_hash]['exit_count'] += 1 + else: + self.json['sorted']['contact'][c_hash]['middle_count'] += 1 + def create_output_dir(self): ''' Ensure config:output_root exists (required for write functions) |