diff --git a/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java b/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java index eb098db15..e14556b82 100644 --- a/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java +++ b/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java @@ -24,9 +24,7 @@ import com.autotune.common.data.result.ContainerData; import com.autotune.common.data.result.IntervalResults; import com.autotune.common.data.result.NamespaceData; -import com.autotune.common.datasource.DataSourceCollection; import com.autotune.common.datasource.DataSourceInfo; -import com.autotune.common.auth.AuthenticationConfig; import com.autotune.common.auth.AuthenticationStrategy; import com.autotune.common.auth.AuthenticationStrategyFactory; import com.autotune.common.exceptions.DataSourceNotExist; @@ -46,9 +44,6 @@ import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletResponse; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; import java.lang.reflect.Method; import java.net.URLEncoder; import java.sql.Timestamp; @@ -1772,15 +1767,11 @@ private String getResults(Map mainKruizeExperimentMAP, Kru } } else if (kruizeObject.getExperiment_usecase_type().isLocal_monitoring()) { // get data from the provided datasource in case of local monitoring - // fetch the DatasourceInfo object from the DB based on datasource name - DataSourceInfo dataSourceInfo = new ExperimentDBService().loadDataSourceFromDBByName(dataSource); - // If the DB result is invalid, check the DataSourceCollection - if (CommonUtils.isInvalidDataSource(dataSourceInfo)) { - dataSourceInfo = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSource); - // If DataSourceCollection is also invalid, return with error - if (CommonUtils.isInvalidDataSource(dataSourceInfo)) { - throw new DataSourceNotExist(KruizeConstants.DataSourceConstants.DataSourceErrorMsgs.INVALID_DATASOURCE_INFO); - } + DataSourceInfo dataSourceInfo; + try { + dataSourceInfo = CommonUtils.getDataSourceInfo(dataSource); + } catch (Exception e) { + throw new DataSourceNotExist(e.getMessage()); } // Fetch metrics dynamically from Metric Profile based on the datasource fetchMetricsBasedOnProfileAndDatasource(kruizeObject, interval_end_time, intervalStartTime, dataSourceInfo); diff --git a/src/main/java/com/autotune/analyzer/services/DSMetadataService.java b/src/main/java/com/autotune/analyzer/services/DSMetadataService.java index 4342ece87..904ada6ad 100644 --- a/src/main/java/com/autotune/analyzer/services/DSMetadataService.java +++ b/src/main/java/com/autotune/analyzer/services/DSMetadataService.java @@ -23,7 +23,6 @@ import com.autotune.analyzer.utils.GsonUTCDateAdapter; import com.autotune.common.data.ValidationOutputData; import com.autotune.common.data.dataSourceMetadata.DataSourceMetadataInfo; -import com.autotune.common.datasource.DataSourceCollection; import com.autotune.common.datasource.DataSourceInfo; import com.autotune.common.datasource.DataSourceManager; import com.autotune.common.datasource.DataSourceMetadataValidation; @@ -116,22 +115,19 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) if (validationOutputData.isSuccess()) { String dataSourceName = metadataAPIObject.getDataSourceName(); - // fetch the DatasourceInfo object from the DB based on datasource name - DataSourceInfo datasource = dataSourceManager.fetchDataSourceFromDBByName(dataSourceName); - // If the DB result is invalid, check the DataSourceCollection - if (CommonUtils.isInvalidDataSource(datasource)) { - datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName); - // If DataSourceCollection is also invalid, return with error - if (CommonUtils.isInvalidDataSource(datasource)) { - sendErrorResponse( - inputData, - response, - new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.INVALID_DATASOURCE_NAME_METADATA_EXCPTN), - HttpServletResponse.SC_BAD_REQUEST, - String.format(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_IMPORT_ERROR_MSG, dataSourceName) - ); - return; - } + // fetch the DatasourceInfo object based on datasource name + DataSourceInfo datasource; + try { + datasource = CommonUtils.getDataSourceInfo(dataSourceName); + } catch (Exception e) { + sendErrorResponse( + inputData, + response, + new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.INVALID_DATASOURCE_NAME_METADATA_EXCPTN), + HttpServletResponse.SC_BAD_REQUEST, + String.format(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_IMPORT_ERROR_MSG, dataSourceName) + ); + return; } DataSourceMetadataInfo metadataInfo = dataSourceManager.importMetadataFromDataSource(datasource); diff --git a/src/main/java/com/autotune/common/utils/CommonUtils.java b/src/main/java/com/autotune/common/utils/CommonUtils.java index b2acf15b5..81cc9ef01 100644 --- a/src/main/java/com/autotune/common/utils/CommonUtils.java +++ b/src/main/java/com/autotune/common/utils/CommonUtils.java @@ -16,7 +16,9 @@ package com.autotune.common.utils; +import com.autotune.common.datasource.DataSourceCollection; import com.autotune.common.datasource.DataSourceInfo; +import com.autotune.common.datasource.DataSourceManager; import com.autotune.utils.KruizeConstants; import java.sql.Timestamp; @@ -294,6 +296,20 @@ public static double getPercentage(double newer, double older) { return ((newer - older)/older) * 100; } + public static DataSourceInfo getDataSourceInfo(String dataSourceName) throws Exception { + DataSourceManager dataSourceManager = new DataSourceManager(); + // fetch the datasource from the DB + DataSourceInfo datasource = dataSourceManager.fetchDataSourceFromDBByName(dataSourceName); + if (isInvalidDataSource(datasource)) { + // fetch the datasource from the config + datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName); + if (isInvalidDataSource(datasource)) { + throw new Exception(KruizeConstants.DataSourceConstants.DataSourceErrorMsgs.INVALID_DATASOURCE_INFO); + } + } + return datasource; + } + // Helper method to validate the DataSourceInfo object public static boolean isInvalidDataSource(DataSourceInfo datasource) { return datasource == null || datasource.getAuthenticationConfig() == null ||