mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
(prometheus) cache metric suggest query result (#9182)
* (prometheus) cache metric suggest query result * add test
This commit is contained in:
committed by
Torkel Ödegaard
parent
5d4b8b5a5c
commit
4446e95155
@@ -23,7 +23,7 @@ export class PromCompleter {
|
||||
var query = prefix;
|
||||
var line = editor.session.getLine(pos.row);
|
||||
|
||||
return this.datasource.performSuggestQuery(query).then(metricNames => {
|
||||
return this.datasource.performSuggestQuery(query, true).then(metricNames => {
|
||||
callback(null, metricNames.map(name => {
|
||||
let value = name;
|
||||
if (prefix === '(') {
|
||||
|
||||
@@ -24,6 +24,7 @@ export class PrometheusDatasource {
|
||||
directUrl: string;
|
||||
basicAuth: any;
|
||||
withCredentials: any;
|
||||
metricsNameCache: any;
|
||||
|
||||
/** @ngInject */
|
||||
constructor(instanceSettings,
|
||||
@@ -157,11 +158,21 @@ export class PrometheusDatasource {
|
||||
return this._request('GET', url, query.requestId);
|
||||
}
|
||||
|
||||
performSuggestQuery(query) {
|
||||
performSuggestQuery(query, cache = false) {
|
||||
var url = '/api/v1/label/__name__/values';
|
||||
|
||||
return this._request('GET', url).then(function(result) {
|
||||
return _.filter(result.data.data, function (metricName) {
|
||||
if (cache && this.metricsNameCache && this.metricsNameCache.expire > Date.now()) {
|
||||
return this.$q.when(_.filter(this.metricsNameCache.data, metricName => {
|
||||
return metricName.indexOf(query) !== 1;
|
||||
}));
|
||||
}
|
||||
|
||||
return this._request('GET', url).then(result => {
|
||||
this.metricsNameCache = {
|
||||
data: result.data.data,
|
||||
expire: Date.now() + (60 * 1000)
|
||||
};
|
||||
return _.filter(result.data.data, metricName => {
|
||||
return metricName.indexOf(query) !== 1;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -107,4 +107,25 @@ describe('PrometheusMetricFindQuery', function() {
|
||||
expect(results[0].text).to.be('metric{job="testjob"} 3846 1443454528000');
|
||||
});
|
||||
});
|
||||
|
||||
describe('When performing performSuggestQuery', function() {
|
||||
var results;
|
||||
var response;
|
||||
it('cache response', function() {
|
||||
response = {
|
||||
status: "success",
|
||||
data: ["value1", "value2", "value3"]
|
||||
};
|
||||
ctx.$httpBackend.expect('GET', 'proxied/api/v1/label/__name__/values').respond(response);
|
||||
ctx.ds.performSuggestQuery('value', true).then(function(data) { results = data; });
|
||||
ctx.$httpBackend.flush();
|
||||
ctx.$rootScope.$apply();
|
||||
expect(results.length).to.be(3);
|
||||
ctx.ds.performSuggestQuery('value', true).then(function (data) {
|
||||
// get from cache, no need to flush
|
||||
results = data;
|
||||
expect(results.length).to.be(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user