diff --git a/puppetboard/app.py b/puppetboard/app.py index 22e67bf..9c6bf02 100644 --- a/puppetboard/app.py +++ b/puppetboard/app.py @@ -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(''.format(url_for( + 'node', env=env, node_name=fact_h.node))) + if not value: + line.append(''.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'), diff --git a/puppetboard/templates/fact.json.tpl b/puppetboard/templates/fact.json.tpl deleted file mode 100644 index 7bd982d..0000000 --- a/puppetboard/templates/fact.json.tpl +++ /dev/null @@ -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 %}{{ fact_h.node }}{% endfilter %} - {%- if not value %},{% endif -%} - {%- endif -%} - {%- if not value -%} - {%- if fact_h.value is mapping -%} - {% filter jsonprint %}
{{ fact_h.value | jsonprint }}
{% endfilter %} - {%- else -%} - {% filter jsonprint %}
{{ fact_h.value }}
{% 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 %} -} diff --git a/test/test_app.py b/test/test_app.py index 79e4d1b..7a6acd6 100644 --- a/test/test_app.py +++ b/test/test_app.py @@ -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,