|
| 1 | +package cli |
| 2 | + |
| 3 | +import com.google.gson.Gson |
| 4 | +import com.google.gson.reflect.TypeToken |
| 5 | +import com.segment.analytics.Analytics |
| 6 | +import com.segment.analytics.Callback |
| 7 | +import com.segment.analytics.messages.* |
| 8 | +import java.util.concurrent.CountDownLatch |
| 9 | +import java.util.concurrent.TimeUnit |
| 10 | +import java.util.concurrent.atomic.AtomicBoolean |
| 11 | + |
| 12 | +data class CLIOutput( |
| 13 | + val success: Boolean, |
| 14 | + val error: String? = null, |
| 15 | + val sentBatches: Int = 0 |
| 16 | +) |
| 17 | + |
| 18 | +data class CLIConfig( |
| 19 | + val flushAt: Int? = null, |
| 20 | + val flushInterval: Long? = null, |
| 21 | + val maxRetries: Int? = null, |
| 22 | + val timeout: Int? = null |
| 23 | +) |
| 24 | + |
| 25 | +data class EventSequence( |
| 26 | + val delayMs: Long = 0, |
| 27 | + val events: List<Map<String, Any>> |
| 28 | +) |
| 29 | + |
| 30 | +data class CLIInput( |
| 31 | + val writeKey: String, |
| 32 | + val apiHost: String, |
| 33 | + val sequences: List<EventSequence>, |
| 34 | + val config: CLIConfig? = null |
| 35 | +) |
| 36 | + |
| 37 | +private val gson = Gson() |
| 38 | + |
| 39 | +fun main(args: Array<String>) { |
| 40 | + var output = CLIOutput(success = false, error = "Unknown error") |
| 41 | + |
| 42 | + try { |
| 43 | + // Parse --input argument |
| 44 | + val inputIndex = args.indexOf("--input") |
| 45 | + if (inputIndex == -1 || inputIndex + 1 >= args.size) { |
| 46 | + throw IllegalArgumentException("Missing required --input argument") |
| 47 | + } |
| 48 | + |
| 49 | + val inputJson = args[inputIndex + 1] |
| 50 | + val input = gson.fromJson(inputJson, CLIInput::class.java) |
| 51 | + |
| 52 | + val flushAt = input.config?.flushAt ?: 20 |
| 53 | + val flushIntervalMs = input.config?.flushInterval ?: 10000L |
| 54 | + |
| 55 | + val flushLatch = CountDownLatch(1) |
| 56 | + val hasError = AtomicBoolean(false) |
| 57 | + var errorMessage: String? = null |
| 58 | + |
| 59 | + val analytics = Analytics.builder(input.writeKey) |
| 60 | + .endpoint(input.apiHost) |
| 61 | + .flushQueueSize(flushAt) |
| 62 | + .flushInterval(maxOf(flushIntervalMs, 1000L), TimeUnit.MILLISECONDS) |
| 63 | + .callback(object : Callback { |
| 64 | + override fun success(message: Message?) { |
| 65 | + // Event sent successfully |
| 66 | + } |
| 67 | + |
| 68 | + override fun failure(message: Message?, throwable: Throwable?) { |
| 69 | + hasError.set(true) |
| 70 | + errorMessage = throwable?.message |
| 71 | + } |
| 72 | + }) |
| 73 | + .build() |
| 74 | + |
| 75 | + // Process event sequences |
| 76 | + for (seq in input.sequences) { |
| 77 | + if (seq.delayMs > 0) { |
| 78 | + Thread.sleep(seq.delayMs) |
| 79 | + } |
| 80 | + |
| 81 | + for (event in seq.events) { |
| 82 | + sendEvent(analytics, event) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Flush and shutdown |
| 87 | + analytics.flush() |
| 88 | + analytics.shutdown() |
| 89 | + |
| 90 | + output = if (hasError.get()) { |
| 91 | + CLIOutput(success = false, error = errorMessage, sentBatches = 0) |
| 92 | + } else { |
| 93 | + CLIOutput(success = true, sentBatches = 1) |
| 94 | + } |
| 95 | + |
| 96 | + } catch (e: Exception) { |
| 97 | + output = CLIOutput(success = false, error = e.message ?: e.toString()) |
| 98 | + } |
| 99 | + |
| 100 | + println(gson.toJson(output)) |
| 101 | +} |
| 102 | + |
| 103 | +fun sendEvent(analytics: Analytics, event: Map<String, Any>) { |
| 104 | + val type = event["type"] as? String |
| 105 | + ?: throw IllegalArgumentException("Event missing 'type' field") |
| 106 | + |
| 107 | + val userId = event["userId"] as? String ?: "" |
| 108 | + val anonymousId = event["anonymousId"] as? String |
| 109 | + val messageId = event["messageId"] as? String |
| 110 | + @Suppress("UNCHECKED_CAST") |
| 111 | + val traits = event["traits"] as? Map<String, Any> ?: emptyMap() |
| 112 | + @Suppress("UNCHECKED_CAST") |
| 113 | + val properties = event["properties"] as? Map<String, Any> ?: emptyMap() |
| 114 | + val eventName = event["event"] as? String |
| 115 | + val name = event["name"] as? String |
| 116 | + val groupId = event["groupId"] as? String |
| 117 | + val previousId = event["previousId"] as? String |
| 118 | + |
| 119 | + val messageBuilder: MessageBuilder<*, *> = when (type) { |
| 120 | + "identify" -> { |
| 121 | + IdentifyMessage.builder().apply { |
| 122 | + traits(traits) |
| 123 | + } |
| 124 | + } |
| 125 | + "track" -> { |
| 126 | + TrackMessage.builder(eventName ?: "Unknown Event").apply { |
| 127 | + properties(properties) |
| 128 | + } |
| 129 | + } |
| 130 | + "page" -> { |
| 131 | + PageMessage.builder(name ?: "Unknown Page").apply { |
| 132 | + properties(properties) |
| 133 | + } |
| 134 | + } |
| 135 | + "screen" -> { |
| 136 | + ScreenMessage.builder(name ?: "Unknown Screen").apply { |
| 137 | + properties(properties) |
| 138 | + } |
| 139 | + } |
| 140 | + "alias" -> { |
| 141 | + AliasMessage.builder(previousId ?: "") |
| 142 | + } |
| 143 | + "group" -> { |
| 144 | + GroupMessage.builder(groupId ?: "").apply { |
| 145 | + traits(traits) |
| 146 | + } |
| 147 | + } |
| 148 | + else -> throw IllegalArgumentException("Unknown event type: $type") |
| 149 | + } |
| 150 | + |
| 151 | + if (userId.isNotEmpty()) { |
| 152 | + messageBuilder.userId(userId) |
| 153 | + } |
| 154 | + if (anonymousId != null) { |
| 155 | + messageBuilder.anonymousId(anonymousId) |
| 156 | + } |
| 157 | + |
| 158 | + analytics.enqueue(messageBuilder) |
| 159 | +} |
0 commit comments