Generate fact JSON directly in python
This commit is contained in:
@@ -78,12 +78,6 @@ def 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):
|
||||
app.update_template_context(context)
|
||||
t = app.jinja_env.get_template(template_name)
|
||||
@@ -752,18 +746,38 @@ def fact_ajax(env, node, fact, value):
|
||||
|
||||
total = len(facts)
|
||||
|
||||
return render_template(
|
||||
'fact.json.tpl',
|
||||
fact=fact,
|
||||
node=node,
|
||||
value=value,
|
||||
draw=draw,
|
||||
total=total,
|
||||
total_filtered=total,
|
||||
render_graph=render_graph,
|
||||
facts=facts,
|
||||
envs=envs,
|
||||
current_env=env)
|
||||
counts = {}
|
||||
json = {
|
||||
'draw': draw,
|
||||
'recordsTotal': total,
|
||||
'recordsFiltered': total,
|
||||
'data': []}
|
||||
|
||||
for fact_h in facts:
|
||||
line = []
|
||||
if not fact:
|
||||
line.append(fact_h.name)
|
||||
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'),
|
||||
|
||||
@@ -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 len(result_json['chart']) == 5
|
||||
# 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,
|
||||
|
||||
Reference in New Issue
Block a user