99
1010import org .elasticsearch .TransportVersion ;
1111import org .elasticsearch .TransportVersions ;
12+ import org .elasticsearch .cluster .metadata .DataStreamGlobalRetention ;
1213import org .elasticsearch .common .Strings ;
1314import org .elasticsearch .common .io .stream .StreamInput ;
1415import org .elasticsearch .common .io .stream .StreamOutput ;
2021import org .elasticsearch .xpack .core .XPackField ;
2122
2223import java .io .IOException ;
24+ import java .util .HashMap ;
2325import java .util .LongSummaryStatistics ;
2426import java .util .Map ;
2527import java .util .Objects ;
@@ -183,41 +185,23 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
183185 builder .field ("count" , dataStreamsWithLifecyclesCount );
184186 builder .field ("default_rollover_used" , defaultRolloverUsed );
185187
186- builder .startObject ("data_retention" );
187- builder .field ("configured_data_streams" , dataRetentionStats .dataStreamCount ());
188- if (dataRetentionStats .dataStreamCount () > 0 ) {
189- builder .field ("minimum_millis" , dataRetentionStats .minMillis );
190- builder .field ("maximum_millis" , dataRetentionStats .maxMillis );
191- builder .field ("average_millis" , dataRetentionStats .avgMillis );
192- }
193- builder .endObject ();
194-
195- builder .startObject ("effective_retention" );
196- builder .field ("retained_data_streams" , effectiveRetentionStats .dataStreamCount ());
197- if (effectiveRetentionStats .dataStreamCount () > 0 ) {
198- builder .field ("minimum_millis" , effectiveRetentionStats .minMillis );
199- builder .field ("maximum_millis" , effectiveRetentionStats .maxMillis );
200- builder .field ("average_millis" , effectiveRetentionStats .avgMillis );
201- }
202- builder .endObject ();
188+ RetentionStats .toXContentFragment (builder , dataRetentionStats , false );
189+ RetentionStats .toXContentFragment (builder , effectiveRetentionStats , true );
203190
204191 builder .startObject ("global_retention" );
205- globalRetentionStatsToXContent (builder , params , LifecycleStats .DEFAULT_RETENTION_FIELD_NAME );
206- globalRetentionStatsToXContent (builder , params , LifecycleStats .MAX_RETENTION_FIELD_NAME );
192+ GlobalRetentionStats .toXContentFragment (
193+ builder ,
194+ LifecycleStats .DEFAULT_RETENTION_FIELD_NAME ,
195+ globalRetentionStats .get (LifecycleStats .DEFAULT_RETENTION_FIELD_NAME )
196+ );
197+ GlobalRetentionStats .toXContentFragment (
198+ builder ,
199+ LifecycleStats .MAX_RETENTION_FIELD_NAME ,
200+ globalRetentionStats .get (LifecycleStats .MAX_RETENTION_FIELD_NAME )
201+ );
207202 builder .endObject ();
208203 return builder ;
209204 }
210-
211- private void globalRetentionStatsToXContent (XContentBuilder builder , Params params , String retentionType ) throws IOException {
212- builder .startObject (retentionType );
213- GlobalRetentionStats stats = globalRetentionStats .get (retentionType );
214- builder .field ("defined" , stats != null );
215- if (stats != null ) {
216- builder .field ("affected_data_streams" , stats .dataStreamCount ());
217- builder .field ("retention_millis" , stats .retention ());
218- }
219- builder .endObject ();
220- }
221205 }
222206
223207 public record RetentionStats (long dataStreamCount , Double avgMillis , Long minMillis , Long maxMillis ) implements Writeable {
@@ -251,6 +235,17 @@ public void writeTo(StreamOutput out) throws IOException {
251235 out .writeVLong (maxMillis );
252236 }
253237 }
238+
239+ static void toXContentFragment (XContentBuilder builder , RetentionStats stats , boolean isEffectiveRetention ) throws IOException {
240+ builder .startObject (isEffectiveRetention ? "effective_retention" : "data_retention" );
241+ builder .field (isEffectiveRetention ? "retained_data_streams" : "configured_data_streams" , stats .dataStreamCount ());
242+ if (stats .dataStreamCount () > 0 ) {
243+ builder .field ("minimum_millis" , stats .minMillis );
244+ builder .field ("maximum_millis" , stats .maxMillis );
245+ builder .field ("average_millis" , stats .avgMillis );
246+ }
247+ builder .endObject ();
248+ }
254249 }
255250
256251 public record GlobalRetentionStats (long dataStreamCount , long retention ) implements Writeable {
@@ -263,10 +258,44 @@ public GlobalRetentionStats(StreamInput in) throws IOException {
263258 this (in .readVLong (), in .readVLong ());
264259 }
265260
261+ public static Map <String , GlobalRetentionStats > getGlobalRetentionStats (
262+ DataStreamGlobalRetention globalRetention ,
263+ long dataStreamsWithDefaultRetention ,
264+ long dataStreamsWithMaxRetention
265+ ) {
266+ if (globalRetention == null ) {
267+ return Map .of ();
268+ }
269+ Map <String , GlobalRetentionStats > globalRetentionStats = new HashMap <>();
270+ if (globalRetention .defaultRetention () != null ) {
271+ globalRetentionStats .put (
272+ LifecycleStats .DEFAULT_RETENTION_FIELD_NAME ,
273+ new GlobalRetentionStats (dataStreamsWithDefaultRetention , globalRetention .defaultRetention ())
274+ );
275+ }
276+ if (globalRetention .maxRetention () != null ) {
277+ globalRetentionStats .put (
278+ LifecycleStats .MAX_RETENTION_FIELD_NAME ,
279+ new GlobalRetentionStats (dataStreamsWithMaxRetention , globalRetention .maxRetention ())
280+ );
281+ }
282+ return globalRetentionStats ;
283+ }
284+
266285 @ Override
267286 public void writeTo (StreamOutput out ) throws IOException {
268287 out .writeVLong (dataStreamCount );
269288 out .writeVLong (retention );
270289 }
290+
291+ static void toXContentFragment (XContentBuilder builder , String retentionType , GlobalRetentionStats stats ) throws IOException {
292+ builder .startObject (retentionType );
293+ builder .field ("defined" , stats != null );
294+ if (stats != null ) {
295+ builder .field ("affected_data_streams" , stats .dataStreamCount ());
296+ builder .field ("retention_millis" , stats .retention ());
297+ }
298+ builder .endObject ();
299+ }
271300 }
272301}
0 commit comments