-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple Import Metadata actions #1178
Changes from 9 commits
46ce94c
1b562b2
a598685
23025dd
9379994
901dbaf
70147bd
38bfee3
53f0fcb
bfe7cfc
de96117
dff11a8
2ed70f8
4b92180
dfcc632
d6aa841
4759f6c
1bae659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,16 @@ | |
|
||
package com.autotune.analyzer.services; | ||
|
||
import com.autotune.analyzer.exceptions.KruizeResponse; | ||
import com.autotune.analyzer.serviceObjects.DSMetadataAPIObject; | ||
import com.autotune.analyzer.utils.AnalyzerConstants; | ||
import com.autotune.analyzer.utils.AnalyzerErrorConstants; | ||
import com.autotune.analyzer.utils.GsonUTCDateAdapter; | ||
import com.autotune.common.data.ValidationOutputData; | ||
import com.autotune.common.data.dataSourceMetadata.DataSourceMetadataInfo; | ||
import com.autotune.common.datasource.DataSourceInfo; | ||
import com.autotune.common.datasource.DataSourceManager; | ||
import com.autotune.database.dao.ExperimentDAOImpl; | ||
import com.autotune.database.service.ExperimentDBService; | ||
import com.autotune.utils.KruizeSupportedTypes; | ||
import com.autotune.utils.MetricsConfig; | ||
|
@@ -38,10 +41,8 @@ | |
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.io.PrintWriter; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.autotune.analyzer.utils.AnalyzerConstants.ServiceConstants.CHARACTER_ENCODING; | ||
|
@@ -90,8 +91,25 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
|
||
DataSourceInfo datasource = new ExperimentDBService().loadDataSourceFromDBByName(dataSourceName); | ||
if(null != datasource) { | ||
new DataSourceManager().importMetadataFromDataSource(datasource); | ||
DataSourceMetadataInfo dataSourceMetadata = new ExperimentDBService().loadMetadataFromDBByName(dataSourceName, "false"); | ||
if (null != dataSourceMetadata) { | ||
ValidationOutputData validationOutputData = new ExperimentDAOImpl().deleteKruizeDSMetadataEntryByName(dataSourceName); | ||
|
||
if (validationOutputData.isSuccess()) { | ||
new DataSourceManager().deleteMetadataFromDataSource(datasource); | ||
} else { | ||
String errorMessage = validationOutputData.getMessage(); | ||
sendErrorResponse( | ||
response, | ||
new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.MISSING_DATASOURCE_METADATA_EXCPTN), | ||
HttpServletResponse.SC_BAD_REQUEST, | ||
errorMessage | ||
); | ||
} | ||
} | ||
|
||
new DataSourceManager().importMetadataFromDataSource(datasource); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in the review, this can cause DB corruption if for any reason the import fails as we would have removed the DB content. Please reverse the order and add validation to the import results There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the code flow including validation of imported |
||
dataSourceMetadata = new ExperimentDBService().loadMetadataFromDBByName(dataSourceName, "false"); | ||
dataSourceMetadataMap.put(dataSourceName,dataSourceMetadata); | ||
} | ||
|
||
|
@@ -283,4 +301,108 @@ private Gson createGsonObject() { | |
private boolean isValidBooleanValue(String value) { | ||
return value != null && (value.equals("true") || value.equals("false")); | ||
} | ||
|
||
/** | ||
* TODO temp solution to delete metadata, Need to evaluate use cases | ||
* | ||
* @param request | ||
* @param response | ||
* @throws ServletException | ||
* @throws IOException | ||
*/ | ||
@Override | ||
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||
dinogun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
HashMap<String, DataSourceMetadataInfo> dataSourceMetadataMap = new HashMap<>(); | ||
String inputData = ""; | ||
try { | ||
// Set the character encoding of the request to UTF-8 | ||
request.setCharacterEncoding(CHARACTER_ENCODING); | ||
|
||
inputData = request.getReader().lines().collect(Collectors.joining()); | ||
if (null == inputData || inputData.isEmpty()) { | ||
throw new Exception("Request input data cannot be null or empty"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move all of the strings to string constants There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
DSMetadataAPIObject metadataAPIObject = new Gson().fromJson(inputData, DSMetadataAPIObject.class); | ||
metadataAPIObject.validateInputFields(); | ||
String dataSourceName = metadataAPIObject.getDataSourceName(); | ||
|
||
if (null == dataSourceName || dataSourceName.isEmpty()) { | ||
sendErrorResponse( | ||
inputData, | ||
response, | ||
null, | ||
HttpServletResponse.SC_BAD_REQUEST, | ||
AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_NAME_MANDATORY); | ||
} | ||
|
||
DataSourceInfo datasource = new ExperimentDBService().loadDataSourceFromDBByName(dataSourceName); | ||
|
||
if (null == 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_DELETE_ERROR_MSG, dataSourceName) | ||
); | ||
} | ||
|
||
try { | ||
DataSourceMetadataInfo dataSourceMetadata = new ExperimentDBService().loadMetadataFromDBByName(dataSourceName, "false"); | ||
if (null == dataSourceMetadata) { | ||
sendErrorResponse( | ||
inputData, | ||
response, | ||
new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.MISSING_DATASOURCE_METADATA_EXCPTN), | ||
HttpServletResponse.SC_BAD_REQUEST, | ||
String.format(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_DELETE_ERROR_MSG, dataSourceName) | ||
); | ||
} | ||
dataSourceMetadataMap.put(dataSourceName, dataSourceMetadata); | ||
|
||
if (!dataSourceMetadataMap.isEmpty() && dataSourceMetadataMap.containsKey(dataSourceName)) { | ||
ValidationOutputData validationOutputData = new ExperimentDAOImpl().deleteKruizeDSMetadataEntryByName(dataSourceName); | ||
if (validationOutputData.isSuccess()) { | ||
new DataSourceManager().deleteMetadataFromDataSource(datasource); | ||
dataSourceMetadataMap.remove(dataSourceName); | ||
} else { | ||
sendErrorResponse( | ||
inputData, | ||
response, | ||
new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_DELETE_EXCPTN), | ||
HttpServletResponse.SC_BAD_REQUEST, | ||
validationOutputData.getMessage() | ||
); | ||
} | ||
} else { | ||
sendErrorResponse( | ||
inputData, | ||
response, | ||
new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_DELETE_EXCPTN), | ||
HttpServletResponse.SC_BAD_REQUEST, | ||
String.format(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_DELETE_ERROR_MSG, dataSourceName) | ||
); | ||
} | ||
sendSuccessResponse(response, "Datasource metadata deleted successfully."); | ||
} catch (Exception e) { | ||
LOGGER.error("Loading saved metadata for datasource {} failed: {} ", dataSourceName, e.getMessage()); | ||
} | ||
|
||
} catch (Exception e) { | ||
sendErrorResponse(inputData, response, e, HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); | ||
} | ||
} | ||
|
||
private void sendSuccessResponse(HttpServletResponse response, String message) throws IOException { | ||
response.setContentType(JSON_CONTENT_TYPE); | ||
response.setCharacterEncoding(CHARACTER_ENCODING); | ||
response.setStatus(HttpServletResponse.SC_CREATED); | ||
PrintWriter out = response.getWriter(); | ||
out.append( | ||
new Gson().toJson( | ||
new KruizeResponse(message + " View imported metadata at GET /dsmetadata", HttpServletResponse.SC_CREATED, "", "SUCCESS") | ||
) | ||
); | ||
out.flush(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,7 +91,6 @@ public interface ExperimentDAO { | |
// add metadata | ||
ValidationOutputData addMetadataToDB(KruizeDSMetadataEntry kruizeDSMetadataEntry); | ||
|
||
// Load metadata | ||
List<KruizeDSMetadataEntry> loadMetadata() throws Exception; | ||
|
||
// Delete metadata | ||
public ValidationOutputData deleteKruizeDSMetadataEntryByName(String dataSourceName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps you could name it something like "removeClusterMetadataByName" or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have followed the similar naming convention as |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move all of the code that handles the doPost to the relevant files under datasource