Skip to content

Commit 9b23bac

Browse files
authored
Merge pull request #19 from HSLdevcom/develop
version 1.0.0
2 parents 91b85cf + be32499 commit 9b23bac

File tree

7 files changed

+145
-81
lines changed

7 files changed

+145
-81
lines changed

.travis.yml

+14-4
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,39 @@ jdk:
77
services:
88
- docker
99

10+
addons:
11+
apt:
12+
packages:
13+
# Needed for `xmllint`.
14+
- libxml2-utils
15+
1016
env:
1117
global:
1218
# Docker Hub cannot handle uppercase characters in repository names. Fix it
1319
# with a bashism as Travis CI cannot handle quoting within command
1420
# substitution.
1521
- LOWERCASE_REPO_SLUG="${TRAVIS_REPO_SLUG,,}"
1622
- DOCKER_LOCAL="${LOWERCASE_REPO_SLUG}:${TRAVIS_COMMIT}"
17-
- DOCKER_MASTER="${LOWERCASE_REPO_SLUG}:latest"
23+
- DOCKER_DEV_TAG="${LOWERCASE_REPO_SLUG}:develop"
24+
- BUILD_HASH=$(echo "${TRAVIS_COMMIT}" | cut -c1-7)
1825

1926
before_install:
2027
- docker --version
2128
- echo "ENV GIT_COMMIT ${TRAVIS_COMMIT}" >> Dockerfile
2229

2330
script:
31+
- set -e
32+
- ./.travis/check_semver
2433
- docker build --tag "${DOCKER_LOCAL}" .
2534

35+
# develop-branch pushes the image with "develop"-tag. Tagged commit will push the image with fully versioned tags
2636
deploy:
2737
- provider: script
28-
script: sh ./.travis/docker_login_tag_push "${DOCKER_LOCAL}" "${DOCKER_MASTER}"
38+
script: sh ./.travis/docker_login_tag_push "${DOCKER_LOCAL}" "${DOCKER_DEV_TAG}"
2939
on:
30-
branch: master
40+
branch: develop
3141
- provider: script
32-
script: sh ./.travis/docker_login_tag_semver_push "${DOCKER_LOCAL}" "${LOWERCASE_REPO_SLUG}" "${TRAVIS_TAG}"
42+
script: sh ./.travis/docker_login_tag_semver_push "${DOCKER_LOCAL}" "${LOWERCASE_REPO_SLUG}" "${TRAVIS_TAG}" "${BUILD_HASH}"
3343
on:
3444
tags: true
3545
all_branches: true

.travis/check_semver

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
matches_semver() {
4+
# Simple regex-check, allows optionally three levels separated with .
5+
if [[ "$1" =~ [0-9]+(\.[0-9]+(\.[0-9]+)?)?$ ]]; then
6+
# 0 = true
7+
return 0
8+
else
9+
# 1 = false
10+
return 1
11+
fi
12+
}
13+
14+
set -u
15+
16+
if [ -n "${TRAVIS_TAG}" ]; then
17+
TRAVIS_SEMVER="$(matches_semver "${TRAVIS_TAG}")"
18+
if [ "$?" -ne 0 ]; then
19+
echo "TRAVIS_TAG ${TRAVIS_TAG} does not follow semver" 1>&2
20+
exit 1
21+
fi
22+
PACKAGE_VERSION="$(xmllint --nsclean --xpath '/project/version/text()' pom.xml)"
23+
PACKAGE_SEMVER="$(matches_semver "${PACKAGE_VERSION}")"
24+
if [ "$?" -ne 0 ]; then
25+
echo "version ${PACKAGE_VERSION} in pom.xml does not follow semver" 1>&2
26+
exit 1
27+
fi
28+
if [ "${TRAVIS_TAG}" != "${PACKAGE_VERSION}" ]; then
29+
echo "According to semver, TRAVIS_TAG ${TRAVIS_TAG} differs from version ${PACKAGE_VERSION} in pom.xml" 1>&2
30+
exit 1
31+
else
32+
echo "Travis tag and pom.xml versions match"
33+
fi
34+
fi

.travis/docker_login_tag_semver_push

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
set -u
44

