Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use typeclasses instead of KNumber/KString/KairosCompatibleType #5

Open
thomastoye opened this issue Aug 10, 2016 · 0 comments
Open

Comments

@thomastoye
Copy link
Contributor

Currently, supported KairosDB types are represented by classes that extend the sealed trait KairosCompatibleType, with implementations KNumber and KString. We might use a typeclass instead so users can specify their own types (for when they have custom data types).

Here's one idea:

trait KairosCompatibleTypeTypeClass[A] extends Format[A] {
  /** The KairosDB name for the type */
  val typeName: String
}

object KairosString extends KairosCompatibleTypeTypeClass[String] {
  override val typeName: String = "string"
  override def writes(o: String): JsValue = JsString(o)
  override def reads(json: JsValue): JsResult[String] = json.validate[String]
}

object KairosBigDecimal extends KairosCompatibleTypeTypeClass[BigDecimal] {
  override val typeName: String = "number"
  override def writes(o: BigDecimal): JsValue = JsNumber(o)
  override def reads(json: JsValue): JsResult[BigDecimal] = json.validate[BigDecimal]
}

case class DataPointWithTypeclassValue[A](metricName: MetricName, value: A, timestamp: Instant, tags: Seq[Tag], ttl: Option[FiniteDuration] = None) extends DataPoint

  def addDataPointsWithTypeClass[A](dataPoints: Seq[DataPointWithTypeclassValue[A]])(implicit evidence: KairosCompatibleTypeTypeClass[A]): Future[Unit] = {
    val body = Json.toJson(dataPoints)

    wsClient
      .url(uri / "api" / "v1" / "datapoints")
      .applyKairosDBAuth
      .post(body)
      .map { res =>
        res.status match {
          case 204 => ()
          case 400 => throw KairosDBResponseBadRequestException(errors = getErrors(res))
          case 500 => throw KairosDBResponseInternalServerErrorException(errors = getErrors(res))
        }
      }
  }

And then using it:

addDataPointsWithTypeClass[String](Seq(DataPointWithTypeclassValue(MetricName("aoeu"), "aoue", Instant.now, Seq())))(KairosString)

I'm not sure, but I believe that this will make it impossible to mix datapoints with values of different types (e.g. a datapoint with a number and a datapoint with a string). Perhaps using some Scala type magic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant