Skip to content

Commit

Permalink
iss #24: use universal config in mt8057 agent
Browse files Browse the repository at this point in the history
  • Loading branch information
maizy committed Feb 13, 2017
1 parent bf7308d commit ebdec8a
Show file tree
Hide file tree
Showing 22 changed files with 396 additions and 436 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ java -jar ambient7-mt8057-agent-x.x.x.jar --writers=interactive
java -jar ambient7-mt8057-agent-x.x.x.jar --writers=influxdb \
--influxdb-database=ambient7 \
--influxdb-baseurl=http://localhost:8086/write \
--influxdb-baseurl=http://localhost:8086/ \
--influxdb-user=user --influxdb-password=123
```

Expand Down Expand Up @@ -103,7 +103,7 @@ Add to crontab or any other scheduler the command:
java -jar ambient7-analysis-x.x.x.jar aggregate-co2 \
--influxdb-database=ambient7 \
--influxdb-agent-name=main \
'--influxdb-baseurl=http://127.0.0.1:8086/' \
'--influxdb-baseurl=http://localhost:8086/' \
--influxdb-user=ambient7_rw \
--influxdb-password=123 \
--influxdb-readonly-user=ambient7_ro \
Expand Down
5 changes: 2 additions & 3 deletions config/ambient7.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ influxdb {
database = "ambient7"

baseurl = "http://localhost:8086/"
user = ""
user = "ambient7_rw"
password = ""

// optional readonly access
// readonly {
// baseurl = "http://localhost:8086/"
// user = ""
// user = "ambient7_ro"
// password = ""
// }

Expand All @@ -27,7 +27,6 @@ devices = [
model = "mt8057"
agent-name = "main"
agent-tags = ""

}

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ case class Ambient7Options(

// TODO: how to extract those specific option to target submodule?
webAppSpecificOptions: Option[WebAppSpecificOptions] = None,


mt8057AgentSpecificOptions: Option[Mt8057AgentSpecificOptions] = None,

// TODO: fix this little hack
showHelp: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.maizy.ambient7.core.config

import java.io.File

/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2017
* See LICENSE.txt for details.
*/

// TODO: should be only in mt8057-agent submodule

trait EnumerationMap extends Enumeration {
self =>
lazy val valuesMap: Map[String, Value] = self.values.map{ v => (v.toString, v) }.toMap
}

object Writers extends Enumeration with EnumerationMap {
self =>
type Writer = Value
val Console = Value("console")
val Interactive = Value("interactive")
val InfluxDb = Value("influxdb")
}

case class Mt8057AgentSpecificOptions(
writers: Set[Writers.Writer] = Set.empty,
useEmulator: Boolean = false,
logFile: Option[File] = None,
verboseLogging: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package ru.maizy.ambient7.core
* See LICENSE.txt for details.
*/

import com.typesafe.scalalogging.{ LazyLogging, Logger }
import com.typesafe.scalalogging.Logger

package object config extends LazyLogging {
val configLogger: Logger = logger
package object config {
val configLogger = Logger("ru.maizy.ambient7.core")
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ trait DevicesConfigReader extends UniversalConfigReader {
.action(saveTags)
.text { "Any additional InfluxDB record tags"}

// Deprecated
cliParser.opt[String]("influxdb-tags")
.validate(validateTags)
.action(saveTags)
.text { "Deprecated, use --agent-tags"}

appendCheck { opts =>
if (opts.fromCliDevice.isDefined && opts.fromCliDevice.exists(_.deviceType.isEmpty)) {
failure("device type is required")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package ru.maizy.ambient7.core.config.reader
* See LICENSE.txt for details.
*/

import ru.maizy.ambient7.core.config.{ Ambient7Options, Defaults, InfluxDbOptions, ParsingError }
import ru.maizy.ambient7.core.config.{ Ambient7Options, Defaults, InfluxDbOptions }


trait InfluxDbConfigReader extends UniversalConfigReader {
Expand All @@ -16,15 +16,7 @@ trait InfluxDbConfigReader extends UniversalConfigReader {
opts.copy(influxDb = Some(fill(opts.influxDb.getOrElse(InfluxDbOptions()))))
}

private def appendInfluxDbOptsCheck(check: InfluxDbOptions => Either[ParsingError, Unit]): Unit =
appendCheck { appOpts =>
appOpts.influxDb match {
case Some(influxDbOptions) => check(influxDbOptions)
case _ => failure("InfluxDB opts not defined")
}
}

def fillInfluxDbOptions(): Unit = {
def fillInfluxDbOptions(required: Boolean = true): Unit = {

cliParser.opt[String]("influxdb-baseurl")
.valueName { s"<${Defaults.INFLUXDB_BASEURL}>" }
Expand All @@ -34,55 +26,27 @@ trait InfluxDbConfigReader extends UniversalConfigReader {
influxDbOpts(opts)(_.copy(baseUrl = value))
}


cliParser.opt[String]("influxdb-database")
.action { (value, opts) => influxDbOpts(opts)(_.copy(database = Some(value))) }

appendSimpleOptionalConfigRule[String]("influxdb.database") { (value, opts) =>
influxDbOpts(opts)(_.copy(database = Some(value)))
}

appendInfluxDbOptsCheck { opts =>
if (opts.database.isDefined) {
success
} else {
failure("influxdb-database is required")
}
}


cliParser.opt[String]("influxdb-user")
.action { (value, opts) => influxDbOpts(opts)(_.copy(user = Some(value))) }

appendSimpleOptionalConfigRule[String]("influxdb.user") { (value, opts) =>
influxDbOpts(opts)(_.copy(user = Some(value)))
}

appendInfluxDbOptsCheck { opts =>
if (opts.user.isDefined) {
success
} else {
failure("influxdb-user is required")
}
}


cliParser.opt[String]("influxdb-password")
.action { (value, opts) => influxDbOpts(opts)(_.copy(password = Some(value))) }

appendSimpleOptionalConfigRule[String]("influxdb.password") { (value, opts) =>
influxDbOpts(opts)(_.copy(password = Some(value)))
}

appendInfluxDbOptsCheck { opts =>
if (opts.password.isDefined) {
success
} else {
failure("influxdb-password is required")
}
}


cliParser.opt[String]("influxdb-readonly-baseurl")
.action { (value, opts) => influxDbOpts(opts)(_.copy(readonlyBaseUrl = Some(value))) }
.text("By default --influxdb-baseurl")
Expand All @@ -91,15 +55,6 @@ trait InfluxDbConfigReader extends UniversalConfigReader {
influxDbOpts(opts)(_.copy(readonlyBaseUrl = Some(value)))
}

appendInfluxDbOptsCheck { opts =>
if (opts.readonlyBaseUrl.isDefined) {
success
} else {
failure("influxdb-readonly-baseurl is required")
}
}


cliParser.opt[String]("influxdb-readonly-user")
.action { (value, opts) => influxDbOpts(opts)(_.copy(readonlyUser = Some(value))) }
.text("By default --influxdb-user")
Expand All @@ -108,15 +63,6 @@ trait InfluxDbConfigReader extends UniversalConfigReader {
influxDbOpts(opts)(_.copy(readonlyUser = Some(value)))
}

appendInfluxDbOptsCheck { opts =>
if (opts.readonlyUser.isDefined) {
success
} else {
failure("influxdb-readonly-user is required")
}
}


cliParser.opt[String]("influxdb-readonly-password")
.action { (value, opts) => influxDbOpts(opts)(_.copy(readonlyPassword = Some(value))) }
.text("By default --influxdb-password")
Expand All @@ -125,12 +71,8 @@ trait InfluxDbConfigReader extends UniversalConfigReader {
influxDbOpts(opts)(_.copy(readonlyPassword = Some(value)))
}

appendInfluxDbOptsCheck { opts =>
if (opts.readonlyPassword.isDefined) {
success
} else {
failure("influxdb-readonly-password is required")
}
appendCheck { opts =>
checkInfluxDbOpts(opts, required)
}

appendPostprocessor { opts =>
Expand All @@ -146,4 +88,35 @@ trait InfluxDbConfigReader extends UniversalConfigReader {
}

}

def checkInfluxDbOpts(opts: Ambient7Options, required: Boolean): CheckResult = {
opts.influxDb match {
case None => failure("InfluxDb options are required")
case Some(influxDbOpts) =>
if (influxDbOpts.database.isDefined) {
val parts = List(
(influxDbOpts.user.isDefined, "influxdb-user is required"),
(influxDbOpts.password.isDefined, "influxdb-password is required"),
(influxDbOpts.readonlyBaseUrl.isDefined, "influxdb-readonly-baseurl is required"),
(influxDbOpts.readonlyUser.isDefined, "influxdb-readonly-user is required"),
(influxDbOpts.readonlyUser.isDefined, "influxdb-readonly-password is required")
)

val errors = for (
(check, error) <- parts
if !check
) yield error

if (errors.nonEmpty) {
failure(errors.toIndexedSeq)
} else {
success
}
} else if (!required) {
success
} else {
failure("influxdb-database is required")
}
}
}
}
13 changes: 12 additions & 1 deletion core/src/main/scala/ru/maizy/ambient7/core/data/AgentTags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ class AgentTags private(val tags: IndexedSeq[AgentTag]) {
def encoded: String = tags.map(_.encoded).mkString(",")
override def toString: String = s"AgentTags(${tags.mkString(",")})"
def asPairs: IndexedSeq[(String, String)] = tags.map(t => (t.name, t.value))

def canEqual(other: Any): Boolean = other.isInstanceOf[AgentTags]

override def equals(other: Any): Boolean = other match {
case that: AgentTags => (that canEqual this) && tags == that.tags
case _ => false
}

override def hashCode(): Int = {
tags.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b)
}
}

object AgentTags {
Expand Down Expand Up @@ -48,7 +59,7 @@ object AgentTags {
part <- parts
if part.count(_ == '=') == 1;
nameValue = part.split("=")
) yield AgentTag(nameValue(0), decodeTag(nameValue(1)))
) yield AgentTag(decodeTag(nameValue(0)), decodeTag(nameValue(1)))

if (parts.size != parsedTags.size) {
// TODO: more precise errors
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.maizy.ambient7.core.tests.data

/**
* Copyright (c) Nikita Kovaliov, maizy.ru, 2017
* See LICENSE.txt for details.
*/

import ru.maizy.ambient7.core.data.{ AgentTag, AgentTags }
import ru.maizy.ambient7.core.tests.BaseSpec

class AgentTagsSpec extends BaseSpec {

"AgentTags" should "encode & decode tags and tags values with escaping" in {
val originalString = "atag=va\\,lue,b\\,tag=value"
AgentTags(originalString) shouldBe
AgentTags(Seq(AgentTag("atag", "va,lue"), AgentTag("b,tag", "value")))

AgentTags(originalString).encoded shouldBe originalString
}
}
Loading

0 comments on commit ebdec8a

Please sign in to comment.