Skip to content

Commit e1c024a

Browse files
authored
fix: convert all object fields from obsolete to generic history (#2288)
Convert all object fields from obsolete to generic history
1 parent d57621b commit e1c024a

File tree

2 files changed

+76
-17
lines changed

2 files changed

+76
-17
lines changed

src/datasets/utils/history.util.spec.ts

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,71 @@ describe("History Utility Functions", () => {
3737
retrieveIntegrityCheck: false,
3838
},
3939
},
40+
scientificMetadata: {
41+
currentValue: {
42+
runNumber: 484,
43+
scanAxis0Name: ["phi"],
44+
scanName: "run0484_diamond_D2FG",
45+
scan_parameters: {
46+
expected_total_number_of_steps: 7,
47+
name: ["phi2"],
48+
scan_name: "run0484_diamond_D2FG",
49+
},
50+
scan_readbacks: [
51+
[73.369784942746],
52+
[73.36755732624964],
53+
[73.3680595489329],
54+
[73.36857790677979],
55+
[73.3690102872549],
56+
[73.36911958727225],
57+
[73.37012839122846],
58+
],
59+
scan_readbacks_raw: [[], [], [], [], [], [], []],
60+
scan_step_info: [
61+
{
62+
step_number: 1,
63+
},
64+
{
65+
step_number: 2,
66+
},
67+
],
68+
},
69+
previousValue: {
70+
runNumber: 0,
71+
scanAxis0Name: ["phi"],
72+
scanName: "run0484_diamond_D2FG",
73+
scan_parameters: {
74+
expected_total_number_of_steps: 7,
75+
name: ["phi"],
76+
scan_name: "run0484_diamond_D2FG",
77+
},
78+
scan_readbacks: [
79+
[73.369784942746],
80+
[73.36755732624964],
81+
[73.3680595489329],
82+
[73.36857790677979],
83+
[73.3690102872549],
84+
[73.36911958727225],
85+
[73.37012839122846],
86+
],
87+
scan_readbacks_raw: [[], [], [], [], [], [], []],
88+
scan_step_info: [
89+
{
90+
step_number: 1,
91+
},
92+
{
93+
step_number: 2,
94+
},
95+
],
96+
},
97+
},
4098
_id: "",
4199
};
42100
const documentId = "pid123";
43101
const genericHistory = convertObsoleteHistoryToGenericHistory(
44102
obsoleteHistory,
45103
documentId,
46104
);
47-
48105
expect(genericHistory).toEqual({
49106
subsystem: "Dataset",
50107
documentId: "pid123",
@@ -53,17 +110,22 @@ describe("History Utility Functions", () => {
53110
timestamp: new Date("2023-10-01T12:00:00Z"),
54111
before: {
55112
isPublished: false,
56-
datasetlifecycle: {
57-
publishedOn: undefined,
58-
archivable: false,
113+
datasetlifecycle: { archivable: false, publishedOn: undefined },
114+
scientificMetadata: {
115+
runNumber: 0,
116+
scan_parameters: { name: ["phi"] },
59117
},
60118
},
61119
after: {
120+
isPublished: true,
62121
datasetlifecycle: {
63-
publishedOn: new Date("2023-10-01T12:00:00Z"),
64122
archivable: true,
123+
publishedOn: new Date("2023-10-01T12:00:00.000Z"),
124+
},
125+
scientificMetadata: {
126+
runNumber: 484,
127+
scan_parameters: { name: ["phi2"] },
65128
},
66-
isPublished: true,
67129
},
68130
});
69131
});

src/datasets/utils/history.util.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
DatasetClass,
55
DatasetDocument,
66
} from "src/datasets/schemas/dataset.schema";
7+
import { computeDeltaWithOriginals } from "src/common/utils/delta.util";
78

89
const IGNORE_FIELDS = ["updatedAt", "updatedBy", "_id"];
910

@@ -35,23 +36,19 @@ export function convertObsoleteHistoryToGenericHistory(
3536
previousValue: unknown;
3637
currentValue: unknown;
3738
};
38-
if (key === "datasetlifecycle") {
39+
if (typeof fieldChange.previousValue == "object") {
3940
const currentValue = fieldChange.currentValue as Record<string, unknown>;
4041
const previousValue = fieldChange.previousValue as Record<
4142
string,
4243
unknown
4344
>;
4445
// only retain the intersection of keys in currentValue and previousValue and whose value has changed. drop all others
45-
const prunedPreviousValue: Record<string, unknown> = {};
46-
const prunedCurrentValue: Record<string, unknown> = {};
47-
for (const subKey of Object.keys(currentValue)) {
48-
if (currentValue[subKey] !== previousValue[subKey]) {
49-
prunedPreviousValue[subKey] = previousValue[subKey];
50-
prunedCurrentValue[subKey] = currentValue[subKey];
51-
}
52-
}
53-
fieldChange.previousValue = prunedPreviousValue;
54-
fieldChange.currentValue = prunedCurrentValue;
46+
const { delta, originals } = computeDeltaWithOriginals(previousValue, {
47+
...previousValue,
48+
...currentValue,
49+
});
50+
fieldChange.previousValue = originals;
51+
fieldChange.currentValue = delta;
5552
}
5653
result.before![key] = fieldChange.previousValue;
5754
result.after![key] = fieldChange.currentValue;

0 commit comments

Comments
 (0)