Browse Source

better task validation

pull/6/head
rozetko 2 years ago
parent
commit
c813d21151
  1. 22
      src/services/exporter.ts

22
src/services/exporter.ts

@ -33,13 +33,14 @@ export class Exporter {
this._task.id = uuidv4();
this._task.progress = _.cloneDeep(DEFAULT_PROGRESS);
this._validateQueries(task.queries);
this._validateDatasourceUrl(datasourceUrl);
const targets = task.queries.map((query: DashboardQuery) => new Target(
query.panel,
query.datasource,
));
this._validateTargets(datasourceUrl, targets);
await this._updateProgress();
const queryConfigs = targets.map(
@ -127,15 +128,22 @@ export class Exporter {
}
}
private _validateTargets(datasourceUrl: string, targets: Target[]) {
if(!targets || !Array.isArray(targets)) {
throw new Error('Incorrect targets format');
private _validateQueries(queries: DashboardQuery[]) {
if(!queries || !Array.isArray(queries)) {
throw new Error('`queries` field is required and should be an array');
}
if(targets.length > 1) {
throw new Error(`Multiple queries are not supported yet`);
for(const query of queries) {
if(!_.isEmpty(query.datasource)) {
throw new Error('all queries should have a `datasource` field');
}
if(!_.isEmpty(query.panel.targets)) {
throw new Error('all queries should have a `panel` field with non-empty `targets` field');
}
}
}
private _validateDatasourceUrl(datasourceUrl: string) {
const host = new URL(datasourceUrl).origin;
const apiKey = apiKeys[host];

Loading…
Cancel
Save