def get_hourly_load(session, endpoint_id, start_date, end_date): """ :param session: session for the database :param endpoint_id: id for the endpoint :param start_date: datetime object :param end_date: datetime object and: end_date >= start_date :return: """ numdays = (end_date - start_date).days + 1 # list of hours: 0:00 - 23:00 hours = ['0{}:00'.format(h) for h in range(0, 10) ] + ['{}:00'.format(h) for h in range(10, 24)] heatmap_data = numpy.zeros((len(hours), numdays)) start_datetime = to_utc_datetime( datetime.datetime.combine(start_date, datetime.time(0, 0, 0, 0))) end_datetime = to_utc_datetime( datetime.datetime.combine(end_date, datetime.time(23, 59, 59))) for time, count in get_num_requests(session, endpoint_id, start_datetime, end_datetime): parsed_time = datetime.datetime.strptime(time, '%Y-%m-%d %H:%M:%S') day_index = (parsed_time - start_datetime).days hour_index = int(to_local_datetime(parsed_time).strftime('%H')) heatmap_data[hour_index][day_index] = count return { 'days': [(start_date + datetime.timedelta(days=i)).strftime('%Y-%m-%d') for i in range(numdays)], "data": heatmap_data.tolist(), }
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 count_requests_per_day(db_session, list_of_days): """ Return the number of hits for all endpoints per day. :param db_session: session for the database :param list_of_days: list with datetime.datetime objects. """ result = [] for day in list_of_days: dt_begin = to_utc_datetime(datetime.datetime.combine(day, datetime.time(0, 0, 0))) dt_end = dt_begin + datetime.timedelta(days=1) result.append(count_rows_group(db_session, Request.id, Request.time_requested >= dt_begin, Request.time_requested < dt_end)) return result
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)
def hourly_load_graph(form, endpoint_id=None): """ Return HTML string for generating a Heatmap. :param form: A SelectDateRangeForm, which is used to filter the selection :param endpoint_id: optionally, filter the data on a specific endpoint :return: HTML code with the graph """ # list of hours: 0:00 - 23:00 hours = ['0{}:00'.format(h) for h in range(0, 10) ] + ['{}:00'.format(h) for h in range(10, 24)] days = form.get_days() # create empty 2D-list: [hour][day] heatmap_data = numpy.zeros((len(hours), len(days))) # add data from database to heatmap_data start_datetime = to_utc_datetime( datetime.datetime.combine(form.start_date.data, datetime.time(0, 0, 0, 0))) end_datetime = to_utc_datetime( datetime.datetime.combine(form.end_date.data, datetime.time(23, 59, 59))) with session_scope() as db_session: for time, count in get_num_requests(db_session, endpoint_id, start_datetime, end_datetime): parsed_time = datetime.datetime.strptime(time, '%Y-%m-%d %H:%M:%S') day_index = (parsed_time - start_datetime).days hour_index = int(to_local_datetime(parsed_time).strftime('%H')) heatmap_data[hour_index][day_index] = count start_datetime = to_local_datetime(start_datetime - datetime.timedelta( days=1)).strftime('%Y-%m-%d 12:00:00') end_datetime = to_local_datetime( form.end_date.data).strftime('%Y-%m-%d 12:00:00') layout = get_layout(xaxis=go.XAxis(range=[start_datetime, end_datetime])) return get_figure(layout, [plot_heatmap(x=days, y=hours, z=heatmap_data)])
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 test_timezone(): dt = datetime.datetime.now() assert to_local_datetime(to_utc_datetime(dt)) == dt assert to_utc_datetime(to_local_datetime(dt)) == dt
def test_timezone_none(): assert to_local_datetime(None) is None assert to_utc_datetime(None) is None
def test_timezone(self): dt = datetime.datetime.now() self.assertEqual(to_local_datetime(to_utc_datetime(dt)), dt) self.assertEqual(to_utc_datetime(to_local_datetime(dt)), dt)
def test_timezone_none(self): self.assertEqual(to_local_datetime(None), None) self.assertEqual(to_utc_datetime(None), None)