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

Add endpoint to expose service ids with assigned block ids #24

Merged
merged 5 commits into from
Sep 14, 2024
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
25 changes: 25 additions & 0 deletions app/src/main/java/org/transitclock/api/data/ApiServiceId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* (C)2023 */
package org.transitclock.api.data;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

/**
* A short description of a serviceId. For when outputting list of block IDs for service.
*/
@Data
public class ApiServiceId {

@JsonProperty
private String id;

@JsonProperty
private List<String> blockIds;

public ApiServiceId(String serviceId, List<String> blockIds) {
this.id = serviceId;
this.blockIds = blockIds;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* (C)2023 */
package org.transitclock.api.data;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

/**
* For outputting simple list of unsorted service IDs with lists of sorted block IDs
*/
@Data
public class ApiServiceIdResponse {

@JsonProperty
private List<ApiServiceId> serviceIds;

/**
* Creates the API unsorted version of list of IDs.
*
* @param services
*/
public ApiServiceIdResponse(Map<String, List<String>> services) {
serviceIds = services.entrySet()
.stream()
.map(entry -> new ApiServiceId(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.transitclock.api.data.ApiRoutesDetailsResponse;
import org.transitclock.api.data.ApiSchedulesHorizStops;
import org.transitclock.api.data.ApiSchedulesVertStopsResponse;
import org.transitclock.api.data.ApiServiceIdResponse;
import org.transitclock.api.data.ApiTrip;
import org.transitclock.api.data.ApiTripPatternsResponse;
import org.transitclock.api.data.ApiTripWithTravelTimes;
Expand Down Expand Up @@ -870,6 +871,23 @@ ResponseEntity<ApiSchedulesHorizStops> getScheduleHorizStops(
tags = {"base data", "serviceId"})
ResponseEntity<ApiIdsResponse> getServiceIds(StandardParameters stdParameters);

/**
* Handles the "serviceIds" command. Returns list of all service IDs with assigned blocks.
*
* @param stdParameters
*
* @return
*
* @
*/
@GetMapping(value = "/command/serviceIdsWithBlocks",
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
@Operation(
summary = "Retrives all service id with block ids.",
description = "Retrives all service id with block ids.",
tags = {"base data", "serviceId"})
ResponseEntity<ApiServiceIdResponse> getServiceIdsWithBlocks(StandardParameters stdParameters);

/**
* Handles the currentServiceIds command. Returns list of service IDs that are currently active.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.transitclock.api.data.ApiRoutesResponse;
import org.transitclock.api.data.ApiSchedulesHorizStops;
import org.transitclock.api.data.ApiSchedulesVertStopsResponse;
import org.transitclock.api.data.ApiServiceIdResponse;
import org.transitclock.api.data.ApiTrip;
import org.transitclock.api.data.ApiTripPatternsResponse;
import org.transitclock.api.data.ApiTripWithTravelTimes;
Expand Down Expand Up @@ -866,6 +867,20 @@ public ResponseEntity<ApiIdsResponse> getServiceIds(StandardParameters stdParame
}
}

@Override
public ResponseEntity<ApiServiceIdResponse> getServiceIdsWithBlocks(StandardParameters stdParameters) {
try {
// Get Vehicle data from server
Map<String, List<String>> idsWithBlockIds = configService.getServiceIdsWithBlockIds();

ApiServiceIdResponse apiServiceIds = new ApiServiceIdResponse(idsWithBlockIds);
return stdParameters.createResponse(apiServiceIds);
} catch (Exception e) {
// If problem getting data then return a Bad Request
throw WebUtils.badRequestException(e);
}
}

@Override
public ResponseEntity<ApiIdsResponse> getCurrentServiceIds(StandardParameters stdParameters) {
try {
Expand Down
17 changes: 17 additions & 0 deletions libs/core/src/main/java/org/transitclock/gtfs/DbConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,23 @@ public int getBlockCount() {
return blockCount;
}

/**
* Returns sorted lists of block IDs what belong to all service IDs
*
* @return Map of all service IDs with belong to block IDs
*/
public Map<String, List<String>> getBlockIdsForAllServiceIds() {
Map<String, List<String>> serviceIdsWithBlocks = new HashMap<>();

blocksByServiceMap.forEach((key, element) -> {
List<String> ids = new ArrayList<>();
element.forEach((innerKey, block) -> ids.add(block.getId()));
Collections.sort(ids);
serviceIdsWithBlocks.put(key, ids);
});
return serviceIdsWithBlocks;
}

/**
* Returns blocks for the specified blockId for all service IDs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.transitclock.core.dataCache.PredictionDataCache;
Expand Down Expand Up @@ -389,4 +390,12 @@ public List<IpcRoute> getRoutesByStopId(String stopId) {
null))
.collect(Collectors.toList());
}
}

/* (non-Javadoc)
* @see org.transitclock.ipc.interfaces.ConfigInterface#getServiceIdsWithBlockIds()
*/
@Override
public Map<String, List<String>> getServiceIdsWithBlockIds() {
return dbConfig.getBlockIdsForAllServiceIds();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
* Defines the RMI interface for getting configuration data.
Expand Down Expand Up @@ -52,7 +53,7 @@ IpcRoute getRoute(String routeIdOrShortName, String directionId, String stopId,
/**
* Obtains ordered list of route details
*
* @param routeIdOrShortName
* @param routeIdsOrShortNames
* @return
*/
List<IpcRoute> getRoutes(List<String> routeIdsOrShortNames);
Expand Down Expand Up @@ -184,4 +185,12 @@ IpcRoute getRoute(String routeIdOrShortName, String directionId, String stopId,
* @return list of IpcRoute
*/
List<IpcRoute> getRoutesByStopId(String stopId);


/**
* Returns sorted lists of block IDs what belong to all service IDs
*
* @return Map of service IDs with belong block IDs
*/
Map<String, List<String>> getServiceIdsWithBlockIds();
}
Loading