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

feat(server): allow A/B test with a restricted set of rules #10966

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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 @@ -104,6 +104,79 @@ protected abstract DetectedLanguage getLanguage(String text, Map<String, String>
private long pingsCleanDateMillis = System.currentTimeMillis();
PipelinePool pipelinePool; // mocked in test -> package-private / not final

/**
* List of usernames for A/B testing with restricted rules.
* Populated from LT_TEST_ONLY_USERS environment variable.
* Format: comma-separated list of usernames
*/
private final static List<String> onlyTestUsers;

/**
* List of rule IDs enabled for A/B testing.
* Populated from LT_TEST_ONLY_RULES environment variable.
* Format: comma-separated list of rule IDs
*/
private final static List<String> onlyTestRules;

/**
* List of languages enabled for A/B testing.
* Populated from LT_TEST_ONLY_LANGUAGES environment variable.
* Format: comma-separated list of language codes
*/
private final static List<String> onlyTestLanguages;

/**
* List of clients enabled for A/B testing.
* Populated from LT_TEST_ONLY_CLIENTS environment variable.
* Format: comma-separated list of client identifiers
*/
private final static List<String> onlyTestClients;

static {
String onlyUsersEnv = System.getenv("LT_TEST_ONLY_USERS");
if (onlyUsersEnv == null || onlyUsersEnv.trim().isEmpty()) {
onlyTestUsers = Collections.emptyList();
} else {
onlyTestUsers = Arrays.stream(onlyUsersEnv.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
}

String onlyRulesEnv = System.getenv("LT_TEST_ONLY_RULES");
if (onlyRulesEnv == null || onlyRulesEnv.trim().isEmpty()) {
onlyTestRules = Collections.emptyList();
} else {
onlyTestRules = Arrays.stream(onlyRulesEnv.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
}

String onlyLanguagesEnv = System.getenv("LT_TEST_ONLY_LANGUAGES");
if (onlyLanguagesEnv == null || onlyLanguagesEnv.trim().isEmpty()) {
onlyTestLanguages = Collections.emptyList();
} else {
onlyTestLanguages = Arrays.stream(onlyLanguagesEnv.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
}

String onlyClientsEnv = System.getenv("LT_TEST_ONLY_CLIENTS");
if (onlyClientsEnv == null || onlyClientsEnv.trim().isEmpty()) {
onlyTestClients = Collections.emptyList();
} else {
onlyTestClients = Arrays.stream(onlyClientsEnv.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
}

log.info("Initialized A/B test restrictions - users: {}, rules: {}, languages: {}, clients: {}",
onlyTestUsers, onlyTestRules, onlyTestLanguages, onlyTestClients);
}

TextChecker(HTTPServerConfig config, boolean internalServer, Queue<Runnable> workQueue, RequestCounter reqCounter) {
this.config = config;
this.workQueue = workQueue;
Expand Down Expand Up @@ -464,6 +537,20 @@ public RuleMatch[] match(AnalyzedSentence sentence) throws IOException {
}
List<String> enabledRules = getEnabledRuleIds(params);

private boolean shouldApplyTestRules(Map<String, String> params, String agent, Language lang, List<String> abTest) {
String username = params.getOrDefault("username", "");
return (onlyTestUsers.contains(username) || (abTest != null && abTest.contains("only"))) &&
onlyTestLanguages.contains(lang.getShortCodeWithCountryAndVariant()) &&
onlyTestClients.contains(agent);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider improving readability of the boolean conditions.

While the logic is correct, the complex boolean expression could be more readable by breaking it down into named components.

Consider this refactoring:

 private boolean shouldApplyTestRules(Map<String, String> params, String agent, Language lang, List<String> abTest) {
   String username = params.getOrDefault("username", "");
-  return (onlyTestUsers.contains(username) || (abTest != null && abTest.contains("only"))) &&
-         onlyTestLanguages.contains(lang.getShortCodeWithCountryAndVariant()) &&
-         onlyTestClients.contains(agent);
+  boolean isTestUser = onlyTestUsers.contains(username);
+  boolean isAbTestOnly = abTest != null && abTest.contains("only");
+  boolean isTestLanguage = onlyTestLanguages.contains(lang.getShortCodeWithCountryAndVariant());
+  boolean isTestClient = onlyTestClients.contains(agent);
+  
+  return (isTestUser || isAbTestOnly) && isTestLanguage && isTestClient;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private boolean shouldApplyTestRules(Map<String, String> params, String agent, Language lang, List<String> abTest) {
String username = params.getOrDefault("username", "");
return (onlyTestUsers.contains(username) || (abTest != null && abTest.contains("only"))) &&
onlyTestLanguages.contains(lang.getShortCodeWithCountryAndVariant()) &&
onlyTestClients.contains(agent);
}
private boolean shouldApplyTestRules(Map<String, String> params, String agent, Language lang, List<String> abTest) {
String username = params.getOrDefault("username", "");
boolean isTestUser = onlyTestUsers.contains(username);
boolean isAbTestOnly = abTest != null && abTest.contains("only");
boolean isTestLanguage = onlyTestLanguages.contains(lang.getShortCodeWithCountryAndVariant());
boolean isTestClient = onlyTestClients.contains(agent);
return (isTestUser || isAbTestOnly) && isTestLanguage && isTestClient;
}


if (shouldApplyTestRules(params, agent, lang, abTest)) {
log.debug("Applying test rules for user: {}, language: {}, client: {}",
params.getOrDefault("username", ""), lang.getShortCodeWithCountryAndVariant(), agent);
useEnabledOnly = true;
enabledRules = onlyTestRules;
}

List<String> disabledRules = getDisabledRuleIds(params);
List<CategoryId> enabledCategories = getCategoryIds("enabledCategories", params);
List<CategoryId> disabledCategories = getCategoryIds("disabledCategories", params);
Expand Down