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

Whitelist #150

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 0 additions & 7 deletions src/index.test.ts

This file was deleted.

20 changes: 20 additions & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ export const addAvailability = async (vehicleId: number, from: Date, to: Date) =
});
};

export type BookingRequestParameters = {
userChosen: Coordinates;
busStops: Coordinates[];
startFixed: boolean;
timeStamps: Date[][];
numPassengers: number;
numWheelchairs: number;
numBikes: number;
luggage: number;
};

export const whitelisting = async (r: BookingRequestParameters) => {
return await fetch('/api/whitelisting', {
method: 'POST',
body: JSON.stringify({
r
})
});
};

export const booking = async (
from: Location,
to: Location,
Expand Down
91 changes: 91 additions & 0 deletions src/lib/capacities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import type { Event } from '$lib/compositionTypes.js';

export type Capacity = {
wheelchairs: number;
bikes: number;
passengers: number;
luggage: number;
};

export type Range = {
earliestPickup: number;
latestDropoff: number;
};

export class CapacitySimulation {
constructor(
bikeCapacity: number,
wheelchairCapacity: number,
seats: number,
storageSpace: number
) {
this.bikeCapacity = bikeCapacity;
this.wheelchairCapacity = wheelchairCapacity;
this.seats = seats;
this.storageSpace = storageSpace;
this.bikes = 0;
this.wheelchairs = 0;
this.passengers = 0;
this.luggage = 0;
}
private bikeCapacity: number;
private wheelchairCapacity: number;
private seats: number;
private storageSpace: number;
private bikes: number;
private wheelchairs: number;
private passengers: number;
private luggage: number;

private adjustValues(event: Event | Capacity) {
if (event instanceof Capacity || event.is_pickup) {
this.bikes += event.bikes;
this.wheelchairs += event.wheelchairs;
this.passengers += event.passengers;
this.luggage += event.luggage;
} else {
this.bikes -= event.bikes;
this.wheelchairs -= event.wheelchairs;
this.passengers -= event.passengers;
this.luggage -= event.luggage;
}
}

private isValid(): boolean {
return (
this.bikeCapacity >= this.bikes &&
this.wheelchairCapacity >= this.wheelchairs &&
this.storageSpace + this.seats >= this.luggage + this.passengers &&
this.seats >= this.passengers
);
}

getPossibleInsertionRanges = (events: Event[], toInsert: Capacity): Range[] => {
this.reset();
const possibleInsertions: Range[] = [];
this.adjustValues(toInsert);
let start: number | undefined = undefined;
for (let i = 0; i != events.length; i++) {
this.adjustValues(events[i]);
if (!this.isValid()) {
if (start != undefined) {
possibleInsertions.push({ earliestPickup: start, latestDropoff: i });
start = undefined;
}
continue;
}
start = start == undefined ? i : start;
}
if (start != undefined) {
possibleInsertions.push({ earliestPickup: start, latestDropoff: events.length });
}
return possibleInsertions;
};

private reset() {
this.bikes = 0;
this.wheelchairs = 0;
this.passengers = 0;
this.luggage = 0;
}
}
37 changes: 37 additions & 0 deletions src/lib/compositionTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Interval } from './interval';
import type { Coordinates } from './location';

export type Company = {
id: number;
coordinates: Coordinates;
vehicles: Vehicle[];
zoneId: number;
};
export type Vehicle = {
id: number;
bike_capacity: number;
storage_space: number;
wheelchair_capacity: number;
seats: number;
tours: Tour[];
availabilities: Interval[];
};

export type Tour = {
departure: Date;
arrival: Date;
id: number;
events: Event[];
};

export type Event = {
passengers: number;
luggage: number;
wheelchairs: number;
bikes: number;
is_pickup: boolean;
time: Interval;
id: number;
coordinates: Coordinates;
tourId: number;
};
5 changes: 4 additions & 1 deletion src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { hoursToMs } from './time_utils';
import { hoursToMs, minutesToMs } from './time_utils';

export const TZ = 'Europe/Berlin';
export const MIN_PREP_MINUTES = 30;
export const MAX_TRAVEL_DURATION = hoursToMs(1);
export const MAX_PASSENGER_WAITING_TIME = minutesToMs(10);
export const SRID = 4326;
export const SEARCH_INTERVAL_SIZE = minutesToMs(30);
21 changes: 21 additions & 0 deletions src/lib/interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,25 @@ export class Interval {
merged.push(unmerged.pop()!);
return merged;
};

intersect(other:Interval):Interval|undefined{
if(this.overlaps(other)){
return new Interval(new Date(Math.max(this.startTime.getTime(), other.startTime.getTime())), new Date(Math.min(this.endTime.getTime(), other.endTime.getTime())));
}
return undefined;
};

static intersect = (many: Interval[], one: Interval): Interval[] => {
const result: Interval[] = [];
for(let i=0;i!=many.length;++i){
if(one.startTime.getTime() > many[i].endTime.getTime()){
break;
}
if(!many[i].overlaps(one)){
continue;
}
result.push(many[i].intersect(one)!);
}
return result;
}
}
49 changes: 48 additions & 1 deletion src/lib/sqlHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { db } from '$lib/database';
import { sql, type SelectQueryBuilder } from 'kysely';
import { db } from './database';
import type { Coordinates } from './location';
import type { Database } from './types';
import { SRID } from './constants';

export const queryCompletedTours = async (companyId: number | undefined) => {
return await db
Expand All @@ -21,3 +25,46 @@ export const queryCompletedTours = async (companyId: number | undefined) => {
])
.execute();
};

export enum ZoneType {
Any,
Community,
CompulsoryArea
}

export const selectZonesContainingCoordinates = (
coordinates: Coordinates,
coordinates2: Coordinates | undefined,
zoneType: ZoneType
) => {
return db
.selectFrom('zone')
.$if(zoneType != ZoneType.Any, (qb) =>
qb.where('zone.is_community', '=', zoneType == ZoneType.Community ? true : false)
)
.$if(coordinates2 != undefined, (qb) =>
qb.where(
sql<boolean>`ST_Covers(zone.area, ST_SetSRID(ST_MakePoint(${coordinates2!.lng}, ${coordinates2!.lat}), ${SRID}))`
)
)
.where(
sql<boolean>`ST_Covers(zone.area, ST_SetSRID(ST_MakePoint(${coordinates.lng}, ${coordinates.lat}), ${SRID}))`
);
};

export const joinInitializedCompaniesOnZones = (
query: SelectQueryBuilder<Database, 'zone', object>
) => {
return query
.innerJoin('company', 'company.zone', 'zone.id')
.where((eb) =>
eb.and([
eb('company.latitude', 'is not', null),
eb('company.longitude', 'is not', null),
eb('company.address', 'is not', null),
eb('company.name', 'is not', null),
eb('company.zone', 'is not', null),
eb('company.community_area', 'is not', null)
])
);
};
Loading
Loading