Skip to content

Commit

Permalink
Add endpoint to expose service ids with assigned block ids (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
TsimurSh authored Sep 14, 2024
1 parent 86eb51f commit 2471dfe
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 2 deletions.
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();
}

0 comments on commit 2471dfe

Please sign in to comment.