Skip to content

Commit

Permalink
[Lens] Fix time shift bug (#102528) (#102752)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Reuter <johannes.reuter@elastic.co>
  • Loading branch information
kibanamachine and flash1293 committed Jun 21, 2021
1 parent 04ae473 commit db83874
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [AggConfigs](./kibana-plugin-plugins-data-public.aggconfigs.md) &gt; [getResolvedTimeRange](./kibana-plugin-plugins-data-public.aggconfigs.getresolvedtimerange.md)

## AggConfigs.getResolvedTimeRange() method

Returns the current time range as moment instance (date math will get resolved using the current "now" value or system time if not set)

<b>Signature:</b>

```typescript
getResolvedTimeRange(): import("../..").TimeRangeBounds | undefined;
```
<b>Returns:</b>

`import("../..").TimeRangeBounds | undefined`

Current time range as resolved date.

Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export declare class AggConfigs
| [getAll()](./kibana-plugin-plugins-data-public.aggconfigs.getall.md) | | |
| [getRequestAggById(id)](./kibana-plugin-plugins-data-public.aggconfigs.getrequestaggbyid.md) | | |
| [getRequestAggs()](./kibana-plugin-plugins-data-public.aggconfigs.getrequestaggs.md) | | |
| [getResolvedTimeRange()](./kibana-plugin-plugins-data-public.aggconfigs.getresolvedtimerange.md) | | Returns the current time range as moment instance (date math will get resolved using the current "now" value or system time if not set) |
| [getResponseAggById(id)](./kibana-plugin-plugins-data-public.aggconfigs.getresponseaggbyid.md) | | Find a response agg by it's id. This may be an agg in the aggConfigs, or one created specifically for a response value |
| [getResponseAggs()](./kibana-plugin-plugins-data-public.aggconfigs.getresponseaggs.md) | | Gets the AggConfigs (and possibly ResponseAggConfigs) that represent the values that will be produced when all aggs are run.<!-- -->With multi-value metric aggs it is possible for a single agg request to result in multiple agg values, which is why the length of a vis' responseValuesAggs may be different than the vis' aggs {<!-- -->array\[AggConfig\]<!-- -->} |
| [getSearchSourceTimeFilter(forceNow)](./kibana-plugin-plugins-data-public.aggconfigs.getsearchsourcetimefilter.md) | | |
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/data/common/search/aggs/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ export class AggConfig {
} else if (!this.aggConfigs.timeRange) {
return;
}
return moment.duration(
moment(this.aggConfigs.timeRange.to).diff(this.aggConfigs.timeRange.from)
);
const resolvedBounds = this.aggConfigs.getResolvedTimeRange()!;
return moment.duration(moment(resolvedBounds.max).diff(resolvedBounds.min));
}
return parsedTimeShift;
}
Expand Down
15 changes: 14 additions & 1 deletion src/plugins/data/common/search/aggs/agg_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { IAggType } from './agg_type';
import { AggTypesRegistryStart } from './agg_types_registry';
import { AggGroupNames } from './agg_groups';
import { IndexPattern } from '../../index_patterns/index_patterns/index_pattern';
import { TimeRange, getTime, isRangeFilter } from '../../../common';
import { TimeRange, getTime, isRangeFilter, calculateBounds } from '../../../common';
import { IBucketAggConfig } from './buckets';
import { insertTimeShiftSplit, mergeTimeShifts } from './utils/time_splits';

Expand Down Expand Up @@ -127,6 +127,19 @@ export class AggConfigs {
this.aggs.forEach(updateAggTimeRange);
}

/**
* Returns the current time range as moment instance (date math will get resolved using the current "now" value or system time if not set)
* @returns Current time range as resolved date.
*/
getResolvedTimeRange() {
return (
this.timeRange &&
calculateBounds(this.timeRange, {
forceNow: this.forceNow,
})
);
}

// clone method will reuse existing AggConfig in the list (will not create new instances)
clone({ enabledOnly = true } = {}) {
const filterAggs = (agg: AggConfig) => {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export class AggConfigs {
getRequestAggById(id: string): AggConfig | undefined;
// (undocumented)
getRequestAggs(): AggConfig[];
getResolvedTimeRange(): import("../..").TimeRangeBounds | undefined;
getResponseAggById(id: string): AggConfig | undefined;
getResponseAggs(): AggConfig[];
// (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ export default function ({
expect(getCell(result, 0, 2)).to.be(4618);
});

it('shifts multiple metrics with relative time range and previous', async () => {
const expression = `
kibana_context timeRange={timerange from='${timeRange.from}' to='now'}
| esaggs index={indexPatternLoad id='logstash-*'}
aggs={aggCount id="1" enabled=true schema="metric"}
aggs={aggCount id="2" enabled=true schema="metric" timeShift="previous"}
`;
const result = await expectExpression(
'esaggs_shift_multi_metric_previous',
expression
).getResponse();
expect(getCell(result, 0, 0)).to.be(9247);
expect(getCell(result, 0, 1)).to.be(4763);
});

it('shifts single percentile', async () => {
const expression = `
kibana_context timeRange={timerange from='${timeRange.from}' to='${timeRange.to}'}
Expand Down Expand Up @@ -137,7 +152,7 @@ export default function ({
customMetric={aggAvg id="3"
field="bytes"
enabled=true
schema="metric"
schema="metric"
}
enabled=true
schema="metric"
Expand All @@ -154,7 +169,7 @@ export default function ({
customMetric={aggAvg id="5"
field="bytes"
enabled=true
schema="metric"
schema="metric"
}
enabled=true
schema="metric"
Expand Down

0 comments on commit db83874

Please sign in to comment.