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 make_report(): arguments = request.json try: comparison_interval = DateInterval( datetime.fromtimestamp( int(arguments['comparison_interval']['from'])), datetime.fromtimestamp(int( arguments['comparison_interval']['to']))) compared_to_interval = DateInterval( datetime.fromtimestamp( int(arguments['compared_to_interval']['from'])), datetime.fromtimestamp(int( arguments['compared_to_interval']['to']))) except Exception: return 'Invalid payload', 422 endpoint_summaries = [] with session_scope() as db_session: for endpoint in get_endpoints(db_session): endpoint_summary = make_endpoint_summary(endpoint, comparison_interval, compared_to_interval) endpoint_summaries.append(endpoint_summary) return dict(summaries=endpoint_summaries)
def endpoints(): """ :return: A JSON-list with information about every endpoint (encoded in a JSON-object) For more information per endpoint, see :func: get_overview """ with session_scope() as session: return jsonify([row2dict(row) for row in get_endpoints(session)])
def test_get_endpoints(self): """ Test whether the function returns the right values. """ from flask_monitoringdashboard.database.endpoint import get_endpoints with session_scope() as db_session: result = get_endpoints(db_session) self.assertEqual(len(result), 1) self.assertEqual(result[0].name, NAME)
def make_endpoint_summaries(requests_criterion, baseline_requests_criterion): endpoint_summaries = [] with session_scope() as db_session: for endpoint in get_endpoints(db_session): endpoint_summary = make_endpoint_summary( endpoint, requests_criterion, baseline_requests_criterion) endpoint_summaries.append(endpoint_summary) return dict(summaries=endpoint_summaries)
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_endpoints(self): """ Test whether the function returns the right values. """ from flask_monitoringdashboard.database.endpoint import get_endpoints from flask_monitoringdashboard import config with session_scope() as db_session: result = get_endpoints(db_session) self.assertEqual(len(result), 1) self.assertEqual(result[0].name, NAME) self.assertEqual(result[0].monitor_level, 1) self.assertEqual(result[0].version_added, config.version) self.assertEqual(result[0].last_requested, TIMES[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) ]
def endpoint_graph(): """ Creates a graph with the execution times per endpoint :return: """ with session_scope() as db_session: data = get_endpoint_data_grouped(db_session, lambda x: simplify(x, 10)) values = [ boxplot(get_value(data, end.id, default=[]), name=end.name) for end in get_endpoints(db_session) ] layout = get_layout(height=350 + 40 * len(values), xaxis={'title': 'Execution time (ms)'}, margin=get_margin()) return get_figure(layout, values)
def get_num_requests_data(session, start_date, end_date): """ :param session: session for the database :param start_date: datetime object :param end_date: datetime object and: end_date >= start_date :return: a list of the number of requests for each endpoint and on which day """ numdays = (end_date - start_date).days + 1 days = [start_date + datetime.timedelta(days=i) for i in range(numdays)] hits = count_requests_per_day(session, days) endpoints = get_endpoints(session) data = [{ 'name': end.name, 'values': [get_value(hits_day, end.id) for hits_day in hits] } for end in endpoints] return {'days': [d.strftime('%Y-%m-%d') for d in days], 'data': data}
def version_usage_graph(db_session, form): """ Used for getting a Heatmap with an overview of which endpoints are used in which versions :param db_session: session for the database :param form: instance of SliderForm :return: """ endpoints = get_endpoints(db_session) versions = get_versions(db_session, limit=form.get_slider_value()) requests = [ count_requests_group(db_session, Request.version_requested == v) for v in versions ] total_hits = [] hits = [[]] * len(endpoints) for hits_version in requests: total_hits.append(max(1, sum([value for key, value in hits_version]))) for j in range(len(endpoints)): hits[j] = [0] * len(versions) for i in range(len(versions)): hits[j][i] = get_value(requests[i], endpoints[j].id) * 100 / total_hits[i] layout = get_layout(xaxis={ 'title': 'Versions', 'type': 'category' }, yaxis={ 'type': 'category', 'autorange': 'reversed' }, margin=get_margin()) trace = heatmap(z=hits, x=versions, y=['{} '.format(e.name) for e in endpoints], colorbar={ 'titleside': 'top', 'tickmode': 'array', }) return get_figure(layout=layout, data=[trace])
def requests_graph(form): """ Returns a horizontal box plot with the number of requests per day. :param form: must be the form that is obtained by get_daterange_form :return: """ days = form.get_days() with session_scope() as db_session: hits = count_requests_per_day(db_session, days) data = [barplot(x=[get_value(hits_day, end.id) for hits_day in hits], y=days, name=end.name) for end in get_endpoints(db_session)] layout = get_layout( barmode='stack', height=350 + 40 * len(days), showlegend=True, xaxis={'title': 'Number of requests'}, yaxis={'type': 'category'} ) return get_figure(layout=layout, data=data)
def get_json_monitor_rules(): """ The returned data is the data that is encrypted using a security token. This security token is set in the configuration. :return: all entries from the database (rules-table) """ data = [] try: with session_scope() as db_session: for entry in get_endpoints(db_session): # nice conversion to json-object data.append({ 'name': entry.name, 'last_requested': str(entry.last_requested), 'monitor_level': entry.monitor_level, 'time_added': str(entry.time_added), 'version_added': entry.version_added }) return jwt.encode({'data': json.dumps(data)}, config.security_token, algorithm='HS256') except ValueError as e: return 'ValueError: {}'.format(e)
def test_get_endpoints(session, endpoint): endpoints = get_endpoints(session) assert endpoint.name in [endpoint.name for endpoint in endpoints]
def test_endpoints(session, endpoint): endpoints = get_endpoints(session) assert endpoints.count() == session.query(Endpoint).count() assert [endpoint.id == e.id for e in endpoints] # check that the endpoint is included.