From b652b0c3a8be2600bd603a7744141088de97b666 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 26 Aug 2021 00:13:33 +0200 Subject: [PATCH] Storage: storage records in JSON file as an array to ease import. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- README.md | 2 +- .../performance-storage/JSONFileStorage.ts | 25 ++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2aed8173..84d9736b 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ make SUBMODULES_INIT=false #### Firmware Management Profile -- :x: GetDiagnostics +- :white_check_mark: GetDiagnostics - :x: DiagnosticsStatusNotification - :x: FirmwareStatusNotification - :x: UpdateFirmware diff --git a/src/utils/performance-storage/JSONFileStorage.ts b/src/utils/performance-storage/JSONFileStorage.ts index 4b5085cb..60e54437 100644 --- a/src/utils/performance-storage/JSONFileStorage.ts +++ b/src/utils/performance-storage/JSONFileStorage.ts @@ -11,10 +11,29 @@ export class JSONFileStorage extends Storage { public storePerformanceStatistics(performanceStatistics: Statistics): void { const performanceJSONFilePath = path.join(path.resolve(__dirname, '../../../'), this.storageURI.pathname.replace(/(?:^\/)|(?:\/$)/g, '')); - fs.appendFile(performanceJSONFilePath, JSON.stringify(performanceStatistics, null, 2), 'utf8', (err) => { - if (err) { - FileUtils.handleFileException(this.logPrefix, 'Performance measurements', performanceJSONFilePath, err); + if (!fs.existsSync(performanceJSONFilePath)) { + this.open(performanceJSONFilePath); + } + fs.readFile(performanceJSONFilePath, 'utf-8', (error, data) => { + if (error) { + FileUtils.handleFileException(this.logPrefix, 'Performance measurements', performanceJSONFilePath, error); + } else { + const performanceRecords: Statistics[] = data ? JSON.parse(data.toString()) as Statistics[] : []; + performanceRecords.push(performanceStatistics); + fs.writeFile(performanceJSONFilePath, JSON.stringify(performanceRecords, null, 2), 'utf-8', (err) => { + if (err) { + FileUtils.handleFileException(this.logPrefix, 'Performance measurements', performanceJSONFilePath, err); + } + }); } }); } + + private open(filePath: string): void { + try { + fs.openSync(filePath, 'w+'); + } catch (error) { + FileUtils.handleFileException(this.logPrefix, 'Performance measurements', filePath, error); + } + } } -- 2.34.1