Skip to content

Commit 95bb2fd

Browse files
author
Patrick Vogel
committed
WIP
1 parent d9f9891 commit 95bb2fd

File tree

119 files changed

+1727
-1913
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+1727
-1913
lines changed

.github/workflows/python-publish.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
name: Upload Python Package
22

33
on:
4-
release:
5-
types: [created]
4+
push:
5+
branches:
6+
- master
7+
- development
68

79
jobs:
810
deploy:

.github/workflows/python-test.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ jobs:
2222
pip install -r requirements-dev.txt
2323
- name: Lint with flake8
2424
run: |
25-
pip install flake8
2625
flake8 . --exit-zero --max-line-length=100 --ignore=F401,W503 --max-complexity=10 --max-line-length=100
27-
- name: Test with unittest
26+
- name: Test with pytest
2827
run: |
29-
coverage run setup.py test
30-
coverage xml
28+
pytest --cov --cov-report=xml
3129
- name: Upload coverage to Codecov
3230
uses: codecov/codecov-action@v1
3331
with:

MANIFEST.in

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
recursive-include flask_monitoringdashboard/static *
22
recursive-include flask_monitoringdashboard/templates *
3-
recursive-include flask_monitoringdashboard/test *
43
include requirements.txt
54
include README.md
65
include docs/changelog.rst

flask_monitoringdashboard/__init__.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
"""
2-
The app tracks the performance of various endpoints over time.
3-
To bind, use the following lines of code:
4-
import dashboard
5-
from flask import Flask
6-
...
7-
app = Flask(__name__)
8-
...
9-
dashboard.bind(app)
10-
11-
The dashboard with the results that are collected can be found at:
12-
localhost:5000/dashboard
1+
"""The app tracks the performance of various endpoints over time.
2+
To bind, use the following lines of code:
3+
4+
>>> import flask_monitoringdashboard as dashboard
5+
>>> from flask import Flask
6+
...
7+
>>> app = Flask(__name__)
8+
...
9+
>>> dashboard.bind(app)
10+
11+
The dashboard with the results that are collected can be found at:
12+
localhost:5000/dashboard
1313
"""
1414

1515
import os
@@ -22,21 +22,21 @@
2222
config = Config()
2323

2424

25-
# get current location of the project
2625
def loc():
26+
"""Get the current location of the project."""
2727
return os.path.abspath(os.path.dirname(__file__)) + '/'
2828

2929

30-
# define the blueprint
3130
blueprint = Blueprint('dashboard', __name__, template_folder=loc() + 'templates')
31+
"""Define the blueprint."""
3232

3333

3434
def bind(app, schedule=True):
35-
"""
36-
Binding the app to this object should happen before importing the routing-
37-
methods below. Thus, the importing statement is part of this function.
38-
:param app: the app for which the performance has to be tracked
39-
:param schedule: flag telling if the background scheduler should be started
35+
"""Binding the app to this object should happen before importing the routing-
36+
methods below. Thus, the importing statement is part of this function.
37+
38+
:param app: the app for which the performance has to be tracked
39+
:param schedule: flag telling if the background scheduler should be started
4040
"""
4141
config.app = app
4242
# Provide a secret-key for using WTF-forms
@@ -79,8 +79,8 @@ def bind(app, schedule=True):
7979

8080

8181
def add_graph(title, func, trigger="interval", **schedule):
82-
"""
83-
Add a custom graph to the dashboard. You must specify the following arguments
82+
"""Add a custom graph to the dashboard. You must specify the following arguments:
83+
8484
:param title: title of the graph (must be unique)
8585
:param schedule: dict containing values for weeks, days, hours, minutes, seconds
8686
:param func: function reference without arguments

