|
|
@ -1,7 +1,6 @@ |
|
|
|
import { Target } from '../models/target'; |
|
|
|
|
|
|
|
import { URL } from 'url'; |
|
|
|
import { URL } from 'url'; |
|
|
|
import { apiKeys } from '../config'; |
|
|
|
import { apiKeys } from '../config'; |
|
|
|
import { promisify } from '../utils'; |
|
|
|
import { promisify, toIsoString } from '../utils'; |
|
|
|
import { DashboardQuery, ExportProgress, ExportStatus, ExportTask } from '../types'; |
|
|
|
import { DashboardQuery, ExportProgress, ExportStatus, ExportTask } from '../types'; |
|
|
|
|
|
|
|
|
|
|
|
import { QueryConfig, queryByConfig } from '@corpglory/tsdb-kit'; |
|
|
|
import { QueryConfig, queryByConfig } from '@corpglory/tsdb-kit'; |
|
|
@ -27,7 +26,7 @@ const DEFAULT_PROGRESS = { |
|
|
|
export class Exporter { |
|
|
|
export class Exporter { |
|
|
|
private _task: ExportTask; |
|
|
|
private _task: ExportTask; |
|
|
|
|
|
|
|
|
|
|
|
public async export(task: ExportTask, datasourceUrl: string) { |
|
|
|
public async export(task: ExportTask, datasourceUrl: string, timeZoneName: string) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
this._task = _.cloneDeep(task); |
|
|
|
this._task = _.cloneDeep(task); |
|
|
|
this._task.id = uuidv4(); |
|
|
|
this._task.id = uuidv4(); |
|
|
@ -36,22 +35,17 @@ export class Exporter { |
|
|
|
this._validateQueries(task.queries); |
|
|
|
this._validateQueries(task.queries); |
|
|
|
this._validateDatasourceUrl(datasourceUrl); |
|
|
|
this._validateDatasourceUrl(datasourceUrl); |
|
|
|
|
|
|
|
|
|
|
|
const targets = task.queries.map((query: DashboardQuery) => new Target( |
|
|
|
|
|
|
|
query.panel, |
|
|
|
|
|
|
|
query.datasource, |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await this._updateProgress(); |
|
|
|
await this._updateProgress(); |
|
|
|
|
|
|
|
|
|
|
|
const queryConfigs = targets.map( |
|
|
|
const queryConfigs = task.queries.map( |
|
|
|
target => |
|
|
|
query => |
|
|
|
new QueryConfig( |
|
|
|
new QueryConfig( |
|
|
|
QueryType.GRAFANA, |
|
|
|
QueryType.GRAFANA, |
|
|
|
{ |
|
|
|
{ |
|
|
|
...target.datasource, |
|
|
|
...query.datasource, |
|
|
|
url: datasourceUrl |
|
|
|
url: datasourceUrl, |
|
|
|
}, |
|
|
|
}, |
|
|
|
target.panel.targets |
|
|
|
[query.target] |
|
|
|
) |
|
|
|
) |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
@ -76,12 +70,31 @@ export class Exporter { |
|
|
|
|
|
|
|
|
|
|
|
const datasourceMetrics = await queryByConfig(queryConfig, datasourceUrl, from, to, apiKey); |
|
|
|
const datasourceMetrics = await queryByConfig(queryConfig, datasourceUrl, from, to, apiKey); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(_.isEmpty(columns)) { |
|
|
|
columns = datasourceMetrics.columns; |
|
|
|
columns = datasourceMetrics.columns; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
columns = _.concat(columns, datasourceMetrics.columns.slice(1)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(_.isEmpty(values)) { |
|
|
|
values = datasourceMetrics.values; |
|
|
|
values = datasourceMetrics.values; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if(values.length !== datasourceMetrics.values.length) { |
|
|
|
|
|
|
|
throw new Error(`All queries should return rows of the same lengths`); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for(const rowIdx in values) { |
|
|
|
|
|
|
|
if(datasourceMetrics.values[rowIdx][0] !== values[rowIdx][0]) { |
|
|
|
|
|
|
|
throw new Error('Queries should return the same timestamps'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
values[rowIdx] = _.concat(values[rowIdx], datasourceMetrics.values[rowIdx].slice(1)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
values = values.map((row: number[]) => [toIsoString(row[0], timeZoneName), ...row.slice(1)]); |
|
|
|
|
|
|
|
|
|
|
|
if(columns.length > 0) { |
|
|
|
if(columns.length > 0) { |
|
|
|
this._writeCsv(stream, { columns, values, }); |
|
|
|
this._writeCsv(stream, { columns, values }); |
|
|
|
} |
|
|
|
} |
|
|
|
await this._updateProgress({ status: ExportStatus.EXPORTING, progress: (day + 1) / days }); |
|
|
|
await this._updateProgress({ status: ExportStatus.EXPORTING, progress: (day + 1) / days }); |
|
|
|
|
|
|
|
|
|
|
@ -137,8 +150,8 @@ export class Exporter { |
|
|
|
if(_.isEmpty(query.datasource)) { |
|
|
|
if(_.isEmpty(query.datasource)) { |
|
|
|
throw new Error('all queries should have a `datasource` field'); |
|
|
|
throw new Error('all queries should have a `datasource` field'); |
|
|
|
} |
|
|
|
} |
|
|
|
if(_.isEmpty(query.panel.targets)) { |
|
|
|
if(_.isEmpty(query.target)) { |
|
|
|
throw new Error('all queries should have a `panel` field with non-empty `targets` field'); |
|
|
|
throw new Error('all queries should have a `target` field'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|