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.
 
 

55 lines
1.6 KiB

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