55import com .featureprobe .api .entity .Environment ;
66import com .featureprobe .api .entity .Event ;
77import com .featureprobe .api .entity .Targeting ;
8+ import com .featureprobe .api .entity .TargetingVersion ;
89import com .featureprobe .api .entity .VariationHistory ;
910import com .featureprobe .api .mapper .TargetingVersionMapper ;
1011import com .featureprobe .api .model .AccessEventPoint ;
1415import com .featureprobe .api .repository .EnvironmentRepository ;
1516import com .featureprobe .api .repository .EventRepository ;
1617import com .featureprobe .api .repository .TargetingRepository ;
18+ import com .featureprobe .api .repository .TargetingVersionRepository ;
1719import com .featureprobe .api .repository .VariationHistoryRepository ;
1820import com .google .common .collect .Lists ;
1921import lombok .AllArgsConstructor ;
@@ -40,6 +42,7 @@ public class MetricService {
4042 private EnvironmentRepository environmentRepository ;
4143 private EventRepository eventRepository ;
4244 private VariationHistoryRepository variationHistoryRepository ;
45+ private TargetingVersionRepository targetingVersionRepository ;
4346 private TargetingRepository targetingRepository ;
4447
4548 private static final int MAX_QUERY_HOURS = 12 * 24 ;
@@ -50,10 +53,11 @@ public MetricResponse query(String projectKey, String environmentKey, String tog
5053 int lastHours ) {
5154 int queryLastHours = Math .min (lastHours , MAX_QUERY_HOURS );
5255 String serverSdkKey = queryEnvironmentServerSdkKey (projectKey , environmentKey );
53-
56+ Long targetingId = queryTargetingId ( projectKey , environmentKey , toggleKey );
5457 Map <String , VariationHistory > variationVersionMap = buildVariationVersionMap (projectKey ,
5558 environmentKey , toggleKey );
56- List <AccessEventPoint > accessEventPoints = queryAccessEventPoints (serverSdkKey , toggleKey , queryLastHours );
59+ List <AccessEventPoint > accessEventPoints = queryAccessEventPoints (serverSdkKey , toggleKey , targetingId ,
60+ queryLastHours );
5761 List <AccessEventPoint > aggregatedAccessEventPoints = aggregatePointByMetricType (variationVersionMap ,
5862 accessEventPoints , metricType );
5963
@@ -100,7 +104,8 @@ protected List<AccessEventPoint> aggregatePointByMetricType(Map<String, Variatio
100104 return accessEventPoints ;
101105 }
102106
103- protected List <AccessEventPoint > queryAccessEventPoints (String serverSdkKey , String toggleKey , int lastHours ) {
107+ private List <AccessEventPoint > queryAccessEventPoints (String serverSdkKey , String toggleKey ,
108+ Long targetId , int lastHours ) {
104109 int pointIntervalCount = getPointIntervalCount (lastHours );
105110 int pointCount = lastHours / pointIntervalCount ;
106111
@@ -110,8 +115,8 @@ protected List<AccessEventPoint> queryAccessEventPoints(String serverSdkKey, Str
110115 List <AccessEventPoint > accessEventPoints = Lists .newArrayList ();
111116 for (int i = 0 ; i < pointCount ; i ++) {
112117 LocalDateTime pointEndTime = pointStartTime .plusHours (pointIntervalCount );
113- AccessEventPoint accessEventPoint = queryAccessEventPoint (serverSdkKey , toggleKey , pointNameFormat ,
114- pointStartTime , pointEndTime );
118+ AccessEventPoint accessEventPoint = queryAccessEventPoint (serverSdkKey , toggleKey , targetId ,
119+ pointNameFormat , pointStartTime , pointEndTime );
115120
116121 accessEventPoints .add (accessEventPoint );
117122 pointStartTime = pointEndTime ;
@@ -129,14 +134,25 @@ protected int getPointIntervalCount(int lastHours) {
129134 return pointIntervalCount ;
130135 }
131136
132- protected AccessEventPoint queryAccessEventPoint (String serverSdkKey , String toggleKey , String pointNameFormat ,
137+ protected AccessEventPoint queryAccessEventPoint (String serverSdkKey , String toggleKey , Long targetingId ,
138+ String pointNameFormat ,
133139 LocalDateTime pointStartTime ,
134140 LocalDateTime pointEndTime ) {
135141 List <VariationAccessCounter > accessEvents = queryAccessEvents (serverSdkKey ,
136142 toggleKey , pointStartTime , pointEndTime );
137143 String pointName = String .format ("%s" , pointEndTime .format (DateTimeFormatter .ofPattern (pointNameFormat )));
144+ Long lastTargetingVersion = queryLastTargetingVersion (targetingId , pointStartTime , pointEndTime );
145+ return new AccessEventPoint (pointName , accessEvents , lastTargetingVersion );
146+ }
138147
139- return new AccessEventPoint (pointName , accessEvents );
148+ private Long queryLastTargetingVersion (Long targetingId , LocalDateTime pointStartTime , LocalDateTime pointEndTime ) {
149+ List <TargetingVersion > targetingVersions = targetingVersionRepository
150+ .findAllByTargetingIdAndCreatedTimeGreaterThanEqualAndCreatedTimeLessThanEqualOrderByCreatedTimeDesc (
151+ targetingId , toDate (pointStartTime ), toDate (pointEndTime ));
152+ if (CollectionUtils .isNotEmpty (targetingVersions )) {
153+ return targetingVersions .get (0 ).getVersion ();
154+ }
155+ return null ;
140156 }
141157
142158 private List <VariationAccessCounter > queryAccessEvents (String serverSdkKey ,
@@ -250,6 +266,12 @@ private String queryEnvironmentServerSdkKey(String projectKey, String environmen
250266 return environment .getServerSdkKey ();
251267 }
252268
269+ private Long queryTargetingId (String projectKey , String environmentKey , String toggleKey ) {
270+ Targeting targeting = targetingRepository .findByProjectKeyAndEnvironmentKeyAndToggleKey (projectKey ,
271+ environmentKey , toggleKey ).get ();
272+ return targeting .getId ();
273+ }
274+
253275 protected boolean isGroupByDay (int queryLastHours ) {
254276 return queryLastHours > GROUP_BY_DAY_HOURS ;
255277 }
0 commit comments