Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add <promql-query> tag #184

Merged
merged 1 commit into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions promgen/static/js/promgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,6 @@ $(document).ready(function() {
$('[data-toggle="popover"]').popover();
$('[data-toggle="tooltip"]').tooltip();

// For querying information such as number of samples being scraped or number
// of active exporters, query Prometheus (based on the passed URL) and return
// a formatted number
// <span class='prom-query'
// data-href='http://prometheus.example/api/v1/query'
// data-query='count(up)'>
$('.prom-query').each(function(index) {
var ele = $(this)
$.ajax({url: this.dataset.href, data: {'query': this.dataset.query}}).done(function(response) {
if (response.status=='success') {
ele.text(
Number.parseInt(response.data.result[0].value[1]).toLocaleString()
)
ele.parent().addClass('label-info').show()
}
}).fail(function(response){
ele.text(response.statusText)
ele.parent().addClass('label-warning').show()
})
})

$('[data-source]').click(function() {
var btn = $(this);
var query = btn.data('source') === 'self' ? btn.text() : $(btn.data('source')).val();
Expand Down
27 changes: 27 additions & 0 deletions promgen/static/js/promgen.vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,30 @@ var app = new Vue({
}
}
})


Vue.component('promql-query', {
props: ['href', 'query'],
data: function () {
return {
count: 0
}
},
template: '<span style="display:none"><slot></slot>{{count}}</span>',
mounted() {
var this_ = this;
var url = new URL(this.href)
url.search = new URLSearchParams({ query: this.query })
fetch(url)
.then(response => response.json())
.then(result => {
this_.count = Number.parseInt(result.data.result[0].value[1]).toLocaleString();
this_.$el.classList.add('label-info')
this_.$el.style.display = "inline";
})
.catch(error => {
this_.$el.classList.add('label-warning')
this_.$el.style.display = "inline";
})
}
})
8 changes: 2 additions & 6 deletions promgen/templates/promgen/project_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,10 @@ <h1>Register new Project</h1>
</td>
{% if shard.proxy %}
<td>
<span class="label" style="display:none;">
Samples: <span class="prom-query" data-href='{{shard.url}}/api/v1/query' data-query='sum(scrape_samples_scraped)'></span>
</span>
<promql-query class="label" href='{{shard.url}}/api/v1/query' query='sum(scrape_samples_scraped)'>Samples: </promql-query>
</td>
<td>
<span class="label" style="display:none;">
Exporters: <span class="prom-query" data-href='{{shard.url}}/api/v1/query' data-query='count(up)'></span>
</span>
<promql-query class="label" href='{{shard.url}}/api/v1/query' query='count(up)'>Exporters: </promql-query>
</td>
{% else %}
<td>&nbsp;</td>
Expand Down
4 changes: 2 additions & 2 deletions promgen/templates/promgen/shard_header.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% load i18n %}
<div class="well">
{% if shard.proxy %}
<span class="label" style="display:none;">Samples: <span class="prom-query" data-href='{{shard.url}}/api/v1/query' data-query='sum(scrape_samples_scraped)'></span></span>
<span class="label" style="display:none;">Exporters: <span class="prom-query" data-href='{{shard.url}}/api/v1/query' data-query='count(up)'></span></span>
<promql-query class="label" href='{{shard.url}}/api/v1/query' query='sum(scrape_samples_scraped)'>Samples: </promql-query>
<promql-query class="label" href='{{shard.url}}/api/v1/query' query='count(up)'>Exporters: </promql-query>
{% endif %}

{% if user.is_superuser %}
Expand Down