Skip to content

Commit

Permalink
lwc-events: support duration type (#1664)
Browse files Browse the repository at this point in the history
Map duration type to seconds when converting to a data
point.
  • Loading branch information
brharrington authored Jun 3, 2024
1 parent 9755109 commit 420d37f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.netflix.spectator.api.Clock
import com.netflix.spectator.impl.AtomicDouble
import com.netflix.spectator.impl.StepDouble

import java.time.Duration
import java.util.concurrent.ConcurrentHashMap

/**
Expand Down Expand Up @@ -83,16 +84,17 @@ private[events] object DatapointConverter {

private[events] def toDouble(value: Any, dflt: Double): Double = {
value match {
case v: Boolean => if (v) 1.0 else 0.0
case v: Byte => v.toDouble
case v: Short => v.toDouble
case v: Int => v.toDouble
case v: Long => v.toDouble
case v: Float => v.toDouble
case v: Double => v
case v: Number => v.doubleValue()
case v: String => parseDouble(v)
case _ => dflt
case v: Boolean => if (v) 1.0 else 0.0
case v: Byte => v.toDouble
case v: Short => v.toDouble
case v: Int => v.toDouble
case v: Long => v.toDouble
case v: Float => v.toDouble
case v: Double => v
case v: Number => v.doubleValue()
case v: String => parseDouble(v)
case v: Duration => v.toNanos / 1e9
case _ => dflt
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.netflix.atlas.core.model.Query
import com.netflix.spectator.api.ManualClock
import munit.FunSuite

import java.time.Duration
import java.util.concurrent.atomic.AtomicLong

class DatapointConverterSuite extends FunSuite {
Expand All @@ -43,6 +44,9 @@ class DatapointConverterSuite extends FunSuite {
assertEquals(DatapointConverter.toDouble(new AtomicLong(42), -1.0), 42.0)
assertEquals(DatapointConverter.toDouble("42", -1.0), 42.0)
assertEquals(DatapointConverter.toDouble("42e3", -1.0), 42e3)
assertEquals(DatapointConverter.toDouble(Duration.ofMillis(42131), -1.0), 42.131)
assertEquals(DatapointConverter.toDouble(Duration.ofSeconds(42131), -1.0), 42131.0)
assertEquals(DatapointConverter.toDouble(Duration.ofMinutes(2), -1.0), 120.0)
assert(DatapointConverter.toDouble("foo", -1.0).isNaN)
}

Expand Down

0 comments on commit 420d37f

Please sign in to comment.