---
setup:
  - requires:
      capabilities:
        - method: POST
          path: /_query
          parameters: [ method, path, parameters, capabilities ]
          capabilities: [ function_stats ]
      reason: "META command removed which changes the count of the data returned"
      test_runner_features: [capabilities]

  - do:
      indices.create:
        index:  test
        body:
          settings:
            number_of_shards: 1
          mappings:
            properties:
              data:
                type: long
              count:
                type: integer
              message:
                type: keyword

---
"Basic ESQL usage output (telemetry) snapshot version":
  - requires:
      test_runner_features: [capabilities]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          # A snapshot function was removed in match_function_options, it can't work on mixed cluster tests otherwise.
          capabilities:
            - snapshot_test_for_telemetry
            - fn_byte_length
            - match_function_options
            - first_over_time
            - sum_over_time
            - count_over_time
            - distinct_over_time
      reason: "Test that should only be executed on snapshot versions"

  - do: {xpack.usage: {}}
  - match: { esql.available: true }
  - match: { esql.enabled: true }
  - length: { esql.features: 27 }
  - set: {esql.features.dissect: dissect_counter}
  - set: {esql.features.drop: drop_counter}
  - set: {esql.features.eval: eval_counter}
  - set: {esql.features.enrich: enrich_counter}
  - set: {esql.features.from: from_counter}
  - set: {esql.features.grok: grok_counter}
  - set: {esql.features.keep: keep_counter}
  - set: {esql.features.limit: limit_counter}
  - set: {esql.features.mv_expand: mv_expand_counter}
  - set: {esql.features.rename: rename_counter}
  - set: {esql.features.row: row_counter}
  - set: {esql.features.show: show_counter}
  - set: {esql.features.sort: sort_counter}
  - set: {esql.features.stats: stats_counter}
  - set: {esql.features.where: where_counter}
  - set: {esql.features.lookup_join: lookup_join_counter}
  - set: {esql.features.lookup: lookup_counter}
  - set: {esql.features.change_point: change_point_counter}
  - set: {esql.features.inlinestats: inlinestats_counter}
  - set: {esql.features.rerank: rerank_counter}
  - set: {esql.features.dedup: dedup_counter}
  - set: {esql.features.insist: insist_counter}
  - set: {esql.features.fork: fork_counter}
  - set: {esql.features.rrf: rrf_counter}
  - set: {esql.features.completion: completion_counter}
  - set: {esql.features.sample: sample_counter}
  - length: { esql.queries: 3 }
  - set: {esql.queries.rest.total: rest_total_counter}
  - set: {esql.queries.rest.failed: rest_failed_counter}
  - set: {esql.queries.kibana.total: kibana_total_counter}
  - set: {esql.queries.kibana.failed: kibana_failed_counter}
  - set: {esql.queries._all.total: all_total_counter}
  - set: {esql.queries._all.failed: all_failed_counter}
  - set: {esql.functions.max: functions_max}
  - set: {esql.functions.min: functions_min}
  - set: {esql.functions.cos: functions_cos}
  - set: {esql.functions.to_long: functions_to_long}
  - set: {esql.functions.coalesce: functions_coalesce}
  - set: {esql.functions.categorize: functions_categorize}

  - do:
      esql.query:
        body:
          query: 'from test
            | where data > 2 and to_long(data) > 2
            | sort count desc
            | limit 5
            | stats m = max(data) by categorize(message)'

  - do: {xpack.usage: {}}
  - match: { esql.available: true }
  - match: { esql.enabled: true }
  - match: {esql.features.dissect: $dissect_counter}
  - match: {esql.features.eval: $eval_counter}
  - match: {esql.features.grok: $grok_counter}
  - gt: {esql.features.limit: $limit_counter}
  - gt: {esql.features.sort: $sort_counter}
  - gt: {esql.features.stats: $stats_counter}
  - gt: {esql.features.where: $where_counter}
  - match: {esql.features.lookup_join: $lookup_join_counter}
  - match: {esql.features.lookup: $lookup_counter}
  - match: {esql.features.change_point: $change_point_counter}
  - match: {esql.features.inlinestats: $inlinestats_counter}
  - match: {esql.features.rerank: $rerank_counter}
  - match: {esql.features.dedup: $dedup_counter}
  - match: {esql.features.insist: $insist_counter}
  - match: {esql.features.fork: $fork_counter}
  - match: {esql.features.rrf: $rrf_counter}
  - match: {esql.features.completion: $completion_counter}
  - match: {esql.features.sample: $sample_counter}
  - gt: {esql.queries.rest.total: $rest_total_counter}
  - match: {esql.queries.rest.failed: $rest_failed_counter}
  - match: {esql.queries.kibana.total: $kibana_total_counter}
  - match: {esql.queries.kibana.failed: $kibana_failed_counter}
  - gt: {esql.queries._all.total: $all_total_counter}
  - match: {esql.queries._all.failed: $all_failed_counter}
  - gt: {esql.functions.max: $functions_max}
  - match: {esql.functions.min: $functions_min}
  - match: {esql.functions.cos: $functions_cos}
  - gt: {esql.functions.to_long: $functions_to_long}
  - match: {esql.functions.coalesce: $functions_coalesce}
  - gt: {esql.functions.categorize: $functions_categorize}
  # Testing for the entire function set isn't feasible, so we just check that we return the correct count as an approximation.
  - length: {esql.functions: 156} # check the "sister" test below for a likely update to the same esql.functions length check

