The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
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.
 
 
 
 
 
 
grafana/public/app/plugins/datasource/influxdb/datasource.mock.ts

54 lines
1.6 KiB

type FieldsDefinition = {
name: string;
// String type, usually something like 'string' or 'float'.
type: string;
};
type Measurements = { [measurement: string]: FieldsDefinition[] };
type FieldReturnValue = { text: string };
/**
* Datasource mock for influx. At the moment this only works for queries that should return measurements or their
* fields and no other functionality is implemented.
*/
export class InfluxDatasourceMock {
constructor(private measurements: Measurements) {}
metricFindQuery(query: string) {
if (isMeasurementsQuery(query)) {
return this.getMeasurements();
} else {
return this.getMeasurementFields(query);
}
}
private getMeasurements(): FieldReturnValue[] {
return Object.keys(this.measurements).map((key) => ({ text: key }));
}
private getMeasurementFields(query: string): FieldReturnValue[] {
const match = query.match(/SHOW FIELD KEYS FROM \"(.+)\"/);
if (!match) {
throw new Error(`Failed to match query="${query}"`);
}
const measurementName = match[1];
if (!measurementName) {
throw new Error(`Failed to match measurement name from query="${query}"`);
}
const fields = this.measurements[measurementName];
if (!fields) {
throw new Error(
`Failed to find measurement with name="${measurementName}" in measurements="[${Object.keys(
this.measurements
).join(', ')}]"`
);
}
return fields.map((field) => ({
text: field.name,
}));
}
}
function isMeasurementsQuery(query: string) {
return /SHOW MEASUREMENTS/.test(query);
}