Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce new compareItinerary method for excluding attributes #50

Merged
merged 2 commits into from
Feb 15, 2025
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
1 change: 1 addition & 0 deletions visual-debugger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"axios": "^1.7.9",
"bits-ui": "^0.22.0",
"clsx": "^2.1.1",
"deep-equal": "^2.2.3",
"lucide-svelte": "^0.469.0",
"svelte": "^5.0.0",
"svelte-check": "^4.0.0",
Expand Down
69 changes: 69 additions & 0 deletions visual-debugger/src/data-processing/compareObjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {Itinerary, Leg} from "@data/type-declarations/planTypes.ts";
// @ts-ignore
import deepEqual from "deep-equal";

/**
* compares the parameters and returns true when all of their attributes are equal in value
* @param currentItinerary
* @param currentDefaultItinerary
*/
export function compareItineraries(currentItinerary: Itinerary, currentDefaultItinerary: Itinerary): boolean {
// First, check if the number of keys are the same
const keys1 = Object.keys(currentItinerary) as (keyof Itinerary)[];


// Compare the values of each key in both objects
return keys1.every((key) => {

// recursively check the legs attribute and evaluate it for equality
if (key == "legs") {
let result = true;
for (let i = 0; i < currentItinerary.legs.length; i++) {
if (compareLegs(currentItinerary.legs[i], currentDefaultItinerary.legs[i]).length != 0) {
result = false;
}
}
return result;
}

// evaluate all attributes except legs for equality
return deepEqual(currentItinerary[key], currentDefaultItinerary[key]);
});
}

/**
* Compares the parameters and puts the mismatched attributes in a list
* IMPORTANT: excludes the source and tripId parameters because they are not deterministic
* @returns an array with the mismatched attributes of the function parameters
* @param currentLeg
* @param currentDefaultLeg
*/
function compareLegs(currentLeg: Leg, currentDefaultLeg: Leg): string[] {

let mismatchedAttributes: string[] = []

//get list of attributes
const keys1 = Object.keys(currentLeg) as (keyof Leg)[];

// iterates over all attributes
keys1.every((key) => {

// exclude "source" and "tripId" parameters and evaluate the rest
switch (key) {
case "source":
return true
case "tripId":
return true
default: {
// add the attribute name to the list if the parameters differ in value
if (!deepEqual(currentLeg[key], currentDefaultLeg[key])) {
mismatchedAttributes.push(key);
return true
}
return true
}
}
});

return mismatchedAttributes
}
14 changes: 7 additions & 7 deletions visual-debugger/src/data-processing/comparePlans.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Plan} from "./type-declarations/planTypes.ts";
import {type Plan} from "./type-declarations/planTypes.ts";
import {
currentDefaultPlanStore,
currentPlanStore,
Expand All @@ -7,6 +7,7 @@ import {
} from "sveltestore";
import {cssClasses} from "./styling/cssClasses.ts";
import {resetCssClassesForPlanEntries} from "./planParsing.ts";
import {compareItineraries} from "@data/compareObjects.ts";

/**
* Compares the computed results of the queries with the uploaded default plan and sets colors of the matches/mismatches
Expand Down Expand Up @@ -54,16 +55,15 @@ export function comparePlans() {
let currentDefaultItinerary = currentDefaultPlan.itineraries[itineraryIndex];

// compare strings of itineraries and set colors(CSS-Classes) accordingly
if (JSON.stringify(currentItinerary) == JSON.stringify(currentDefaultItinerary)) {
if (compareItineraries(currentItinerary, currentDefaultItinerary)) {
// itineraries are equal, mark them as such
plans[planIndex].itineraries[itineraryIndex].cssClass = cssClasses.planEntryValid
defaultPlans[planIndex].itineraries[itineraryIndex].cssClass = cssClasses.planEntryValid

currentItinerary.cssClass = cssClasses.planEntryValid
currentDefaultItinerary.cssClass = cssClasses.planEntryValid

} else {
// itineraries are not equal
plans[planIndex].itineraries[itineraryIndex].cssClass = cssClasses.planEntryInvalid
defaultPlans[planIndex].itineraries[itineraryIndex].cssClass = cssClasses.planEntryInvalid
currentItinerary.cssClass = cssClasses.planEntryInvalid
currentDefaultItinerary.cssClass = cssClasses.planEntryInvalid
}

}
Expand Down
1 change: 1 addition & 0 deletions visual-debugger/src/data-processing/planParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export async function computePlan() {
resetCssClassesForPlanEntries(plan)
}

// index all itineraries as they are parsed
for (let plan of plans) {
let itineraryIndex =0
for (let itinerary of plan.itineraries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export class Leg {
legGeometry: EncodedPolyline = new EncodedPolyline();
headsign: string = "";
routeShortName: string = "";
intermediateStops: Array<Place> = new Array<Place>();
source: string = "";
tripId: string = "";
}

/**
Expand Down
Loading