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.

69 lines
2.0 KiB

2 years ago
import { DataTable, QueryType } from './metrics/metric';
import { QueryConfig } from './models/query_config';
2 years ago
import { queryDirect } from './services/direct_service';
import { queryGrafana } from './services/grafana_service';
import { BadRange } from './types';
2 years ago
export { QueryConfig } from './models/query_config';
export { Datasource, DatasourceType, DataTable } from './metrics/metric'
2 years ago
export { DatasourceUnavailable } from './types';
export { GrafanaUnavailable } from './services/grafana_service';
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
2 years ago
queryConfig: QueryConfig, url: string, from: number, to: number, queryType: QueryType,
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(
`Data-kit got wrong range: from ${from} > to ${to}`,
2 years ago
queryConfig.datasource.type,
2 years ago
url
);
}
if(from === to) {
console.warn(`Data-kit got from === to`);
}
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);
2 years ago
let res: any;
// TODO: use polymorphic `query` method instead
switch(queryType) {
case QueryType.GRAFANA:
2 years ago
res = await queryGrafana(query, apiKey as string, queryConfig.datasource);
2 years ago
break;
case QueryType.DIRECT:
2 years ago
res = await queryDirect(query, queryConfig.datasource);
2 years ago
break;
default:
throw new Error(`Unknown query type: ${queryType}`);
}
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;
}