Skip to content

Commit

Permalink
remove redundant convert method, fix the null issue in listExperiment
Browse files Browse the repository at this point in the history
Signed-off-by: saakhan <[email protected]>
  • Loading branch information
khansaad committed Nov 7, 2023
1 parent da75ffe commit 836b8e0
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 151 deletions.
102 changes: 0 additions & 102 deletions src/main/java/com/autotune/analyzer/serviceObjects/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<KubernetesAPIObject> kubernetesAPIObjects = new ArrayList<>();
KubernetesAPIObject kubernetesAPIObject;
for (K8sObject k8sObject : kruizeObject.getKubernetes_objects()) {
kubernetesAPIObject = new KubernetesAPIObject(k8sObject.getName(), k8sObject.getType(), k8sObject.getNamespace());
HashMap<String, ContainerData> containerDataMap = new HashMap<>();
List<ContainerAPIObject> 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<Timestamp, MappedRecommendationForTimestamp> 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<Timestamp> 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<Timestamp, MappedRecommendationForTimestamp> recommendations = clonedContainerData.getContainerRecommendations().getData();
Timestamp latestTimestamp = null;
List<Timestamp> 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
*/
Expand Down
51 changes: 2 additions & 49 deletions src/main/java/com/autotune/analyzer/services/ListExperiments.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -384,7 +385,7 @@ private void buildRecommendationsResponse(Map<String, KruizeObject> mKruizeExper
ko,
getLatest,
false,
(String) null);
null);

mergeRecommendationsInKruizeObject(listRecommendationsAPIObject, ko);
} catch (Exception e) {
Expand All @@ -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) {
Expand Down

0 comments on commit 836b8e0

Please sign in to comment.