From 9c22ac2cf3418971fa08fd576afc7e848cf41fbd Mon Sep 17 00:00:00 2001 From: Philipp Zehnder Date: Fri, 23 Feb 2024 08:23:27 +0100 Subject: [PATCH] fix(#2413): Add clean up for assets and extract methods of deletion steps (#2478) --- .../streampipes/rest/ResetManagement.java | 110 +++++++++++++----- 1 file changed, 78 insertions(+), 32 deletions(-) diff --git a/streampipes-rest/src/main/java/org/apache/streampipes/rest/ResetManagement.java b/streampipes-rest/src/main/java/org/apache/streampipes/rest/ResetManagement.java index 31f3fbe720..77fb60bb42 100644 --- a/streampipes-rest/src/main/java/org/apache/streampipes/rest/ResetManagement.java +++ b/streampipes-rest/src/main/java/org/apache/streampipes/rest/ResetManagement.java @@ -18,6 +18,7 @@ package org.apache.streampipes.rest; +import org.apache.streampipes.commons.exceptions.SpRuntimeException; import org.apache.streampipes.commons.exceptions.connect.AdapterException; import org.apache.streampipes.commons.prometheus.adapter.AdapterMetricsManager; import org.apache.streampipes.connect.management.management.AdapterMasterManagement; @@ -36,12 +37,15 @@ import org.apache.streampipes.storage.api.IDashboardStorage; import org.apache.streampipes.storage.api.IDashboardWidgetStorage; import org.apache.streampipes.storage.api.IDataExplorerWidgetStorage; +import org.apache.streampipes.storage.api.IGenericStorage; import org.apache.streampipes.storage.management.StorageDispatcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; import java.util.List; +import java.util.Map; public class ResetManagement { // This class should be moved into another package. I moved it here because I got a cyclic maven @@ -52,28 +56,56 @@ public class ResetManagement { /** * Remove all configurations for this user. This includes: - * [pipeline assembly cache, pipelines, adapters, files] + * [pipeline assembly cache, pipelines, adapters, files, assets] * - * @param username + * @param username of the user to delte the resources */ public static void reset(String username) { logger.info("Start resetting the system"); - // Set hide tutorial to false for user + setHideTutorialToFalse(username); + + clearPipelineAssemblyCache(username); + + stopAndDeleteAllPipelines(); + + stopAndDeleteAllAdapters(); + + deleteAllFiles(); + + removeAllDataInDataLake(); + + removeAllDataViewWidgets(); + + removeAllDataViews(); + + removeAllDashboardWidgets(); + + removeAllDashboards(); + + removeAllAssets(username); + + logger.info("Resetting the system was completed"); + } + + private static void setHideTutorialToFalse(String username) { UserResourceManager.setHideTutorial(username, true); + } - // Clear pipeline assembly Cache + private static void clearPipelineAssemblyCache(String username) { PipelineCacheManager.removeCachedPipeline(username); PipelineCanvasMetadataCacheManager.removeCanvasMetadataFromCache(username); + } - // Stop and delete all pipelines + private static void stopAndDeleteAllPipelines() { List allPipelines = PipelineManager.getAllPipelines(); allPipelines.forEach(pipeline -> { PipelineManager.stopPipeline(pipeline.getPipelineId(), true); PipelineManager.deletePipeline(pipeline.getPipelineId()); }); + } - // Stop and delete all adapters + private static void stopAndDeleteAllAdapters() { AdapterMasterManagement adapterMasterManagement = new AdapterMasterManagement( StorageDispatcher.INSTANCE.getNoSqlStore() .getAdapterInstanceStorage(), @@ -94,14 +126,14 @@ public static void reset(String username) { } catch (AdapterException e) { logger.error("Failed to load all adapter descriptions", e); } + } - // Stop and delete all files + private static void deleteAllFiles() { List allFiles = FileManager.getAllFiles(); - allFiles.forEach(fileMetadata -> { - FileManager.deleteFile(fileMetadata.getFileId()); - }); + allFiles.forEach(fileMetadata -> FileManager.deleteFile(fileMetadata.getFileId())); + } - // Remove all data in data lake + private static void removeAllDataInDataLake() { var dataLakeStorage = StorageDispatcher.INSTANCE .getNoSqlStore() .getDataLakeStorage(); @@ -116,34 +148,48 @@ public static void reset(String username) { dataLakeMeasureManagement.deleteMeasurementByName(measurement.getMeasureName()); } }); + } - // Remove all data views widgets + private static void removeAllDataViewWidgets() { IDataExplorerWidgetStorage widgetStorage = - StorageDispatcher.INSTANCE.getNoSqlStore().getDataExplorerWidgetStorage(); - widgetStorage.getAllDataExplorerWidgets().forEach(widget -> { - widgetStorage.deleteDataExplorerWidget(widget.getId()); - }); + StorageDispatcher.INSTANCE.getNoSqlStore() + .getDataExplorerWidgetStorage(); + widgetStorage.getAllDataExplorerWidgets() + .forEach(widget -> widgetStorage.deleteDataExplorerWidget(widget.getId())); + } - // Remove all data views + private static void removeAllDataViews() { IDashboardStorage dataLakeDashboardStorage = - StorageDispatcher.INSTANCE.getNoSqlStore().getDataExplorerDashboardStorage(); - dataLakeDashboardStorage.getAllDashboards().forEach(dashboard -> { - dataLakeDashboardStorage.deleteDashboard(dashboard.getCouchDbId()); - }); + StorageDispatcher.INSTANCE.getNoSqlStore() + .getDataExplorerDashboardStorage(); + dataLakeDashboardStorage.getAllDashboards() + .forEach(dashboard -> dataLakeDashboardStorage.deleteDashboard(dashboard.getCouchDbId())); + } - // Remove all dashboard widgets + private static void removeAllDashboardWidgets() { IDashboardWidgetStorage dashobardWidgetStorage = - StorageDispatcher.INSTANCE.getNoSqlStore().getDashboardWidgetStorage(); - dashobardWidgetStorage.getAllDashboardWidgets().forEach(widget -> { - dashobardWidgetStorage.deleteDashboardWidget(widget.getId()); - }); + StorageDispatcher.INSTANCE.getNoSqlStore() + .getDashboardWidgetStorage(); + dashobardWidgetStorage.getAllDashboardWidgets() + .forEach(widget -> dashobardWidgetStorage.deleteDashboardWidget(widget.getId())); + } - // Remove all dashboards - IDashboardStorage dashboardStorage = StorageDispatcher.INSTANCE.getNoSqlStore().getDashboardStorage(); - dashboardStorage.getAllDashboards().forEach(dashboard -> { - dashboardStorage.deleteDashboard(dashboard.getCouchDbId()); - }); + private static void removeAllDashboards() { + IDashboardStorage dashboardStorage = StorageDispatcher.INSTANCE.getNoSqlStore() + .getDashboardStorage(); + dashboardStorage.getAllDashboards() + .forEach(dashboard -> dashboardStorage.deleteDashboard(dashboard.getCouchDbId())); + } - logger.info("Resetting the system was completed"); + private static void removeAllAssets(String username) { + IGenericStorage genericStorage = StorageDispatcher.INSTANCE.getNoSqlStore() + .getGenericStorage(); + try { + for (Map asset : genericStorage.findAll("asset-management")) { + genericStorage.delete((String) asset.get("_id"), (String) asset.get("_rev")); + } + } catch (IOException e) { + throw new SpRuntimeException("Could not delete assets of user %s".formatted(username)); + } } }