Performance statistics: add JSON file storage support.
[e-mobility-charging-stations-simulator.git] / src / utils / performance-storage / JSONFileStorage.ts
1 import FileUtils from '../FileUtils';
2 import Statistics from '../../types/Statistics';
3 import { Storage } from './Storage';
4 import fs from 'fs';
5 import path from 'path';
6
7 export class JSONFileStorage extends Storage {
8 constructor(storageURI: string, logPrefix: string) {
9 super(storageURI, logPrefix);
10 }
11
12 public storePerformanceStatistics(performanceStatistics: Statistics): void {
13 const performanceJSONFilePath = path.join(path.resolve(__dirname, '../../../'), this.storageURI.pathname.replace(/^\/|\/$/g, ''));
14 fs.appendFile(performanceJSONFilePath, JSON.stringify(performanceStatistics, null, 2), 'utf8', (err) => {
15 if (err) {
16 FileUtils.handleFileException(this.logPrefix, 'Performance measurements', performanceJSONFilePath, err);
17 }
18 });
19 }
20 }