-
Notifications
You must be signed in to change notification settings - Fork 50
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
added Planout4jConfigClasspathBackend #33
Open
reikje
wants to merge
3
commits into
Glassdoor:master
Choose a base branch
from
reikje:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
config/src/main/java/com/glassdoor/planout4j/config/Planout4jConfigClasspathBackend.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.glassdoor.planout4j.config; | ||
|
||
import com.google.common.io.Files; | ||
import com.typesafe.config.Config; | ||
|
||
import java.io.InputStream; | ||
import java.util.*; | ||
|
||
/** | ||
* A {@link Planout4jConfigBackend} which loads namespace content found on the classpath. | ||
* | ||
* @author rschatz | ||
*/ | ||
public class Planout4jConfigClasspathBackend implements Planout4jConfigBackend { | ||
|
||
/** | ||
* Returns the name of the directory on the classpath which contains the namespace files. | ||
* @return String | ||
*/ | ||
protected String getPlanoutSubdirectory() { | ||
return "planout"; | ||
} | ||
|
||
/** | ||
* {@link Planout4jConfigClasspathBackend} does not need to be configured. | ||
* | ||
* @param config any {@link Config} | ||
*/ | ||
@Override | ||
public void configure(Config config) { } | ||
|
||
/** | ||
* Loads and returns a mapping of namespace names to json based on files in the {@code planout} resource directory. | ||
* | ||
* @return a map of namespace name to content in the corresponding persistence layer | ||
*/ | ||
@Override | ||
public Map<String, String> loadAll() { | ||
final Map<String, String> name2Content = new HashMap<>(); | ||
|
||
for (String jsonFile : getJsonResourceFiles()) { | ||
final String namespace = Files.getNameWithoutExtension(jsonFile); | ||
final String fileName = String.format( | ||
"%s%s%s", getPlanoutSubdirectory(), System.getProperty("file.separator"), jsonFile | ||
); | ||
final String json = readJson(fileName); | ||
name2Content.put(namespace, json); | ||
} | ||
return name2Content; | ||
} | ||
|
||
private String readJson(final String jsonFileName) { | ||
return readInputStream( | ||
Planout4jConfigClasspathBackend.class.getClassLoader().getResourceAsStream(jsonFileName) | ||
); | ||
} | ||
|
||
private Iterable<String> getJsonResourceFiles() { | ||
final List<String> files = new ArrayList<>(); | ||
|
||
final InputStream in = Planout4jConfigClasspathBackend.class.getClassLoader().getResourceAsStream( | ||
String.format("%s/", getPlanoutSubdirectory()) | ||
); | ||
|
||
try (final Scanner scanner = new Scanner(in)) { | ||
while (scanner.hasNextLine()) { | ||
final String fileName = scanner.nextLine(); | ||
if (fileName.endsWith(".json")) { | ||
files.add(fileName); | ||
} | ||
} | ||
} | ||
return files; | ||
} | ||
|
||
private String readInputStream(InputStream is) { | ||
try (final Scanner scanner = new Scanner(is).useDelimiter("\\A")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
return scanner.hasNext() ? scanner.next() : ""; | ||
} | ||
} | ||
|
||
/** | ||
* {@link Planout4jConfigClasspathBackend} does not persist anything. | ||
* | ||
* @param namespace2Content namespace2Content to be stored | ||
*/ | ||
@Override | ||
public void persist(Map<String, String> namespace2Content) { | ||
throw new UnsupportedOperationException(String.format("%s does not support persisting.", this.getClass().getSimpleName())); | ||
} | ||
|
||
@Override | ||
public String persistenceLayer() { | ||
return "CLASSPATH"; | ||
} | ||
|
||
@Override | ||
public String persistenceDestination() { | ||
return this.getClass().getName(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should return |
||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
config/src/test/java/com/glassdoor/planout4j/config/Planout4jConfigClasspathBackendTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.glassdoor.planout4j.config; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Tests the {@link Planout4jConfigClasspathBackend} class. | ||
* | ||
* @author rschatz | ||
*/ | ||
public class Planout4jConfigClasspathBackendTest { | ||
|
||
@Test | ||
public void testLoadingAll() { | ||
final Planout4jConfigBackend backend = new Planout4jConfigClasspathBackend() { | ||
@Override | ||
protected String getPlanoutSubdirectory() { | ||
return "classpathbackend"; | ||
} | ||
}; | ||
final Map<String, String> namespaces = backend.loadAll(); | ||
assertEquals(2, namespaces.size()); | ||
assertTrue(namespaces.containsKey("ns_test1")); | ||
assertTrue(namespaces.get("ns_test1").contains("experiment_definitions")); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
config/src/test/resources/classpathbackend/ns_test1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
{ | ||
"namespace": { | ||
"name": "internal.smoke_test", | ||
"unit": "user_guid", | ||
"segments": 100 | ||
}, | ||
"experiment_definitions": [ | ||
{ | ||
"definition": "Speed_NumResults_Test", | ||
"assign": { | ||
"op": "seq", | ||
"seq": [ | ||
{ | ||
"op": "set", | ||
"var": "srJobsResultsPerPage", | ||
"value": { | ||
"choices": { | ||
"op": "array", | ||
"values": [1, 5, 15] | ||
}, | ||
"unit": { | ||
"op": "get", | ||
"var": "user_guid" | ||
}, | ||
"op": "uniformChoice" | ||
} | ||
}, | ||
{ | ||
"op": "set", | ||
"var": "useParallelSegmentSearch", | ||
"value": { | ||
"p": 0.5, | ||
"unit": { | ||
"op": "get", | ||
"var": "user_guid" | ||
}, | ||
"op": "bernoulliTrial" | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"definition": "Sample_Experiment_2", | ||
"assign": { | ||
"op": "seq", | ||
"seq": [ | ||
{ | ||
"op": "set", | ||
"var": "rating_filter_boxes", | ||
"value": { | ||
"choices": { | ||
"op": "array", | ||
"values": [ | ||
"radio", | ||
"checkbox" | ||
] | ||
}, | ||
"unit": { | ||
"op": "get", | ||
"var": "user_guid" | ||
}, | ||
"op": "uniformChoice" | ||
} | ||
}, | ||
{ | ||
"op": "set", | ||
"var": "use_check_boxes_for_job_type_filter", | ||
"value": { | ||
"p": 0.5, | ||
"unit": { | ||
"op": "get", | ||
"var": "user_guid" | ||
}, | ||
"op": "bernoulliTrial" | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"definition": "default", | ||
"assign": { | ||
"op": "seq", | ||
"seq": [ | ||
{ | ||
"op": "set", | ||
"var": "srJobsResultsPerPage", | ||
"value": 15 | ||
}, | ||
{ | ||
"op": "set", | ||
"var": "use_pclick", | ||
"value": true | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"default_experiment": "default", | ||
"experiment_sequence": [ | ||
{ | ||
"action": "add", | ||
"definition": "Speed_NumResults_Test", | ||
"name": "Speed_NR_Ph1", | ||
"segments": 20 | ||
} | ||
] | ||
} |
121 changes: 121 additions & 0 deletions
121
config/src/test/resources/classpathbackend/ns_test2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
{ | ||
"namespace": { | ||
"name": "internal.smoke_test", | ||
"unit": "user_guid", | ||
"segments": 100 | ||
}, | ||
"experiment_definitions": [ | ||
{ | ||
"definition": "Speed_NumResults_Test", | ||
"assign": { | ||
"op": "seq", | ||
"seq": [ | ||
{ | ||
"op": "set", | ||
"var": "srJobsResultsPerPage", | ||
"value": { | ||
"choices": { | ||
"op": "array", | ||
"values": [1, 5, 15] | ||
}, | ||
"unit": { | ||
"op": "get", | ||
"var": "user_guid" | ||
}, | ||
"op": "uniformChoice" | ||
} | ||
}, | ||
{ | ||
"op": "set", | ||
"var": "useParallelSegmentSearch", | ||
"value": { | ||
"p": 0.5, | ||
"unit": { | ||
"op": "get", | ||
"var": "user_guid" | ||
}, | ||
"op": "bernoulliTrial" | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"definition": "UI_Button_Test", | ||
"assign": { | ||
"op": "seq", | ||
"seq": [ | ||
{ | ||
"op": "set", | ||
"var": "rating_filter_boxes", | ||
"value": { | ||
"choices": { | ||
"op": "array", | ||
"values": [ | ||
"radio", | ||
"checkbox" | ||
] | ||
}, | ||
"unit": { | ||
"op": "get", | ||
"var": "user_guid" | ||
}, | ||
"op": "uniformChoice" | ||
} | ||
}, | ||
{ | ||
"op": "set", | ||
"var": "use_check_boxes_for_job_type_filter", | ||
"value": { | ||
"p": 0.5, | ||
"unit": { | ||
"op": "get", | ||
"var": "user_guid" | ||
}, | ||
"op": "bernoulliTrial" | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"definition": "default", | ||
"assign": { | ||
"op": "seq", | ||
"seq": [ | ||
{ | ||
"op": "set", | ||
"var": "srJobsResultsPerPage", | ||
"value": 15 | ||
}, | ||
{ | ||
"op": "set", | ||
"var": "use_pclick", | ||
"value": true | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"default_experiment": "default", | ||
"experiment_sequence": [ | ||
{ | ||
"action": "add", | ||
"definition": "Speed_NumResults_Test", | ||
"name": "Speed_NR_Ph1", | ||
"segments": 20 | ||
}, | ||
{ | ||
"action": "add", | ||
"definition": "UI_Button_Test", | ||
"name": "UI_Ph1", | ||
"segments": 40 | ||
}, | ||
{ | ||
"action": "add", | ||
"definition": "Speed_NumResults_Test", | ||
"name": "Speed_NR_Ph2", | ||
"segments": 40 | ||
} | ||
] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class path subdir can be a configurable parameter, but I guess this can wait for future refactoring if someone needs it