Transformations: Allow negatives in limit (#94746)

* Allow negatives in limit

* Add test
This commit is contained in:
Kristina 2024-10-17 13:13:00 -05:00 committed by GitHub
parent 0ff7783745
commit c12a662f9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 66 additions and 7 deletions

View File

@ -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 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.
### Merge series/tables

View File

@ -12,7 +12,7 @@ describe('Limit transformer', () => {
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({
name: 'A',
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 () => {
const testSeries = toDataFrame({
name: 'A',

View File

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

View File

@ -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 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.
`;
},