From 40a6f0db1c8977875cf5860250fd330f26e08e7c Mon Sep 17 00:00:00 2001 From: Marty Pitt Date: Fri, 13 Dec 2024 15:17:05 +1100 Subject: [PATCH] feat: new demo - working with Kafka --- .../orbital/nebula/stack.nebula.kts | 106 +++++++++++++++--- working-with-kafka/src/common.types.taxi | 5 +- working-with-kafka/src/customer.api.taxi | 17 ++- working-with-kafka/taxi.conf | 4 + 4 files changed, 105 insertions(+), 27 deletions(-) diff --git a/working-with-kafka/orbital/nebula/stack.nebula.kts b/working-with-kafka/orbital/nebula/stack.nebula.kts index ed0b743..4ba2a42 100644 --- a/working-with-kafka/orbital/nebula/stack.nebula.kts +++ b/working-with-kafka/orbital/nebula/stack.nebula.kts @@ -1,10 +1,98 @@ +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.orbitalhq.nebula.utils.duration import java.math.BigDecimal import java.time.Instant import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicInteger +import kotlin.random.Random +// Enum for RewardsStatus +enum class RewardsStatus { + Gold, Silver, Bronze +} + +// Data class for Customer +data class Customer(val id: String, val name: String, val email: String, val rewardsStatus: RewardsStatus) + +/** + * This stack stands up: + * - A Kafka broker that emits: + * - Food order events + * - Delivery update events + * + * - An HTTP server that provides customer and restaurant data + * + */ stack { + val restaurants = listOf( + mapOf( + "id" to "REST-1", + "name" to "Pizza Palace", + "cuisineType" to "Italian", + "latitude" to 40.7128, + "longitude" to -74.0060 + ), + mapOf( + "id" to "REST-2", + "name" to "Sushi Express", + "cuisineType" to "Japanese", + "latitude" to 40.7589, + "longitude" to -73.9851 + ) + ) + val firstNames = listOf( + "Alice", + "Bob", + "Charlie", + "Diana", + "Edward", + "Fiona", + "George", + "Hannah", + "Ian", + "Julia", + "Kevin", + "Laura", + "Michael", + "Nina", + "Oliver" + ) + val lastNames = listOf( + "Smith", + "Johnson", + "Williams", + "Brown", + "Jones", + "Miller", + "Davis", + "Garcia", + "Martinez", + "Hernandez", + "Lopez", + "Gonzalez", + "Wilson", + "Anderson", + "Thomas" + ) + val customers = (0..50).mapIndexed { index, _ -> + val firstName = firstNames.random() + val lastName = lastNames.random() + Customer( + "CUST-$index", + "$firstName $lastName", + "${firstName.substring(0..0).lowercase()}.${lastName.lowercase()}@example.com", + RewardsStatus.entries.random() + ) + } + + http { + val mapper = jacksonObjectMapper() + get("/customers/{id}") { call -> + val id = call.parameters["id"]!!.toInt() + + call.respondText(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customers[id]!!)) + } + } kafka { val counter = AtomicInteger(0) @@ -23,22 +111,6 @@ stack { val activeDeliveries = ConcurrentHashMap() - val restaurants = listOf( - mapOf( - "id" to "REST-1", - "name" to "Pizza Palace", - "cuisineType" to "Italian", - "latitude" to 40.7128, - "longitude" to -74.0060 - ), - mapOf( - "id" to "REST-2", - "name" to "Sushi Express", - "cuisineType" to "Japanese", - "latitude" to 40.7589, - "longitude" to -73.9851 - ) - ) // Generate new orders producer("2000ms".duration(), "orders") { @@ -62,7 +134,7 @@ stack { val order = mapOf( "orderId" to orderId, "restaurantId" to restaurant["id"], - "customerId" to "CUST-${Random.nextInt(1, 100)}", + "customerId" to "CUST-${Random.nextInt(1, 50)}", "items" to items, "totalAmount" to items.sumOf { (it["price"] as BigDecimal) * (it["quantity"] as Int).toBigDecimal() diff --git a/working-with-kafka/src/common.types.taxi b/working-with-kafka/src/common.types.taxi index 9b390a7..990d0e9 100644 --- a/working-with-kafka/src/common.types.taxi +++ b/working-with-kafka/src/common.types.taxi @@ -10,7 +10,10 @@ type Amount inherits Decimal type Latitude inherits Decimal type Longitude inherits Decimal type CuisineType inherits String -type LoyaltyStatus inherits String + type DeliveryStatus inherits String +enum LoyaltyStatus { + Gold, Silver, Bronze +} diff --git a/working-with-kafka/src/customer.api.taxi b/working-with-kafka/src/customer.api.taxi index 5fade1f..28a4b22 100644 --- a/working-with-kafka/src/customer.api.taxi +++ b/working-with-kafka/src/customer.api.taxi @@ -1,17 +1,16 @@ +import taxi.http.PathVariable +import taxi.http.HttpOperation namespace com.orbitEats.customer closed model Customer { - @Id - id: CustomerId - name: String - loyaltyStatus: LoyaltyStatus - address: { - latitude: Latitude - longitude: Longitude - } + @Id + id: CustomerId + name: CustomerName inherits String + loyaltyStatus: LoyaltyStatus } service CustomerService { - operation findCustomer(CustomerId): Customer + @HttpOperation(url="http://customerApi/customers/{customerId}", method = "GET") + operation findCustomer(@PathVariable("customerId") customerId: CustomerId): Customer } diff --git a/working-with-kafka/taxi.conf b/working-with-kafka/taxi.conf index 7bf7552..c08a8f8 100644 --- a/working-with-kafka/taxi.conf +++ b/working-with-kafka/taxi.conf @@ -5,3 +5,7 @@ additionalSources: { "@orbital/config" : "orbital/config/*.conf", "@orbital/nebula" : "orbital/nebula/*.nebula.kts" } +dependencies: { + "com.orbitalhq/core": 0.30.0 +} +