44import com .featureprobe .api .dto .MetricResponse ;
55import com .featureprobe .api .entity .Environment ;
66import com .featureprobe .api .entity .Event ;
7+ import com .featureprobe .api .entity .Targeting ;
78import com .featureprobe .api .entity .VariationHistory ;
9+ import com .featureprobe .api .mapper .TargetingVersionMapper ;
810import com .featureprobe .api .model .AccessEventPoint ;
11+ import com .featureprobe .api .model .TargetingContent ;
12+ import com .featureprobe .api .model .Variation ;
913import com .featureprobe .api .model .VariationAccessCounter ;
1014import com .featureprobe .api .repository .EnvironmentRepository ;
1115import com .featureprobe .api .repository .EventRepository ;
16+ import com .featureprobe .api .repository .TargetingRepository ;
1217import com .featureprobe .api .repository .VariationHistoryRepository ;
1318import com .google .common .collect .Lists ;
1419import lombok .AllArgsConstructor ;
@@ -35,6 +40,7 @@ public class MetricService {
3540 private EnvironmentRepository environmentRepository ;
3641 private EventRepository eventRepository ;
3742 private VariationHistoryRepository variationHistoryRepository ;
43+ private TargetingRepository targetingRepository ;
3844
3945 private static final int MAX_QUERY_HOURS = 12 * 24 ;
4046 private static final int MAX_QUERY_POINT_COUNT = 12 ;
@@ -51,7 +57,12 @@ public MetricResponse query(String projectKey, String environmentKey, String tog
5157 List <AccessEventPoint > aggregatedAccessEventPoints = aggregatePointByMetricType (variationVersionMap ,
5258 accessEventPoints , metricType );
5359
54- return new MetricResponse (accessEventPoints , summaryAccessEvents (aggregatedAccessEventPoints ));
60+ List <VariationAccessCounter > accessCounters = summaryAccessEvents (aggregatedAccessEventPoints );
61+ Targeting latestTargeting = targetingRepository .findByProjectKeyAndEnvironmentKeyAndToggleKey (projectKey ,
62+ environmentKey , toggleKey ).get ();
63+ appendLatestVariations (accessCounters , latestTargeting , metricType );
64+
65+ return new MetricResponse (accessEventPoints , accessCounters );
5566 }
5667
5768 private Map <String , VariationHistory > buildVariationVersionMap (String projectKey , String environmentKey ,
@@ -63,7 +74,7 @@ private Map<String, VariationHistory> buildVariationVersionMap(String projectKey
6374 return variationHistories .stream ().collect (Collectors .toMap (this ::toIndexValue , Function .identity ()));
6475 }
6576
66- private List <AccessEventPoint > aggregatePointByMetricType (Map <String , VariationHistory > variationVersionMap ,
77+ protected List <AccessEventPoint > aggregatePointByMetricType (Map <String , VariationHistory > variationVersionMap ,
6778 List <AccessEventPoint > accessEventPoints ,
6879 MetricType metricType ) {
6980
@@ -74,22 +85,22 @@ private List<AccessEventPoint> aggregatePointByMetricType(Map<String, VariationH
7485 VariationHistory variationHistory = variationVersionMap .get (variationAccessCounter .getValue ());
7586 if (variationHistory != null ) {
7687 variationAccessCounter .setValue (metricType .isNameType ()
77- ? variationHistory .getName () : variationHistory .getValue ());
88+ ? variationHistory .getName () : variationHistory .getValue ());
7889 }
7990 });
8091 Map <String , Long > variationCounts =
8192 accessEventPoint .getValues ().stream ().collect (Collectors .toMap (VariationAccessCounter ::getValue ,
8293 VariationAccessCounter ::getCount , Long ::sum ));
8394
8495 List <VariationAccessCounter > values = variationCounts .entrySet ().stream ().map (e ->
85- new VariationAccessCounter (e .getKey (), e .getValue (), null , null ))
96+ new VariationAccessCounter (e .getKey (), e .getValue ()))
8697 .collect (Collectors .toList ());
8798 accessEventPoint .setValues (values );
8899 });
89100 return accessEventPoints ;
90101 }
91102
92- private List <AccessEventPoint > queryAccessEventPoints (String serverSdkKey , String toggleKey , int lastHours ) {
103+ protected List <AccessEventPoint > queryAccessEventPoints (String serverSdkKey , String toggleKey , int lastHours ) {
93104 int pointIntervalCount = getPointIntervalCount (lastHours );
94105 int pointCount = lastHours / pointIntervalCount ;
95106
@@ -142,7 +153,7 @@ private List<VariationAccessCounter> queryAccessEvents(String serverSdkKey,
142153 return toAccessEvent (currentPointEvents );
143154 }
144155
145- private List <VariationAccessCounter > toAccessEvent (List <Event > events ) {
156+ protected List <VariationAccessCounter > toAccessEvent (List <Event > events ) {
146157 if (CollectionUtils .isEmpty (events )) {
147158 return Collections .emptyList ();
148159 }
@@ -199,6 +210,41 @@ protected List<VariationAccessCounter> summaryAccessEvents(List<AccessEventPoint
199210 return summaryEvents ;
200211 }
201212
213+ protected void appendLatestVariations (List <VariationAccessCounter > accessCounters , Targeting latestTargeting ,
214+ MetricType metricType ) {
215+ TargetingContent targetingContent = TargetingVersionMapper .INSTANCE .toTargetingContent (
216+ latestTargeting .getContent ());
217+
218+ if (CollectionUtils .isEmpty (targetingContent .getVariations ())) {
219+ return ;
220+ }
221+ List <String > latestVariations = targetingContent .getVariations ()
222+ .stream ()
223+ .map (metricType .isNameType () ? Variation ::getName : Variation ::getValue )
224+ .collect (Collectors .toList ());
225+
226+ setVariationDeletedIfNotInLatest (accessCounters , latestVariations );
227+ appendVariationIfInLatest (accessCounters , latestVariations );
228+ }
229+
230+ private void setVariationDeletedIfNotInLatest (List <VariationAccessCounter > accessCounters ,
231+ List <String > namesOrValues ) {
232+ accessCounters .stream ()
233+ .filter (accessCounter -> !namesOrValues .contains (accessCounter .getValue ()))
234+ .forEach (accessCounter -> accessCounter .setDeleted (true ));
235+ }
236+
237+ private void appendVariationIfInLatest (List <VariationAccessCounter > accessCounters , List <String > namesOrValues ) {
238+ namesOrValues .forEach (value -> {
239+ if (!accessCounters .stream ()
240+ .filter (accessCounter -> StringUtils .equals (accessCounter .getValue (), value ))
241+ .findFirst ()
242+ .isPresent ()) {
243+ accessCounters .add (new VariationAccessCounter (value , 0L ));
244+ }
245+ });
246+ }
247+
202248 private String queryEnvironmentServerSdkKey (String projectKey , String environmentKey ) {
203249 Environment environment = this .environmentRepository .findByProjectKeyAndKey (projectKey , environmentKey ).get ();
204250 return environment .getServerSdkKey ();
0 commit comments