Skip to content
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

[Draft] Single model - Single Term changes #1457

Open
wants to merge 2 commits into
base: mvp_demo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/main/java/com/autotune/analyzer/kruizeObject/KruizeObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.autotune.analyzer.exceptions.InvalidValueException;
import com.autotune.analyzer.recommendations.term.Terms;
import com.autotune.analyzer.serviceObjects.CreateExperimentAPIObject;
import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.analyzer.utils.ExperimentTypeAware;
import com.autotune.analyzer.utils.ExperimentTypeUtil;
Expand All @@ -26,10 +27,12 @@
import com.autotune.utils.KruizeConstants;
import com.autotune.utils.KruizeSupportedTypes;
import com.autotune.utils.Utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import io.fabric8.kubernetes.api.model.ObjectReference;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -133,12 +136,16 @@ public KruizeObject() {
@param terms A map to store the default terms with term name as the key and Terms object as the value.
@param kruizeObject The KruizeObject for which the default terms are being set.
*/
// take this from json and put value here step 2
public static void setDefaultTerms(Map<String, Terms> terms, KruizeObject kruizeObject) {
// TODO: define term names like daily, weekly, fortnightly etc
// TODO: add CRD for terms

// TODO : fixx this part from json objecttt figure out this
terms.put(KruizeConstants.JSONKeys.SHORT_TERM, new Terms(KruizeConstants.JSONKeys.SHORT_TERM, KruizeConstants.RecommendationEngineConstants
.DurationBasedEngine.DurationAmount.SHORT_TERM_DURATION_DAYS, KruizeConstants.RecommendationEngineConstants
.DurationBasedEngine.DurationAmount.SHORT_TERM_DURATION_DAYS_THRESHOLD, 4, 0.25));
// need to discuss how will these value change based on change in term
terms.put(KruizeConstants.JSONKeys.MEDIUM_TERM, new Terms(KruizeConstants.JSONKeys.MEDIUM_TERM, KruizeConstants
.RecommendationEngineConstants.DurationBasedEngine.DurationAmount.MEDIUM_TERM_DURATION_DAYS, KruizeConstants
.RecommendationEngineConstants.DurationBasedEngine.DurationAmount.MEDIUM_TERM_DURATION_DAYS_THRESHOLD, 7, 1));
Expand All @@ -149,6 +156,29 @@ public static void setDefaultTerms(Map<String, Terms> terms, KruizeObject kruize
kruizeObject.setTerms(terms);
}


public static void setCustomTerms(Map<String, Terms> terms, KruizeObject kruizeObject) {
// TODO: define term names like daily, weekly, fortnightly etc
// TODO: add CRD for terms

// TODO : fixx this part from json objecttt figure out this
// for loop for multiple terms

terms.put(kruizeObject.getRecommendation_settings().getTermSettings().getTerms().get(0), new Terms(kruizeObject.getRecommendation_settings().getTermSettings().getTerms().get(0)
, kruizeObject.getRecommendation_settings().getTermSettings().getTermDuration(),
KruizeConstants.RecommendationEngineConstants.DurationBasedEngine.DurationAmount.SHORT_TERM_DURATION_DAYS_THRESHOLD,
4, 0.25));
// need to discuss how will these value change based on change in term
// terms.put(KruizeConstants.JSONKeys.MEDIUM_TERM, new Terms(KruizeConstants.JSONKeys.MEDIUM_TERM, KruizeConstants
// .RecommendationEngineConstants.DurationBasedEngine.DurationAmount.MEDIUM_TERM_DURATION_DAYS, KruizeConstants
// .RecommendationEngineConstants.DurationBasedEngine.DurationAmount.MEDIUM_TERM_DURATION_DAYS_THRESHOLD, 7, 1));
// terms.put(KruizeConstants.JSONKeys.LONG_TERM, new Terms(KruizeConstants.JSONKeys.LONG_TERM, KruizeConstants
// .RecommendationEngineConstants.DurationBasedEngine.DurationAmount.LONG_TERM_DURATION_DAYS, KruizeConstants
// .RecommendationEngineConstants.DurationBasedEngine.DurationAmount.LONG_TERM_DURATION_DAYS_THRESHOLD, 15, 1));

kruizeObject.setTerms(terms);
}

public String getExperimentName() {
return experimentName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.autotune.analyzer.kruizeObject;

import java.util.List;

public class ModelSettings {

private List<String> models;

public List<String> getModels() {
return models;
}

public void setModels(List<String> models) {
this.models = models;
}

@Override
public String toString() {
return "ModelSettings{" +
"models=" + models +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

public class RecommendationSettings {
private Double threshold;
private ModelSettings modelSettings;
private TermSettings termSettings;

public Double getThreshold() {
return threshold;
Expand All @@ -26,10 +28,28 @@ public void setThreshold(Double threshold) {
this.threshold = threshold;
}

public ModelSettings getModelSettings() {
return modelSettings;
}

public void setModelSettings(ModelSettings modelSettings) {
this.modelSettings = modelSettings;
}

public TermSettings getTermSettings() {
return termSettings;
}

public void setTermSettings(TermSettings termSettings) {
this.termSettings = termSettings;
}

@Override
public String toString() {
return "RecommendationSettings{" +
"threshold=" + threshold +
", modelSettings=" + modelSettings +
", termSettings=" + termSettings +
'}';
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/autotune/analyzer/kruizeObject/TermSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.autotune.analyzer.kruizeObject;

import java.util.List;

public class TermSettings {
private List<String> terms;
private String termDuration;

public List<String> getTerms() {
return terms;
}

public void setTerms(List<String> terms) {
this.terms = terms;
}

public String getTermDuration() {
return termDuration;
}

public void setTermDuration(String termDuration) {
this.termDuration = termDuration;
}

@Override
public String toString() {
return "TermSettings{" +
"terms=" + terms +
", singleTerm='" + termDuration + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ public class RecommendationEngine {
private Map<String, Terms> terms;
private KruizeObject kruizeObject;
private Timestamp interval_end_time;
private String modelName;
// private ModelName modelName = kruizeObject.getRecommendationSettings().getModelSettings().getModels();


public RecommendationEngine(String experimentName, String intervalEndTimeStr, String intervalStartTimeStr) {
this.experimentName = experimentName;
this.intervalEndTimeStr = intervalEndTimeStr;
this.intervalStartTimeStr = intervalStartTimeStr;
this.init();
// this.modelName = modelName;
// this.init(modelName);
}

private static int getNumPods(Map<Timestamp, IntervalResults> filteredResultsMap) {
Expand Down Expand Up @@ -123,16 +126,27 @@ private static int getNumPodsForNamespace(Map<Timestamp, IntervalResults> filter
return (int) Math.ceil(max_pods_cpu);
}

private void init() {
private void LoadRecommendationModel(String modelName) {
// Add new models
recommendationModels = new ArrayList<>();
// Create Cost based model
CostBasedRecommendationModel costBasedRecommendationModel = new CostBasedRecommendationModel();
// TODO: Create profile based model
registerModel(costBasedRecommendationModel);
// Create Performance based model
PerformanceBasedRecommendationModel performanceBasedRecommendationModel = new PerformanceBasedRecommendationModel();
registerModel(performanceBasedRecommendationModel);
/// TODO: add if else pick models for the get func we created -- first change
if("cost".equalsIgnoreCase(modelName)) {
CostBasedRecommendationModel costBasedRecommendationModel = new CostBasedRecommendationModel();
// TODO: Create profile based model
registerModel(costBasedRecommendationModel);
} else if ("performance".equalsIgnoreCase(modelName)) {
// Create Performance based model
PerformanceBasedRecommendationModel performanceBasedRecommendationModel = new PerformanceBasedRecommendationModel();
registerModel(performanceBasedRecommendationModel);
} else {
CostBasedRecommendationModel costBasedRecommendationModel = new CostBasedRecommendationModel();
// TODO: Create profile based model
registerModel(costBasedRecommendationModel);
// Create Performance based model
PerformanceBasedRecommendationModel performanceBasedRecommendationModel = new PerformanceBasedRecommendationModel();
registerModel(performanceBasedRecommendationModel);
}
// TODO: Add profile based once recommendation algos are available
}

Expand Down Expand Up @@ -168,10 +182,22 @@ public void setExperimentName(String experimentName) {
this.experimentName = experimentName;
}

public String getModelName() {
return modelName;
}

public void setModelName(String modelName) {
this.modelName = modelName;
}

public Timestamp getInterval_end_time() {
return interval_end_time;
}

public String getIntervalEndTimeStr() {
return intervalEndTimeStr;
}

public void setInterval_end_time(Timestamp interval_end_time) {
this.interval_end_time = interval_end_time;
}
Expand Down Expand Up @@ -291,9 +317,15 @@ public KruizeObject prepareRecommendations(int calCount, String target_cluster)
KruizeObject.setDefaultTerms(terms, kruizeObject);
// set the performance profile
setPerformanceProfile(kruizeObject.getPerformanceProfile());
// set custom terms
KruizeObject.setCustomTerms(terms, kruizeObject);
// get the datasource
// TODO: If no data source given use KruizeDeploymentInfo.monitoring_agent / default datasource
String dataSource = kruizeObject.getDataSource();

setModelName(kruizeObject.getRecommendation_settings().getModelSettings().getModels().get(0));
LoadRecommendationModel(modelName);

LOGGER.debug(String.format(KruizeConstants.APIMessages.EXPERIMENT_DATASOURCE, kruizeObject.getExperimentName(), dataSource));

int maxDay = Terms.getMaxDays(terms);
Expand Down
47 changes: 42 additions & 5 deletions src/main/java/com/autotune/analyzer/serviceObjects/Converters.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.autotune.analyzer.serviceObjects;

import com.autotune.analyzer.exceptions.InvalidValueException;
import com.autotune.analyzer.kruizeObject.ExperimentUseCaseType;
import com.autotune.analyzer.kruizeObject.KruizeObject;
import com.autotune.analyzer.kruizeObject.ObjectiveFunction;
import com.autotune.analyzer.kruizeObject.SloInfo;
import com.autotune.analyzer.kruizeObject.*;
import com.autotune.analyzer.performanceProfiles.PerformanceProfile;
import com.autotune.analyzer.recommendations.ContainerRecommendations;
import com.autotune.analyzer.recommendations.NamespaceRecommendations;
import com.autotune.analyzer.recommendations.engine.RecommendationEngine;
import com.autotune.analyzer.recommendations.objects.MappedRecommendationForTimestamp;
import com.autotune.analyzer.recommendations.term.Terms;
import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.common.data.ValidationOutputData;
import com.autotune.common.data.metrics.AggregationFunctions;
Expand Down Expand Up @@ -70,6 +69,41 @@ public static KruizeObject convertCreateExperimentAPIObjToKruizeObject(CreateExp
LOGGER.debug("Experiment Type: {}", createExperimentAPIObject.getExperimentType());
k8sObjectList.add(k8sObject);
}
// TODO : some modification to add custom terms and models automatically here

// RecommendationSettings recommendationSettings = new RecommendationSettings();
// RecommendationAPIObject recommendationAPIObject = createExperimentAPIObject.getRecommendationSettings();
// // Process the threshold value (can be null or a default value in case it's missing)
// String threshold = recommendationAPIObject.getThreshold();
// recommendationSettings.setThreshold(threshold);
//
// // Process the model settings (models is an array)
// ModelSettings modelSettings = recommendationAPIObject.getModelSettings();
// if (modelSettings != null && modelSettings.getModels() != null) {
// recommendationSettings.setModels(modelSettings.getModels());
// }
//
// // Process the term settings
// TermSettings termSettings = recommendationAPIObject.getTermSettings();
// if (termSettings != null) {
// // Extract the terms array (if available)
// List<String> terms = termSettings.getTerms();
// recommendationSettings.setTerms(terms);
//
// // Extract individual terms with their values (like "short" => "1 day")
// Map<String, String> termValues = new HashMap<>();
// if (terms != null && !terms.isEmpty()) {
// for (String term : terms) {
// String value = termSettings.getTermValue(term); // Fetch value based on term name (e.g., "short" => "1 day")
// termValues.put(term, value);
// }
// }
// recommendationSettings.setTermValues(termValues);
// }
//
// // Return the fully populated RecommendationSettings object
// return recommendationSettings;

kruizeObject.setKubernetes_objects(k8sObjectList);
kruizeObject.setExperimentName(createExperimentAPIObject.getExperimentName());
kruizeObject.setApiVersion(createExperimentAPIObject.getApiVersion());
Expand All @@ -81,7 +115,10 @@ public static KruizeObject convertCreateExperimentAPIObjToKruizeObject(CreateExp
kruizeObject.setExperimentType(createExperimentAPIObject.getExperimentType());
kruizeObject.setSloInfo(createExperimentAPIObject.getSloInfo());
kruizeObject.setTrial_settings(createExperimentAPIObject.getTrialSettings());
kruizeObject.setRecommendation_settings(createExperimentAPIObject.getRecommendationSettings());
TermSettings termSettings = createExperimentAPIObject.getRecommendationSettings().getTermSettings();
RecommendationSettings recommendationSettings = new RecommendationSettings();
recommendationSettings.setTermSettings(termSettings);
kruizeObject.setRecommendation_settings(recommendationSettings);
kruizeObject.setExperiment_id(createExperimentAPIObject.getExperiment_id());
kruizeObject.setStatus(createExperimentAPIObject.getStatus());
kruizeObject.setExperiment_usecase_type(new ExperimentUseCaseType(kruizeObject));
Expand Down
Loading