Browse Source

Merge pull request 'change csv filename from id to a human-readable string' (#10) from change-csv-filename into master

Reviewed-on: #10
pull/11/head 0.7.3
rozetko 2 years ago
parent
commit
380381ddb9
  1. 2
      package.json
  2. 2
      src/routes/api.ts
  3. 9
      src/services/api_keys.ts
  4. 9
      src/services/exporter.ts
  5. 1
      src/types.ts
  6. 4
      src/utils.ts

2
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
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'] }));

9
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);

9
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 {

1
src/types.ts

@ -105,6 +105,7 @@ export type ExportTask = {
csvDelimiter: string;
progress?: ExportProgress;
id?: string;
filename?: string;
};
export type DashboardQuery = {

4
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$/, '');
}

Loading…
Cancel
Save