Skip to content
Closed
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
11 changes: 11 additions & 0 deletions src/main/java/org/entur/lamassu/cache/VehicleSpatialIndexId.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class VehicleSpatialIndexId
private PropulsionType propulsionType;
private boolean isReserved;
private boolean isDisabled;
private boolean isAtNonVirtualStation;

public FormFactor getFormFactor() {
return formFactor;
Expand Down Expand Up @@ -44,6 +45,14 @@ public void setDisabled(boolean disabled) {
isDisabled = disabled;
}

public boolean getAtNonVirtualStation() {
return isAtNonVirtualStation;
}

public void setAtNonVirtualStation(boolean atNonVirtualStation) {
isAtNonVirtualStation = atNonVirtualStation;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -54,6 +63,7 @@ public boolean equals(Object o) {

if (isReserved != that.isReserved) return false;
if (isDisabled != that.isDisabled) return false;
if (isAtNonVirtualStation != that.isAtNonVirtualStation) return false;
if (formFactor != that.formFactor) return false;
return propulsionType == that.propulsionType;
}
Expand All @@ -65,6 +75,7 @@ public int hashCode() {
result = 31 * result + (propulsionType != null ? propulsionType.hashCode() : 0);
result = 31 * result + (isReserved ? 1 : 0);
result = 31 * result + (isDisabled ? 1 : 0);
result = 31 * result + (isAtNonVirtualStation ? 1 : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.entur.lamassu.service.GeoSearchService;
import org.entur.lamassu.service.RangeQueryParameters;
import org.entur.lamassu.service.VehicleFilterParameters;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
Expand All @@ -22,15 +23,21 @@ public class VehicleQueryController {
private final GeoSearchService geoSearchService;
private final EntityReader<Vehicle> vehicleReader;
private final QueryParameterValidator validationService;
private final boolean defaultIncludeVehiclesAtNonVirtualStations;

public VehicleQueryController(
GeoSearchService geoSearchService,
EntityReader<Vehicle> vehicleReader,
QueryParameterValidator validationService
QueryParameterValidator validationService,
@Value(
"${org.entur.lamassu.graphql.default-include-vehicles-at-non-virtual-stations:false}"
) boolean defaultIncludeVehiclesAtNonVirtualStations
) {
this.geoSearchService = geoSearchService;
this.vehicleReader = vehicleReader;
this.validationService = validationService;
this.defaultIncludeVehiclesAtNonVirtualStations =
defaultIncludeVehiclesAtNonVirtualStations;
}

@QueryMapping
Expand All @@ -55,7 +62,8 @@ public Collection<Vehicle> vehicles(
@Argument List<FormFactor> formFactors,
@Argument List<PropulsionType> propulsionTypes,
@Argument Boolean includeReserved,
@Argument Boolean includeDisabled
@Argument Boolean includeDisabled,
@Argument Boolean includeVehiclesAtNonVirtualStations
) {
if (ids != null && !ids.isEmpty()) {
return vehicleReader.getAll(new HashSet<>(ids));
Expand All @@ -73,7 +81,10 @@ public Collection<Vehicle> vehicles(
formFactors,
propulsionTypes,
includeReserved,
includeDisabled
includeDisabled,
includeVehiclesAtNonVirtualStations != null
? includeVehiclesAtNonVirtualStations
: defaultIncludeVehiclesAtNonVirtualStations
);

Collection<Vehicle> vehicles;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package org.entur.lamassu.graphql.subscription;

import java.util.List;
import org.entur.lamassu.cache.EntityCache;
import org.entur.lamassu.graphql.subscription.filter.VehicleUpdateFilter;
import org.entur.lamassu.graphql.subscription.handler.VehicleSubscriptionHandler;
import org.entur.lamassu.graphql.subscription.model.VehicleUpdate;
import org.entur.lamassu.graphql.validation.QueryParameterValidator;
import org.entur.lamassu.model.entities.FormFactor;
import org.entur.lamassu.model.entities.PropulsionType;
import org.entur.lamassu.model.entities.Station;
import org.entur.lamassu.service.BoundingBoxQueryParameters;
import org.entur.lamassu.service.FeedProviderService;
import org.entur.lamassu.service.RangeQueryParameters;
import org.entur.lamassu.service.VehicleFilterParameters;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
import org.springframework.stereotype.Controller;
Expand All @@ -26,15 +29,24 @@ public class VehicleSubscriptionController {
private final VehicleSubscriptionHandler vehicleSubscriptionHandler;
private final QueryParameterValidator validationService;
private final FeedProviderService feedProviderService;
private final EntityCache<Station> stationCache;
private final boolean defaultIncludeVehiclesAtNonVirtualStations;

public VehicleSubscriptionController(
VehicleSubscriptionHandler vehicleSubscriptionHandler,
QueryParameterValidator validationService,
FeedProviderService feedProviderService
FeedProviderService feedProviderService,
EntityCache<Station> stationCache,
@Value(
"${org.entur.lamassu.graphql.default-include-vehicles-at-non-virtual-stations:false}"
) boolean defaultIncludeVehiclesAtNonVirtualStations
) {
this.vehicleSubscriptionHandler = vehicleSubscriptionHandler;
this.validationService = validationService;
this.feedProviderService = feedProviderService;
this.stationCache = stationCache;
this.defaultIncludeVehiclesAtNonVirtualStations =
defaultIncludeVehiclesAtNonVirtualStations;
}

/**
Expand Down Expand Up @@ -72,7 +84,8 @@ public Publisher<List<VehicleUpdate>> vehicles(
@Argument List<FormFactor> formFactors,
@Argument List<PropulsionType> propulsionTypes,
@Argument Boolean includeReserved,
@Argument Boolean includeDisabled
@Argument Boolean includeDisabled,
@Argument Boolean includeVehiclesAtNonVirtualStations
) {
// Validate parameters
validationService.validateCodespaces(codespaces);
Expand All @@ -96,7 +109,10 @@ public Publisher<List<VehicleUpdate>> vehicles(
formFactors,
propulsionTypes,
includeReserved != null && includeReserved,
includeDisabled != null && includeDisabled
includeDisabled != null && includeDisabled,
includeVehiclesAtNonVirtualStations != null
? includeVehiclesAtNonVirtualStations
: defaultIncludeVehiclesAtNonVirtualStations
);

// Create subscription handler
Expand All @@ -114,7 +130,8 @@ public Publisher<List<VehicleUpdate>> vehicles(
filterParams,
queryParams,
systemId ->
feedProviderService.getFeedProviderBySystemId(systemId).getCodespace()
feedProviderService.getFeedProviderBySystemId(systemId).getCodespace(),
this::isNonVirtualStation
);
} else {
var queryParams = new BoundingBoxQueryParameters(
Expand All @@ -128,11 +145,20 @@ public Publisher<List<VehicleUpdate>> vehicles(
filterParams,
queryParams,
systemId ->
feedProviderService.getFeedProviderBySystemId(systemId).getCodespace()
feedProviderService.getFeedProviderBySystemId(systemId).getCodespace(),
this::isNonVirtualStation
);
}

// Return publisher and ensure listener is removed when subscription ends
return vehicleSubscriptionHandler.getPublisher(filter);
}

private boolean isNonVirtualStation(String stationId) {
var station = stationCache.get(stationId);
if (station == null) {
return false;
}
return !Boolean.TRUE.equals(station.getVirtualStation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.entur.lamassu.graphql.subscription.filter;

import java.util.List;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import org.entur.lamassu.graphql.subscription.model.VehicleUpdate;
import org.entur.lamassu.model.entities.FormFactor;
Expand All @@ -34,23 +35,28 @@
implements EntityUpdateFilter<VehicleUpdate> {

private final VehicleFilterParameters filterParameters;
private final Predicate<String> nonVirtualStationPredicate;

public VehicleUpdateFilter(
VehicleFilterParameters filterParameters,
RangeQueryParameters rangeQueryParameters,
UnaryOperator<String> codespaceResolver
UnaryOperator<String> codespaceResolver,
Predicate<String> nonVirtualStationPredicate
) {
super(filterParameters, rangeQueryParameters, codespaceResolver);
this.filterParameters = filterParameters;
this.nonVirtualStationPredicate = nonVirtualStationPredicate;
}

public VehicleUpdateFilter(
VehicleFilterParameters filterParameters,
BoundingBoxQueryParameters boundingBoxParameters,
UnaryOperator<String> codespaceResolver
UnaryOperator<String> codespaceResolver,
Predicate<String> nonVirtualStationPredicate
) {
super(filterParameters, boundingBoxParameters, codespaceResolver);
this.filterParameters = filterParameters;
this.nonVirtualStationPredicate = nonVirtualStationPredicate;
}

@Override
Expand Down Expand Up @@ -127,10 +133,24 @@
}

// Filter by disabled status
return (
filterParameters.getIncludeDisabled() ||
vehicle.getDisabled() == null ||
!Boolean.TRUE.equals(vehicle.getDisabled())
);
if (
!filterParameters.getIncludeDisabled() &&
vehicle.getDisabled() != null &&
Boolean.TRUE.equals(vehicle.getDisabled())
) {
return false;
}

// Filter by non-virtual station assignment
if (

Check warning on line 145 in src/main/java/org/entur/lamassu/graphql/subscription/filter/VehicleUpdateFilter.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this if-then-else statement by a single return statement.

See more on https://sonarcloud.io/project/issues?id=entur_lamassu&issues=AZ0xJQcw9Tz2Boh2mx0R&open=AZ0xJQcw9Tz2Boh2mx0R&pullRequest=850
!filterParameters.getIncludeVehiclesAtNonVirtualStations() &&
vehicle.getStationId() != null &&
nonVirtualStationPredicate != null &&
nonVirtualStationPredicate.test(vehicle.getStationId())
) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
public class SpatialIndexIdGeneratorService {

private final EntityCache<VehicleType> vehicleTypeCache;
private final EntityCache<Station> stationCache;

@Autowired
public SpatialIndexIdGeneratorService(EntityCache<VehicleType> vehicleTypeCache) {
public SpatialIndexIdGeneratorService(
EntityCache<VehicleType> vehicleTypeCache,
EntityCache<Station> stationCache
) {
this.vehicleTypeCache = vehicleTypeCache;
this.stationCache = stationCache;
}

public VehicleSpatialIndexId createVehicleIndexId(
Expand All @@ -43,9 +48,21 @@ public VehicleSpatialIndexId createVehicleIndexId(
id.setPropulsionType(vehicleType.getPropulsionType());
id.setReserved(vehicle.getReserved());
id.setDisabled(vehicle.getDisabled());
id.setAtNonVirtualStation(isAtNonVirtualStation(vehicle));
return id;
}

private boolean isAtNonVirtualStation(Vehicle vehicle) {
if (vehicle.getStationId() == null) {
return false;
}
var station = stationCache.get(vehicle.getStationId());
if (station == null) {
return false;
}
return !Boolean.TRUE.equals(station.getVirtualStation());
}

public StationSpatialIndexId createStationIndexId(
Station station,
FeedProvider provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class VehicleFilterParameters extends FilterParameters {
private List<PropulsionType> propulsionTypes;
private boolean includeReserved;
private boolean includeDisabled;
private boolean includeVehiclesAtNonVirtualStations;

public VehicleFilterParameters(
List<String> codespaces,
Expand All @@ -19,13 +20,15 @@ public VehicleFilterParameters(
List<FormFactor> formFactors,
List<PropulsionType> propulsionTypes,
boolean includeReserved,
boolean includeDisabled
boolean includeDisabled,
boolean includeVehiclesAtNonVirtualStations
) {
super(codespaces, systems, operators, count);
this.formFactors = formFactors;
this.propulsionTypes = propulsionTypes;
this.includeReserved = includeReserved;
this.includeDisabled = includeDisabled;
this.includeVehiclesAtNonVirtualStations = includeVehiclesAtNonVirtualStations;
}

public List<FormFactor> getFormFactors() {
Expand Down Expand Up @@ -59,4 +62,14 @@ public boolean getIncludeDisabled() {
public void setIncludeDisabled(boolean includeDisabled) {
this.includeDisabled = includeDisabled;
}

public boolean getIncludeVehiclesAtNonVirtualStations() {
return includeVehiclesAtNonVirtualStations;
}

public void setIncludeVehiclesAtNonVirtualStations(
boolean includeVehiclesAtNonVirtualStations
) {
this.includeVehiclesAtNonVirtualStations = includeVehiclesAtNonVirtualStations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

private SpatialIndexIdFilter() {}

public static boolean filterVehicle(

Check failure on line 12 in src/main/java/org/entur/lamassu/util/SpatialIndexIdFilter.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=entur_lamassu&issues=AZ0xJQfj9Tz2Boh2mx0S&open=AZ0xJQfj9Tz2Boh2mx0S&pullRequest=850
VehicleSpatialIndexId parsedId,
VehicleFilterParameters filters
) {
Expand Down Expand Up @@ -56,6 +56,13 @@
return false;
}

if (

Check warning on line 59 in src/main/java/org/entur/lamassu/util/SpatialIndexIdFilter.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this if-then-else statement by a single return statement.

See more on https://sonarcloud.io/project/issues?id=entur_lamassu&issues=AZ0xJQfj9Tz2Boh2mx0T&open=AZ0xJQfj9Tz2Boh2mx0T&pullRequest=850
!filters.getIncludeVehiclesAtNonVirtualStations() &&
parsedId.getAtNonVirtualStation()
) {
return false;
}

return true;
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/graphql/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Subscription {
propulsionTypes: [PropulsionType]
includeReserved: Boolean = false
includeDisabled: Boolean = false
includeVehiclesAtNonVirtualStations: Boolean
): [VehicleUpdate] @deprecated(reason: "Experimental feature - API is subject to change")

stations(
Expand Down Expand Up @@ -105,6 +106,9 @@ type Query {

"Include disabled vehicles in result"
includeDisabled: Boolean = false

"Include vehicles assigned to non-virtual stations in result"
includeVehiclesAtNonVirtualStations: Boolean
): [Vehicle]

station(id: String!): Station
Expand Down
Loading
Loading