Skip to content

Commit

Permalink
feat: new demo - working with Kafka
Browse files Browse the repository at this point in the history
  • Loading branch information
martypitt committed Dec 13, 2024
1 parent a47b70b commit 40a6f0d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 27 deletions.
106 changes: 89 additions & 17 deletions working-with-kafka/orbital/nebula/stack.nebula.kts
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -23,22 +111,6 @@ stack {

val activeDeliveries = ConcurrentHashMap<String, DeliveryState>()

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") {
Expand All @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion working-with-kafka/src/common.types.taxi
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

17 changes: 8 additions & 9 deletions working-with-kafka/src/customer.api.taxi
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions working-with-kafka/taxi.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ additionalSources: {
"@orbital/config" : "orbital/config/*.conf",
"@orbital/nebula" : "orbital/nebula/*.nebula.kts"
}
dependencies: {
"com.orbitalhq/core": 0.30.0
}

0 comments on commit 40a6f0d

Please sign in to comment.