Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 68 additions & 6 deletions src/datasets/utils/history.util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,71 @@ describe("History Utility Functions", () => {
retrieveIntegrityCheck: false,
},
},
scientificMetadata: {
currentValue: {
runNumber: 484,
scanAxis0Name: ["phi"],
scanName: "run0484_diamond_D2FG",
scan_parameters: {
expected_total_number_of_steps: 7,
name: ["phi2"],
scan_name: "run0484_diamond_D2FG",
},
scan_readbacks: [
[73.369784942746],
[73.36755732624964],
[73.3680595489329],
[73.36857790677979],
[73.3690102872549],
[73.36911958727225],
[73.37012839122846],
],
scan_readbacks_raw: [[], [], [], [], [], [], []],
scan_step_info: [
{
step_number: 1,
},
{
step_number: 2,
},
],
},
previousValue: {
runNumber: 0,
scanAxis0Name: ["phi"],
scanName: "run0484_diamond_D2FG",
scan_parameters: {
expected_total_number_of_steps: 7,
name: ["phi"],
scan_name: "run0484_diamond_D2FG",
},
scan_readbacks: [
[73.369784942746],
[73.36755732624964],
[73.3680595489329],
[73.36857790677979],
[73.3690102872549],
[73.36911958727225],
[73.37012839122846],
],
scan_readbacks_raw: [[], [], [], [], [], [], []],
scan_step_info: [
{
step_number: 1,
},
{
step_number: 2,
},
],
},
},
_id: "",
};
const documentId = "pid123";
const genericHistory = convertObsoleteHistoryToGenericHistory(
obsoleteHistory,
documentId,
);

expect(genericHistory).toEqual({
subsystem: "Dataset",
documentId: "pid123",
Expand All @@ -53,17 +110,22 @@ describe("History Utility Functions", () => {
timestamp: new Date("2023-10-01T12:00:00Z"),
before: {
isPublished: false,
datasetlifecycle: {
publishedOn: undefined,
archivable: false,
datasetlifecycle: { archivable: false, publishedOn: undefined },
scientificMetadata: {
runNumber: 0,
scan_parameters: { name: ["phi"] },
},
},
after: {
isPublished: true,
datasetlifecycle: {
publishedOn: new Date("2023-10-01T12:00:00Z"),
archivable: true,
publishedOn: new Date("2023-10-01T12:00:00.000Z"),
},
scientificMetadata: {
runNumber: 484,
scan_parameters: { name: ["phi2"] },
},
isPublished: true,
},
});
});
Expand Down
19 changes: 8 additions & 11 deletions src/datasets/utils/history.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DatasetClass,
DatasetDocument,
} from "src/datasets/schemas/dataset.schema";
import { computeDeltaWithOriginals } from "src/common/utils/delta.util";

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

Expand Down Expand Up @@ -35,23 +36,19 @@ export function convertObsoleteHistoryToGenericHistory(
previousValue: unknown;
currentValue: unknown;
};
if (key === "datasetlifecycle") {
if (typeof fieldChange.previousValue == "object") {
const currentValue = fieldChange.currentValue as Record<string, unknown>;
const previousValue = fieldChange.previousValue as Record<
string,
unknown
>;
// only retain the intersection of keys in currentValue and previousValue and whose value has changed. drop all others
const prunedPreviousValue: Record<string, unknown> = {};
const prunedCurrentValue: Record<string, unknown> = {};
for (const subKey of Object.keys(currentValue)) {
if (currentValue[subKey] !== previousValue[subKey]) {
prunedPreviousValue[subKey] = previousValue[subKey];
prunedCurrentValue[subKey] = currentValue[subKey];
}
}
fieldChange.previousValue = prunedPreviousValue;
fieldChange.currentValue = prunedCurrentValue;
const { delta, originals } = computeDeltaWithOriginals(previousValue, {
...previousValue,
...currentValue,
});
Comment on lines +46 to +49
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rationale behind this logic is:
in the old backend's history (code), previousValue will contain datsetInstance[field], and currentValue will contain the fields to patch. computeDeltaWithOriginals expects both "complete" (oldObject, newObject). so we construct the newObject by overriding previousValue i.e. datsetInstance[field] with ...currentValue

fieldChange.previousValue = originals;
fieldChange.currentValue = delta;
}
result.before![key] = fieldChange.previousValue;
result.after![key] = fieldChange.currentValue;
Expand Down