|
| 1 | +package spp.cli.commands.instrument |
| 2 | + |
| 3 | +import com.apollographql.apollo3.api.CustomScalarAdapters |
| 4 | +import com.apollographql.apollo3.api.Optional |
| 5 | +import com.apollographql.apollo3.api.json.MapJsonWriter |
| 6 | +import com.github.ajalt.clikt.core.CliktCommand |
| 7 | +import com.github.ajalt.clikt.parameters.arguments.argument |
| 8 | +import com.github.ajalt.clikt.parameters.options.default |
| 9 | +import com.github.ajalt.clikt.parameters.options.option |
| 10 | +import com.github.ajalt.clikt.parameters.types.enum |
| 11 | +import com.github.ajalt.clikt.parameters.types.int |
| 12 | +import com.github.ajalt.clikt.parameters.types.long |
| 13 | +import kotlinx.coroutines.runBlocking |
| 14 | +import spp.cli.Main |
| 15 | +import spp.cli.PlatformCLI.apolloClient |
| 16 | +import spp.cli.PlatformCLI.echoError |
| 17 | +import spp.cli.protocol.instrument.AddLiveMeterMutation |
| 18 | +import spp.cli.protocol.instrument.adapter.AddLiveMeterMutation_ResponseAdapter.AddLiveMeter |
| 19 | +import spp.cli.protocol.type.* |
| 20 | +import spp.cli.util.JsonCleaner |
| 21 | +import kotlin.system.exitProcess |
| 22 | + |
| 23 | +class AddMeter : CliktCommand() { |
| 24 | + |
| 25 | + val source by argument(help = "Qualified class name") |
| 26 | + val line by argument(help = "Line number").int() |
| 27 | + val meterName by argument(help = "Meter name") |
| 28 | + val meterType by argument(help = "Meter type").enum<MeterType>() |
| 29 | + val valueType by argument(help = "Metric value type").enum<MetricValueType>() |
| 30 | + val value by option("-value", "-v", help = "Metric value") |
| 31 | + val condition by option("-condition", "-c", help = "Trigger condition") |
| 32 | + val expiresAt by option("-expiresAt", "-e", help = "Expiration time (epoch time [ms])").long() |
| 33 | + val hitLimit by option("-hitLimit", "-h", help = "Trigger hit limit").int() |
| 34 | + val throttleLimit by option("-throttleLimit", "-t", help = "Trigger throttle limit").int().default(1) |
| 35 | + val throttleStep by option("-throttleStep", "-s", help = "Trigger throttle step").enum<ThrottleStep>() |
| 36 | + .default(ThrottleStep.SECOND) |
| 37 | + |
| 38 | + override fun run() = runBlocking { |
| 39 | + val input = LiveMeterInput( |
| 40 | + meterName = meterName, |
| 41 | + meterType = meterType, |
| 42 | + metricValue = MetricValueInput( |
| 43 | + valueType = valueType, |
| 44 | + value = Optional.presentIfNotNull(value) |
| 45 | + ), |
| 46 | + location = LiveSourceLocationInput(source, line), |
| 47 | + condition = Optional.Present(condition), |
| 48 | + expiresAt = Optional.Present(expiresAt), |
| 49 | + hitLimit = Optional.Present(hitLimit), |
| 50 | + throttle = Optional.Present(InstrumentThrottleInput(throttleLimit, throttleStep)) |
| 51 | + ) |
| 52 | + val response = try { |
| 53 | + apolloClient.mutation(AddLiveMeterMutation(input)).execute() |
| 54 | + } catch (e: Exception) { |
| 55 | + echoError(e) |
| 56 | + if (Main.standalone) exitProcess(-1) else return@runBlocking |
| 57 | + } |
| 58 | + if (response.hasErrors()) { |
| 59 | + echo(response.errors?.get(0)?.message, err = true) |
| 60 | + if (Main.standalone) exitProcess(-1) else return@runBlocking |
| 61 | + } |
| 62 | + |
| 63 | + echo(JsonCleaner.cleanJson(MapJsonWriter().let { |
| 64 | + it.beginObject() |
| 65 | + AddLiveMeter.toJson(it, CustomScalarAdapters.Empty, response.data!!.addLiveMeter) |
| 66 | + it.endObject() |
| 67 | + (it.root() as LinkedHashMap<*, *>) |
| 68 | + }).encodePrettily()) |
| 69 | + if (Main.standalone) exitProcess(0) |
| 70 | + } |
| 71 | +} |
0 commit comments