Generate fact JSON directly in python
This commit is contained in:
@@ -78,12 +78,6 @@ def version():
|
|||||||
return __version__
|
return __version__
|
||||||
|
|
||||||
|
|
||||||
@app.template_filter()
|
|
||||||
def format_attribute(obj, attr, format_str):
|
|
||||||
setattr(obj, attr, format_str.format(getattr(obj, attr)))
|
|
||||||
return obj
|
|
||||||
|
|
||||||
|
|
||||||
def stream_template(template_name, **context):
|
def stream_template(template_name, **context):
|
||||||
app.update_template_context(context)
|
app.update_template_context(context)
|
||||||
t = app.jinja_env.get_template(template_name)
|
t = app.jinja_env.get_template(template_name)
|
||||||
@@ -752,18 +746,38 @@ def fact_ajax(env, node, fact, value):
|
|||||||
|
|
||||||
total = len(facts)
|
total = len(facts)
|
||||||
|
|
||||||
return render_template(
|
counts = {}
|
||||||
'fact.json.tpl',
|
json = {
|
||||||
fact=fact,
|
'draw': draw,
|
||||||
node=node,
|
'recordsTotal': total,
|
||||||
value=value,
|
'recordsFiltered': total,
|
||||||
draw=draw,
|
'data': []}
|
||||||
total=total,
|
|
||||||
total_filtered=total,
|
for fact_h in facts:
|
||||||
render_graph=render_graph,
|
line = []
|
||||||
facts=facts,
|
if not fact:
|
||||||
envs=envs,
|
line.append(fact_h.name)
|
||||||
current_env=env)
|
if not node:
|
||||||
|
line.append('<a href="{0}">'.format(url_for(
|
||||||
|
'node', env=env, node_name=fact_h.node)))
|
||||||
|
if not value:
|
||||||
|
line.append('<a href="{0}">'.format(url_for(
|
||||||
|
'fact', env=env, fact=fact_h.name, value=fact_h.value)))
|
||||||
|
|
||||||
|
json['data'].append(line)
|
||||||
|
|
||||||
|
if render_graph:
|
||||||
|
if fact_h.value not in counts:
|
||||||
|
counts[fact_h.value] = 0
|
||||||
|
counts[fact_h.value] += 1
|
||||||
|
|
||||||
|
if render_graph:
|
||||||
|
json['chart'] = [
|
||||||
|
{"label": "{0}".format(k).replace('\n', ' '),
|
||||||
|
"value": counts[k]}
|
||||||
|
for k in sorted(counts, key=lambda k: counts[k], reverse=True)]
|
||||||
|
|
||||||
|
return jsonify(json)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/query', methods=('GET', 'POST'),
|
@app.route('/query', methods=('GET', 'POST'),
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
{
|
|
||||||
"draw": {{draw}},
|
|
||||||
"recordsTotal": {{total}},
|
|
||||||
"recordsFiltered": {{total_filtered}},
|
|
||||||
"data": [
|
|
||||||
{% for fact_h in facts -%}
|
|
||||||
{%- if not loop.first %},{%- endif -%}
|
|
||||||
[
|
|
||||||
{%- if not fact -%}
|
|
||||||
{{ fact_h.name | jsonprint }}
|
|
||||||
{%- if node or value %},{% endif -%}
|
|
||||||
{%- endif -%}
|
|
||||||
{%- if not node -%}
|
|
||||||
{% filter jsonprint %}<a href="{{ url_for('node', env=current_env, node_name=fact_h.node) }}">{{ fact_h.node }}</a>{% endfilter %}
|
|
||||||
{%- if not value %},{% endif -%}
|
|
||||||
{%- endif -%}
|
|
||||||
{%- if not value -%}
|
|
||||||
{%- if fact_h.value is mapping -%}
|
|
||||||
{% filter jsonprint %}<a href="{{ url_for('fact', env=current_env, fact=fact_h.name, value=fact_h.value) }}"><pre>{{ fact_h.value | jsonprint }}</pre></a>{% endfilter %}
|
|
||||||
{%- else -%}
|
|
||||||
{% filter jsonprint %}<a href="{{ url_for('fact', env=current_env, fact=fact_h.name, value=fact_h.value) }}"><pre>{{ fact_h.value }}</pre></a>{% endfilter %}
|
|
||||||
{%- endif -%}
|
|
||||||
{%- endif -%}
|
|
||||||
]
|
|
||||||
{% endfor -%}
|
|
||||||
]
|
|
||||||
{%- if render_graph %},
|
|
||||||
"chart": [
|
|
||||||
{% for fact_h in facts | map('format_attribute', 'value', '{0}') | groupby('value') -%}
|
|
||||||
{%- if not loop.first %},{%- endif -%}
|
|
||||||
{
|
|
||||||
"label": {{ fact_h.grouper | replace("\n", " ") | jsonprint }},
|
|
||||||
"value": {{ fact_h.list|length }}
|
|
||||||
}
|
|
||||||
{% endfor %}
|
|
||||||
]
|
|
||||||
{% endif %}
|
|
||||||
}
|
|
||||||
@@ -727,7 +727,7 @@ def test_fact_json_with_graph(client, mocker,
|
|||||||
assert 'chart' in result_json
|
assert 'chart' in result_json
|
||||||
assert len(result_json['chart']) == 5
|
assert len(result_json['chart']) == 5
|
||||||
# Test group_by
|
# Test group_by
|
||||||
assert result_json['chart'][3]['value'] == 2
|
assert result_json['chart'][0]['value'] == 2
|
||||||
|
|
||||||
|
|
||||||
def test_fact_json_without_graph(client, mocker,
|
def test_fact_json_without_graph(client, mocker,
|
||||||
|
|||||||
Reference in New Issue
Block a user