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

[FFM-11847]: Extend SDK client to expose FeatureSnapshot #196

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions src/main/java/io/harness/cf/client/api/InnerClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package io.harness.cf.client.api;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.harness.cf.client.common.SdkCodes;
import io.harness.cf.client.connector.*;
import io.harness.cf.client.dto.Message;
import io.harness.cf.client.dto.Target;
import io.harness.cf.model.FeatureConfig;
import io.harness.cf.model.FeatureSnapshot;
import io.harness.cf.model.Variation;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
Expand Down Expand Up @@ -307,6 +312,40 @@ public synchronized void waitForInitialization()
}
}

public List<FeatureSnapshot> getFeatureSnapshots() {
// TODO return null/empty list if snapshot is not enabled in config.
return getFeatureSnapshots("");
}

public List<FeatureSnapshot> getFeatureSnapshots(String prefix) {
// TODO return null/empty list if snapshot is not enabled in config.
List<String> identifiers = repository.getAllFeatureIdentifiers(prefix);
List<FeatureSnapshot> snapshots = new LinkedList<>();

for (String identifier : identifiers) {
FeatureSnapshot snapshot = getFeatureSnapshot(identifier);
snapshots.add(snapshot);
}

return snapshots;
}

public FeatureSnapshot getFeatureSnapshot(@NonNull String identifier) {
// TODO return null/empty list if snapshot is not enabled in config.
Optional<FeatureConfig[]> ofc = repository.getCurrentAndPreviousFeatureConfig(identifier);
FeatureSnapshot result = new FeatureSnapshot();
if (ofc.isPresent()) {
FeatureConfig[] fc = ofc.get();
result.setPrevious(fc[0]);
result.setCurrent(fc[1]);
}
// this is here to create a deep copy of the object before its returned.
// this way we protect the cache.
Gson gson = new Gson();
FeatureSnapshot deepCopySnapshot = gson.fromJson(gson.toJson(result), FeatureSnapshot.class);
return deepCopySnapshot;
}

public void on(@NonNull final Event event, @NonNull final Consumer<String> consumer) {
final CopyOnWriteArrayList<Consumer<String>> consumers =
events.getOrDefault(event, new CopyOnWriteArrayList<>());
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/client-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,13 @@ components:
type: string
required:
- variation
FeatureSnapshot:
type: object
properties:
current:
$ref: '#/components/schemas/FeatureConfig'
previous:
$ref: '#/components/schemas/FeatureConfig'
FeatureConfig:
type: object
properties:
Expand Down
Loading