---
"Basic ESQL usage output (telemetry) non-snapshot version":
  - requires:
      test_runner_features: [capabilities]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [ non_snapshot_test_for_telemetry, fn_byte_length ]
      reason: "Test that should only be executed on release versions"

  - do: {xpack.usage: {}}
  - match: { esql.available: true }
  - match: { esql.enabled: true }
  - length: { esql.features: 26 }
  - set: {esql.features.dissect: dissect_counter}
  - set: {esql.features.drop: drop_counter}
  - set: {esql.features.eval: eval_counter}
  - set: {esql.features.enrich: enrich_counter}
  - set: {esql.features.from: from_counter}
  - set: {esql.features.grok: grok_counter}
  - set: {esql.features.keep: keep_counter}
  - set: {esql.features.limit: limit_counter}
  - set: {esql.features.mv_expand: mv_expand_counter}
  - set: {esql.features.rename: rename_counter}
  - set: {esql.features.row: row_counter}
  - set: {esql.features.show: show_counter}
  - set: {esql.features.sort: sort_counter}
  - set: {esql.features.stats: stats_counter}
  - set: {esql.features.where: where_counter}
  - set: {esql.features.lookup_join: lookup_join_counter}
  - set: {esql.features.lookup: lookup_counter}
  - set: {esql.features.change_point: change_point_counter}
  - set: {esql.features.inlinestats: inlinestats_counter}
  - set: {esql.features.rerank: rerank_counter}
  - set: {esql.features.dedup: dedup_counter}
  - set: {esql.features.insist: insist_counter}
  - set: {esql.features.fork: fork_counter}
  - set: {esql.features.rrf: rrf_counter}
  - set: {esql.features.completion: completion_counter}
  - set: {esql.features.sample: sample_counter}
  - length: { esql.queries: 3 }
  - set: {esql.queries.rest.total: rest_total_counter}
  - set: {esql.queries.rest.failed: rest_failed_counter}
  - set: {esql.queries.kibana.total: kibana_total_counter}
  - set: {esql.queries.kibana.failed: kibana_failed_counter}
  - set: {esql.queries._all.total: all_total_counter}
  - set: {esql.queries._all.failed: all_failed_counter}
  - set: {esql.functions.max: functions_max}
  - set: {esql.functions.min: functions_min}
  - set: {esql.functions.cos: functions_cos}
  - set: {esql.functions.to_long: functions_to_long}
  - set: {esql.functions.coalesce: functions_coalesce}
  - set: {esql.functions.categorize: functions_categorize}

  - do:
      esql.query:
        body:
          query: 'from test
            | where data > 2 and to_long(data) > 2
            | sort count desc
            | limit 5
            | stats m = max(data) by categorize(message)'

  - do: {xpack.usage: {}}
  - match: { esql.available: true }
  - match: { esql.enabled: true }
  - match: {esql.features.dissect: $dissect_counter}
  - match: {esql.features.eval: $eval_counter}
  - match: {esql.features.grok: $grok_counter}
  - gt: {esql.features.limit: $limit_counter}
  - gt: {esql.features.sort: $sort_counter}
  - gt: {esql.features.stats: $stats_counter}
  - gt: {esql.features.where: $where_counter}
  - match: {esql.features.lookup_join: $lookup_join_counter}
  - match: {esql.features.lookup: $lookup_counter}
  - match: {esql.features.change_point: $change_point_counter}
  - match: {esql.features.inlinestats: $inlinestats_counter}
  - match: {esql.features.rerank: $rerank_counter}
  - match: {esql.features.dedup: $dedup_counter}
  - match: {esql.features.insist: $insist_counter}
  - match: {esql.features.fork: $fork_counter}
  - match: {esql.features.rrf: $rrf_counter}
  - match: {esql.features.completion: $completion_counter}
  - gt: {esql.queries.rest.total: $rest_total_counter}
  - match: {esql.queries.rest.failed: $rest_failed_counter}
  - match: {esql.queries.kibana.total: $kibana_total_counter}
  - match: {esql.queries.kibana.failed: $kibana_failed_counter}
  - gt: {esql.queries._all.total: $all_total_counter}
  - match: {esql.queries._all.failed: $all_failed_counter}
  - gt: {esql.functions.max: $functions_max}
  - match: {esql.functions.min: $functions_min}
  - match: {esql.functions.cos: $functions_cos}
  - gt: {esql.functions.to_long: $functions_to_long}
  - match: {esql.functions.coalesce: $functions_coalesce}
  - gt: {esql.functions.categorize: $functions_categorize}
  - length: {esql.functions: 146} # check the "sister" test above for a likely update to the same esql.functions length check

---
took:
  - requires:
      test_runner_features: [capabilities]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [usage_contains_took]
      reason: "Test that should only be executed on snapshot versions"

  - do: {xpack.usage: {}}
  - exists: esql.took.lt_10ms
  - exists: esql.took.lt_1s
  - exists: esql.took.lt_10s
  - exists: esql.took.lt_1m
  - exists: esql.took.lt_10m
  - exists: esql.took.lt_1h
  - exists: esql.took.lt_10h
  - exists: esql.took.lt_1d
  - exists: esql.took.gt_1d
