Storage: storage records in JSON file as an array to ease import.
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 25 Aug 2021 22:13:33 +0000 (00:13 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 25 Aug 2021 22:13:33 +0000 (00:13 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
README.md
src/utils/performance-storage/JSONFileStorage.ts

index 2aed81736da3ba4cab922738e83ee6c5f8be1f8f..84d9736b638beefd634632e7b548dc5439bf25e1 100644 (file)
--- 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
index 4b5085cb1787f005e023b45e64bffe0a31c5db9c..60e544372f0650ffdedb6d4a7c7c34c61831d340 100644 (file)
@@ -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);
+    }
+  }
 }