def init_measurement(): """ This should be added to the list of functions that are executed before the first request. This function is used in the config-method in __init__ of this folder It adds wrappers to the endpoints for tracking their performance and last access times. """ with session_scope() as session: for rule in get_rules(): endpoint = get_endpoint_by_name(session, rule.endpoint) add_decorator(endpoint)
def get_endpoint_details(db_session, endpoint_id): """ Returns details about an endpoint. :param db_session: session for the database :param endpoint_id: id of the endpoint :return dictionary """ endpoint = get_endpoint_by_id(db_session, endpoint_id) endpoint.time_added = to_local_datetime(endpoint.time_added) flask_rule = get_rules(endpoint.name) methods = [list(rule.methods) for rule in flask_rule] methods = sum(methods, []) # flatten list return { 'id': endpoint_id, 'color': get_color(endpoint.name), 'methods': list(dict.fromkeys(methods)), 'endpoint': endpoint.name, 'rules': [r.rule for r in get_rules(endpoint.name)], 'monitor-level': endpoint.monitor_level, 'url': get_url(endpoint.name), 'total_hits': count_requests(db_session, endpoint.id) }
def init_cache(): """ This should be added to the list of functions that are executed before the first request. It initializes the in-memory cache from the db """ global memory_cache with session_scope() as db_session: last_req_dict = dict(get_last_requested(db_session)) hits_dict = dict(get_endpoints_hits(db_session)) averages_dict = dict(get_endpoint_averages(db_session)) for rule in get_rules(): memory_cache[rule.endpoint] = EndpointInfo( last_requested=last_req_dict.get(rule.endpoint), average_duration=averages_dict.get(rule.endpoint), hits=hits_dict.get(rule.endpoint), )
def get_endpoint_details(db_session, endpoint_id): """ Returns details about an endpoint. :param db_session: session for the database :param endpoint_id: id of the endpoint :return dictionary """ endpoint = get_endpoint_by_id(db_session, endpoint_id) endpoint.time_added = to_local_datetime(endpoint.time_added) return { 'id': endpoint_id, 'endpoint': endpoint.name, 'rules': ', '.join([r.rule for r in get_rules(endpoint.name)]), 'rule': endpoint, 'url': get_url(endpoint.name), 'total_hits': count_requests(db_session, endpoint.id) }
def rules(): """ Renders a table with all rules from the user_app. The fmd_dashboard rules are excluded In case of the POST request, the data from the form is validated and processed, such that the required rules are monitored :return: """ if request.method == 'POST': with session_scope() as db_session: endpoint_name = request.form['name'] value = int(request.form['value']) update_endpoint(db_session, endpoint_name, value=value) # Remove wrapper original = getattr(user_app.view_functions[endpoint_name], 'original', None) if original: user_app.view_functions[endpoint_name] = original with session_scope() as db_session: add_decorator(get_endpoint_by_name(db_session, endpoint_name)) return 'OK' with session_scope() as db_session: last_accessed = get_last_requested(db_session) all_rules = [] for rule in get_rules(): db_rule = get_endpoint_by_name(db_session, rule.endpoint) all_rules.append({ 'color': get_color(rule.endpoint), 'rule': rule.rule, 'endpoint': rule.endpoint, 'methods': rule.methods, 'last_accessed': get_value(last_accessed, rule.endpoint, default=None), 'form': get_monitor_form(rule.endpoint, db_rule.monitor_level) }) return render_template('fmd_rules.html', rules=all_rules, information=get_rules_info())
def test_rules(self): with self.app.app_context(): self.assertEqual(len(get_rules()), 2) self.assertEqual(len(get_rules(NAME)), 1) self.assertEqual(get_rules('unknown'), [])
def test_rules(endpoint): assert len(get_rules()) == 1 assert len(get_rules(endpoint.name)) == 1 assert get_rules('unknown') == []