diff options
Diffstat (limited to 'src/main/scala/org/xapek/influxdb/influxdb.scala')
-rw-r--r-- | src/main/scala/org/xapek/influxdb/influxdb.scala | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/main/scala/org/xapek/influxdb/influxdb.scala b/src/main/scala/org/xapek/influxdb/influxdb.scala new file mode 100644 index 0000000..0aeb856 --- /dev/null +++ b/src/main/scala/org/xapek/influxdb/influxdb.scala @@ -0,0 +1,62 @@ +package org.xapek.influxdb + +import java.util.Date +import java.net.URI +import scala.collection.JavaConversions._ +import shapeless.Sized +import org.apache.http.client.utils.URLEncodedUtils +import org.apache.http.NameValuePair + +import org.xapek.influxdb.types.{InfluxIdentifier, InfluxString, InfluxNumber, InfluxDate} + + +class InfluxdbError(val serverMessage: String) + extends Exception(s"Server responded: $serverMessage") + +case class InfluxDB(val host: String, val port: Int = 8086, + val db: String = "data", val path: String = "query", + val username: String = "", val password: String = "") + +private case class UriNameValue(val name: String, val value: String) { + def this(nvp: NameValuePair) = this(nvp.getName, nvp.getValue) +} + +object InfluxDB { + def SELECT(column: String) = { + new QueryBuilderSelect(Sized[List](influxIdentifier(column))) + } + + implicit def influxIdentifier(s: String): InfluxIdentifier = new InfluxIdentifier(s) + + implicit def influxString(s: String) = new InfluxString(s) + + implicit def influxNumber(n: Int) = new InfluxNumber(n) + + implicit def influxNumber(n: Double) = new InfluxNumber(n) + + implicit def influxDate(n: Date): InfluxDate = new InfluxDate(n) + + def fromUrl(url: String): InfluxDB = { + val uri = new URI(url) + var influxdb = new InfluxDB(uri.getHost) + URLEncodedUtils.parse(uri, "UTF-8").toList + .map(new UriNameValue(_)).foreach { x => + x match { + case UriNameValue("db", db) => influxdb = influxdb.copy(db = db) + case UriNameValue("username", username) => influxdb = influxdb.copy(username = username) + case UriNameValue("password", password) => influxdb = influxdb.copy(password = password) + case _ => + } + } + if (uri.getPort != -1) + influxdb = influxdb.copy(port = uri.getPort) + + if (uri.getPath.length() > 1) + influxdb = influxdb.copy(path = uri.getPath) + influxdb + } + + def col(col: String) = new InfluxIdentifier(col) + + def time = new InfluxIdentifier("time") +}
\ No newline at end of file |