Example #1
0
def get_endpoint_overview(db_session):
    """
    :param db_session: session for the database
    :return: A list of properties for each endpoint that is found in the database
    """
    week_ago = datetime.datetime.utcnow() - datetime.timedelta(days=7)
    now_local = to_local_datetime(datetime.datetime.utcnow())
    today_local = now_local.replace(hour=0, minute=0, second=0, microsecond=0)
    today_utc = to_utc_datetime(today_local)

    hits_today = count_requests_group(db_session, Request.time_requested > today_utc)
    hits_week = count_requests_group(db_session, Request.time_requested > week_ago)
    hits = count_requests_group(db_session)

    median_today = get_endpoint_data_grouped(db_session, median, Request.time_requested > today_utc)
    median_week = get_endpoint_data_grouped(db_session, median, Request.time_requested > week_ago)
    median_overall = get_endpoint_data_grouped(db_session, median)
    access_times = get_last_requested(db_session)

    return [{
        'id': endpoint.id,
        'name': endpoint.name,
        'monitor': endpoint.monitor_level,
        'color': get_color(endpoint.name),
        'hits-today': get_value(hits_today, endpoint.id),
        'hits-week': get_value(hits_week, endpoint.id),
        'hits-overall': get_value(hits, endpoint.id),
        'median-today': get_value(median_today, endpoint.id),
        'median-week': get_value(median_week, endpoint.id),
        'median-overall': get_value(median_overall, endpoint.id),
        'last-accessed': get_value(access_times, endpoint.name, default=None)
    } for endpoint in get_endpoints(db_session)]
def overview():
    week_ago = datetime.datetime.utcnow() - datetime.timedelta(days=7)
    now_local = to_local_datetime(datetime.datetime.utcnow())
    today_local = now_local.replace(hour=0, minute=0, second=0, microsecond=0)
    today_utc = to_utc_datetime(today_local)

    result = []
    with session_scope() as db_session:
        from numpy import median

        hits_today = count_requests_group(db_session,
                                          Request.time_requested > today_utc)
        hits_week = count_requests_group(db_session,
                                         Request.time_requested > week_ago)
        hits = count_requests_group(db_session)

        median_today = get_endpoint_data_grouped(
            db_session, median, Request.time_requested > today_utc)
        median_week = get_endpoint_data_grouped(
            db_session, median, Request.time_requested > week_ago)
        median = get_endpoint_data_grouped(db_session, median)
        access_times = get_last_requested(db_session)

        for endpoint in get_endpoints(db_session):
            result.append({
                'id':
                endpoint.id,
                'name':
                endpoint.name,
                'color':
                get_color(endpoint.name),
                'hits-today':
                get_value(hits_today, endpoint.id),
                'hits-week':
                get_value(hits_week, endpoint.id),
                'hits-overall':
                get_value(hits, endpoint.id),
                'median-today':
                get_value(median_today, endpoint.id),
                'median-week':
                get_value(median_week, endpoint.id),
                'median-overall':
                get_value(median, endpoint.id),
                'last-accessed':
                get_value(access_times, endpoint.name, default=None)
            })
        version = get_details(db_session)['dashboard-version']
    return render_template('fmd_dashboard/overview.html',
                           result=result,
                           is_admin=is_admin(),
                           title='Dashboard Overview',
                           version=version)
 def test_update_last_accessed(self):
     """
         Test whether the function returns the right values.
     """
     import datetime
     time = datetime.datetime.utcnow()
     from flask_monitoringdashboard.database.endpoint import update_last_accessed, get_last_requested
     from flask_monitoringdashboard.database.count_group import get_value
     with session_scope() as db_session:
         update_last_accessed(db_session, NAME)
         result = get_value(get_last_requested(db_session), NAME)
         result_utc = to_utc_datetime(result)
         self.assertTrue((result_utc - time).seconds < 1)
Example #4
0
def get_endpoint_overview(session):
    """
    :param session: session for the database
    :return: A list of properties for each endpoint that is found in the database
    """
    week_ago = datetime.datetime.utcnow() - datetime.timedelta(days=7)
    now_local = to_local_datetime(datetime.datetime.utcnow())
    today_local = now_local.replace(hour=0, minute=0, second=0, microsecond=0)
    today_utc = to_utc_datetime(today_local)

    # First flush last requested info to db
    cache.flush_cache()
    error_hits_criterion = and_(Request.status_code >= 400, Request.status_code < 600)

    hits_today = count_requests_group(session, Request.time_requested > today_utc)
    hits_today_errors = count_requests_group(
        session, and_(Request.time_requested > today_utc, error_hits_criterion)
    )

    hits_week = count_requests_group(session, Request.time_requested > week_ago)
    hits_week_errors = count_requests_group(
        session, and_(Request.time_requested > week_ago, error_hits_criterion)
    )

    hits = count_requests_group(session)

    median_today = get_endpoint_data_grouped(session, median, Request.time_requested > today_utc)
    median_week = get_endpoint_data_grouped(session, median, Request.time_requested > week_ago)
    median_overall = get_endpoint_data_grouped(session, median)
    access_times = get_last_requested(session)

    return [
        {
            'id': endpoint.id,
            'name': endpoint.name,
            'monitor': endpoint.monitor_level,
            'color': get_color(endpoint.name),
            'hits-today': get_value(hits_today, endpoint.id),
            'hits-today-errors': get_value(hits_today_errors, endpoint.id),
            'hits-week': get_value(hits_week, endpoint.id),
            'hits-week-errors': get_value(hits_week_errors, endpoint.id),
            'hits-overall': get_value(hits, endpoint.id),
            'median-today': get_value(median_today, endpoint.id),
            'median-week': get_value(median_week, endpoint.id),
            'median-overall': get_value(median_overall, endpoint.id),
            'last-accessed': get_value(access_times, endpoint.name, default=None),
        }
        for endpoint in get_endpoints(session)
    ]
Example #5
0
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 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())
Example #7
0
def test_update_last_accessed(session, endpoint, timestamp):
    update_last_requested(session, endpoint.name, timestamp=timestamp)
    result = get_value(get_last_requested(session), endpoint.name)
    assert result == timestamp