Skip to content

Commit

Permalink
[issue #48] fix parse data from prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeiSporyshev committed Dec 16, 2020
1 parent 5e54abf commit 84f2c09
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 35 deletions.
53 changes: 37 additions & 16 deletions dist/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -18106,12 +18106,41 @@ var PrometheusProxy =
/** @class */
function () {
function PrometheusProxy(ds) {
this.__getLastNonNullValue = function (dataset) {
if (dataset) {
var skiper = dataset.filter(function (item) {
this.__getLastNonNullValue = function (item) {
if (item.fields) {
var valueField = item.fields.filter(function (row) {
return row.name && row.name === "Value";
});

if (valueField) {
var vArr = valueField[0].values.buffer.filter(function (point) {
return point != null;
});
var value = vArr[vArr.length - 1];

if (value == null) {
console.log(item);
}

return value;
}
} else if (item.dataset) {
var skiper = item.dataset.filter(function (item) {
return item[0] != null;
});
return skiper[skiper.length - 1][0];
} else {
return 0;
}
};

this.__getName = function (item, query) {
if (item.target !== undefined) {
return item.target.substring(query.legend.length + 3, item.target.length - 2);
}

if (item.name !== undefined) {
return item.name.substring(query.legend.length + 3, item.name.length - 2);
}
};

Expand All @@ -18137,7 +18166,6 @@ function () {
legendFormat: '{{' + query.legend + '}}',
interval: '15s'
};
if (debug) console.log(body);
var res = this.ds.query(body);

if (typeof res.then !== "function") {
Expand All @@ -18160,21 +18188,14 @@ function () {
debug = false;
}

if (debug) {
console.log(data);
console.log(query);
}

return data.map(function (item) {
if (debug) {
console.log(item.target);
console.log(query.legend);
console.log(item.target.substring(query.legend.length + 3, item.target.length - 2));
console.log(item);
}

return {
target: item.target.substring(query.legend.length + 3, item.target.length - 2),
datapoint: _this.__getLastNonNullValue(item.datapoints)
target: _this.__getName(item, query),
datapoint: _this.__getLastNonNullValue(item)
};
});
};
Expand Down Expand Up @@ -20361,9 +20382,9 @@ function () {
expr: "sum(node_memory_MemTotal_bytes{instance=~\"" + instance + "\"}) by (instance)",
legend: 'instance'
}, false), this.prometheusDS.query({
expr: "node_memory_SwapTotal_bytes{instance=~\"" + instance + "\"}",
expr: "sum(node_memory_SwapTotal_bytes{instance=~\"" + instance + "\"}) by (instance)",
legend: 'instance'
}), this.prometheusDS.query({
}, false), this.prometheusDS.query({
expr: "sum(node_filesystem_size_bytes{instance=~\"" + instance + "\",mountpoint=\"/\",fstype!=\"rootfs\"}) by (instance)",
legend: 'instance'
}, false), this.prometheusDS.query({
Expand Down
2 changes: 1 addition & 1 deletion dist/module.js.map

Large diffs are not rendered by default.

52 changes: 36 additions & 16 deletions src/common/proxies/prometheusProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import q from "q";
import moment from 'moment';
export class PrometheusProxy {
ds: any;
version: any;

constructor(ds){
this.ds = ds;

}

query(query: any, debug: boolean = false){
Expand All @@ -15,11 +17,7 @@ export class PrometheusProxy {
interval: '15s'
};

if(debug)
console.log(body);

let res = this.ds.query(body)

if (typeof res.then !== "function") {
res = res.toPromise()
}
Expand All @@ -34,32 +32,54 @@ export class PrometheusProxy {
}

formData(data, query, debug = false){
if(debug){
console.log(data);
console.log(query);
}
return data.map(item =>
{
if(debug){
console.log(item.target);
console.log(query.legend);
console.log(item.target.substring(query.legend.length + 3, item.target.length - 2));
console.log(item);
}


return {
target : item.target.substring(query.legend.length + 3, item.target.length - 2),
datapoint : this.__getLastNonNullValue(item.datapoints)
target : this.__getName(item, query),
datapoint : this.__getLastNonNullValue(item)
};
}

);
}

__getLastNonNullValue = dataset => {
if(dataset){
let skiper = dataset.filter(item => item[0]!= null);
__getLastNonNullValue = item => {
if(item.fields){
let valueField = item.fields.filter(row => {
return row.name && row.name === "Value";
})

if (valueField){
let vArr = valueField[0].values.buffer.filter(point => point != null);
let value = vArr[vArr.length - 1];
if(value == null){
console.log(item);
}
return value;

}
}else if(item.dataset){
let skiper = item.dataset.filter(item => item[0]!= null);
return skiper[skiper.length-1][0];
}else{
return 0;
}
};

__getName = (item, query) => {
if(item.target !== undefined){
return item.target.substring(query.legend.length + 3, item.target.length - 2);

}

if(item.name !== undefined){
return item.name.substring(query.legend.length + 3, item.name.length - 2);
}

}
}
4 changes: 2 additions & 2 deletions src/components/k8s-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ export class K8sPage {
legend: 'instance'
}, false), //ramTotal
this.prometheusDS.query({
expr: `node_memory_SwapTotal_bytes{instance=~"${instance}"}`,
expr: `sum(node_memory_SwapTotal_bytes{instance=~"${instance}"}) by (instance)`,
legend: 'instance'
}), //swapTotal
}, false), //swapTotal
this.prometheusDS.query({
expr: `sum(node_filesystem_size_bytes{instance=~"${instance}",mountpoint="/",fstype!="rootfs"}) by (instance)`,
legend: 'instance'
Expand Down

0 comments on commit 84f2c09

Please sign in to comment.