1 import Constants from
'../Constants';
2 import FileUtils from
'../FileUtils';
3 import Statistics from
'../../types/Statistics';
4 import { Storage
} from
'./Storage';
6 import path from
'path';
8 export class JSONFileStorage
extends Storage
{
9 private fd
: number | null = null;
11 constructor(storageURI
: string, logPrefix
: string) {
12 super(storageURI
, logPrefix
);
13 this.dbName
= path
.join(path
.resolve(__dirname
, '../../../'), this.storageURI
.pathname
.replace(/(?:^\
/)|(?:\
/$
)/g
, ''));
16 public storePerformanceStatistics(performanceStatistics
: Statistics
): void {
17 this.checkPerformanceRecordsFile();
18 fs
.readFile(this.dbName
, 'utf-8', (error
, data
) => {
20 FileUtils
.handleFileException(this.logPrefix
, Constants
.PERFORMANCE_RECORDS_FILETYPE
, this.dbName
, error
);
22 const performanceRecords
: Statistics
[] = data
? JSON
.parse(data
) as Statistics
[] : [];
23 performanceRecords
.push(performanceStatistics
);
24 fs
.writeFile(this.dbName
, JSON
.stringify(performanceRecords
, null, 2), 'utf-8', (err
) => {
26 FileUtils
.handleFileException(this.logPrefix
, Constants
.PERFORMANCE_RECORDS_FILETYPE
, this.dbName
, err
);
35 this.fd
= fs
.openSync(this.dbName
, 'a+');
37 FileUtils
.handleFileException(this.logPrefix
, Constants
.PERFORMANCE_RECORDS_FILETYPE
, this.dbName
, error
);
41 public close(): void {
44 fs
.closeSync(this.fd
);
48 FileUtils
.handleFileException(this.logPrefix
, Constants
.PERFORMANCE_RECORDS_FILETYPE
, this.dbName
, error
);
52 private checkPerformanceRecordsFile(): void {
54 throw new Error(`${this.logPrefix} Performance records '${this.dbName}' file descriptor not found`);