Skip to content

Commit

Permalink
Merge pull request #73 from flask-dashboard/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
FlyingBird95 committed Mar 30, 2018
2 parents e1b2607 + 17c0913 commit cc53407
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Please note that the changes before version 1.10.0 have not been documented.

Unreleased
----------


v1.11.0
-------
Changed

- Added new graph: Version usage
Expand Down
18 changes: 14 additions & 4 deletions flask_monitoringdashboard/database/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import datetime

import time
from sqlalchemy import func, desc, text, asc

from flask_monitoringdashboard import config
Expand Down Expand Up @@ -70,13 +70,16 @@ def get_average(date_from=None):
return result


def get_data_from(time_from):
def get_data_between(time_from, time_to=None):
"""
Returns all data in the FunctionCall table, for the export data option.
This function returns all data after the time_from date.
"""
with session_scope() as db_session:
result = db_session.query(FunctionCall).filter(FunctionCall.time >= time_from).all()
result = db_session.query(FunctionCall).filter(FunctionCall.time >= time_from)
if time_to:
result = result.filter(FunctionCall.time <= time_to)
result = result.all()
db_session.expunge_all()
return result

Expand All @@ -86,7 +89,7 @@ def get_data():
Equivalent function to get_data_from, but returns all data.
:return: all data from the database in the Endpoint-table.
"""
return get_data_from(datetime.date(1970, 1, 1))
return get_data_between(datetime.date(1970, 1, 1), datetime.datetime.utcnow())


def get_data_per_version(version):
Expand Down Expand Up @@ -135,3 +138,10 @@ def get_endpoints():
group_by(FunctionCall.endpoint).order_by(asc('cnt')).all()
db_session.expunge_all()
return result


def get_date_of_first_request():
""" return the date (as unix timestamp) of the first request """
with session_scope() as db_session:
result = db_session.query(FunctionCall.time).first()
return time.mktime(result[0].timetuple())
17 changes: 13 additions & 4 deletions flask_monitoringdashboard/routings/export_data.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import datetime
import jwt
import pkg_resources
import time

from flask import make_response, render_template, session, request, json, jsonify

from flask_monitoringdashboard import blueprint, config
from flask_monitoringdashboard.database.function_calls import get_data, get_data_from
from flask_monitoringdashboard.database.function_calls import get_data, get_data_between, get_date_of_first_request
from flask_monitoringdashboard.database.monitor_rules import get_monitor_data
from flask_monitoringdashboard.database.tests import add_or_update_test, add_test_result, get_suite_nr
from flask_monitoringdashboard.database.tests_grouped import reset_tests_grouped, add_tests_grouped
Expand Down Expand Up @@ -57,17 +58,24 @@ def submit_test_results():

@blueprint.route('/get_json_data', defaults={'time_from': 0})
@blueprint.route('/get_json_data/<time_from>')
def get_json_data_from(time_from):
@blueprint.route('/get_json_data/<time_from>/<time_to>')
def get_json_data_from(time_from, time_to=None):
"""
The returned data is the data that is encrypted using a security token. This security token is set in the
configuration.
:param time_from: (optional) if specified, only the data-values after this date are returned.
input must be an timestamp value (utc) (= integer)
:param time_to: (optional) if specified, only the data-values before this date are returned.
input must be an timestamp value (utc) (= integer)
:return: all entries from the database. (endpoint-table)
"""
data = []
try:
for entry in get_data_from(datetime.datetime.utcfromtimestamp(int(time_from))):
time1 = datetime.datetime.utcfromtimestamp(int(time_from))
time2 = None
if time_to:
time2 = datetime.datetime.utcfromtimestamp(int(time_to))
for entry in get_data_between(time1, time2):
# nice conversion to json-object
data.append({
'endpoint': entry.endpoint,
Expand Down Expand Up @@ -112,4 +120,5 @@ def get_json_details():
:return: a json-object with the details.
"""
version = pkg_resources.require("Flask-MonitoringDashboard")[0].version
return jsonify({'version': version})
return jsonify({'version': version,
'first_request': get_date_of_first_request()})
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
</thead>
<tbody>
{% for record in times %}
<tr>
<tr style="cursor: pointer"
onclick="window.location='{{ url_for('dashboard.result_heatmap', end=record.endpoint) }}';">
<td style="background-color: {{ colors[record.endpoint] }}"></td>
<td style="max-width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;">{{ record.endpoint }}</td>
<td style="text-align: right;">
Expand Down Expand Up @@ -90,11 +91,8 @@
{% endif %}
{% endfor %}

<td>
<a href="{{ url_for('dashboard.result_heatmap', end=record.endpoint) }}"
class="btn btn-default btn-sm right-align-custom">
Details
</a>
<td><a href="{{ url_for('dashboard.result_heatmap', end=record.endpoint) }}"
class="btn btn-default btn-sm right-align-custom">Details</a>
</td>
</tr>
{% endfor %}
Expand Down
4 changes: 2 additions & 2 deletions flask_monitoringdashboard/test/db/test_function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def test_get_data_from(self):
"""
Test whether the function returns the right values.
"""
from flask_monitoringdashboard.database.function_calls import get_data_from
result = get_data_from(TIMES[len(TIMES)-1])
from flask_monitoringdashboard.database.function_calls import get_data_between
result = get_data_between(TIMES[len(TIMES)-1], TIMES[len(TIMES)-1])
self.assertEqual(len(result), 1)
self.assertEqual(result[0].endpoint, NAME)
self.assertEqual(result[0].execution_time, EXECUTION_TIMES[len(TIMES)-1])
Expand Down
7 changes: 5 additions & 2 deletions flask_monitoringdashboard/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
Some useful functions for setting up the testing environment, adding data, etc..
"""
import datetime
import time

from flask import Flask

NAME = 'main'
IP = '127.0.0.1'
GROUP_BY = '1'
EXECUTION_TIMES = [1000, 2000, 3000, 4000, 50000]
TIMES = [datetime.datetime.now()] * 5
TIMES = [datetime.datetime.utcnow()] * 5
for i in range(len(TIMES)):
TIMES[i] += datetime.timedelta(seconds=i)
TEST_NAMES = ['test_name1', 'test_name2']
print('Sleeping 10 seconds before executing tests')
time.sleep(10)


def set_test_environment():
Expand Down Expand Up @@ -43,7 +46,7 @@ def add_fake_data():

# Add MonitorRule
with session_scope() as db_session:
db_session.add(MonitorRule(endpoint=NAME, monitor=True, time_added=datetime.datetime.now(),
db_session.add(MonitorRule(endpoint=NAME, monitor=True, time_added=datetime.datetime.utcnow(),
version_added=config.version, last_accessed=TIMES[0]))

# Add Tests
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def desc():

setuptools.setup(
name="Flask-MonitoringDashboard",
version='1.11.0',
version='1.11.1',
packages=setuptools.find_packages(),
include_package_data=True,
platforms='Any',
Expand Down

0 comments on commit cc53407

Please sign in to comment.