Skip to content

Commit

Permalink
refactor code to get datasourceInfo from a common util method
Browse files Browse the repository at this point in the history
Signed-off-by: Saad Khan <[email protected]>
  • Loading branch information
khansaad committed Sep 18, 2024
1 parent e798581 commit 9d50dfc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1772,15 +1767,11 @@ private String getResults(Map<String, KruizeObject> 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);
Expand Down
30 changes: 13 additions & 17 deletions src/main/java/com/autotune/analyzer/services/DSMetadataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/autotune/common/utils/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ||
Expand Down

0 comments on commit 9d50dfc

Please sign in to comment.