Changeset - 7653ca815385
[Not reviewed]
Dennis Fink - 9 years ago 2016-03-03 00:16:04
dennis.fink@c3l.lu
Merged dev
5 files changed with 44 insertions and 26 deletions:
0 comments (0 inline, 0 general)
ennstatus/api/model.py
Show inline comments
 
@@ -24,10 +24,11 @@ from datetime import datetime
 

	
 
import jsonschema
 
import strict_rfc3339
 
import requests
 

	
 
from flask import current_app
 
from pkg_resources import resource_filename
 
from onion_py.manager import Manager
 
from onion_py.caching import OnionSimpleCache
 

	
 
from ..utils import check_ip
 

	
 
@@ -45,6 +46,8 @@ validate = functools.partial(
 
    format_checker=jsonschema.FormatChecker()
 
)
 

	
 
manager = Manager(OnionSimpleCache())
 

	
 

	
 
def calculate_weight(data):
 

	
 
@@ -57,9 +60,9 @@ def calculate_weight(data):
 
        except KeyError:
 
            continue
 

	
 
        factor = subdata['factor']
 
        factor = subdata.factor
 

	
 
        values = [x * factor for x in subdata['values'] if x is not None]
 
        values = [x * factor for x in subdata.values if x is not None]
 

	
 
        if values:
 
            obj[subkey] = statistics.mean(values) * 100
 
@@ -117,6 +120,7 @@ class Server:
 
        self.last_updated = kwargs['last_updated']
 
        self.country = kwargs['country']
 
        self.bandwidth = kwargs.get('bandwidth')
 
        self.flags = kwargs.get('flags')
 

	
 
        if self.type == 'bridge':
 
            self.obfs = kwargs.get('obfs')
 
@@ -224,30 +228,37 @@ class Server:
 
        if self.type not in ('exit', 'relay'):
 
            raise NotImplementedError
 

	
 
        url = 'https://onionoo.torproject.org/weights?lookup={}'.format(
 
            self.fingerprint
 
        try:
 
            data = manager.query('weights', lookup=self.fingerprint)
 
        except:
 
            raise NotImplementedError
 

	
 
        if data is not None:
 
            data = data.relays[0]
 

	
 
        self.mean_consensus_weight = calculate_weight(data.consensus_weight)
 
        self.mean_exit_probability = calculate_weight(data.exit_probability)
 
        self.mean_guard_probability = calculate_weight(
 
            data.guard_probability
 
        )
 
        self.mean_middle_probability = calculate_weight(
 
            data.middle_probability
 
        )
 
        self.mean_consensus_weight_fraction = calculate_weight(
 
            data.consensus_weight_fraction
 
        )
 

	
 
        data = requests.get(url)
 
    def update_flags(self):
 

	
 
        try:
 
            data.raise_for_status()
 
        except requests.HTTPError as e:
 
            raise e
 
        else:
 
            data = data.json()['relays'][0]
 
            data = manager.query('details', lookup=self.fingerprint)
 
        except:
 
            raise NotImplementedError
 

	
 
        self.mean_consensus_weight = calculate_weight(data['consensus_weight'])
 
        self.mean_exit_probability = calculate_weight(data['exit_probability'])
 
        self.mean_guard_probability = calculate_weight(
 
            data['guard_probability']
 
        )
 
        self.mean_middle_probability = calculate_weight(
 
            data['middle_probability']
 
        )
 
        self.mean_consensus_weight_fraction = calculate_weight(
 
            data['consensus_weight_fraction']
 
        )
 
        if data is not None:
 
            self.flags = data.relays[0].flags
 
        else:
 
            raise NotImplementedError
 

	
 
    def check_status(self):
 

	
ennstatus/api/schema/server.json
Show inline comments
 
@@ -100,6 +100,11 @@
 
        },
 
        "type": {
 
            "type": "string"
 
        },
 
        "flags": {
 
            "type": "array",
 
            "items": { "type": "string" },
 
            "uniqueItems": true
 
        }
 
    },
 
    "required": [
ennstatus/api/views.py
Show inline comments
 
@@ -26,7 +26,6 @@ from werkzeug.exceptions import BadReque
 

	
 
import strict_rfc3339
 
import pygeoip
 
import requests
 

	
 
from ennstatus import csrf
 
from ennstatus.status.functions import (single_server, all_servers,
 
@@ -120,8 +119,10 @@ def update():
 
        server.update_weights()
 
    except NotImplementedError:
 
        pass
 
    except requests.HTTPError as e:
 
        current_app.logger.error(str(e), exc_info=True)
 

	
 
    try:
 
        server.update_flags()
 
    except NotImplementedError:
 
        pass
 

	
 
    try:
requirements.in
Show inline comments
 
@@ -11,3 +11,4 @@ pygeoip
 
python-gnupg
 
requests
 
strict-rfc3339
 
OnionPy
requirements.txt
Show inline comments
 
@@ -18,10 +18,10 @@ itsdangerous==0.24        # via flask
 
Jinja2==2.8               # via flask
 
jsonschema==2.5.1
 
MarkupSafe==0.23          # via jinja2
 
onionpy==0.3.2
 
pygeoip==0.3.2
 
python-gnupg==0.3.8
 
pytz==2015.7              # via babel
 
requests==2.9.1
 
strict-rfc3339==0.6
 
visitor==0.1.2            # via flask-bootstrap
 
Werkzeug==0.11.4          # via flask, flask-wtf
0 comments (0 inline, 0 general)