From 836b8e0fa203a121b51d7896a70c4315db672f4f Mon Sep 17 00:00:00 2001 From: saakhan Date: Tue, 7 Nov 2023 17:01:38 +0530 Subject: [PATCH] remove redundant convert method, fix the null issue in listExperiment Signed-off-by: saakhan --- .../analyzer/serviceObjects/Converters.java | 102 ------------------ .../analyzer/services/ListExperiments.java | 51 +-------- 2 files changed, 2 insertions(+), 151 deletions(-) diff --git a/src/main/java/com/autotune/analyzer/serviceObjects/Converters.java b/src/main/java/com/autotune/analyzer/serviceObjects/Converters.java index e25295fc9..2016aade7 100644 --- a/src/main/java/com/autotune/analyzer/serviceObjects/Converters.java +++ b/src/main/java/com/autotune/analyzer/serviceObjects/Converters.java @@ -183,108 +183,6 @@ public static ListRecommendationsAPIObject convertKruizeObjectToListRecommendati return listRecommendationsAPIObject; } - public static ListRecommendationsAPIObject convertKruizeObjectToListRecommendationSO( - KruizeObject kruizeObject, - boolean getLatest, - boolean checkForTimestamp, - String monitoringEndTimestamp) { - ListRecommendationsAPIObject listRecommendationsAPIObject = new ListRecommendationsAPIObject(); - try { - listRecommendationsAPIObject.setApiVersion(AnalyzerConstants.VersionConstants.APIVersionConstants.CURRENT_LIST_RECOMMENDATIONS_VERSION); - listRecommendationsAPIObject.setExperimentName(kruizeObject.getExperimentName()); - listRecommendationsAPIObject.setClusterName(kruizeObject.getClusterName()); - List kubernetesAPIObjects = new ArrayList<>(); - KubernetesAPIObject kubernetesAPIObject; - for (K8sObject k8sObject : kruizeObject.getKubernetes_objects()) { - kubernetesAPIObject = new KubernetesAPIObject(k8sObject.getName(), k8sObject.getType(), k8sObject.getNamespace()); - HashMap containerDataMap = new HashMap<>(); - List containerAPIObjects = new ArrayList<>(); - for (ContainerData containerData : k8sObject.getContainerDataMap().values()) { - ContainerAPIObject containerAPIObject; - // if a Time stamp is passed it holds the priority than latest - if (checkForTimestamp) { - // This step causes a performance degradation, need to be replaced with a better flow of creating SO's - ContainerData clonedContainerData = Utils.getClone(containerData, ContainerData.class); - if (null != clonedContainerData) { - HashMap recommendations = clonedContainerData.getContainerRecommendations().getData(); - Date medDate = Utils.DateUtils.getDateFrom(KruizeConstants.DateFormats.STANDARD_JSON_DATE_FORMAT, monitoringEndTimestamp); - Timestamp givenTimestamp = new Timestamp(medDate.getTime()); - if (recommendations.containsKey(givenTimestamp)) { - List tempList = new ArrayList<>(); - for (Timestamp timestamp : recommendations.keySet()) { - if (!timestamp.equals(givenTimestamp)) - tempList.add(timestamp); - } - for (Timestamp timestamp : tempList) { - recommendations.remove(timestamp); - } - clonedContainerData.getContainerRecommendations().setData(recommendations); - containerAPIObject = new ContainerAPIObject(clonedContainerData.getContainer_name(), - clonedContainerData.getContainer_image_name(), - clonedContainerData.getContainerRecommendations(), - new ArrayList<>(clonedContainerData.getMetrics().values())); - containerAPIObjects.add(containerAPIObject); - } - } - } else if (getLatest) { - // This step causes a performance degradation, need to be replaced with a better flow of creating SO's - containerData = getLatestRecommendations(containerData); - containerAPIObject = new ContainerAPIObject(containerData.getContainer_name(), - containerData.getContainer_image_name(), - containerData.getContainerRecommendations(), - new ArrayList<>(containerData.getMetrics().values())); - containerAPIObjects.add(containerAPIObject); - } else { - containerAPIObject = new ContainerAPIObject(containerData.getContainer_name(), - containerData.getContainer_image_name(), - containerData.getContainerRecommendations(), - new ArrayList<>(containerData.getMetrics().values())); - containerAPIObjects.add(containerAPIObject); - containerDataMap.put(containerData.getContainer_name(), containerData); - } - } - kubernetesAPIObject.setContainerAPIObjects(containerAPIObjects); - kubernetesAPIObjects.add(kubernetesAPIObject); - } - listRecommendationsAPIObject.setKubernetesObjects(kubernetesAPIObjects); - } catch (Exception e) { - e.printStackTrace(); - } - return listRecommendationsAPIObject; - } - - - /** - * @param containerData - * @return - */ - public static ContainerData getLatestRecommendations(ContainerData containerData) { - ContainerData clonedContainerData = Utils.getClone(containerData, ContainerData.class); - if (null != clonedContainerData) { - HashMap recommendations = clonedContainerData.getContainerRecommendations().getData(); - Timestamp latestTimestamp = null; - List tempList = new ArrayList<>(); - for (Timestamp timestamp : recommendations.keySet()) { - if (null == latestTimestamp) { - latestTimestamp = timestamp; - } else { - if (timestamp.after(latestTimestamp)) { - tempList.add(latestTimestamp); - latestTimestamp = timestamp; - } else { - tempList.add(timestamp); - } - } - } - for (Timestamp timestamp : tempList) { - recommendations.remove(timestamp); - } - clonedContainerData.getContainerRecommendations().setData(recommendations); - } - return clonedContainerData; - } - - /** * @param containerData */ diff --git a/src/main/java/com/autotune/analyzer/services/ListExperiments.java b/src/main/java/com/autotune/analyzer/services/ListExperiments.java index 115d44391..2a3915401 100644 --- a/src/main/java/com/autotune/analyzer/services/ListExperiments.java +++ b/src/main/java/com/autotune/analyzer/services/ListExperiments.java @@ -56,6 +56,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.sql.Timestamp; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @@ -384,7 +385,7 @@ private void buildRecommendationsResponse(Map mKruizeExper ko, getLatest, false, - (String) null); + null); mergeRecommendationsInKruizeObject(listRecommendationsAPIObject, ko); } catch (Exception e) { @@ -397,54 +398,6 @@ private void mergeRecommendationsInKruizeObject(ListRecommendationsAPIObject lis ko.setKubernetes_objects(convertKubernetesAPIObjectListToK8sObjectList(listRecommendationsAPIObject.getKubernetesObjects())); } - //TODO this function no more used. - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - response.setStatus(HttpServletResponse.SC_OK); - response.setContentType(JSON_CONTENT_TYPE); - response.setCharacterEncoding(CHARACTER_ENCODING); - - LOGGER.info("Processing trial result..."); - try { - String experimentName = request.getParameter(AnalyzerConstants.ServiceConstants.EXPERIMENT_NAME); - // String deploymentName = request.getParameter(AnalyzerConstants.ServiceConstants.DEPLOYMENT_NAME); - - String trialResultsData = request.getReader().lines().collect(Collectors.joining()); - JSONObject trialResultsJson = new JSONObject(trialResultsData); - - // Read in the experiment name and the deployment name in the received JSON from EM - String experimentNameJson = trialResultsJson.getString(EXPERIMENT_NAME); - String trialNumber = trialResultsJson.getString("trialNumber"); - - JSONArray deploymentsJsonArray = trialResultsJson.getJSONArray("deployments"); - for (Object deploymentObject : deploymentsJsonArray) { - JSONObject deploymentJsonObject = (JSONObject) deploymentObject; - String deploymentNameJson = deploymentJsonObject.getString(DEPLOYMENT_NAME); - KruizeExperiment kruizeExperiment = experimentsMap.get(deploymentNameJson); - - // Check if the passed in JSON has the same info as in the URL - if (!experimentName.equals(experimentNameJson) || kruizeExperiment == null) { - LOGGER.error("Bad results JSON passed: {}", experimentNameJson); - response.setStatus(HttpServletResponse.SC_BAD_REQUEST); - break; - } - - try { - updateExperimentTrial(trialNumber, kruizeExperiment, trialResultsJson); - } catch (InvalidValueException | IncompatibleInputJSONException e) { - e.printStackTrace(); - } - RunExperiment runExperiment = kruizeExperiment.getExperimentThread(); - // Received a metrics JSON from EM after a trial, let the waiting thread know - LOGGER.info("Received trial result for experiment: " + experimentNameJson + "; Deployment name: " + deploymentNameJson); - runExperiment.send(); - } - response.getWriter().close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - public void sendErrorResponse(HttpServletResponse response, Exception e, int httpStatusCode, String errorMsg) throws IOException { if (null != e) {