Merge pull request #349 from redref/fix_dailychart
Fix dailychart in python3 + write a test for it
This commit is contained in:
@@ -9,7 +9,7 @@ DATE_FORMAT = "%Y-%m-%d"
|
|||||||
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
|
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
|
||||||
|
|
||||||
|
|
||||||
def _iter_dates(days_number):
|
def _iter_dates(days_number, reverse=False):
|
||||||
"""Return a list of datetime pairs AB, BC, CD, ... that represent the
|
"""Return a list of datetime pairs AB, BC, CD, ... that represent the
|
||||||
24hs time ranges of today (until this midnight) and the
|
24hs time ranges of today (until this midnight) and the
|
||||||
previous days.
|
previous days.
|
||||||
@@ -18,6 +18,9 @@ def _iter_dates(days_number):
|
|||||||
today = datetime.utcnow().replace(hour=0, minute=0, second=0,
|
today = datetime.utcnow().replace(hour=0, minute=0, second=0,
|
||||||
microsecond=0, tzinfo=UTC())
|
microsecond=0, tzinfo=UTC())
|
||||||
days_list = list(today + one_day * (1 - i) for i in range(days_number + 1))
|
days_list = list(today + one_day * (1 - i) for i in range(days_number + 1))
|
||||||
|
if reverse:
|
||||||
|
days_list.reverse()
|
||||||
|
return zip(days_list, days_list[1:])
|
||||||
return zip(days_list[1:], days_list)
|
return zip(days_list[1:], days_list)
|
||||||
|
|
||||||
|
|
||||||
@@ -65,7 +68,7 @@ def get_daily_reports_chart(db, env, days_number, certname=None):
|
|||||||
the database will be considered.
|
the database will be considered.
|
||||||
"""
|
"""
|
||||||
result = []
|
result = []
|
||||||
for start, end in reversed(_iter_dates(days_number)):
|
for start, end in _iter_dates(days_number, reverse=True):
|
||||||
query = _build_query(
|
query = _build_query(
|
||||||
env=env,
|
env=env,
|
||||||
start=start.strftime(DATETIME_FORMAT),
|
start=start.strftime(DATETIME_FORMAT),
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import pytest
|
import pytest
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
from datetime import datetime
|
||||||
from puppetboard import app
|
from puppetboard import app
|
||||||
from pypuppetdb.types import Node, Report
|
from pypuppetdb.types import Node, Report
|
||||||
from puppetboard import default_settings
|
from puppetboard import default_settings
|
||||||
@@ -523,3 +524,37 @@ def test_json_report_ok(client, mocker, input_data):
|
|||||||
|
|
||||||
assert 'data' in result_json
|
assert 'data' in result_json
|
||||||
assert len(result_json['data']) == 100
|
assert len(result_json['data']) == 100
|
||||||
|
|
||||||
|
|
||||||
|
def test_json_daily_reports_chart_ok(client, mocker):
|
||||||
|
mock_puppetdb_environments(mocker)
|
||||||
|
mock_puppetdb_default_nodes(mocker)
|
||||||
|
|
||||||
|
query_data = {
|
||||||
|
'reports': [
|
||||||
|
[{'status': 'changed', 'count': 1}]
|
||||||
|
for i in range(app.app.config['DAILY_REPORTS_CHART_DAYS'])
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logging.error(query_data)
|
||||||
|
|
||||||
|
dbquery = MockDbQuery(query_data)
|
||||||
|
|
||||||
|
mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)
|
||||||
|
|
||||||
|
rv = client.get('/daily_reports_chart.json')
|
||||||
|
result_json = json.loads(rv.data.decode('utf-8'))
|
||||||
|
|
||||||
|
assert 'result' in result_json
|
||||||
|
assert (len(result_json['result']) ==
|
||||||
|
app.app.config['DAILY_REPORTS_CHART_DAYS'])
|
||||||
|
day_format = '%Y-%m-%d'
|
||||||
|
cur_day = datetime.strptime(result_json['result'][0]['day'], day_format)
|
||||||
|
for day in result_json['result'][1:]:
|
||||||
|
next_day = datetime.strptime(day['day'], day_format)
|
||||||
|
assert cur_day < next_day
|
||||||
|
cur_day = next_day
|
||||||
|
|
||||||
|
assert rv.status_code == 200
|
||||||
|
|||||||
Reference in New Issue
Block a user