Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import prerna.auth.User;
import prerna.engine.impl.model.Room;
import prerna.engine.impl.model.inferencetracking.ModelInferenceLogsUtils;
import prerna.reactor.AbstractReactor;
Expand All @@ -37,19 +40,40 @@

public class GetInsightActiveRoomModelReactor extends AbstractReactor {

private static final String MODEL_ID_OPTION_KEY = "modelId";
/** Older rooms stored the model under these option keys. Same keys AgentRunner reads. */
private static final String[] LEGACY_MODEL_ID_OPTION_KEYS = { "engine", "model", "engineId" };

@Override
public NounMetadata execute() {
String roomId = this.insight.getRoomId();
if (roomId == null) {
throw new IllegalArgumentException("Insight is not associated with any room");
}
String userId = this.insight.getUser().getPrimaryLoginToken().getId();
Room room = ModelInferenceLogsUtils.getRoomById(roomId, userId);
User user = this.insight.getUser();
String userId = user.getPrimaryLoginToken().getId();

String modelId = null;
Map<String, Object> optionsMap = room.getOptionsMap();
if (optionsMap.containsKey("modelId")) {
modelId = (String) optionsMap.get("modelId");
// The cached room is the one RunAgent works on, so it carries the model the
// run resolved even when that never reached the stored row.
Room cachedRoom = user.getRoomHash().get(roomId);
Room storedRoom = ModelInferenceLogsUtils.getRoomById(roomId, userId);
if (cachedRoom == null && storedRoom == null) {
throw new IllegalArgumentException("Room " + roomId + " does not exist");
}

// The playground writes the selected model to options.modelId, so it wins
String modelId = getOption(storedRoom, MODEL_ID_OPTION_KEY);
if (modelId == null) {
modelId = getOption(cachedRoom, MODEL_ID_OPTION_KEY);
}
if (modelId == null) {
modelId = getModelId(cachedRoom);
}
if (modelId == null) {
modelId = getModelId(storedRoom);
}
for (int i = 0; modelId == null && i < LEGACY_MODEL_ID_OPTION_KEYS.length; i++) {
modelId = getOption(storedRoom, LEGACY_MODEL_ID_OPTION_KEYS[i]);
}
if (modelId == null) {
throw new IllegalArgumentException("No model associated with the room");
Expand All @@ -58,6 +82,22 @@ public NounMetadata execute() {
return new NounMetadata(modelId, PixelDataType.CONST_STRING);
}

private static String getOption(Room room, String key) {
if (room == null) {
return null;
}
Map<String, Object> optionsMap = room.getOptionsMap();
if (optionsMap == null) {
return null;
}
Object value = optionsMap.get(key);
return value instanceof String ? StringUtils.trimToNull((String) value) : null;
}

private static String getModelId(Room room) {
return room == null ? null : StringUtils.trimToNull(room.getModelId());
}

@Override
public String getReactorDescription() {
return "Get the active model for the room set in the current insight";
Expand Down
Loading