def get_all_rounds_results(): results = {} results['scores'] = {} results['rounds'] = [] rounds = [] scores = {} blue_teams = session.query(Team).filter(Team.color == 'Blue').all() last_round_obj = session.query(Round).order_by(Round.number.desc()).first() if last_round_obj: last_round = last_round_obj.number for round_num in range(0, last_round + 1): rounds.append("Round " + str(round_num)) rgb_colors = {} team_names = [] for team in blue_teams: scores[team.name] = team.get_array_of_scores(last_round) rgb_colors[team.name] = team.rgb_color team_names.append(team.name) results['team_names'] = team_names results['rgb_colors'] = rgb_colors results['rounds'] = rounds results['scores'] = scores return results
def get_all_rounds_results(): results = {} results["scores"] = {} results["rounds"] = [] rounds = [] scores = {} blue_teams = session.query(Team).filter(Team.color == "Blue").all() last_round_obj = session.query(func.max(Round.number)).scalar() if last_round_obj: last_round = last_round_obj rounds = ["Round {}".format(x) for x in range(0, last_round + 1)] # for round_num in range(0, last_round + 1): # rounds.append("Round " + str(round_num)) rgb_colors = {} team_names = [] for team in blue_teams: scores[team.name] = team.get_array_of_scores(last_round) rgb_colors[team.name] = team.rgb_color team_names.append(team.name) results["team_names"] = team_names results["rgb_colors"] = rgb_colors results["rounds"] = rounds results["scores"] = scores return results
class TestIntegration(object): def test_overall(self): blue_teams = Team.get_all_blue_teams() assert len(blue_teams) == 10, \ "Incorrect number of blue teams" def test_round_num(self): assert Round.get_last_round_num() == NUM_OVERALL_ROUNDS, \ "Expecting only {0} of rounds to have run...".format(NUM_OVERALL_ROUNDS) @pytest.mark.parametrize("blue_team", Team.get_all_blue_teams()) def test_blue_teams(self, blue_team): assert len(blue_team.services) == NUM_TESTBED_SERVICES, \ "Invalid number of services enabled per team {0}".format(blue_team.name) assert blue_team.current_score == (SERVICE_TOTAL_POINTS_PER_ROUND * NUM_OVERALL_ROUNDS), \ "Invalid number of overall points per team {0}".format(blue_team.name) @pytest.mark.parametrize("service", session.query(Service).all()) def test_services(self, service): assert service.last_check_result() is True, \ "{0} service failed on {1}".format(service.name, service.team.name) assert service.percent_earned == 100 @pytest.mark.parametrize("check", session.query(Check).all()) def test_checks(self, check): assert check.result is True, \ "{0} on round {1} failed for team {2}\nReason: {3}\nOutput: {4}".format(check.service.name, check.round.number, check.service.team.name, check.reason, check.output)
def manage(): if current_user.is_white_team: users = session.query(User).with_entities(User.id, User.username).all() teams = session.query(Team).with_entities(Team.id, Team.name).all() blue_teams = Team.get_all_blue_teams() return render_template('admin/manage.html', users=sorted(users, key=itemgetter(0)), teams=teams, blue_teams=blue_teams) else: return redirect(url_for('auth.unauthorized'))
def admin_get_engine_stats(): if current_user.is_white_team: engine_stats = {} engine_stats['round_number'] = Round.get_last_round_num() engine_stats['num_passed_checks'] = session.query(Check).filter_by( result=True).count() engine_stats['num_failed_checks'] = session.query(Check).filter_by( result=False).count() engine_stats['total_checks'] = session.query(Check).count() return jsonify(engine_stats) else: return {'status': 'Unauthorized'}, 403
def overview_get_data(): # columns = get_table_columns() data = [] blue_teams = Team.get_all_blue_teams() last_round = Round.get_last_round_num() current_scores = ["Current Score"] current_places = ["Current Place"] service_ratios = ["Up/Down Ratio"] if len(blue_teams) > 0: for blue_team in blue_teams: num_up_services = (session.query( Service.team_id, ).join(Check).join(Round).filter( Check.result.is_(True)).filter( Service.team_id == blue_team.id).filter( Round.number == last_round).count()) num_down_services = (session.query( Service.team_id, ).join(Check).join(Round).filter( Check.result.is_(False)).filter( Service.team_id == blue_team.id).filter( Round.number == last_round).count()) current_scores.append(str(blue_team.current_score)) current_places.append(str(blue_team.place)) service_ratios.append("{0} / {1}".format(num_up_services, num_down_services)) data.append(current_scores) data.append(current_places) data.append(service_ratios) services = [] services.append([ service[0] for service in session.query(Service.name).distinct( Service.name).group_by(Service.name).all() ]) for blue_team in blue_teams: checks = (session.query(Check.result).join(Service).filter( Check.round_id == last_round).filter( Service.team_id == blue_team.id).order_by( Service.name).all()) services.append([check[0] for check in checks]) data += list( zip(*services)) # zip these to get the right datatables format return json.dumps({"data": data}) else: return "{}"
def overview_data(): # services_data = session.query( # Service.team_id, # Service.check_name, # Service.host, # Service.port, # Check.result, # func.max(Check.completed_timestamp), # ) \ # .join(Check) \ # .group_by(Service.team_id) \ # .group_by(Service.check_name) \ # .all() team_data = {} teams = session.query(Team).filter(Team.color == "Blue").order_by( Team.name).all() # teams = session.query(Team.id, Team.name).filter(Team.color == 'Blue').order_by(Team.name).all() for team in teams: query_data = (session.query( Service.team_id, Team.name, Service.name, Check.result, func.max(Check.completed_timestamp), Service.host, Service.port, ).join(Check).join(Team).filter(Service.team_id == team.id).group_by( Service.team_id).group_by(Service.name).all()) service_data = { x[2]: { "host": x[5], "passing": x[3], "port": x[6] } for x in query_data } # service_data = {} # for service in team.services: # service_data[service.name] = { # 'passing': service.last_check_result(), # 'host': service.host, # 'port': service.port, # } team_data[team.name] = service_data return jsonify(team_data)
def api_services(team_id): team = session.query(Team).get(team_id) if team is None or not current_user.team == team or not current_user.is_blue_team: return {'status': 'Unauthorized'}, 403 data = [] sorted_services = sorted(team.services, key=lambda service: service.id) for service in sorted_services: if not service.checks: check = 'Undetermined' else: if service.last_check_result(): check = 'UP' else: check = 'DOWN' data.append( dict(service_id=service.id, service_name=service.name, host=service.host, port=service.port, check=check, rank=service.rank, score_earned=service.score_earned, max_score=service.max_score, percent_earned=service.percent_earned, pts_per_check=service.points, last_ten_checks=[ check.result for check in service.last_ten_checks[::-1] ])) return jsonify(data=data)
def services_get_team_data(team_id): team = session.query(Team).get(team_id) if team is None or not current_user.team == team or not current_user.is_blue_team: return {'status': 'Unauthorized'}, 403 data = {'place': team.place, 'current_score': team.current_score} return jsonify(data)
def overview_get_data(): columns = get_table_columns() data = [] blue_teams = Team.get_all_blue_teams() if len(blue_teams) > 0: current_score_row_data = {'': 'Current Score'} current_place_row_data = {'': 'Current Place'} for blue_team in blue_teams: current_score_row_data[blue_team.name] = blue_team.current_score current_place_row_data[blue_team.name] = blue_team.place data.append(current_score_row_data) data.append(current_place_row_data) for service in blue_teams[0].services: service_row_data = {'': service.name} for blue_team in blue_teams: service = session.query(Service).filter( Service.name == service.name).filter( Service.team == blue_team).first() service_text = service.host if str(service.port) != '0': service_text += ':' + str(service.port) service_data = { 'result': str(service.last_check_result()), 'host_info': service_text } service_row_data[blue_team.name] = service_data data.append(service_row_data) return json.dumps({'columns': columns, 'data': data}) else: return '{}'
def admin_update_check(): if current_user.is_white_team: if 'name' in request.form and 'value' in request.form and 'pk' in request.form: check = session.query(Check).get(int(request.form['pk'])) if check: modified_check = False if request.form['name'] == 'check_value': if request.form['value'] == '1': check.result = True elif request.form['value'] == '2': check.result = False modified_check = True elif request.form['name'] == 'check_reason': modified_check = True check.reason = request.form['value'] if modified_check: session.add(check) session.commit() update_scoreboard_data() update_overview_data() update_services_navbar(check.service.team.id) update_team_stats(check.service.team.id) update_services_data(check.service.team.id) update_service_data(check.service.id) return jsonify({'status': 'Updated Property Information'}) return jsonify({'error': 'Incorrect permissions'}) return jsonify({'error': 'Incorrect permissions'})
def admin_update_check(): if current_user.is_white_team: if "name" in request.form and "value" in request.form and "pk" in request.form: check = session.query(Check).get(int(request.form["pk"])) if check: modified_check = False if request.form["name"] == "check_value": if request.form["value"] == "1": check.result = True elif request.form["value"] == "2": check.result = False modified_check = True elif request.form["name"] == "check_reason": modified_check = True check.reason = request.form["value"] if modified_check: session.add(check) session.commit() update_scoreboard_data() update_overview_data() update_services_navbar(check.service.team.id) update_team_stats(check.service.team.id) update_services_data(check.service.team.id) update_service_data(check.service.id) return jsonify({"status": "Updated Property Information"}) return jsonify({"error": "Incorrect permissions"}) return jsonify({"error": "Incorrect permissions"})
def get_last_round_num(): round_obj = session.query(Round.number).order_by( Round.number.desc()).first() if round_obj is None: return 0 else: return round_obj.number
def current_score(self): score = (session.query(func.sum( Service.points)).select_from(Team).join(Service).join( Check).filter(Service.team_id == self.id).filter( Check.result.is_(True)).group_by(Service.team_id).scalar()) if not score: return 0 return score
def current_score(self): score = session.query( Score.value).filter(Score.team_id == self.id).join( Score.round).order_by(Round.number.desc()).first() if score is None: return 0 else: # It returns a tuple that we want to unwrap return score[0]
def get_round_scores(self, round_num): if round_num == 0: return 0 round_obj = session.query(Round).filter(Round.number == round_num).all()[0] round_score = 0 for check in round_obj.checks: if check.service.team == self: if check.result is True: round_score += check.service.points return round_score
def overview_get_round_data(): round_obj = session.query(Round).order_by(Round.number.desc()).first() if round_obj: round_start = round_obj.local_round_start number = round_obj.number else: round_start = "" number = 0 data = {'round_start': round_start, 'number': number} return jsonify(data)
def service(id): if current_user.is_white_team: service = session.query(Service).get(id) blue_teams = Team.get_all_blue_teams() if service is None: return redirect(url_for('auth.unauthorized')) return render_template('admin/service.html', service=service, blue_teams=blue_teams) else: return redirect(url_for('auth.unauthorized'))
def admin_update_points(): if current_user.is_white_team: if 'name' in request.form and 'value' in request.form and 'pk' in request.form: service = session.query(Service).get(int(request.form['pk'])) if service: if request.form['name'] == 'points': service.points = int(request.form['value']) session.add(service) session.commit() return jsonify({'status': 'Updated Service Information'}) return jsonify({'error': 'Incorrect permissions'})
def admin_update_points(): if current_user.is_white_team: if "name" in request.form and "value" in request.form and "pk" in request.form: service = session.query(Service).get(int(request.form["pk"])) if service: if request.form["name"] == "points": service.points = int(request.form["value"]) session.add(service) session.commit() return jsonify({"status": "Updated Service Information"}) return jsonify({"error": "Incorrect permissions"})
def team_services_status(id): if current_user.is_blue_team and current_user.team.id == int(id): services = OrderedDict() team = session.query(Team).get(id) sorted_services = sorted(team.services, key=lambda service: service.id) for service in sorted_services: services[service.name] = { 'id': str(service.id), 'result': str(service.last_check_result()), } return json.dumps(services) return jsonify({'status': 'Unauthorized'}), 403
def get_round_scores(self, round_num): if round_num == 0: return 0 score = (session.query(func.sum( Service.points)).join(Check).join(Round).filter( Service.team_id == self.id).filter( Check.result.is_(True)).filter( Round.number == round_num).group_by( Check.round_id).scalar()) return score if score else 0
def update_team_score(team_id: int, first_round: int, last_round: int, add: bool = False) -> None: """Update the scores of a specific team. Note that it doesn't really make sense to perform an update that doesn't continue all the way to the most recent round of scoring, since that will be used as the basis for the team's score for the next round. :param team_id: The ID of the team whose scores should be updated. :type team_id: int :param first_round: The first round that scores will be recalculated for. :type first_round: int :param last_round: The last round (inclusive) that scores will be recalculated for. :type last_round: int :param add: Whether to add new score objects if they don't exist. Defaults to False. :tpe add: bool """ # Validate the first and last round if first_round < 1: first_round = 1 max_round = Round.get_last_round_num( ) # save value so we only it database once if last_round > max_round: last_round = max_round # Get score of previous round score = 0 if first_round > 1: prev_round = session.query(Round).filter_by(number=(first_round - 1)).first() prev_score = (session.query(Score).filter_by( team_id=team_id, round_id=prev_round.id).first()) score = prev_score.value # Get all services for the team services = session.query(Service).filter_by(team_id=team_id).all() # Iterate through each round for round_num in range(first_round, last_round + 1): round_obj = session.query(Round).filter_by(number=round_num).first() # Determine the check result for each service for service_obj in services: # Get the service's check check_obj = (session.query(Check).filter_by( service_id=service_obj.id, round_id=round_obj.id).first()) if check_obj.result: score += service_obj.points # Update the round's score score_obj = (session.query(Score).filter_by( team_id=team_id, round_id=round_obj.id).first()) if (score_obj is None) and add: score_obj = Score(value=score, team_id=team_id, round=round_obj) session.add(score_obj) elif score_obj is not None: score_obj.value = score session.commit()
def get_queue_stats(): finished_queue_facts = [] queues_facts = {} all_services = session.query(Service).all() for service in all_services: if service.worker_queue not in queues_facts: queues_facts[service.worker_queue] = { "name": service.worker_queue, "workers": [], "services_running": {} } if service.team.name not in queues_facts[ service.worker_queue]['services_running']: queues_facts[service.worker_queue]['services_running'][ service.team.name] = [] queues_facts[service.worker_queue]['services_running'][ service.team.name].append(service.name) for queue_name, queue_facts in queues_facts.items(): # If all of the services are listed for this specific worker, let's just alias it as 'All' queue_services_total_running = 0 for team_name, team_services in queues_facts[ service.worker_queue]['services_running'].items(): queue_services_total_running += len(team_services) if queue_services_total_running == len(all_services): queues_facts[service.worker_queue]['services_running'] = 'All' else: blue_teams = Team.get_all_blue_teams() for blue_team in blue_teams: if blue_team.name in queue_facts[ 'services_running'] and len( blue_team.services) == len( queue_facts['services_running'][ blue_team.name]): # Summarize it for each team if the worker runs all services queues_facts[service.worker_queue]['services_running'][ blue_team.name] = 'ALL' for queue_name, queue_facts in queues_facts.items(): # Get which workers are assigned to which queues active_queues = celery_app.control.inspect().active_queues() # If we don't have any queues, we also have no workers if active_queues is not None: for worker_name, queues in active_queues.items(): if queue_name in [k['name'] for k in queues]: queue_facts['workers'].append(worker_name) for queue_name, queue_facts in queues_facts.items(): finished_queue_facts.append(queue_facts) return finished_queue_facts
def get_check_progress_total(): if current_user.is_white_team: task_id_settings = session.query(KB).filter_by( name='task_ids').order_by(KB.round_num.desc()).first() total_stats = {} total_stats['finished'] = 0 total_stats['pending'] = 0 team_stats = {} if task_id_settings: task_dict = json.loads(task_id_settings.value) for team_name, task_ids in task_dict.items(): for task_id in task_ids: task = execute_command.AsyncResult(task_id) if team_name not in team_stats: team_stats[team_name] = {} team_stats[team_name]['pending'] = 0 team_stats[team_name]['finished'] = 0 if task.state == 'PENDING': team_stats[team_name]['pending'] += 1 total_stats['pending'] += 1 else: team_stats[team_name]['finished'] += 1 total_stats['finished'] += 1 total_percentage = 0 total_tasks = total_stats['finished'] + total_stats['pending'] if total_stats['finished'] == 0: total_percentage = 0 elif total_tasks == 0: total_percentage = 100 elif total_stats and total_stats['finished']: total_percentage = int( (total_stats['finished'] / total_tasks) * 100) output_dict = {'Total': total_percentage} for team_name, team_stat in team_stats.items(): team_total_percentage = 0 team_total_tasks = team_stat['finished'] + team_stat['pending'] if team_stat['finished'] == 0: team_total_percentage = 0 elif team_total_tasks == 0: team_total_percentage = 100 elif team_stat and team_stat['finished']: team_total_percentage = int( (team_stat['finished'] / team_total_tasks) * 100) output_dict[team_name] = team_total_percentage return json.dumps(output_dict) else: return {'status': 'Unauthorized'}, 403
def service_get_checks(service_id): service = session.query(Service).get(service_id) if service is None or not (current_user.team == service.team or current_user.team.is_white_team): return jsonify({"status": "Unauthorized"}), 403 data = [] check_output = (session.query(Check, Round.number).join(Round).filter( Check.service_id == service_id).order_by(Check.id.desc()).all()) data = [{ "id": check[0].id, "round": check[1], "result": check[0].result, "timestamp": check[0].local_completed_timestamp, "reason": check[0].reason, "output": check[0].output, "command": check[0].command, } for check in check_output] if (Setting.get_setting("blue_team_view_check_output").value is False and current_user.is_blue_team): for check in data: check["output"] = "REDACTED" return jsonify(data=data)
def service_get_checks(id): service = session.query(Service).get(id) if service is None or not current_user.team == service.team: return jsonify({'status': 'Unauthorized'}), 403 data = [] for check in service.checks_reversed: data.append({ 'round': check.round.number, 'result': check.result, 'timestamp': check.local_completed_timestamp, 'reason': check.reason, 'output': check.output, }) return jsonify(data=data)
def admin_update_host(): if current_user.is_white_team: if 'name' in request.form and 'value' in request.form and 'pk' in request.form: service = session.query(Service).get(int(request.form['pk'])) if service: if request.form['name'] == 'host': service.host = html.escape(request.form['value']) session.add(service) session.commit() update_overview_data() update_services_data(service.team.id) update_service_data(service.id) return jsonify({'status': 'Updated Service Information'}) return jsonify({'error': 'Incorrect permissions'})
def admin_update_property(): if current_user.is_white_team: if 'name' in request.form and 'value' in request.form and 'pk' in request.form: property_obj = session.query(Property).get(int(request.form['pk'])) if property_obj: if request.form['name'] == 'property_name': property_obj.name = html.escape(request.form['value']) elif request.form['name'] == 'property_value': property_obj.value = html.escape(request.form['value']) session.add(property_obj) session.commit() return jsonify({'status': 'Updated Property Information'}) return jsonify({'error': 'Incorrect permissions'}) return jsonify({'error': 'Incorrect permissions'})
def admin_update_environment(): if current_user.is_white_team: if 'name' in request.form and 'value' in request.form and 'pk' in request.form: environment = session.query(Environment).get( int(request.form['pk'])) if environment: if request.form['name'] == 'matching_regex': environment.matching_regex = html.escape( request.form['value']) session.add(environment) session.commit() return jsonify({'status': 'Updated Environment Information'}) return jsonify({'error': 'Incorrect permissions'}) return jsonify({'error': 'Incorrect permissions'})