flask_monitoringdashboard/cli.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
"""
2-
Contains custom commands for the Flask-MonitoringDashboard
1+
"""Contains custom commands for the Flask-MonitoringDashboard
32
For a list of all commands, open a terminal and type:
4-
flask fmd --help
3+
4+
>>> flask fmd --help
55
"""
66

77
import click
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
"""
2-
This directory is used for implementing the logic between the database and the API
3-
"""
1+
"""This directory is used for implementing the logic between the database and the API."""
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import datetime
22

33
from numpy import median
4+
from sqlalchemy import and_
45

5-
import flask_monitoringdashboard.core.cache as cache
66
from flask_monitoringdashboard import config
7+
from flask_monitoringdashboard.core import cache
78
from flask_monitoringdashboard.core.colors import get_color
89
from flask_monitoringdashboard.core.measurement import add_decorator
910
from flask_monitoringdashboard.core.timezone import to_local_datetime, to_utc_datetime
@@ -22,12 +23,11 @@
2223
update_endpoint,
2324
)
2425
from flask_monitoringdashboard.database.versions import get_first_requests
25-
from sqlalchemy import and_
2626

2727

28-
def get_endpoint_overview(db_session):
28+
def get_endpoint_overview(session):
2929
"""
30-
:param db_session: session for the database
30+
:param session: session for the database
3131
:return: A list of properties for each endpoint that is found in the database
3232
"""
3333
week_ago = datetime.datetime.utcnow() - datetime.timedelta(days=7)
@@ -39,22 +39,22 @@ def get_endpoint_overview(db_session):
3939
cache.flush_cache()
4040
error_hits_criterion = and_(Request.status_code >= 400, Request.status_code < 600)
4141

42-
hits_today = count_requests_group(db_session, Request.time_requested > today_utc)
42+
hits_today = count_requests_group(session, Request.time_requested > today_utc)
4343
hits_today_errors = count_requests_group(
44-
db_session, and_(Request.time_requested > today_utc, error_hits_criterion)
44+
session, and_(Request.time_requested > today_utc, error_hits_criterion)
4545
)
4646

47-
hits_week = count_requests_group(db_session, Request.time_requested > week_ago)
47+
hits_week = count_requests_group(session, Request.time_requested > week_ago)
4848
hits_week_errors = count_requests_group(
49-
db_session, and_(Request.time_requested > week_ago, error_hits_criterion)
49+
session, and_(Request.time_requested > week_ago, error_hits_criterion)
5050
)
5151

52-
hits = count_requests_group(db_session)
52+
hits = count_requests_group(session)
5353

54-
median_today = get_endpoint_data_grouped(db_session, median, Request.time_requested > today_utc)
55-
median_week = get_endpoint_data_grouped(db_session, median, Request.time_requested > week_ago)
56-
median_overall = get_endpoint_data_grouped(db_session, median)
57-
access_times = get_last_requested(db_session)
54+
median_today = get_endpoint_data_grouped(session, median, Request.time_requested > today_utc)
55+
median_week = get_endpoint_data_grouped(session, median, Request.time_requested > week_ago)
56+
median_overall = get_endpoint_data_grouped(session, median)
57+
access_times = get_last_requested(session)
5858

5959
return [
6060
{
@@ -72,21 +72,21 @@ def get_endpoint_overview(db_session):
7272
'median-overall': get_value(median_overall, endpoint.id),
7373
'last-accessed': get_value(access_times, endpoint.name, default=None),
7474
}
75-
for endpoint in get_endpoints(db_session)
75+
for endpoint in get_endpoints(session)
7676
]
7777

7878

79-
def get_endpoint_users(db_session, endpoint_id, users):
79+
def get_endpoint_users(session, endpoint_id, users):
8080
"""
81-
:param db_session: session for the database
81+
:param session: session for the database
8282
:param endpoint_id: id for the endpoint
8383
:param users: a list of users to be filtered on
8484
:return: a list of dicts with the performance of each user
8585
"""
8686
times = get_user_data_grouped(
87-
db_session, lambda x: simplify(x, 100), Request.endpoint_id == endpoint_id
87+
session, lambda x: simplify(x, 100), Request.endpoint_id == endpoint_id
8888
)
89-
first_requests = get_first_requests(db_session, endpoint_id)
89+
first_requests = get_first_requests(session, endpoint_id)
9090
return [
9191
{
9292
'user': u,
@@ -98,17 +98,17 @@ def get_endpoint_users(db_session, endpoint_id, users):
9898
]
9999

100100

101-
def get_endpoint_versions(db_session, endpoint_id, versions):
101+
def get_endpoint_versions(session, endpoint_id, versions):
102102
"""
103-
:param db_session: session for the database
103+
:param session: session for the database
104104
:param endpoint_id: id for the endpoint
105105
:param versions: a list of version to be filtered on
106106
:return: a list of dicts with the performance of each version
107107
"""
108108
times = get_version_data_grouped(
109-
db_session, lambda x: simplify(x, 100), Request.endpoint_id == endpoint_id
109+
session, lambda x: simplify(x, 100), Request.endpoint_id == endpoint_id
110110
)
111-
first_requests = get_first_requests(db_session, endpoint_id)
111+
first_requests = get_first_requests(session, endpoint_id)
112112
return [
113113
{
114114
'version': v,
@@ -120,32 +120,32 @@ def get_endpoint_versions(db_session, endpoint_id, versions):
120120
]
121121

122122

123-
def get_api_performance(db_session, endpoints):
123+
def get_api_performance(session, endpoints):
124124
"""
125-
:param db_session: session for the database
125+
:param session: session for the database
126126
:param endpoints: a list of endpoints, encoded by their name
127127
:return: for every endpoint in endpoints, a list with the performance
128128
"""
129-
db_endpoints = [get_endpoint_by_name(db_session, end) for end in endpoints]
130-
data = get_endpoint_data_grouped(db_session, lambda x: simplify(x, 10))
129+
db_endpoints = [get_endpoint_by_name(session, end) for end in endpoints]
130+
data = get_endpoint_data_grouped(session, lambda x: simplify(x, 10))
131131
return [
132-
{'name': end.name, 'values': get_value(data, end.id, default=[])} for end in db_endpoints
132+
{'name': end.name, 'values': get_value(data, end.id, default=[])}
133+
for end in db_endpoints
133134
]
134135

135136

136-
def set_endpoint_rule(db_session, endpoint_name, monitor_level):
137+
def set_endpoint_rule(session, endpoint_name, monitor_level):
137138
"""
138-
:param db_session: session for the database
139+
:param session: session for the database
139140
:param endpoint_name: name of the endpoint
140141
:param monitor_level: integer, representing the monitoring-level
141-
:return:
142142
"""
143-
update_endpoint(db_session, endpoint_name, value=monitor_level)
143+
update_endpoint(session, endpoint_name, value=monitor_level)
144144

145145
# Remove wrapper
146146
original = getattr(config.app.view_functions[endpoint_name], 'original', None)
147147
if original:
148148
config.app.view_functions[endpoint_name] = original
149-
db_session.commit()
149+
session.commit()
150150

151-
add_decorator(get_endpoint_by_name(db_session, endpoint_name))
151+
add_decorator(get_endpoint_by_name(session, endpoint_name))

flask_monitoringdashboard/controllers/outliers.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
from flask_monitoringdashboard.database.outlier import get_outliers_cpus, get_outliers_sorted
99

1010

11-
def get_outlier_graph(db_session, endpoint_id):
11+
def get_outlier_graph(session, endpoint_id):
1212
"""
13-
:param db_session: session for the database
13+
:param session: session for the database
1414
:param endpoint_id: id of the endpoint
1515
:return: a list with data about each CPU performance
1616
"""
17-
all_cpus = get_outliers_cpus(db_session, endpoint_id)
17+
all_cpus = get_outliers_cpus(session, endpoint_id)
1818
cpu_data = [ast.literal_eval(cpu) for cpu in all_cpus]
1919

2020
return [
@@ -23,15 +23,15 @@ def get_outlier_graph(db_session, endpoint_id):
2323
]
2424

2525

26-
def get_outlier_table(db_session, endpoint_id, offset, per_page):
26+
def get_outlier_table(session, endpoint_id, offset, per_page):
2727
"""
28-
:param db_session: session for the database
28+
:param session: session for the database
2929
:param endpoint_id: id of the endpoint
3030
:param offset: number of items to be skipped
3131
:param per_page: maximum number of items to be returned
3232
:return: a list of length at most 'per_page' with data about each outlier
3333
"""
34-
table = get_outliers_sorted(db_session, endpoint_id, offset, per_page)
34+
table = get_outliers_sorted(session, endpoint_id, offset, per_page)
3535
for idx, row in enumerate(table):
3636
row.request.time_requested = to_local_datetime(row.request.time_requested)
3737
try:

flask_monitoringdashboard/controllers/profiler.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
)
1212

1313

14-
def get_profiler_table(db_session, endpoint_id, offset, per_page):
14+
def get_profiler_table(session, endpoint_id, offset, per_page):
1515
"""
16-
:param db_session: session for the database
16+
:param session: session for the database
1717
:param endpoint_id: endpoint to filter on
1818
:param offset: number of items that are skipped
1919
:param per_page: number of items that are returned (at most)
2020
"""
21-
table = get_profiled_requests(db_session, endpoint_id, offset, per_page)
21+
table = get_profiled_requests(session, endpoint_id, offset, per_page)
2222

2323
for idx, row in enumerate(table):
2424
row.time_requested = to_local_datetime(row.time_requested)
@@ -32,14 +32,14 @@ def get_profiler_table(db_session, endpoint_id, offset, per_page):
3232
return table
3333

3434

35-
def get_grouped_profiler(db_session, endpoint_id):
35+
def get_grouped_profiler(session, endpoint_id):
3636
"""
37-
:param db_session: session for the database
37+
:param session: session for the database
3838
:param endpoint_id: endpoint to filter on
3939
:return:
4040
"""
41-
requests = get_grouped_profiled_requests(db_session, endpoint_id)
42-
db_session.expunge_all()
41+
requests = get_grouped_profiled_requests(session, endpoint_id)
42+
session.expunge_all()
4343

4444
histogram = defaultdict(list) # path -> [list of values]
4545
path_hash = PathHash()

0 commit comments

Comments
 (0)