---
"Test data stream lifecycle usage stats":
  - requires:
      reason: "Global retention telemetry was added in 8.16+"
      test_runner_features: [ capabilities, allowed_warnings ]
      capabilities:
        - method: GET
          path: /_xpack/usage
          capabilities: [ 'global_retention_telemetry' ]

  - do:
      xpack.usage: {}

  - match: { data_lifecycle.available: true }
  - match: { data_lifecycle.enabled: true }
  - match: { data_lifecycle.count: 0 }
  - match: { data_lifecycle.default_rollover_used: true }

  - match: { data_lifecycle.data_retention.configured_data_streams: 0 }
  - is_false: data_lifecycle.data_retention.minimum_millis
  - is_false: data_lifecycle.data_retention.maximum_millis
  - is_false: data_lifecycle.data_retention.average_millis

  - match: { data_lifecycle.effective_retention.retained_data_streams: 0 }
  - is_false: data_lifecycle.effective_retention.minimum_millis
  - is_false: data_lifecycle.effective_retention.maximum_millis
  - is_false: data_lifecycle.effective_retention.average_millis

  - match: { data_lifecycle.global_retention.max.defined: false }
  - match: { data_lifecycle.global_retention.default.defined: false }

  - do:
      allowed_warnings:
        - "index template [my-template-1] has index patterns [foo-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template-1] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template-1
        body:
          index_patterns: [foo-*]
          template:
            mappings:
              properties:
                '@timestamp':
                  type: date
            lifecycle:
              data_retention: 10d
          data_stream: {}
  - is_true: acknowledged

  - do:
      indices.create_data_stream:
        name: foo-foobar
  - is_true: acknowledged

  - do:
      allowed_warnings:
        - "index template [my-template-2] has index patterns [bar-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template-2] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template-2
        body:
          index_patterns: [bar-*]
          template:
            mappings:
              properties:
                '@timestamp':
                  type: date
            lifecycle:
              data_retention: 5d
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: bar-foobar
  - is_true: acknowledged

  - do:
      allowed_warnings:
        - "index template [my-template-3] has index patterns [baz-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template-3] will take precedence during new index creation"
      indices.put_index_template:
        name: my-template-3
        body:
          index_patterns: [ baz-* ]
          template:
            mappings:
              properties:
                '@timestamp':
                  type: date
            lifecycle:
              enabled: false
              data_retention: 1d
          data_stream: { }

  - do:
      indices.create_data_stream:
        name: baz-foobar
  - is_true: acknowledged

  - do:
      xpack.usage: {}

  - match: { data_streams.data_streams: 3 }
  - match: { data_lifecycle.available: true }
  - match: { data_lifecycle.enabled: true }
  - match: { data_lifecycle.count: 2 }
  - match: { data_lifecycle.default_rollover_used: true }
  - match: { data_lifecycle.data_retention.configured_data_streams: 2 }
  - match: { data_lifecycle.data_retention.minimum_millis: 432000000 }
  - match: { data_lifecycle.data_retention.maximum_millis: 864000000 }
  - match: { data_lifecycle.data_retention.average_millis: 648000000 }

  - match: { data_lifecycle.effective_retention.retained_data_streams: 2 }
  - match: { data_lifecycle.effective_retention.minimum_millis: 432000000 }
  - match: { data_lifecycle.data_retention.maximum_millis: 864000000 }
  - match: { data_lifecycle.data_retention.average_millis: 648000000 }

  - do:
      indices.delete_data_stream:
        name: foo-foobar
  - is_true: acknowledged

  - do:
      xpack.usage: {}

  - match: { data_streams.data_streams: 2 }
  - match: { data_lifecycle.available: true }
  - match: { data_lifecycle.enabled: true }
  - match: { data_lifecycle.count: 1 }
  - match: { data_lifecycle.default_rollover_used: true }

  - match: { data_lifecycle.data_retention.configured_data_streams: 1 }
  - match: { data_lifecycle.data_retention.minimum_millis: 432000000 }
  - match: { data_lifecycle.data_retention.maximum_millis: 432000000 }
  - match: { data_lifecycle.data_retention.average_millis: 432000000 }

  - match: { data_lifecycle.effective_retention.retained_data_streams: 1 }
  - match: { data_lifecycle.effective_retention.minimum_millis: 432000000 }
  - match: { data_lifecycle.effective_retention.maximum_millis: 432000000 }
  - match: { data_lifecycle.effective_retention.average_millis: 432000000 }
