Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
37 changed files
with
465 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10
ambient7-analysis/src/main/scala/ru/maizy/ambient7/analysis/AnalysisConfigReader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
ambient7-analysis/src/main/scala/ru/maizy/ambient7/analysis/influxdb/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ru.maizy.ambient7.analysis | ||
|
||
/** | ||
* Copyright (c) Nikita Kovaliov, maizy.ru, 2017 | ||
* See LICENSE.txt for details. | ||
*/ | ||
|
||
import scala.concurrent.duration.DurationInt | ||
import ru.maizy.ambient7.core.config.Ambient7Options | ||
import ru.maizy.influxdbclient.InfluxDbClient | ||
|
||
|
||
package object influxdb { | ||
def buildClient(opts: Ambient7Options): Option[InfluxDbClient] = { | ||
( | ||
opts.influxDb.flatMap(_.clientConnectionSettings), | ||
opts.influxDb.flatMap(_.readonlyClientConnectionSetting) | ||
) match { | ||
case (Some(setting), Some(readonlySettings)) => | ||
Some( | ||
new InfluxDbClient( | ||
influxDbSettings = setting, | ||
_influxDbReadonlySettings = Some(readonlySettings), | ||
userAgent = Some("ambient7-analysis"), | ||
connectTimeout = 500.millis, | ||
readTimeout = 10.seconds | ||
) | ||
) | ||
case _ => None | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
ambient7-analysis/src/main/scala/ru/maizy/ambient7/analysis/service/AggregateType.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ru.maizy.ambient7.analysis.service | ||
|
||
/** | ||
* Copyright (c) Nikita Kovaliov, maizy.ru, 2017 | ||
* See LICENSE.txt for details. | ||
*/ | ||
|
||
sealed trait AggregateType { | ||
def code: String | ||
def apply(value: String): String | ||
} | ||
|
||
object MAX extends AggregateType { | ||
override def code: String = "max" | ||
override def apply(value: String): String = s"max($value)" | ||
} | ||
|
||
object MIN extends AggregateType { | ||
override def code: String = "min" | ||
override def apply(value: String): String = s"min($value)" | ||
} | ||
|
||
object MEAN extends AggregateType { | ||
override def code: String = "mean" | ||
override def apply(value: String): String = s"mean($value)" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
ambient7-analysis/src/main/scala/ru/maizy/ambient7/analysis/service/InfluxDbUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package ru.maizy.ambient7.analysis.service | ||
|
||
/** | ||
* Copyright (c) Nikita Kovaliov, maizy.ru, 2017 | ||
* See LICENSE.txt for details. | ||
*/ | ||
|
||
import java.time.ZonedDateTime | ||
import scala.concurrent.{ ExecutionContext, Future } | ||
import scala.concurrent.duration.Duration | ||
import scala.util.{ Failure, Success, Try } | ||
import ru.maizy.ambient7.core.util.DateTimeIterator | ||
import ru.maizy.influxdbclient.InfluxDbClient | ||
import ru.maizy.influxdbclient.data.{ NumberValue, StringValue, Value } | ||
import ru.maizy.influxdbclient.util.Escape.{ escapeIdentifier, escapeValue } | ||
import ru.maizy.influxdbclient.util.Dates | ||
|
||
private[service] object InfluxDbUtils { | ||
|
||
def getValuesForTimePeriod[T]( | ||
client: InfluxDbClient, | ||
from: ZonedDateTime, | ||
until: ZonedDateTime, | ||
table: String, | ||
valueColumn: String, | ||
valueExtractor: Value => Option[T], | ||
aggregate: AggregateType, | ||
segment: Duration, | ||
timeColumn: String = "time", | ||
condition: String = "true" | ||
)(implicit ex: ExecutionContext): Future[List[(ZonedDateTime, Option[T])]] = | ||
{ | ||
require(from.compareTo(until) < 0) | ||
require(segment.isFinite() && segment.toSeconds >= 1) | ||
|
||
val dateFrom = Dates.toInfluxDbFormat(from) | ||
val dateUntil = Dates.toInfluxDbFormat(until) | ||
|
||
val query = | ||
s"select ${aggregate(valueColumn)} as value " + | ||
s"from ${escapeIdentifier(table)} " + | ||
s"where ${escapeIdentifier(timeColumn)} >= ${escapeValue(dateFrom)} " + | ||
s"and ${escapeIdentifier(timeColumn)} < ${escapeValue(dateUntil)} " + | ||
s"and ($condition) " + | ||
s"group by time(${segment.toSeconds}s)" | ||
|
||
// TODO: little bit shitty code | ||
client.query(query) map { res => | ||
val timeToValue: Map[ZonedDateTime, Option[T]] = res.firstSeriesItems.map { seriesItem => | ||
(seriesItem.findColumnIndex(timeColumn), seriesItem.findColumnIndex("value")) match { | ||
case (Some(timeIndex), Some(valueIndex)) => | ||
seriesItem.values.zipWithIndex.map { case (row, rowNum) => | ||
val rawTime = row(timeIndex) | ||
rawTime match { | ||
case timeValue: StringValue => | ||
Dates.fromInfluxDbToZonedDateTime(timeValue.value) match { | ||
case Success(time) => | ||
row(valueIndex) match { | ||
case value: Value => (time, valueExtractor(value)) | ||
case _ => (time, None) | ||
} | ||
|
||
case Failure(e) => throw new Error(s"unparsed time $rawTime", e) | ||
} | ||
case _ => throw new Error(s"time not found in row #$rowNum") | ||
} | ||
}.toMap | ||
case _ => throw new Error(s"time & value columns not found in results") | ||
} | ||
}.getOrElse(Map.empty) | ||
|
||
DateTimeIterator(from, until, segment).toList.map { time => | ||
(time, timeToValue.getOrElse(time, None)) | ||
} | ||
} | ||
} | ||
|
||
val intExtractor: (Value => Option[Int]) = { | ||
case value: NumberValue => Try(value.value.toInt) match { | ||
case Success(i) => Some(i) | ||
case Failure(_) => None | ||
} | ||
case _ => None | ||
} | ||
|
||
val floatExtractor: (Value => Option[Float]) = { | ||
case value: NumberValue => Try(value.value.toFloat) match { | ||
case Success(i) => Some(i) | ||
case Failure(_) => None | ||
} | ||
case _ => None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
// sample production config | ||
db { | ||
url = "jdbc:h2:file:/var/ambient7/analysis;AUTO_SERVER=TRUE" | ||
user = "ambient7" | ||
password = "" | ||
url: "jdbc:h2:file:/var/ambient7/analysis;AUTO_SERVER=TRUE" | ||
user: "ambient7" | ||
password: "" | ||
} | ||
|
||
influxdb { | ||
database = "ambient7" | ||
database: "ambient7" | ||
|
||
baseurl = "http://localhost:8086/" | ||
user = "ambient7_rw" | ||
password = "" | ||
baseurl: "http://localhost:8086/" | ||
user: "ambient7_rw" | ||
password: "" | ||
|
||
// optional readonly access | ||
// readonly { | ||
// baseurl = "http://localhost:8086/" | ||
// user = "ambient7_ro" | ||
// password = "" | ||
// baseurl: "http://localhost:8086/" | ||
// user: "ambient7_ro" | ||
// password: "" | ||
// } | ||
|
||
} | ||
|
||
devices = [ | ||
{ | ||
id = "main" | ||
model = "mt8057" | ||
agent-name = "main" | ||
agent-tags = "" | ||
id: "main" | ||
model: "mt8057" | ||
agent-name: "main" | ||
agent-tags: "" | ||
} | ||
|
||
{ | ||
id = "additional-mt8057" | ||
model = "mt8057" | ||
agent-name = "additional" | ||
agent-tags = "place=livingroom,altitude=200" | ||
id: "additional-mt8057" | ||
model: "mt8057" | ||
agent-name: "additional" | ||
agent-tags: "place=livingroom,altitude=200" | ||
} | ||
] | ||
|
||
webapp { | ||
port = 22480 | ||
port: 22480 | ||
} |
Oops, something went wrong.