-
Notifications
You must be signed in to change notification settings - Fork 32
feat: Added ODPSegmentManager #484
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
Changes from all commits
4d43f2b
a7c3ee0
f68442e
81f35a5
89f6e66
96d8558
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* | ||
* Copyright 2022, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.odp; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ODPConfig { | ||
|
||
private String apiKey; | ||
|
||
private String apiHost; | ||
|
||
private List<String> allSegments; | ||
|
||
public ODPConfig(String apiKey, String apiHost, List<String> allSegments) { | ||
this.apiKey = apiKey; | ||
this.apiHost = apiHost; | ||
this.allSegments = allSegments; | ||
} | ||
|
||
public ODPConfig(String apiKey, String apiHost) { | ||
this(apiKey, apiHost, Collections.emptyList()); | ||
} | ||
|
||
public synchronized Boolean isReady() { | ||
return !( | ||
this.apiKey == null || this.apiKey.isEmpty() | ||
|| this.apiHost == null || this.apiHost.isEmpty() | ||
); | ||
} | ||
|
||
public synchronized Boolean hasSegments() { | ||
return allSegments != null && !allSegments.isEmpty(); | ||
} | ||
|
||
public synchronized void setApiKey(String apiKey) { | ||
this.apiKey = apiKey; | ||
} | ||
|
||
public synchronized void setApiHost(String apiHost) { | ||
this.apiHost = apiHost; | ||
} | ||
|
||
public synchronized String getApiKey() { | ||
return apiKey; | ||
} | ||
|
||
public synchronized String getApiHost() { | ||
return apiHost; | ||
} | ||
|
||
public synchronized List<String> getAllSegments() { | ||
return allSegments; | ||
} | ||
|
||
public synchronized void setAllSegments(List<String> allSegments) { | ||
this.allSegments = allSegments; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* | ||
* Copyright 2022, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.odp; | ||
|
||
import com.optimizely.ab.internal.Cache; | ||
import com.optimizely.ab.internal.DefaultLRUCache; | ||
import com.optimizely.ab.odp.parser.ResponseJsonParser; | ||
import com.optimizely.ab.odp.parser.ResponseJsonParserFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ODPSegmentManager { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ODPSegmentManager.class); | ||
|
||
private static final String SEGMENT_URL_PATH = "/v3/graphql"; | ||
|
||
private final ODPApiManager apiManager; | ||
|
||
private final ODPConfig odpConfig; | ||
|
||
private final Cache<List<String>> segmentsCache; | ||
|
||
public ODPSegmentManager(ODPConfig odpConfig, ODPApiManager apiManager) { | ||
this(odpConfig, apiManager, Cache.DEFAULT_MAX_SIZE, Cache.DEFAULT_TIMEOUT_SECONDS); | ||
} | ||
|
||
public ODPSegmentManager(ODPConfig odpConfig, ODPApiManager apiManager, Cache<List<String>> cache) { | ||
this.apiManager = apiManager; | ||
this.odpConfig = odpConfig; | ||
this.segmentsCache = cache; | ||
} | ||
|
||
public ODPSegmentManager(ODPConfig odpConfig, ODPApiManager apiManager, Integer cacheSize, Integer cacheTimeoutSeconds) { | ||
this.apiManager = apiManager; | ||
this.odpConfig = odpConfig; | ||
this.segmentsCache = new DefaultLRUCache<>(cacheSize, cacheTimeoutSeconds); | ||
} | ||
|
||
public List<String> getQualifiedSegments(String fsUserId) { | ||
return getQualifiedSegments(ODPUserKey.FS_USER_ID, fsUserId, Collections.emptyList()); | ||
} | ||
public List<String> getQualifiedSegments(String fsUserId, List<ODPSegmentOption> options) { | ||
return getQualifiedSegments(ODPUserKey.FS_USER_ID, fsUserId, options); | ||
} | ||
|
||
public List<String> getQualifiedSegments(ODPUserKey userKey, String userValue) { | ||
return getQualifiedSegments(userKey, userValue, Collections.emptyList()); | ||
} | ||
|
||
public List<String> getQualifiedSegments(ODPUserKey userKey, String userValue, List<ODPSegmentOption> options) { | ||
if (!odpConfig.isReady()) { | ||
logger.error("Audience segments fetch failed (ODP is not enabled)"); | ||
return Collections.emptyList(); | ||
} | ||
|
||
if (!odpConfig.hasSegments()) { | ||
logger.debug("No Segments are used in the project, Not Fetching segments. Returning empty list"); | ||
return Collections.emptyList(); | ||
} | ||
|
||
List<String> qualifiedSegments; | ||
String cacheKey = getCacheKey(userKey.getKeyString(), userValue); | ||
|
||
if (options.contains(ODPSegmentOption.RESET_CACHE)) { | ||
segmentsCache.reset(); | ||
} else if (!options.contains(ODPSegmentOption.IGNORE_CACHE)) { | ||
qualifiedSegments = segmentsCache.lookup(cacheKey); | ||
if (qualifiedSegments != null) { | ||
logger.debug("ODP Cache Hit. Returning segments from Cache."); | ||
return qualifiedSegments; | ||
} | ||
} | ||
|
||
logger.debug("ODP Cache Miss. Making a call to ODP Server."); | ||
|
||
ResponseJsonParser parser = ResponseJsonParserFactory.getParser(); | ||
String qualifiedSegmentsResponse = apiManager.fetchQualifiedSegments(odpConfig.getApiKey(), odpConfig.getApiHost() + SEGMENT_URL_PATH, userKey.getKeyString(), userValue, odpConfig.getAllSegments()); | ||
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. Can we add filtering for cases when odpConfig.getAllSegments() is empty? 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. Added a check and a unit test. 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. what if odpConfig is not ready (apiKey and apiHost null)? 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. Added a check and a unit test. |
||
qualifiedSegments = parser.parseQualifiedSegments(qualifiedSegmentsResponse); | ||
|
||
if (qualifiedSegments != null && !options.contains(ODPSegmentOption.IGNORE_CACHE)) { | ||
segmentsCache.save(cacheKey, qualifiedSegments); | ||
} | ||
|
||
return qualifiedSegments; | ||
} | ||
|
||
private String getCacheKey(String userKey, String userValue) { | ||
return userKey + "-$-" + userValue; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* | ||
* Copyright 2022, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.odp; | ||
|
||
public enum ODPSegmentOption { | ||
|
||
IGNORE_CACHE, | ||
|
||
RESET_CACHE; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* | ||
* Copyright 2022, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.odp; | ||
|
||
public enum ODPUserKey { | ||
|
||
VUID("vuid"), | ||
|
||
FS_USER_ID("fs_user_id"); | ||
|
||
private final String keyString; | ||
|
||
ODPUserKey(String keyString) { | ||
this.keyString = keyString; | ||
} | ||
|
||
public String getKeyString() { | ||
return keyString; | ||
} | ||
} |
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.
thread-safety for all props?
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.
Made all getters and setters synchronized