Transformations: Allow negatives in limit (#94746)

* Allow negatives in limit

* Add test
pull/94911/head
Kristina 1 year ago committed by GitHub
parent 0ff7783745
commit c12a662f9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 8
      docs/sources/panels-visualizations/query-transform-data/transform-data/index.md
  2. 46
      packages/grafana-data/src/transformations/transformers/limit.test.ts
  3. 10
      packages/grafana-data/src/transformations/transformers/limit.ts
  4. 9
      public/app/features/transformers/docs/content.ts

@ -1069,6 +1069,14 @@ Here is the result after adding a Limit transformation with a value of '3':
| 2020-07-07 11:34:20 | Humidity | 22 | | 2020-07-07 11:34:20 | Humidity | 22 |
| 2020-07-07 10:32:20 | Humidity | 29 | | 2020-07-07 10:32:20 | Humidity | 29 |
Using a negative number, you can keep values from the end of the set. Here is the result after adding a Limit transformation with a value of '-3':
| Time | Metric | Value |
| ------------------- | ----------- | ----- |
| 2020-07-07 10:31:22 | Temperature | 22 |
| 2020-07-07 09:30:57 | Humidity | 33 |
| 2020-07-07 09:30:05 | Temperature | 19 |
This transformation helps you tailor the visual presentation of your data to focus on the most relevant information. This transformation helps you tailor the visual presentation of your data to focus on the most relevant information.
### Merge series/tables ### Merge series/tables

@ -12,7 +12,7 @@ describe('Limit transformer', () => {
mockTransformationsRegistry([limitTransformer]); mockTransformationsRegistry([limitTransformer]);
}); });
it('should limit the number of items', async () => { it('should limit the number of items by removing from the end if the number is positive', async () => {
const testSeries = toDataFrame({ const testSeries = toDataFrame({
name: 'A', name: 'A',
fields: [ fields: [
@ -56,6 +56,50 @@ describe('Limit transformer', () => {
}); });
}); });
it('should limit the number of items by removing from the front if the limit is negative', async () => {
const testSeries = toDataFrame({
name: 'A',
fields: [
{ name: 'time', type: FieldType.time, values: [3000, 4000, 5000, 6000, 7000, 8000] },
{ name: 'message', type: FieldType.string, values: ['one', 'two', 'two', 'three', 'three', 'three'] },
{ name: 'values', type: FieldType.number, values: [1, 2, 2, 3, 3, 3] },
],
});
const cfg: DataTransformerConfig<LimitTransformerOptions> = {
id: DataTransformerID.limit,
options: {
limitField: -3,
},
};
await expect(transformDataFrame([cfg], [testSeries])).toEmitValuesWith((received) => {
const result = received[0];
const expected: Field[] = [
{
name: 'time',
type: FieldType.time,
values: [6000, 7000, 8000],
config: {},
},
{
name: 'message',
type: FieldType.string,
values: ['three', 'three', 'three'],
config: {},
},
{
name: 'values',
type: FieldType.number,
values: [3, 3, 3],
config: {},
},
];
expect(result[0].fields).toEqual(expected);
});
});
it('should not limit the number of items if limit > number of items', async () => { it('should not limit the number of items if limit > number of items', async () => {
const testSeries = toDataFrame({ const testSeries = toDataFrame({
name: 'A', name: 'A',

@ -34,10 +34,7 @@ export const limitTransformer: DataTransformerInfo<LimitTransformerOptions> = {
limit = options.limitField; limit = options.limitField;
} }
} }
// Prevent negative limit
if (limit < 0) {
limit = 0;
}
return data.map((frame) => { return data.map((frame) => {
if (frame.length > limit) { if (frame.length > limit) {
return { return {
@ -45,10 +42,11 @@ export const limitTransformer: DataTransformerInfo<LimitTransformerOptions> = {
fields: frame.fields.map((f) => { fields: frame.fields.map((f) => {
return { return {
...f, ...f,
values: f.values.slice(0, limit), values:
limit >= 0 ? f.values.slice(0, limit) : f.values.slice(f.values.length + limit, f.values.length),
}; };
}), }),
length: limit, length: Math.abs(limit),
}; };
} }

@ -1080,6 +1080,15 @@ Here is the result after adding a Limit transformation with a value of '3':
| 2020-07-07 11:34:20 | Humidity | 22 | | 2020-07-07 11:34:20 | Humidity | 22 |
| 2020-07-07 10:32:20 | Humidity | 29 | | 2020-07-07 10:32:20 | Humidity | 29 |
Using a negative number, you can keep values from the end of the set. Here is the result after adding a Limit transformation with a value of '-3':
| Time | Metric | Value |
| ------------------- | ----------- | ----- |
| 2020-07-07 10:31:22 | Temperature | 22 |
| 2020-07-07 09:30:57 | Humidity | 33 |
| 2020-07-07 09:30:05 | Temperature | 19 |
This transformation helps you tailor the visual presentation of your data to focus on the most relevant information. This transformation helps you tailor the visual presentation of your data to focus on the most relevant information.
`; `;
}, },

Loading…
Cancel
Save