From 98a9e6b64b9ca2a6747e27940f48e7a98a022ec3 Mon Sep 17 00:00:00 2001 From: rozetko Date: Mon, 6 Feb 2023 17:25:12 +0300 Subject: [PATCH 1/2] change csv filename from id to a human-readable string --- src/routes/api.ts | 2 +- src/services/api_keys.ts | 9 +++++---- src/services/exporter.ts | 9 +++++++-- src/types.ts | 1 + src/utils.ts | 4 ++++ 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/routes/api.ts b/src/routes/api.ts index ca40d86..7ce7c4a 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -11,4 +11,4 @@ export const router = express.Router(); router.use('/status', statusRouter); router.use('/connect', connectRouter); router.use('/task', tasksRouter); -router.use('/static', express.static(CSV_PATH)); +router.use('/static', express.static(CSV_PATH, { extensions: ['csv'] })); diff --git a/src/services/api_keys.ts b/src/services/api_keys.ts index 9e7eccf..7a1dd99 100644 --- a/src/services/api_keys.ts +++ b/src/services/api_keys.ts @@ -6,10 +6,6 @@ import * as _ from 'lodash'; const API_KEYS_FILE = path.join(DATA_PATH, 'api-keys.json'); -if(!fs.existsSync(API_KEYS_FILE)) { - console.log(`${API_KEYS_FILE} doesn't exist, creating`); - fs.writeFileSync(API_KEYS_FILE, JSON.stringify({}), 'utf8'); -} export function getApiKey(grafanaUrl: string): string | null { const data = fs.readFileSync(API_KEYS_FILE, 'utf8'); @@ -22,6 +18,11 @@ export function getApiKey(grafanaUrl: string): string | null { } export function upsertApiKey(grafanaUrl: string, apiKey: string): void { + if (!fs.existsSync(API_KEYS_FILE)) { + console.log(`${API_KEYS_FILE} doesn't exist, creating`); + fs.writeFileSync(API_KEYS_FILE, JSON.stringify({}), 'utf8'); + } + const data = fs.readFileSync(API_KEYS_FILE, 'utf8'); const apiKeys = JSON.parse(data); diff --git a/src/services/exporter.ts b/src/services/exporter.ts index 0275676..85605f6 100644 --- a/src/services/exporter.ts +++ b/src/services/exporter.ts @@ -1,5 +1,5 @@ import { getApiKey } from './api_keys'; -import { toIsoString } from '../utils'; +import { toDateString, toIsoString } from '../utils'; import { DashboardQuery, ExportProgress, ExportStatus, ExportTask } from '../types'; import { CSV_PATH } from '../config'; @@ -37,6 +37,11 @@ export class Exporter { this._validateQueries(task.queries); + const panelName = _.snakeCase(task.queries[0].panel.title); + const dateFrom = toDateString(task.timeRange.from, timeZoneName); + const dateTo = toDateString(task.timeRange.to, timeZoneName); + this._task.filename = `${panelName}_${task.dashboardUid}_${dateFrom}_${dateTo}`; + await this._updateProgress(); const queryConfigs = task.queries.map( @@ -170,7 +175,7 @@ export class Exporter { } private _getFilename(extension: string): string { - return `${this._task.id}.${extension}`; + return `${this._task.filename}.${extension}`; } private _getFilePath(extension: string): string { diff --git a/src/types.ts b/src/types.ts index 90703d5..77faf55 100644 --- a/src/types.ts +++ b/src/types.ts @@ -105,6 +105,7 @@ export type ExportTask = { csvDelimiter: string; progress?: ExportProgress; id?: string; + filename?: string; }; export type DashboardQuery = { diff --git a/src/utils.ts b/src/utils.ts index 1377075..42496e3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,9 @@ import * as moment from 'moment-timezone'; +export function toDateString(msTimestamp: number, timeZone: string): string { + return moment.tz(msTimestamp, timeZone).format('YYYY-MM-DD'); +} + export function toIsoString(msTimestamp: number, timeZone: string): string { return moment.tz(msTimestamp, timeZone).format('YYYY-MM-DD HH:mm:ssZ').replace(/:00$/, ''); } -- 2.34.1 From 1126665b7f422566f10e15d427ba7eefb9c38236 Mon Sep 17 00:00:00 2001 From: rozetko Date: Mon, 6 Feb 2023 17:26:16 +0300 Subject: [PATCH 2/2] 0.7.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b443b63..e209fdd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "grafana-data-exporter", - "version": "0.7.2", + "version": "0.7.3", "description": "Server for fetching data from Grafana datasources", "scripts": { "start": "node dist/server.js", -- 2.34.1