kibana/packages/kbn-io-ts-utils/index.ts
Carlos Crespo 4a3af9c97b
[Infrastructure UI] Add parameter Snapshot API to not query using histogram (#146573)
## Summary

closes #146517 

This PR expands the current `includeTimeseries` parameter behaviour, by
not using the `date_histogram` in the `composite` aggregation when it is
set to `false`.

This way, not only the API won't return a `timeseries` object, but the
query will calculate the aggregations on ES side as opposed to when
`includeTimeseries` is true, that does an extra `avg` and `max`
calculation on TS side using the histogram buckets.

The main motivation behind this change is to run the snapshot query
without the need of returning buckets, to avoid the max bucket error.

### How to test
- Enable the system module on metricbeat
- Start a local Kibana 
- Start metricbeat
- Explore `Infrastructure > Metrics`, `Infrastructure > Inventory` and
`Infrastructure > Hosts` and play with the filters

#### cURL

##### Run a query with Composite aggregation
###### With timeseries
```bash
curl -XPOST -f 'http://0.0.0.0:5601/ftw/api/metrics/snapshot'  \
  -H 'kbn-xsrf:localhost' \
  -H 'Content-Type: application/json'  \
  --data-raw '{"filterQuery":"{\"bool\":{\"must\":[],\"filter\":[],\"should\":[],\"must_not\":[]}}","metrics":[{"type":"memory"}],"groupBy":[],"nodeType":"host","sourceId":"default","accountId":"","region":"","timerange":{"from":1670859470896,"to":1670861030896,"interval":"1m","ignoreLookback":true},"includeTimeseries":true}' \
  -u elastic:changeme
```

###### Without timeseries
```bash
curl -XPOST -f 'http://0.0.0.0:5601/ftw/api/metrics/snapshot'  \
  -H 'kbn-xsrf:localhost' \
  -H 'Content-Type: application/json'  \
  --data-raw '{"filterQuery":"{\"bool\":{\"must\":[],\"filter\":[],\"should\":[],\"must_not\":[]}}","metrics":[{"type":"memory"}],"groupBy":[],"nodeType":"host","sourceId":"default","accountId":"","region":"","timerange":{"from":1670859470896,"to":1670861030896,"interval":"1m","ignoreLookback":true},"includeTimeseries":false}' \
  -u elastic:changeme
```

##### Run a query with just Date Histogram
###### With timeseries
```bash
curl -XPOST -f 'http://0.0.0.0:5601/ftw/api/metrics/snapshot'  \
  -H 'kbn-xsrf:localhost' \
  -H 'Content-Type: application/json'  \
  --data-raw '{"filterQuery":"{\"bool\":{\"must\":[],\"filter\":[],\"should\":[],\"must_not\":[]}}","metrics":[{"type":"memory"}],"groupBy":null,"nodeType":"host","sourceId":"default","accountId":"","region":"","timerange":{"from":1670859470896,"to":1670861030896,"interval":"1m","ignoreLookback":true},"includeTimeseries":true}' \
  -u elastic:changeme
```
##### Without timeseries
```bash
curl -XPOST -f 'http://0.0.0.0:5601/ftw/api/metrics/snapshot'  \
  -H 'kbn-xsrf:localhost' \
  -H 'Content-Type: application/json'  \
  --data-raw '{"filterQuery":"{\"bool\":{\"must\":[],\"filter\":[],\"should\":[],\"must_not\":[]}}","metrics":[{"type":"memory"}],"groupBy":null,"nodeType":"host","sourceId":"default","accountId":"","region":"","timerange":{"from":1670859470896,"to":1670861030896,"interval":"1m","ignoreLookback":true},"includeTimeseries":false}' \
  -u elastic:changeme
```

### For maintainers
The `logRate` metric type <b>doesn't</b> work without being in a date
histogram, because it uses [cumulative
sum](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-cumulative-sum-aggregation.html).
Therefore `includeTimeseries` false in a `composite` aggregation will
return a 400 error. I haven't found anywhere that would make a request
like below.

#### 400 error

```bash
curl -XPOST -f 'http://0.0.0.0:5601/ftw/api/metrics/snapshot'  \
  -H 'kbn-xsrf:localhost' \
  -H 'Content-Type: application/json'  \
  --data-raw '{"filterQuery":"{\"bool\":{\"must\":[],\"filter\":[],\"should\":[],\"must_not\":[]}}","metrics":[{"type":"logRate"}],"groupBy":[],"nodeType":"host","sourceId":"default","accountId":"","region":"","timerange":{"from":1670859470896,"to":1670861030896,"interval":"1m","ignoreLookback":true},"includeTimeseries":false}' \
  -u elastic:changeme
```

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2022-12-16 14:34:29 +01:00

22 lines
1.1 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
export type { IndexPatternType } from './src/index_pattern_rt';
export type { NonEmptyStringBrand } from './src/non_empty_string_rt';
export { deepExactRt } from './src/deep_exact_rt';
export { indexPatternRt } from './src/index_pattern_rt';
export { jsonRt } from './src/json_rt';
export { mergeRt } from './src/merge_rt';
export { strictKeysRt } from './src/strict_keys_rt';
export { isoToEpochRt } from './src/iso_to_epoch_rt';
export { toNumberRt } from './src/to_number_rt';
export { toBooleanRt } from './src/to_boolean_rt';
export { toJsonSchema } from './src/to_json_schema';
export { nonEmptyStringRt } from './src/non_empty_string_rt';
export { createLiteralValueFromUndefinedRT } from './src/literal_value_from_undefined_rt';