KPI macros refactoring, percentage goal is added

Change-Id: I7e1463f0323c1e17fbdf9d677e926d0e31db7f27
This commit is contained in:
Ilya Shakhat 2013-12-04 17:40:02 +04:00
parent cc39170901
commit c1008dd766
2 changed files with 88 additions and 32 deletions

View File

@ -7,63 +7,110 @@
{% block scripts %}
<script type="text/javascript">
function goal_position_in_top_loader(id, url, target_id, target_value) {
function process_stats(marker_id, url, item_id, comparator) {
$.ajax({
url: make_uri(url),
dataType: "jsonp",
success: function (data) {
data = data["stats"];
var position = -1;
var index = -1;
var sum = 0;
for (var i = 0; i < data.length; i++) {
if (data[i].id == target_id) {
position = data[i].index;
break;
sum += data[i].metric;
if (data[i].id == item_id) {
index = i;
}
}
var isOk = false;
var message = "";
if (position >= 0) {
if (position <= target_value) {
isOk = true;
message = "Target position is " + target_value;
} else {
message = "Position " + position + " is worse than target position " + target_value;
}
} else {
message = "Target " + target_id + " is not found";
}
$("#position_target_marker_" + id).addClass(isOk? "kpi_good": "kpi_bad").text(position);
$("#position_target_note_" + id).show().text(message);
var isOk = false;
var message = "";
var value = "";
if (index < 0) {
message = "Item " + item_id + " is not found in the stats";
}
else {
var compare_result = comparator(data[index], sum);
isOk = compare_result.result;
message = compare_result.message;
value = compare_result.value;
}
$("#kpi_marker_" + marker_id).addClass(isOk ? "kpi_good" : "kpi_bad").text(value);
$("#kpi_note_" + marker_id).show().text(message);
}
});
}
function goal_position_in_top(marker_id, url, item_id, position) {
$(document).ready(function () {
process_stats(marker_id, url, item_id,
function(item, sum) {
var result = item.index <= position;
return {
result: result,
message: result? "Target position is " + position:
"Position " + item.index + " is worse than target position " + position,
value: item.index
}
});
});
}
function goal_percentage_in_top_less_than(marker_id, url, item_id, goal_percentage) {
$(document).ready(function () {
process_stats(marker_id, url, item_id,
function(item, sum) {
var percentage = item.metric / sum;
var result = percentage < goal_percentage;
var percentage_formatted = Math.round(percentage * 100) + "%";
var goal_percentage_formatted = Math.round(goal_percentage * 100) + "%";
return {
result: result,
message: result? "Target percentage is " + goal_percentage_formatted:
"Value " + percentage_formatted + " is more than target " + goal_percentage_formatted,
value: percentage_formatted
}
});
});
}
</script>
{% endblock %}
{% macro goal_position_in_top(record_filter, item_type, item_id, target_position, title) -%}
{% macro marker_block(id, title) %}
<div id="position_target" class="kpi_block">
<div id="kpi_marker_{{ id }}" class="kpi_marker"></div>
<div class="kpi_title_block">
<div class="kpi_title">{{ title }}</div>
<div class="kpi_note" id="kpi_note_{{ id }}"></div>
</div>
</div>
{%- endmacro %}
{% set id = title.replace(' ', '_') %}
{% macro goal_position_in_top(record_filter, item_type, item_id, target_position, title) -%}
{% set id = title|remove_ctrl_chars %}
{% set uri = '/api/1.0/stats/' + item_type %}
<script type="application/javascript">
$(document).ready(function () {
goal_position_in_top_loader("{{ id }}", "{{ record_filter|make_url(uri)|safe }}", "{{ item_id }}", "{{ target_position }}");
});
goal_position_in_top("{{ id }}", "{{ record_filter|make_url(uri)|safe }}", "{{ item_id }}", {{ target_position }});
</script>
<div id="position_target" class="kpi_block">
<div id="position_target_marker_{{ id }}" class="kpi_marker"></div>
<div class="kpi_title_block">
<div class="kpi_title">{{ title }}</div>
<div class="kpi_note" id="position_target_note_{{ id }}"></div>
</div>
</div>
{{ marker_block(id, title) }}
{%- endmacro %}
{% macro goal_percentage_in_top_less_than(record_filter, item_type, item_id, target_percentage, title) -%}
{% set id = title|remove_ctrl_chars %}
{% set uri = '/api/1.0/stats/' + item_type %}
<script type="application/javascript">
goal_percentage_in_top_less_than("{{ id }}", "{{ record_filter|make_url(uri)|safe }}", "{{ item_id }}", {{ target_percentage }});
</script>
{{ marker_block(id, title) }}
{%- endmacro %}
{% block content %}
<h1>Metrics</h1>
@ -72,4 +119,7 @@
{{ goal_position_in_top({'release': 'icehouse', 'project_type': 'openstack', 'metric': 'marks', 'module': 'glance', 'exclude': 'core'},
'engineers', 'boris-42', 3, 'Top-3 in non-core top reviewers') }}
{{ goal_percentage_in_top_less_than({'release': 'all', 'project_type': 'stackforge', 'metric': 'commits', 'module': 'stackalytics'},
'companies', 'Mirantis', 0.8, 'Internal contribution is less than 80%') }}
{% endblock %}

View File

@ -20,6 +20,7 @@ import time
import flask
from flask.ext import gravatar as gravatar_ext
from oslo.config import cfg
import re
from dashboard import decorators
from dashboard import helpers
@ -472,6 +473,11 @@ def to_url_params(dict_params, base_url):
['%s=%s' % (k, v) for k, v in dict_params.iteritems()])
@app.template_filter('remove_ctrl_chars')
def remove_ctrl_chars(text):
return re.sub(r'[\W]', '_', text)
def main():
app.run(cfg.CONF.listen_host, cfg.CONF.listen_port)