5-
[ "$#" -ne 3 ] && {
6-
echo "Usage: $(basename "$0") SOURCE_IMAGE[:TAG] TARGET_IMAGE_WITHOUT_TAG TRAVIS_TAG" 1>&2
5+
[ "$#" -lt 3 ] && {
6+
echo "Usage: $(basename "$0") SOURCE_IMAGE[:TAG] TARGET_IMAGE_WITHOUT_TAG TRAVIS_TAG [BUILD_HASH]" 1>&2
77
exit 1
88
}
99

1010
SOURCE_IMAGE_TAG="$1"
1111
TARGET_IMAGE_WITHOUT_TAG="$2"
1212
TAG_FROM_TRAVIS="$3"
13+
if [ "$#" -ge 4 ]; then
14+
BUILD_HASH="$4"
15+
fi
1316

1417
PUSH='./.travis/docker_login_tag_push'
1518

@@ -27,6 +30,9 @@ if [ "${IS_PRE_RELEASE}" != true ]; then
2730
SEMVER_MAJOR="$(echo "${SEMVER_MINOR}" | cut -d . -f 1)"
2831
TARGET_IMAGES="${TARGET_IMAGES} ${TARGET_IMAGE_WITHOUT_TAG}:${SEMVER_MINOR}"
2932
TARGET_IMAGES="${TARGET_IMAGES} ${TARGET_IMAGE_WITHOUT_TAG}:${SEMVER_MAJOR}"
33+
if [ -n "${BUILD_HASH-}" ]; then
34+
TARGET_IMAGES="${TARGET_IMAGES} ${TARGET_IMAGE_WITHOUT_TAG}:${SEMVER_FULL}-${BUILD_HASH}"
35+
fi
3036
fi
31-
37+
echo "Pushing source image with tags $TARGET_IMAGES"
3238
"${PUSH}" "${SOURCE_IMAGE_TAG}" ${TARGET_IMAGES}

pom.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>fi.hsl.transitdata</groupId>
44
<artifactId>transitdata-pubtrans-source</artifactId>
5-
<version>0.3.0</version>
5+
<version>1.0.0</version>
66
<packaging>jar</packaging>
77

88
<repositories>
@@ -17,8 +17,7 @@
1717
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1818
<maven.compiler.source>1.8</maven.compiler.source>
1919
<maven.compiler.target>1.8</maven.compiler.target>
20-
<pulsar.version>2.1.0-incubating</pulsar.version>
21-
<common.version>0.4.0</common.version>
20+
<common.version>1.0.0</common.version>
2221
</properties>
2322

2423
<dependencies>

src/main/java/fi/hsl/transitdata/pulsarpubtransconnect/ArrivalHandler.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import fi.hsl.common.pulsar.PulsarApplicationContext;
44
import fi.hsl.common.transitdata.TransitdataProperties;
5+
import fi.hsl.common.transitdata.TransitdataSchema;
56
import fi.hsl.common.transitdata.proto.PubtransTableProtos;
67
import org.apache.pulsar.client.api.Producer;
78
import org.apache.pulsar.client.api.TypedMessageBuilder;
@@ -15,6 +16,12 @@
1516

1617
public class ArrivalHandler extends PubtransTableHandler {
1718

19+
static final TransitdataSchema schema;
20+
static {
21+
int defaultVersion = PubtransTableProtos.ROIArrival.newBuilder().getSchemaVersion();
22+
schema = new TransitdataSchema(TransitdataProperties.ProtobufSchema.PubtransRoiArrival, Optional.of(defaultVersion));
23+
}
24+
1825
public ArrivalHandler(PulsarApplicationContext context) {
1926
super(context, TransitdataProperties.ProtobufSchema.PubtransRoiArrival);
2027
}
@@ -25,9 +32,16 @@ protected String getTimetabledDateTimeColumnName() {
2532
}
2633

2734
@Override
28-
protected byte[] createPayload(ResultSet resultSet, PubtransTableProtos.Common common) throws SQLException {
35+
protected TransitdataSchema getSchema() {
36+
return schema;
37+
}
38+
39+
@Override
40+
protected byte[] createPayload(ResultSet resultSet, PubtransTableProtos.Common common, PubtransTableProtos.DOITripInfo tripInfo) throws SQLException {
2941
PubtransTableProtos.ROIArrival.Builder arrivalBuilder = PubtransTableProtos.ROIArrival.newBuilder();
42+
arrivalBuilder.setSchemaVersion(arrivalBuilder.getSchemaVersion());
3043
arrivalBuilder.setCommon(common);
44+
arrivalBuilder.setTripInfo(tripInfo);
3145
PubtransTableProtos.ROIArrival arrival = arrivalBuilder.build();
3246
return arrival.toByteArray();
3347
}

src/main/java/fi/hsl/transitdata/pulsarpubtransconnect/DepartureHandler.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import fi.hsl.common.pulsar.PulsarApplicationContext;
44
import fi.hsl.common.transitdata.TransitdataProperties;
5+
import fi.hsl.common.transitdata.TransitdataSchema;
56
import fi.hsl.common.transitdata.proto.PubtransTableProtos;
67
import org.apache.pulsar.client.api.Producer;
78
import org.apache.pulsar.client.api.TypedMessageBuilder;
@@ -17,6 +18,12 @@
1718

1819
public class DepartureHandler extends PubtransTableHandler {
1920

21+
static final TransitdataSchema schema;
22+
static {
23+
int defaultVersion = PubtransTableProtos.ROIDeparture.newBuilder().getSchemaVersion();
24+
schema = new TransitdataSchema(TransitdataProperties.ProtobufSchema.PubtransRoiDeparture, Optional.of(defaultVersion));
25+
}
26+
2027
public DepartureHandler(PulsarApplicationContext context) {
2128
super(context, TransitdataProperties.ProtobufSchema.PubtransRoiDeparture);
2229
}
@@ -27,9 +34,16 @@ protected String getTimetabledDateTimeColumnName() {
2734
}
2835

2936
@Override
30-
protected byte[] createPayload(ResultSet resultSet, PubtransTableProtos.Common common) throws SQLException {
37+
protected TransitdataSchema getSchema() {
38+
return schema;
39+
}
40+
41+
@Override
42+
protected byte[] createPayload(ResultSet resultSet, PubtransTableProtos.Common common, PubtransTableProtos.DOITripInfo tripInfo) throws SQLException {
3143
PubtransTableProtos.ROIDeparture.Builder departureBuilder = PubtransTableProtos.ROIDeparture.newBuilder();
44+
departureBuilder.setSchemaVersion(departureBuilder.getSchemaVersion());
3245
departureBuilder.setCommon(common);
46+
departureBuilder.setTripInfo(tripInfo);
3347
if (resultSet.getBytes("HasDestinationDisplayId") != null)
3448
departureBuilder.setHasDestinationDisplayId(resultSet.getLong("HasDestinationDisplayId"));
3549
if (resultSet.getBytes("HasDestinationStopAreaGid") != null)

src/main/java/fi/hsl/transitdata/pulsarpubtransconnect/PubtransTableHandler.java

+56-69
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import fi.hsl.common.pulsar.PulsarApplicationContext;
44
import fi.hsl.common.transitdata.TransitdataProperties;
5+
import fi.hsl.common.transitdata.TransitdataSchema;
56
import fi.hsl.common.transitdata.proto.PubtransTableProtos;
67
import org.apache.pulsar.client.api.Producer;
78
import org.apache.pulsar.client.api.TypedMessageBuilder;
@@ -63,10 +64,12 @@ public static Optional<Long> toUtcEpochMs(String localTimestamp, String zoneId)
6364
}
6465
}
6566

66-
abstract protected byte[] createPayload(ResultSet resultSet, PubtransTableProtos.Common common) throws SQLException;
67+
abstract protected byte[] createPayload(ResultSet resultSet, PubtransTableProtos.Common common, PubtransTableProtos.DOITripInfo tripInfo) throws SQLException;
6768

6869
abstract protected String getTimetabledDateTimeColumnName();
6970

71+
abstract protected TransitdataSchema getSchema();
72+
7073
public Queue<TypedMessageBuilder<byte[]>> handleResultSet(ResultSet resultSet) throws SQLException {
7174

7275
Queue<TypedMessageBuilder<byte[]>> messageBuilderQueue = new LinkedList<>();
@@ -83,10 +86,16 @@ public Queue<TypedMessageBuilder<byte[]>> handleResultSet(ResultSet resultSet) t
8386
final String key = resultSet.getString("IsOnDatedVehicleJourneyId") + resultSet.getString("JourneyPatternSequenceNumber");
8487
final long dvjId = common.getIsOnDatedVehicleJourneyId();
8588
final long jppId = common.getIsTargetedAtJourneyPatternPointGid();
86-
final byte[] data = createPayload(resultSet, common);
8789

88-
Optional<TypedMessageBuilder<byte[]>> maybeBuilder = createMessage(key, eventTimestampUtcMs, dvjId, jppId, data);
89-
maybeBuilder.ifPresent(messageBuilderQueue::add);
90+
Optional<PubtransTableProtos.DOITripInfo> maybeTripInfo = getTripInfo(dvjId, jppId);
91+
if (!maybeTripInfo.isPresent()) {
92+
log.warn("Could not find valid DOITripInfo from Redis for dvjId {}, jppId {}. Ignoring this update ", dvjId, jppId);
93+
}
94+
else {
95+
final byte[] data = createPayload(resultSet, common, maybeTripInfo.get());
96+
TypedMessageBuilder<byte[]> msgBuilder = createMessage(key, eventTimestampUtcMs, dvjId, data, getSchema());
97+
messageBuilderQueue.add(msgBuilder);
98+
}
9099

91100
//Update latest ts for next round
92101
if (eventTimestampUtcMs > tempTimeStamp) {
@@ -133,77 +142,55 @@ protected PubtransTableProtos.Common parseCommon(ResultSet resultSet) throws SQL
133142
return commonBuilder.build();
134143
}
135144

136-
137-
class JourneyInfo {
138-
String direction;
139-
String routeName;
140-
String startTime;
141-
String operatingDay;
142-
143-
JourneyInfo(Map<String, String> map) {
144-
direction = map.get(TransitdataProperties.KEY_DIRECTION);
145-
routeName = map.get(TransitdataProperties.KEY_ROUTE_NAME);
146-
startTime = map.get(TransitdataProperties.KEY_START_TIME);
147-
operatingDay = map.get(TransitdataProperties.KEY_OPERATING_DAY);
148-
if (!isValid()) {
149-
//Let's print more info for debugging purposes:
150-
log.warn("JourneyInfo is missing some fields. Content: " + this.toString());
151-
}
152-
}
153-
154-
boolean isValid() {
155-
return direction != null && routeName != null &&
156-
startTime != null && operatingDay != null;
157-
}
158-
159-
@Override
160-
public String toString() {
161-
return new StringBuilder()
162-
.append("direction: ").append(direction)
163-
.append(" routeName: ").append(routeName)
164-
.append(" startTime: ").append(startTime)
165-
.append(" operatingDay: ").append(operatingDay)
166-
.toString();
167-
}
168-
}
169-
170-
private Optional<JourneyInfo> getJourneyInfo(long dvjId) {
171-
String key = TransitdataProperties.REDIS_PREFIX_DVJ + Long.toString(dvjId);
172-
Optional<Map<String, String>> maybeJourneyInfoMap = Optional.ofNullable(jedis.hgetAll(key));
173-
174-
return maybeJourneyInfoMap
175-
.map(JourneyInfo::new)
176-
.filter(JourneyInfo::isValid);
145+
private Optional<String> getStopId(long jppId) {
146+
String stopIdKey = TransitdataProperties.REDIS_PREFIX_JPP + Long.toString(jppId);
147+
return Optional.ofNullable(jedis.get(stopIdKey));
177148
}
178149

179-
private Optional<String> getStopId(long jppId) {
180-
String key = TransitdataProperties.REDIS_PREFIX_JPP + Long.toString(jppId);
181-
return Optional.ofNullable(jedis.get(key));
150+
private Optional<Map<String, String>> getTripInfoFields(long dvjId) {
151+
String tripInfoKey = TransitdataProperties.REDIS_PREFIX_DVJ + Long.toString(dvjId);
152+
return Optional.ofNullable(jedis.hgetAll(tripInfoKey));
182153
}
183154

184-
Optional<TypedMessageBuilder<byte[]>> createMessage(String key, long eventTime, long dvjId, long jppId, byte[] data) {
185-
Optional<JourneyInfo> maybeJourneyInfo = getJourneyInfo(dvjId);
186-
if (!maybeJourneyInfo.isPresent()) {
187-
log.warn("Could not find valid JourneyInfo from Redis for dvjId " + dvjId);
155+
protected Optional<PubtransTableProtos.DOITripInfo> getTripInfo(long dvjId, long jppId) {
156+
try {
157+
Optional<String> maybeStopId = getStopId(jppId);
158+
Optional<Map<String, String>> maybeTripInfoMap = getTripInfoFields(dvjId);
159+
160+
if (maybeStopId.isPresent() && maybeTripInfoMap.isPresent()) {
161+
PubtransTableProtos.DOITripInfo.Builder builder = PubtransTableProtos.DOITripInfo.newBuilder();
162+
builder.setStopId(maybeStopId.get());
163+
maybeTripInfoMap.ifPresent(map -> {
164+
if (map.containsKey(TransitdataProperties.KEY_DIRECTION))
165+
builder.setDirectionId(Integer.parseInt(map.get(TransitdataProperties.KEY_DIRECTION)));
166+
if (map.containsKey(TransitdataProperties.KEY_ROUTE_NAME))
167+
builder.setRouteId(map.get(TransitdataProperties.KEY_ROUTE_NAME));
168+
if (map.containsKey(TransitdataProperties.KEY_START_TIME))
169+
builder.setStartTime(map.get(TransitdataProperties.KEY_START_TIME));
170+
if (map.containsKey(TransitdataProperties.KEY_OPERATING_DAY))
171+
builder.setOperatingDay(map.get(TransitdataProperties.KEY_OPERATING_DAY));
172+
});
173+
builder.setDvjId(dvjId);
174+
return Optional.of(builder.build());
175+
}
176+
else {
177+
log.error("Failed to get data from Redis for dvjId {}, jppId {}", dvjId, jppId);
178+
return Optional.empty();
179+
}
188180
}
189-
Optional<String> maybeStopId = getStopId(jppId);
190-
if (!maybeStopId.isPresent()) {
191-
log.warn("Could not find StopId from Redis for dvjId " + dvjId);
181+
catch (Exception e) {
182+
log.error("Failed to get Trip Info for dvj-id " + dvjId, e);
183+
return Optional.empty();
192184
}
185+
}
193186

194-
return maybeJourneyInfo.flatMap(journeyInfo ->
195-
maybeStopId.map(stopId ->
196-
producer.newMessage()
197-
.key(key)
198-
.eventTime(eventTime)
199-
.property(TransitdataProperties.KEY_DVJ_ID, Long.toString(dvjId))
200-
.property(TransitdataProperties.KEY_PROTOBUF_SCHEMA, schema.toString())
201-
.property(TransitdataProperties.KEY_DIRECTION, journeyInfo.direction)
202-
.property(TransitdataProperties.KEY_ROUTE_NAME, journeyInfo.routeName)
203-
.property(TransitdataProperties.KEY_START_TIME, journeyInfo.startTime)
204-
.property(TransitdataProperties.KEY_OPERATING_DAY, journeyInfo.operatingDay)
205-
.property(TransitdataProperties.KEY_STOP_ID, stopId)
206-
.value(data))
207-
);
187+
protected TypedMessageBuilder<byte[]> createMessage(String key, long eventTime, long dvjId, byte[] data, TransitdataSchema schema) {
188+
return producer.newMessage()
189+
.key(key)
190+
.eventTime(eventTime)
191+
.property(TransitdataProperties.KEY_DVJ_ID, Long.toString(dvjId))
192+
.property(TransitdataProperties.KEY_PROTOBUF_SCHEMA, schema.schema.toString())
193+
.property(TransitdataProperties.KEY_SCHEMA_VERSION, Integer.toString(schema.schemaVersion.get()))
194+
.value(data);
208195
}
209196
}

0 commit comments

Comments
 (0)