1 import FileUtils from
'../FileUtils';
2 import Statistics from
'../../types/Statistics';
3 import { Storage
} from
'./Storage';
5 import path from
'path';
7 export class JSONFileStorage
extends Storage
{
8 constructor(storageURI
: string, logPrefix
: string) {
9 super(storageURI
, logPrefix
);
12 public storePerformanceStatistics(performanceStatistics
: Statistics
): void {
13 const performanceJSONFilePath
= path
.join(path
.resolve(__dirname
, '../../../'), this.storageURI
.pathname
.replace(/(?:^\
/)|(?:\
/$
)/g
, ''));
14 if (!fs
.existsSync(performanceJSONFilePath
)) {
15 this.open(performanceJSONFilePath
);
17 fs
.readFile(performanceJSONFilePath
, 'utf-8', (error
, data
) => {
19 FileUtils
.handleFileException(this.logPrefix
, 'Performance measurements', performanceJSONFilePath
, error
);
21 const performanceRecords
: Statistics
[] = data
? JSON
.parse(data
.toString()) as Statistics
[] : [];
22 performanceRecords
.push(performanceStatistics
);
23 fs
.writeFile(performanceJSONFilePath
, JSON
.stringify(performanceRecords
, null, 2), 'utf-8', (err
) => {
25 FileUtils
.handleFileException(this.logPrefix
, 'Performance measurements', performanceJSONFilePath
, err
);
32 private open(filePath
: string): void {
34 fs
.openSync(filePath
, 'w+');
36 FileUtils
.handleFileException(this.logPrefix
, 'Performance measurements', filePath
, error
);