|
@@ -110,6 +110,22 @@ def init_db():
|
|
|
db.create_all()
|
|
|
|
|
|
|
|
|
+def get_targets(uuid):
|
|
|
+ """Returns the queryset of potential targets for the given participant
|
|
|
+ UUID, that is, targets that have not already been handed out to this
|
|
|
+ participant.
|
|
|
+ """
|
|
|
+ participant = Participant.query.filter_by(uuid=uuid, active=True).first_or_404()
|
|
|
+ # We want to get all targets that do not have a relationship with the
|
|
|
+ # given participant. Note that the following lines manipulate SQL
|
|
|
+ # queries, which are only executed at the very end.
|
|
|
+ # This gives all targets that have already been sent to the given
|
|
|
+ # participant.
|
|
|
+ already_done = Target.query.join(handled_targets).filter_by(participant_id=participant.id).with_entities(Target.id)
|
|
|
+ # This takes the negation of the previous set.
|
|
|
+ return Target.query.filter(~Target.id.in_(already_done))
|
|
|
+
|
|
|
+
|
|
|
@app.route('/')
|
|
|
def homepage():
|
|
|
return render_template('home.html')
|
|
@@ -137,21 +153,23 @@ def create_participant():
|
|
|
@app.route('/target/<uuid>')
|
|
|
def get_next_target(uuid):
|
|
|
""""Returns the next target to ping for the given participant"""
|
|
|
- participant = Participant.query.filter_by(uuid=uuid, active=True).first_or_404()
|
|
|
- # We want to get all targets that do not have a relationship with the
|
|
|
- # given participant. Note that the following lines manipulate SQL
|
|
|
- # queries, which are only executed at the very end.
|
|
|
- # This gives all targets that have already been sent to the given
|
|
|
- # participant.
|
|
|
- already_done = Target.query.join(handled_targets).filter_by(participant_id=participant.id).with_entities(Target.id)
|
|
|
- # This takes the negation of the previous set.
|
|
|
- todo = Target.query.filter(~Target.id.in_(already_done))
|
|
|
- target = todo.first()
|
|
|
+ target = get_targets(uuid).first()
|
|
|
if target is not None:
|
|
|
return "{} {}".format(target.id, target)
|
|
|
else:
|
|
|
return ""
|
|
|
|
|
|
+@app.route('/target/<uuid>/<family>')
|
|
|
+def get_next_target_family(uuid, family):
|
|
|
+ """Same as above, but for a specific family ("ipv4" or "ipv6")"""
|
|
|
+ if family not in ("ipv4", "ipv6"):
|
|
|
+ return "Invalid family, should be ipv4 or ipv6\n"
|
|
|
+ predicate = lambda t: t.is_v4() if family == "ipv4" else t.is_v6()
|
|
|
+ targets = [t for t in get_targets(uuid).all() if predicate(t)]
|
|
|
+ if not targets:
|
|
|
+ return ""
|
|
|
+ return "{} {}".format(targets[0].id, targets[0])
|
|
|
+
|
|
|
@app.route('/result/report/<uuid>', methods=['POST'])
|
|
|
def report_result(uuid):
|
|
|
if {'rtt', 'target'}.issubset(request.form):
|