Skip to content

Commit

Permalink
feat: add option excludeABTest to remote rule
Browse files Browse the repository at this point in the history
allows rules to be disabled when an abtest parameter matching a regex is set
  • Loading branch information
fabrichter committed Jul 25, 2024
1 parent 5daf668 commit 4217836
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ public List<Rule> getRelevantRemoteRules(ResourceBundle messageBundle, List<Remo
.forEach(rules::add);
rules.removeIf(rule -> {
String activeRemoteRuleAbTest = ((RemoteRule) rule).getServiceConfiguration().getOptions().get("abtest"); //abtest option value must match the abtest value from server.properties
// allow disabling based on A/B test flags to compare multiple models
String excludeABTest = ((RemoteRule) rule).getServiceConfiguration().getOptions().get("excludeABTest");
List<String> activeAbTestsForUser = userConfig.getAbTest();
if (excludeABTest != null && activeAbTestsForUser != null &&
activeAbTestsForUser.stream().anyMatch(flag -> flag.matches(excludeABTest))) {
return true;
}
if (activeRemoteRuleAbTest != null && !activeRemoteRuleAbTest.trim().isEmpty()) { // A/B-Test active for remote rule
if (activeAbTestsForUser == null) {
return true; // No A/B-Tests are not active for user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@
import org.junit.Test;
import org.languagetool.AnalyzedSentence;
import org.languagetool.JLanguageTool;
import org.languagetool.UserConfig;
import org.languagetool.language.Demo;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;

public class RemoteRuleTest {

Expand Down Expand Up @@ -173,4 +175,48 @@ public void testFailedRequests() throws IOException {
assertMatches("no matches for failing requests", 0);
}

private UserConfig getUserConfigWithAbTest(List<String> abTest) {
UserConfig userConfig = new UserConfig(Collections.emptyList(), Collections.emptyList(), Collections.emptyMap(),
5, null, null, null, null, false, abTest, null, false, null);
return userConfig;
}

@Test
public void testAbFlags() throws IOException {
lt = new JLanguageTool(new Demo());
assertFalse(lt.getAllActiveRules().stream().anyMatch(r -> r.getId().equals(config.ruleId)));

RemoteRuleConfig c1 = new RemoteRuleConfig(config);
c1.options.put("abtest", "foo");
lt = new JLanguageTool(new Demo());
lt.activateRemoteRules(Arrays.asList(c1));

assertFalse(lt.getAllActiveRules().stream().anyMatch(r -> r.getId().equals(config.ruleId)));

UserConfig u1 = getUserConfigWithAbTest(Arrays.asList("foo"));
lt = new JLanguageTool(new Demo(), null, u1);
lt.activateRemoteRules(Arrays.asList(c1));

assertTrue(lt.getAllActiveRules().stream().anyMatch(r -> r.getId().equals(config.ruleId)));

UserConfig u2 = getUserConfigWithAbTest(Arrays.asList("foo", "bar"));
RemoteRuleConfig c2 = new RemoteRuleConfig(config);
c2.options.put("abtest", "bar");
c2.options.put("excludeABTest", "fo.");
lt = new JLanguageTool(new Demo(), null, u2);
lt.activateRemoteRules(Arrays.asList(c2));

assertFalse(lt.getAllActiveRules().stream().anyMatch(r -> r.getId().equals(config.ruleId)));

UserConfig u3 = getUserConfigWithAbTest(Arrays.asList("bar"));
lt = new JLanguageTool(new Demo(), null, u3);
lt.activateRemoteRules(Arrays.asList(c2));

assertTrue(lt.getAllActiveRules().stream().anyMatch(r -> r.getId().equals(config.ruleId)));

RemoteRuleConfig c3 = new RemoteRuleConfig(config);
UserConfig u4 = getUserConfigWithAbTest(Arrays.asList());
c2.options.put("excludeABTest", "fo.");
}

}

0 comments on commit 4217836

Please sign in to comment.