diff --git a/.gitignore b/.gitignore index b020604..a6ca136 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ .vscode target/ .DS_Store +.gitignore +lib/example_surveyadminservice.yaml +lib/example_surveygeocoderservice.yaml diff --git a/lib/example_surveyadminkafkasource.yaml b/lib/example_surveyadminkafkasource.yaml index e9a7928..6ba3bb4 100644 --- a/lib/example_surveyadminkafkasource.yaml +++ b/lib/example_surveyadminkafkasource.yaml @@ -4,7 +4,7 @@ metadata: name: geocodetopicsource spec: bootstrapServers: - - my-cluster-kafka-bootstrap.amq-streams-kafka.svc:9092 + - es1-kafka-bootstrap.cp4i.svc:9092 sink: ref: apiVersion: serving.knative.dev/v1 diff --git a/lib/example_surveyadminservice.yaml.template b/lib/example_surveyadminservice.yaml.template index efbec47..5e99a58 100644 --- a/lib/example_surveyadminservice.yaml.template +++ b/lib/example_surveyadminservice.yaml.template @@ -16,7 +16,7 @@ spec: imagePullPolicy: Always env: - name: mp.messaging.connector.liberty-kafka.bootstrap.servers - value: my-cluster-kafka-bootstrap.amq-streams-kafka.svc:9092 + value: es1-kafka-bootstrap.cp4i.svc:9092 - name: GOOGLE_API_KEY value: INSERT_API_KEY - name: QRCODE_URL diff --git a/lib/example_surveygeocoderkafkasource.yaml b/lib/example_surveygeocoderkafkasource.yaml index ec750e3..72c01cc 100644 --- a/lib/example_surveygeocoderkafkasource.yaml +++ b/lib/example_surveygeocoderkafkasource.yaml @@ -4,7 +4,7 @@ metadata: name: locationtopicsource spec: bootstrapServers: - - my-cluster-kafka-bootstrap.amq-streams-kafka.svc:9092 + - es1-kafka-bootstrap.cp4i.svc:9092 sink: ref: apiVersion: serving.knative.dev/v1 diff --git a/lib/example_surveygeocoderservice.yaml.template b/lib/example_surveygeocoderservice.yaml.template index b166a93..2155520 100644 --- a/lib/example_surveygeocoderservice.yaml.template +++ b/lib/example_surveygeocoderservice.yaml.template @@ -15,7 +15,7 @@ spec: imagePullPolicy: Always env: - name: mp.messaging.connector.liberty-kafka.bootstrap.servers - value: my-cluster-kafka-bootstrap.amq-streams-kafka.svc:9092 + value: es1-kafka-bootstrap.cp4i.svc:9092 - name: GOOGLE_API_KEY value: INSERT_API_KEY securityContext: diff --git a/lib/example_surveyinputservice.yaml b/lib/example_surveyinputservice.yaml index d84a8d2..65dc603 100644 --- a/lib/example_surveyinputservice.yaml +++ b/lib/example_surveyinputservice.yaml @@ -15,7 +15,7 @@ spec: imagePullPolicy: Always env: - name: mp.messaging.connector.liberty-kafka.bootstrap.servers - value: my-cluster-kafka-bootstrap.amq-streams-kafka.svc:9092 + value: es1-kafka-bootstrap.cp4i.svc:9092 securityContext: allowPrivilegeEscalation: true privileged: false diff --git a/surveyAdminService/pom.xml b/surveyAdminService/pom.xml index a598ae2..89b9859 100755 --- a/surveyAdminService/pom.xml +++ b/surveyAdminService/pom.xml @@ -103,6 +103,16 @@ under the License. lucene-core ${dependency.version.lucene-core} + + com.google.code.gson + gson + 2.10.1 + + + org.apache.kafka + kafka-clients + 2.8.0 + ${project.artifactId} diff --git a/surveyAdminService/src/main/java/com/example/demo/reactive/CloudEventsProcessor.java b/surveyAdminService/src/main/java/com/example/demo/reactive/CloudEventsProcessor.java index 392b945..f53cce0 100644 --- a/surveyAdminService/src/main/java/com/example/demo/reactive/CloudEventsProcessor.java +++ b/surveyAdminService/src/main/java/com/example/demo/reactive/CloudEventsProcessor.java @@ -24,6 +24,8 @@ import com.example.demo.Configuration; import com.example.demo.websockets.GeolocationWebSocket; +import com.google.gson.Gson; +import com.google.gson.JsonObject; import io.cloudevents.CloudEvent; import io.cloudevents.CloudEventData; @@ -55,26 +57,27 @@ public Response geocodeComplete(CloudEvent incoming) { if (LOG.isLoggable(Level.INFO)) LOG.info("CloudEventData: " + data); - String geocodeResults = null; + String jsonString = null; try (StringDeserializer deserializer = new StringDeserializer()) { - geocodeResults = deserializer.deserialize(null, data.toBytes()); + jsonString = deserializer.deserialize(null, data.toBytes()); } if (LOG.isLoggable(Level.INFO)) - LOG.info("Geocode results: " + geocodeResults); + LOG.info("Geocode results: " + jsonString); - // latitude ' ' longitude ' ' location - String latitudeStr = geocodeResults.substring(0, geocodeResults.indexOf(' ')); - geocodeResults = geocodeResults.substring(geocodeResults.indexOf(' ') + 1); - String longitudeStr = geocodeResults.substring(0, geocodeResults.indexOf(' ')); - geocodeResults = geocodeResults.substring(geocodeResults.indexOf(' ') + 1); + JsonObject jsonObj = (new Gson()).fromJson(jsonString, JsonObject.class); + String latitudeStr = jsonObj.get("latitude").getAsString(); + String longitudeStr = jsonObj.get("longitude").getAsString(); + String location = jsonObj.get("location").getAsString(); + String color = jsonObj.get("color").getAsString(); + String key = jsonObj.get("key").getAsString(); double latitude = Double.parseDouble(latitudeStr); double longitude = Double.parseDouble(longitudeStr); double approximateDistance = SloppyMath.haversinMeters(latitude, longitude, Configuration.getSurveyLatitude(), Configuration.getSuveyLongitude()); - String finalResults = latitude + " " + longitude + " " + approximateDistance + " " + geocodeResults; + String finalResults = latitude + " " + longitude + " " + approximateDistance + " " + color + " " + key + " " + location; try { GeolocationWebSocket.sendMessageToAllBrowsers(finalResults); diff --git a/surveyAdminService/src/main/java/com/example/demo/websockets/GeolocationWebSocket.java b/surveyAdminService/src/main/java/com/example/demo/websockets/GeolocationWebSocket.java index ef7a81c..27a55e6 100644 --- a/surveyAdminService/src/main/java/com/example/demo/websockets/GeolocationWebSocket.java +++ b/surveyAdminService/src/main/java/com/example/demo/websockets/GeolocationWebSocket.java @@ -1,10 +1,17 @@ package com.example.demo.websockets; import java.io.IOException; +import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import java.util.logging.Logger; +import org.apache.kafka.clients.CommonClientConfigs; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.serialization.StringSerializer; + import jakarta.websocket.EncodeException; import jakarta.websocket.OnClose; import jakarta.websocket.OnError; @@ -51,7 +58,11 @@ public void onBrowserMessage(Session session, String message) { if (LOG.isLoggable(Level.INFO)) LOG.info("GeolocationWebSocket received message from " + session.getId() + ": " + message); + + // Send message from websocket to Kafka topic + sendToKafka(message, "feedbacktopic"); } + } @OnClose @@ -106,4 +117,18 @@ public static final void sendMessageToAllBrowsers(String text) throws IOExceptio } } } + + private void sendToKafka(String message, String topic) { + String bootstrapServer = System.getenv("mp.messaging.connector.liberty-kafka.bootstrap.servers"); + + Properties props = new Properties(); + props.setProperty(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer); + props.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); + props.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); + KafkaProducer producer = new KafkaProducer(props); + ProducerRecord record = new ProducerRecord(topic, message); + producer.send(record); + producer.flush(); + producer.close(); + } } diff --git a/surveyAdminService/src/main/webapp/geolocation.jsp b/surveyAdminService/src/main/webapp/geolocation.jsp index 2f7edce..d4828ef 100644 --- a/surveyAdminService/src/main/webapp/geolocation.jsp +++ b/surveyAdminService/src/main/webapp/geolocation.jsp @@ -5,558 +5,578 @@ Location Survey - - - + h1 { + margin: 0; + } + + #qrcode { + position: absolute; + bottom: 0; + left: 0; + height: auto; + <%= "true".equals(request.getParameter("showqr")) ? "" : "display: none;" %> + } + + #sizecontrols { + position: absolute; + bottom: 0; + left: 10px; + display: flex; + flex-direction: row; + } + + .pseudobutton { + cursor: pointer; + font-size: x-small; + } + + let farthest = null; + for (let surveyResult of search) { + if (farthest) { + if (surveyResult.distance > farthest.distance) { + farthest = surveyResult; + } + } else { + farthest = surveyResult; + } + } + let kilometers = farthest.distance / 1000; + let miles = kilometers * 0.621371; + let suffix = " miles"; + let distance = miles; + <% + if ("true".equals(request.getParameter("km"))) { + %> + distance = kilometers; + suffix = " kilometers"; + <% + } + %> + alert("Farthest location: " + farthest.name + " at " + distance.toLocaleString(undefined, { maximumFractionDigits: 0 }) + suffix); + } + - - Location Survey - - - - - - - + - - - - - ↺ - - 🔍 - - ⥁ - - 📏 - + + Location Survey + + + + + + + + + + - + + ↺ + + 🔍 + + ⥁ + + 📏 + - - - - Please use a browser that supports the canvas element - - Spin - - + + + + Please use a browser that supports the canvas element + + Spin + +
Please use a browser that supports the canvas element