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.

56 lines
1.6 KiB

import { DataTable } from './connectors';
2 years ago
import { QueryConfig } from './models/query_config';
2 years ago
import { BadRange } from './types';
2 years ago
export { QueryConfig } from './models/query_config';
2 years ago
export { Datasource, DatasourceType, DataTable } from './connectors'
2 years ago
export { DatasourceUnavailable } from './types';
export { GrafanaUnavailable } from './services/query_service/grafana';
2 years ago
const CHUNK_SIZE = 50000;
/**
2 years ago
* @param queryConfig
2 years ago
* @returns { values: [time, value][], columns: string[] }
*/
2 years ago
export async function queryByConfig(
2 years ago
// TODO: check how did we wanna use `url` field
queryConfig: QueryConfig, url: string, from: number, to: number,
2 years ago
// TODO: we need an abstract DatasourceConfig class which will differ in direct and grafana queries
apiKey?: string
2 years ago
): Promise<DataTable> {
2 years ago
if(from > to) {
throw new BadRange(
`TSDB-kit got wrong range: from ${from} > to ${to}`,
2 years ago
queryConfig.datasource.type,
2 years ago
url
);
}
if(from === to) {
console.warn(`TSDB-kit got from === to`);
2 years ago
}
2 years ago
let data: DataTable = {
2 years ago
values: [],
columns: []
};
while(true) {
2 years ago
let query = queryConfig.datasourceConnector.getQuery(from, to, CHUNK_SIZE, data.values.length);
const res = await queryConfig.queryService.query(query, apiKey);
2 years ago
let chunk = queryConfig.datasourceConnector.parseResponse(res);
2 years ago
let values = chunk.values;
data.values = data.values.concat(values);
data.columns = chunk.columns;
if(values.length < CHUNK_SIZE) {
// because if we get less that we could, then there is nothing more
break;
}
}
return data;
}