From 7318355b2ac7a76fe915268394f42e2b9f69ffce Mon Sep 17 00:00:00 2001 From: Arek Szostak Date: Thu, 8 Aug 2024 12:55:47 +0100 Subject: [PATCH 1/2] Added snapshot structure + and expose fucntions to fetch the snapshot. --- .../io/harness/cf/client/api/InnerClient.java | 36 +++++++++++++++++++ src/main/resources/client-v1.yaml | 7 ++++ 2 files changed, 43 insertions(+) diff --git a/src/main/java/io/harness/cf/client/api/InnerClient.java b/src/main/java/io/harness/cf/client/api/InnerClient.java index f29a3bc1..4331de3c 100644 --- a/src/main/java/io/harness/cf/client/api/InnerClient.java +++ b/src/main/java/io/harness/cf/client/api/InnerClient.java @@ -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; @@ -307,6 +312,37 @@ public synchronized void waitForInitialization() } } + public List getFeatureSnapshots() { + return getFeatureSnapshots(""); + } + + public List getFeatureSnapshots(String prefix) { + List identifiers = repository.getAllFeatureIdentifiers(prefix); + List snapshots = new LinkedList<>(); + + for (String identifier : identifiers) { + FeatureSnapshot snapshot = getFeatureSnapshot(identifier); + snapshots.add(snapshot); + } + + return snapshots; + } + + public FeatureSnapshot getFeatureSnapshot(@NonNull String identifier) { + Optional 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 consumer) { final CopyOnWriteArrayList> consumers = events.getOrDefault(event, new CopyOnWriteArrayList<>()); diff --git a/src/main/resources/client-v1.yaml b/src/main/resources/client-v1.yaml index f4643fe3..23c7661c 100644 --- a/src/main/resources/client-v1.yaml +++ b/src/main/resources/client-v1.yaml @@ -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: From 2ae5f4724c69950f97d3c2f2d13c6a990df143f3 Mon Sep 17 00:00:00 2001 From: Arek Szostak Date: Thu, 8 Aug 2024 13:00:01 +0100 Subject: [PATCH 2/2] Updated TODO --- src/main/java/io/harness/cf/client/api/InnerClient.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/io/harness/cf/client/api/InnerClient.java b/src/main/java/io/harness/cf/client/api/InnerClient.java index 4331de3c..8ad56fbd 100644 --- a/src/main/java/io/harness/cf/client/api/InnerClient.java +++ b/src/main/java/io/harness/cf/client/api/InnerClient.java @@ -313,10 +313,12 @@ public synchronized void waitForInitialization() } public List getFeatureSnapshots() { + // TODO return null/empty list if snapshot is not enabled in config. return getFeatureSnapshots(""); } public List getFeatureSnapshots(String prefix) { + // TODO return null/empty list if snapshot is not enabled in config. List identifiers = repository.getAllFeatureIdentifiers(prefix); List snapshots = new LinkedList<>(); @@ -329,6 +331,7 @@ public List getFeatureSnapshots(String prefix) { } public FeatureSnapshot getFeatureSnapshot(@NonNull String identifier) { + // TODO return null/empty list if snapshot is not enabled in config. Optional ofc = repository.getCurrentAndPreviousFeatureConfig(identifier); FeatureSnapshot result = new FeatureSnapshot(); if (ofc.isPresent()) {