Browse Source

change csv filename from id to a human-readable string

pull/10/head
rozetko 1 year ago
parent
commit
98a9e6b64b
  1. 2
      src/routes/api.ts
  2. 9
      src/services/api_keys.ts
  3. 9
      src/services/exporter.ts
  4. 1
      src/types.ts
  5. 4
      src/utils.ts

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