DashboardDS: Clone series to not mutate original frames (#104518)

* DashboardDS: Clone series to not mutate original frames

* rename test
This commit is contained in:
Victor Marin
2025-05-05 20:18:12 +03:00
committed by GitHub
parent 72932e10b2
commit 2f397516e6
2 changed files with 34 additions and 1 deletions
@@ -102,6 +102,26 @@ describe('DashboardDatasource', () => {
expect(first).not.toHaveBeenCalled();
});
it('Should not mutate field state in dataframe', () => {
const { observable } = setup({ refId: 'A', panelId: 1, withTransforms: true });
let rsp: DataQueryResponse | undefined;
const test = observable.subscribe({ next: (data) => (rsp = data) });
// modifying series in dashboard DS should not affect the original dataframe
rsp!.data[0].fields[0].state = {
calcs: { sum: 3 },
};
test.unsubscribe();
observable.subscribe({ next: (data) => (rsp = data) });
// on further emissions the result should be the unmodified original dataframe
expect(rsp!.data[0].fields[0].state).toEqual({});
});
});
function setup(query: DashboardQuery, requestId?: string) {
@@ -11,6 +11,7 @@ import {
PanelData,
DataFrame,
LoadingState,
Field,
} from '@grafana/data';
import { SceneDataProvider, SceneDataTransformer, SceneObject } from '@grafana/scenes';
import {
@@ -104,7 +105,19 @@ export class DashboardDatasource extends DataSourceApi<DashboardQuery> {
},
}));
} else {
return [...data.series, ...annotations];
const series = data.series.map((s) => {
return {
...s,
fields: s.fields.map((field: Field) => ({
...field,
state: {
...field.state,
},
})),
};
});
return [...series, ...annotations];
}
}