You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.1 KiB
83 lines
2.1 KiB
import { DatasourceConnector, Datasource, DatasourceQuery, DataTable } from '.'; |
|
|
|
|
|
const QUERY_TIME_REGEX = /\&start=[^\&]*\&end=[^\&]*\&/; |
|
|
|
export class PrometheusConnector extends DatasourceConnector { |
|
|
|
constructor(datasource: Datasource, targets: any[]) { |
|
super(datasource, targets); |
|
} |
|
|
|
getQuery(from: number, to: number, limit: number, offset: number): DatasourceQuery { |
|
let url = this.datasource.url; |
|
from = Math.floor(from / 1000); //prometheus uses seconds for timestamp |
|
to = Math.floor(to / 1000); |
|
|
|
url = url.replace(/\&start=[^\&]+/, `&start=${from}`); |
|
url = url.replace(/\&end=[^\&]+/, `&end=${to}`); |
|
return { |
|
url, |
|
method: 'GET', |
|
schema: { |
|
params: this.datasource.params |
|
}, |
|
auth: this.datasource.auth, |
|
} |
|
} |
|
|
|
parseResponse(res): DataTable { |
|
|
|
if(res.data === undefined || res.data.data.result.length < 1) { |
|
console.log('datasource return empty response, no data'); |
|
return { |
|
columns: ['timestamp', 'target'], |
|
values: [] |
|
}; |
|
} |
|
|
|
let result = res.data.data.result; |
|
let result_matrix: DataTable = { |
|
columns: ['timestamp'], |
|
values: [] |
|
}; |
|
|
|
result.map(r => { |
|
let keys: string[] = []; |
|
for(let key in r.metric) { |
|
keys.push(`${key}=${r.metric[key]}`); |
|
} |
|
result_matrix.columns.push(keys.join(':')); |
|
}); |
|
|
|
let values = result.map(r => r.values); |
|
|
|
let timestamps: (number | null)[] = []; |
|
values.forEach(v => v.forEach((row: number[]) => timestamps.push(row[0]))); |
|
timestamps = timestamps.filter(function(item, i, ar) { |
|
return ar.indexOf(item) === i; //uniq values |
|
}); |
|
|
|
for(let t of timestamps) { |
|
let row = [t]; |
|
values.map(v => { |
|
if(v[0] === undefined) { |
|
row.push(0); |
|
} |
|
|
|
let currentTimestamp = v[0][0]; |
|
let currentValue = v[0][1]; |
|
|
|
if(currentTimestamp === t) { |
|
row.push(+currentValue); |
|
v.shift(); |
|
} else { |
|
row.push(null); |
|
} |
|
}); |
|
row[0] = +(row[0] as number) * 1000; //convert timestamp to ms |
|
result_matrix.values.push(row); |
|
}; |
|
return result_matrix; |
|
} |
|
}
|
|
|