org.joda.time.LocalDateTime Scala Examples

The following examples show how to use org.joda.time.LocalDateTime. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: BinaryTransitionGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.composite

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.composite.TransitionTimeSeries
import org.joda.time.{Duration, LocalDateTime}
import spray.json.{JsObject, JsString, JsValue, _}


class BinaryTransitionGenerator(name: Option[String],
                          val first: Either[String, Generator[Any]],
                          val second: Either[String, Generator[Any]],
                          val time: LocalDateTime) extends Generator[Boolean](name, "binary-transition")
{
   override def timeseries(generators: (String) => Generator[Any]) =
   {
      val firstBase = Model.generator(generators)(first).timeseries(generators) match {
         case t: TimeSeries[Boolean] => t
      }

      val secondBase = Model.generator(generators)(second).timeseries(generators) match {
         case t: TimeSeries[Boolean] => t
      }

      TransitionTimeSeries[Boolean](firstBase, secondBase, time, None)
   }

   override def toString = "BinaryTransitionGenerator(" + name + "," + first + "," + second + "," + time + ")"

   override def equals(o: Any) = o match {
      case that: BinaryTransitionGenerator => that.name == this.name &&
         that.first == this.first &&
         that.second == this.second &&
         that.time == this.time
      case _ => false
   }

   override def toJson: JsValue = {
      val _first = (first match {
         case Left(s) => s.toJson
         case Right(g) => g.toJson
      }).toJson
      val _second = (second match {
         case Left(s) => s.toJson
         case Right(g) => g.toJson
      }).toJson

      var t = Map(
         "type" -> `type`.toJson,
         "first" -> _first,
         "second" -> _second,
         "time" -> time.toJson
      )

      if(name.isDefined)
         t = t.updated("name", name.get.toJson)

      new JsObject(t)
   }
}

object BinaryTransitionGenerator extends TimeToJson
{
   def apply(value: JsValue): BinaryTransitionGenerator = {
      val fields = value.asJsObject.fields

      val name = fields.get("name").map
      {
         case JsString(x) => x
      }

      val first = fields("first") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g))
      }

      val second = fields("second") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g))
      }

      val time = fields("time").convertTo[LocalDateTime]

      new BinaryTransitionGenerator(name, first, second, time)
   }
} 
Example 2
Source File: SinusGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.primary

import be.cetic.tsimulus.config.GeneratorFormat
import be.cetic.tsimulus.generators.primary.{ConstantGenerator, SinusGenerator}
import org.joda.time.LocalDateTime
import org.scalatest.{FlatSpec, Matchers}
import spray.json._

class SinusGeneratorTest extends FlatSpec with Matchers
{
   val source =
      """
        |{
        |   "name": "sinus-generator",
        |   "type": "sinus",
        |   "origin": "2020-06-07 01:02:03",
        |   "period": 1000
        |}
      """.stripMargin

   "A sinus generator" should "be correctly read from a json document" in {
      val generator = SinusGenerator(source.parseJson)

      generator.name shouldBe Some("sinus-generator")
      generator.`type` shouldBe "sinus"
      generator.origin shouldBe new LocalDateTime(2020, 6, 7, 1, 2, 3)
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe SinusGenerator(source.parseJson)
   }


   it should "be correctly exported to a json document" in {
      val generator = new SinusGenerator(Some("sinus-generator"), new LocalDateTime(2020, 6, 7, 1, 2, 3), 1000)
      generator shouldBe SinusGenerator(generator.toJson)
   }
} 
Example 3
Source File: GaussianNoiseGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.primary

import be.cetic.tsimulus.config.{ARMAModel, GeneratorFormat}
import be.cetic.tsimulus.generators.primary.{ARMAGenerator, GaussianNoiseGenerator}
import org.joda.time.{Duration, LocalDateTime}
import org.scalatest.{FlatSpec, Matchers}
import spray.json._

class GaussianNoiseGeneratorTest extends FlatSpec with Matchers
{
   val source = """
                  |{
                  |  "name": "generator",
                  |  "type": "gaussian",
                  |  "seed": 42,
                  |  "std": 0.5
                  |}
                """.stripMargin

   "An Gaussian Noise generator" should "be correctly read from a json document" in {
      val generator = GaussianNoiseGenerator(source.parseJson)

      generator.name shouldBe Some("generator")
      generator.`type` shouldBe "gaussian"
      generator.seed shouldBe 42
      generator.std should equal (0.5 +- 0.0001)
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe GaussianNoiseGenerator(source.parseJson)
   }


   it should "be correctly exported to a json document" in {
      val generator = new GaussianNoiseGenerator(
         Some("generator"),
         42,
         0.5F
      )
      generator shouldBe GaussianNoiseGenerator(generator.toJson)
   }

} 
Example 4
Source File: RTSTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test

import be.cetic.tsimulus.timeseries.binary.{FalseTimeSeries, TrueTimeSeries}
import be.cetic.tsimulus.timeseries.missing.UndefinedTimeSeries
import org.joda.time.LocalDateTime
import com.github.nscala_time.time.Imports._

trait RTSTest
{
   val t = new TrueTimeSeries()
   val f = new FalseTimeSeries()
   val u = new UndefinedTimeSeries()

   val dates = Seq(
      LocalDateTime.now(),
      LocalDateTime.now() + 5.seconds,
      LocalDateTime.now() + 10.seconds
   ).toStream

   val fixedDate = new LocalDateTime(2019, 3, 27, 10, 33, 17, 0)
} 
Example 5
Source File: RandomWalkTimeSeriesTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.timeseries.primary

import be.cetic.tsimulus.Utils
import be.cetic.tsimulus.test.RTSTest
import be.cetic.tsimulus.timeseries.primary.{ARMA, RandomWalkTimeSeries}
import org.joda.time.LocalDateTime
import org.scalatest.{FlatSpec, Inspectors, Matchers}
import com.github.nscala_time.time.Imports._


class RandomWalkTimeSeriesTest extends FlatSpec with Matchers with Inspectors with RTSTest
{
   "A random walk time series" should "provide identical results, by each moment at a time, or on a batch, when the origin is before the dates" in {
      val dates = Utils.sampling(new LocalDateTime(2016, 1, 1, 0, 0), new LocalDateTime(2016, 1, 2, 0, 0), 100)
      val ts = new RandomWalkTimeSeries(ARMA(Array(), Array(), 0.01, 0, 42), new LocalDateTime(2015, 12, 30, 0, 0), 1 minute)

      val individuals = dates.map(d => ts.compute(d).get)
      val batched = ts.compute(dates).map(_._2.get)

      individuals shouldBe batched
   }

   "A random walk time series" should "provide identical results, by each moment at a time, or on a batch, when the origin is among the dates" in {
      val dates = Utils.sampling(new LocalDateTime(2016, 1, 1, 0, 0), new LocalDateTime(2016, 1, 2, 0, 0), 100)
      val ts = new RandomWalkTimeSeries(ARMA(Array(), Array(), 0.01, 0, 42), new LocalDateTime(2016, 1, 1, 2, 0), 1 minute)

      val individuals = dates.map(d => ts.compute(d).get)
      val batched = ts.compute(dates).map(_._2.get)

      individuals shouldBe batched
   }

   "A random walk time series" should "provide identical results, by each moment at a time, or on a batch, when the origin is after the dates" in {
      val dates = Utils.sampling(new LocalDateTime(2016, 1, 1, 0, 0), new LocalDateTime(2016, 1, 2, 0, 0), 100)
      val ts = new RandomWalkTimeSeries(ARMA(Array(), Array(), 0.01, 0, 42), new LocalDateTime(2016, 2, 1, 0, 0), 1 minute)

      val individuals = dates.map(d => ts.compute(d).get)
      val batched = ts.compute(dates).map(_._2.get)

      individuals shouldBe batched
   }
} 
Example 6
Source File: SinusTimeSeriesTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.timeseries.primary

import be.cetic.tsimulus.Utils
import be.cetic.tsimulus.test.RTSTest
import be.cetic.tsimulus.timeseries.primary.SinusTimeSeries
import org.joda.time.LocalDateTime
import org.scalatest.{FlatSpec, Inspectors, Matchers}

class SinusSeriesTest extends FlatSpec with Matchers with Inspectors with RTSTest
{
   "A sinus series" should "provide a value close to 0 for its origin" in
   {
      val time = new LocalDateTime(2020, 6, 7, 1, 2, 3)
      val ts = SinusTimeSeries(time, 1000)
      val value = ts.compute(time).get

      value shouldBe (0.0 +- 0.0001)
   }

   it should "provide a value close to 0 for its origin + 1 period" in
   {
      val time = new LocalDateTime(2020, 6, 7, 1, 2, 3)
      val ts = SinusTimeSeries(time, 1000)
      val value = ts.compute(time.plusMillis(1000)).get

      value shouldBe (0.0 +- 0.0001)
   }

   it should "provide a value close to 0 for its origin - 1 period" in
   {
     val time = new LocalDateTime(2020, 6, 7, 1, 2, 3)
     val ts = SinusTimeSeries(time, 1000)
     val value = ts.compute(time.plusMillis(-1000)).get

     value shouldBe (0.0 +- 0.0001)
   }

   it should "provide a value close to 0 for its origin + half its period" in
   {
      val time = new LocalDateTime(2020, 6, 7, 1, 2, 3)
      val ts = SinusTimeSeries(time, 1000)
      val value = ts.compute(time.plusMillis(500)).get

      value shouldBe (0.0 +- 0.0001)
   }

   it should "provide a value close to 1 for its origin + 1/4 its period" in
   {
      val time = new LocalDateTime(2020, 6, 7, 1, 2, 3)
      val ts = SinusTimeSeries(time, 1000)
      val value = ts.compute(time.plusMillis(250)).get

      value shouldBe (1.0 +- 0.0001)
   }

   it should "provide a value close to 0 for its origin - half its period" in
   {
      val time = new LocalDateTime(2020, 6, 7, 1, 2, 3)
      val ts = SinusTimeSeries(time, 1000)
      val value = ts.compute(time.plusMillis(-500)).get

      value shouldBe (0.0 +- 0.0001)
   }

   it should "provide a value close to -1 for its origin - 1/4 its period" in
   {
      val time = new LocalDateTime(2020, 6, 7, 1, 2, 3)
      val ts = SinusTimeSeries(time, 1000)
      val value = ts.compute(time.plusMillis(-250)).get

      value shouldBe (-1.0 +- 0.0001)
   }
} 
Example 7
Source File: TimeToJson.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators

import com.github.nscala_time.time.Imports._
import org.joda.time.format.DateTimeFormatterBuilder
import org.joda.time.{Duration, LocalDateTime, LocalTime}
import spray.json.{JsString, JsValue, RootJsonFormat, _}


trait TimeToJson extends DefaultJsonProtocol
{
   val dtf = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS")
   val ttf = DateTimeFormat.forPattern("HH:mm:ss.SSS")

   val datetimeFormatter = {
      val parsers = Array(
         DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS").getParser,
         DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss").getParser
      )

      new DateTimeFormatterBuilder().append(null, parsers).toFormatter()
   }

   val timeFormatter = {
      val parsers = Array(
         DateTimeFormat.forPattern("HH:mm:ss.SSS").getParser,
         DateTimeFormat.forPattern("HH:mm:ss").getParser
      )

      new DateTimeFormatterBuilder().append(null, parsers).toFormatter()
   }

   implicit object LocalDateTimeJsonFormat extends RootJsonFormat[LocalDateTime] {
      def write(d: LocalDateTime) = JsString(dtf.print(d))
      def read(value: JsValue) = value match {
         case JsString(s) => datetimeFormatter.parseLocalDateTime(s)
         case unrecognized => serializationError(s"Serialization problem $unrecognized")
      }
   }

   implicit object LocalTimeJsonFormat extends RootJsonFormat[LocalTime] {
      def write(t: LocalTime) = JsString(ttf.print(t))
      def read(value: JsValue) = value match {
         case JsString(s) => timeFormatter.parseLocalTime(s)
         case unknown => deserializationError(s"unknown LocalTime object: $unknown")
      }
   }

   implicit object DurationFormat extends RootJsonFormat[Duration] {
      def write(d: Duration) = d.getMillis.toJson
      def read(value: JsValue) = new Duration(value.toString.toLong)
   }

   def either2json(element: Either[String,Generator[Any]]) = element match {
      case Left(s) => s.toJson
      case Right(g) => g.toJson
   }
} 
Example 8
Source File: LimitedGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.missing

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.missing.LimitedTimeSeries
import org.joda.time.LocalDateTime
import spray.json.{JsObject, JsString, JsValue, _}


class LimitedGenerator(name: Option[String],
                       val generator: Either[String, Generator[Any]],
                       val from: Option[LocalDateTime],
                       val to: Option[LocalDateTime]) extends Generator[Any](name, "limited")
{
   override def timeseries(generators: (String) => Generator[Any]) =
      LimitedTimeSeries(Model.generator(generators)(generator).timeseries(generators), from, to)

   override def toString = "Limited(" + name + ", " + generator + ", " + from + ", " + to + ")"

   override def equals(o: Any) = o match {
      case that: LimitedGenerator => that.name == this.name &&
         that.generator == this.generator &&
         that.from == this.from &&
         that.to == this.to
      case _ => false
   }

   override def toJson: JsValue =
   {
      val t = Map(
         "type" -> `type`.toJson,
         "generator" -> either2json(generator),
         "from" -> from.get.toJson,
         "to" -> to.get.toJson
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object LimitedGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(value: JsValue): LimitedGenerator = {
      val fields = value.asJsObject.fields

      val name = fields.get("name").map
      {
         case JsString(x) => x
      }

      val generator = fields("generator") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g))
      }
      val from = fields.get("from").map(_.convertTo[LocalDateTime])
      val to = fields.get("to").map(_.convertTo[LocalDateTime])
      val missingRate = fields.get("missing-rate").map(_.convertTo[Double])

      new LimitedGenerator(name, generator, from, to)
   }
} 
Example 9
Source File: PartialGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.missing

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.missing.PartialTimeSeries
import org.joda.time.LocalDateTime
import spray.json.{JsObject, JsString, JsValue, _}


class PartialGenerator(name: Option[String],
                       val generator: Either[String, Generator[Any]],
                       val from: Option[LocalDateTime],
                       val to: Option[LocalDateTime],
                       val missingRate: Option[Double]) extends Generator[Any](name, "partial")
{
   override def timeseries(generators: (String) => Generator[Any]) =
   {
      val ts = Model.generator(generators)(generator).timeseries(generators)
      PartialTimeSeries(ts, from, to, missingRate)
   }

   override def toString = "Partial(" + name + ", " + generator + ", " + from + ", " + to + ", " + missingRate + ")"

   override def equals(o: Any) = o match {
      case that: PartialGenerator => that.name == this.name &&
         that.generator == this.generator &&
         that.from == this.from &&
         that.to == this.to &&
         that.missingRate == this.missingRate
      case _ => false
   }

   override def toJson: JsValue =
   {
     var t = Map(
         "type" -> `type`.toJson,
         "generator" -> either2json(generator),
         "from" -> from.toJson,
         "to" -> to.toJson
      )

      if(missingRate.isDefined) t = t.updated("missing-rate" , missingRate.toJson)
      if(name.isDefined) t = t.updated("name", name.toJson)

      new JsObject(t)
   }
}

object PartialGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(value: JsValue): PartialGenerator = {
      val fields = value.asJsObject.fields

      val name = fields.get("name").map
      {
         case JsString(x) => x
      }

      val generator = fields("generator") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g))
      }
      val from = fields.get("from").map(_.convertTo[LocalDateTime])
      val to = fields.get("to").map(_.convertTo[LocalDateTime])
      val missingRate = fields.get("missing-rate").map(_.convertTo[Double])

      new PartialGenerator(name, generator, from, to, missingRate)
   }
} 
Example 10
Source File: ARMAGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.primary

import be.cetic.tsimulus.config.{ARMAModel, GeneratorFormat}
import org.joda.time.{Duration, LocalDateTime}
import org.scalatest.{FlatSpec, Matchers}
import spray.json._
import be.cetic.tsimulus.generators.primary.ARMAGenerator

class ARMAGeneratorTest extends FlatSpec with Matchers
{
   val source = """
                      |{
                      |  "name": "g3",
                      |  "type": "arma",
                      |  "model": {
                      |      "phi": [1,2,3],
                      |      "theta": [4,3,2,1],
                      |      "std": 0.5,
                      |      "c": 4.2,
                      |      "seed": 1809
                      |   },
                      |   "timestep": 180000,
                      |   "origin": "2016-01-01 12:34:56.789"
                      |}
                    """.stripMargin

   "An ARMA generator" should "be correctly read from a json document" in {
      val generator = ARMAGenerator(source.parseJson)

      generator.name shouldBe Some("g3")
      generator.`type` shouldBe "arma"
      generator.timestep shouldBe new Duration(180000)
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe ARMAGenerator(source.parseJson)
   }


   it should "be correctly exported to a json document" in {
      val generator = new ARMAGenerator(
         Some("g3"),
         ARMAModel(
            Some(Seq(1, 2, 3)),
            Some(Seq(4, 3, 2, 1)),
            0.5,
            4.2,
            Some(1809)
         ),
         new LocalDateTime(2016, 1, 2, 12, 34, 56, 789),
         new Duration(180000)
      )
      generator shouldBe ARMAGenerator(generator.toJson)
   }
} 
Example 11
Source File: NowGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.primary

import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.primary.NowTimeSeries
import org.joda.time.LocalDateTime
import spray.json._


class NowGenerator(name: Option[String]) extends Generator[LocalDateTime](name, "now")
{
   override def timeseries(generators: String => Generator[Any]) = NowTimeSeries()

   override def toString = "NowGenerator()"

   override def equals(o: Any) = o match {
      case that: NowGenerator => that.name == this.name
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object NowGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String]) = new NowGenerator(name)
   def apply(name: String) = new NowGenerator(Some(name))
   def apply() = new NowGenerator(None)

   def apply(json: JsValue): NowGenerator = {

      val fields = json.asJsObject.fields
      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      new NowGenerator(name)
   }
} 
Example 12
Source File: SinusGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.primary

import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.primary.SinusTimeSeries
import org.joda.time.LocalDateTime
import spray.json._



   override def timeseries(generators: String => Generator[Any]): TimeSeries[Double] =
      SinusTimeSeries(origin, period)

   override def equals(o: Any) = o match {
      case that: SinusGenerator => that.name == this.name
      case _ => false
   }

   override def toJson: JsValue =
   {
      val t = Map(
         "type" -> `type`.toJson,
         "origin" -> origin.toJson,
         "period" -> period.toJson
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object SinusGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], origin: LocalDateTime, period: Long) = new SinusGenerator(name, origin, period)
   def apply(name: String, origin: LocalDateTime, period: Long) = new SinusGenerator(Some(name), origin, period)
   def apply(origin: LocalDateTime, period: Long) = new SinusGenerator(None, origin, period)

   def apply(json: JsValue): SinusGenerator =
   {
      val fields = json.asJsObject.fields

      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val origin: LocalDateTime = fields.get("origin").get.convertTo[LocalDateTime]
      val period: Long = fields.get("period").get.convertTo[Long]

      new SinusGenerator(name, origin, period)
   }
} 
Example 13
Source File: MinuteGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.dt

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.dt.{MillisecondTimeSeries, MinuteTimeSeries}
import org.joda.time.LocalDateTime
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _}


class MinuteGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "minute")
{
   override def timeseries(generators: String => Generator[Any]) = {
      val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      new MinuteTimeSeries(ts)
   }

   override def toString = "MinuteGenerator()"

   override def equals(o: Any) = o match {
      case that: MinuteGenerator => (that.name == this.name) && (that.base == this.base)
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson,
         "base" -> either2json(base)
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object MinuteGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], base: String) = new MinuteGenerator(name, Left(base))
   def apply(name: Option[String], base: Generator[LocalDateTime]) = new MinuteGenerator(name, Right(base))

   def apply(json: JsValue): MinuteGenerator = {

      val fields = json.asJsObject.fields
      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val base = fields("base") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      new MinuteGenerator(name, base)
   }
} 
Example 14
Source File: MillisecondTimeGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.dt

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.dt.MillisecondTimeSeries
import org.joda.time.LocalDateTime
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _}


class MillisecondTimeGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "ms")
{
   override def timeseries(generators: String => Generator[Any]) = {
      val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      new MillisecondTimeSeries(ts)
   }

   override def toString = "MillisecondTimeGenerator()"

   override def equals(o: Any) = o match {
      case that: MillisecondTimeGenerator => (that.name == this.name) && (that.base == this.base)
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson,
         "base" -> either2json(base)
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object MillisecondTimeGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], base: String) = new MillisecondTimeGenerator(name, Left(base))
   def apply(name: Option[String], base: Generator[LocalDateTime]) = new MillisecondTimeGenerator(name, Right(base))

   def apply(json: JsValue): MillisecondTimeGenerator = {

      val fields = json.asJsObject.fields
      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val base = fields("base") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      new MillisecondTimeGenerator(name, base)
   }
} 
Example 15
Source File: DayOfYearGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.dt

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.dt.{DayOfWeekTimeSeries, DayOfYearTimeSeries}
import org.joda.time.LocalDateTime
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _}


class DayOfYearGenerator(name: Option[String], base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "doy")
{
   override def timeseries(generators: String => Generator[Any]) = {
      val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      new DayOfYearTimeSeries(ts)
   }

   override def toString = "DayOfYearGenerator()"

   override def equals(o: Any) = o match {
      case that: DayOfYearGenerator => that.name == this.name
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson,
         "base" -> either2json(base)
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object DayOfYearGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], base: String) = new DayOfYearGenerator(name, Left(base))
   def apply(name: Option[String], base: Generator[LocalDateTime]) = new DayOfYearGenerator(name, Right(base))

   def apply(json: JsValue): DayOfYearGenerator = {

      val fields = json.asJsObject.fields
      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val base = fields("base") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      new DayOfYearGenerator(name, base)
   }
} 
Example 16
Source File: YearGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.dt

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.dt.YearTimeSeries
import org.joda.time.{Duration, LocalDateTime}
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue}
import spray.json._


class YearGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "year")
{
   override def timeseries(generators: String => Generator[Any]) = {
      val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      new YearTimeSeries(ts)
   }

   override def toString = "YearGenerator()"

   override def equals(o: Any) = o match {
      case that: YearGenerator => (that.name == this.name) && (that.base == this.base)
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson,
         "base" -> either2json(base)
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object YearGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], base: String) = new YearGenerator(name, Left(base))
   def apply(name: Option[String], base: Generator[LocalDateTime]) = new YearGenerator(name, Right(base))

   def apply(json: JsValue): YearGenerator = {

      val fields = json.asJsObject.fields

      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val base = fields("base") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      new YearGenerator(name, base)
   }
} 
Example 17
Source File: WeekGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.dt

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.dt.{MinuteTimeSeries, WeekTimeSeries}
import org.joda.time.LocalDateTime
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _}


class WeekGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "week")
{
   override def timeseries(generators: String => Generator[Any]) = {
      val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      new WeekTimeSeries(ts)
   }

   override def toString = "WeekGenerator()"

   override def equals(o: Any) = o match {
      case that: WeekGenerator => (that.name == this.name) && (that.base == this.base)
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson,
         "base" -> either2json(base)
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object WeekGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], base: String) = new WeekGenerator(name, Left(base))
   def apply(name: Option[String], base: Generator[LocalDateTime]) = new WeekGenerator(name, Right(base))

   def apply(json: JsValue): WeekGenerator = {

      val fields = json.asJsObject.fields

      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val base = fields("base") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      new WeekGenerator(name, base)
   }
} 
Example 18
Source File: UndefinedGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.missing

import be.cetic.tsimulus.config.GeneratorFormat
import be.cetic.tsimulus.generators.missing.{LimitedGenerator, PartialGenerator, UndefinedGenerator}
import org.joda.time.LocalDateTime
import org.scalatest.{FlatSpec, Inspectors, Matchers}
import spray.json._


class UndefinedGeneratorTest extends FlatSpec with Matchers with Inspectors
{
   val source =
      """
        |{
        |  "name": "undefined-generator",
        |  "type": "undefined"
        |}
      """.stripMargin

   "An Undefined generator" should "be correctly read from a json document" in {
      val generator = UndefinedGenerator(source.parseJson)

      generator.name shouldBe Some("undefined-generator")
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe UndefinedGenerator(source.parseJson)
   }

   it should "be correctly exported to a json document" in {
      val generator = new UndefinedGenerator(
         Some("undefined-generator")
      )
      generator shouldBe UndefinedGenerator(generator.toJson)
   }
} 
Example 19
Source File: JodaSerializerTest.scala    From scio   with Apache License 2.0 5 votes vote down vote up
package com.spotify.scio.coders.instances.kryo

import com.spotify.scio.coders.{CoderTestUtils, KryoAtomicCoder, KryoOptions}
import org.joda.time.{DateTime, DateTimeZone, LocalDate, LocalDateTime, LocalTime}
import org.scalacheck._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatestplus.scalacheck.Checkers

import scala.jdk.CollectionConverters._
import scala.util.Try

class JodaSerializerTest extends AnyFlatSpec with Checkers {
  // TODO: remove this once https://github.com/scalatest/scalatest/issues/1090 is addressed
  implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
    PropertyCheckConfiguration(minSuccessful = 100)

  implicit val dateTimeArb = Arbitrary {
    for {
      year <- Gen.choose(-292275054, 292278993)
      month <- Gen.choose(1, 12)
      maxDayOfMonth <- Try {
        Gen.const(new LocalDateTime(year, month, 1, 0, 0).dayOfMonth().getMaximumValue)
      }.getOrElse(Gen.fail)
      day <- Gen.choose(1, maxDayOfMonth)
      hour <- Gen.choose(0, 23)
      minute <- Gen.choose(0, 59)
      second <- Gen.choose(0, 59)
      ms <- Gen.choose(0, 999)
      tz <- Gen.oneOf(DateTimeZone.getAvailableIDs.asScala.toSeq)
      attempt <- Try {
        val ldt = new DateTime(year, month, day, hour, minute, second, ms, DateTimeZone.forID(tz))
        Gen.const(ldt)
      }.getOrElse(Gen.fail)
    } yield attempt
  }

  implicit val localDateTimeArb = Arbitrary {
    Arbitrary.arbitrary[DateTime].map(_.toLocalDateTime)
  }

  implicit val localTimeArb = Arbitrary {
    Arbitrary.arbitrary[LocalDateTime].map(_.toLocalTime)
  }

  implicit val localDateArb = Arbitrary {
    Arbitrary.arbitrary[LocalDateTime].map(_.toLocalDate)
  }

  val coder = new KryoAtomicCoder[Any](KryoOptions())

  def roundTripProp[T](value: T): Prop = Prop.secure {
    CoderTestUtils.testRoundTrip(coder, value)
  }

  "KryoAtomicCoder" should "roundtrip LocalDate" in {
    check(roundTripProp[LocalDate] _)
  }

  it should "roundtrip LocalTime" in {
    check(roundTripProp[LocalTime] _)
  }

  it should "roundtrip LocalDateTime" in {
    check(roundTripProp[LocalDateTime] _)
  }

  it should "roundtrip DateTime" in {
    check(roundTripProp[DateTime] _)
  }
} 
Example 20
Source File: TypedBigQueryIT.scala    From scio   with Apache License 2.0 5 votes vote down vote up
package com.spotify.scio.bigquery

import com.google.protobuf.ByteString
import com.spotify.scio._
import com.spotify.scio.bigquery.client.BigQuery
import com.spotify.scio.testing._
import magnolify.scalacheck.auto._
import org.apache.beam.sdk.options.PipelineOptionsFactory
import org.joda.time.format.DateTimeFormat
import org.joda.time.{Instant, LocalDate, LocalDateTime, LocalTime}
import org.scalacheck._
import org.scalatest.BeforeAndAfterAll

import scala.util.Random

object TypedBigQueryIT {
  @BigQueryType.toTable
  case class Record(
    bool: Boolean,
    int: Int,
    long: Long,
    float: Float,
    double: Double,
    string: String,
    byteString: ByteString,
    timestamp: Instant,
    date: LocalDate,
    time: LocalTime,
    datetime: LocalDateTime
  )

  // Workaround for millis rounding error
  val epochGen = Gen.chooseNum[Long](0L, 1000000000000L).map(x => x / 1000 * 1000)
  implicit val arbByteString = Arbitrary(Gen.alphaStr.map(ByteString.copyFromUtf8))
  implicit val arbInstant = Arbitrary(epochGen.map(new Instant(_)))
  implicit val arbDate = Arbitrary(epochGen.map(new LocalDate(_)))
  implicit val arbTime = Arbitrary(epochGen.map(new LocalTime(_)))
  implicit val arbDatetime = Arbitrary(epochGen.map(new LocalDateTime(_)))

  private val recordGen = {
    implicitly[Arbitrary[Record]].arbitrary
  }

  private val table = {
    val TIME_FORMATTER = DateTimeFormat.forPattern("yyyyMMddHHmmss")
    val now = Instant.now().toString(TIME_FORMATTER)
    val spec =
      "data-integration-test:bigquery_avro_it.records_" + now + "_" + Random.nextInt(Int.MaxValue)
    Table.Spec(spec)
  }
  private val records = Gen.listOfN(1000, recordGen).sample.get
  private val options = PipelineOptionsFactory
    .fromArgs(
      "--project=data-integration-test",
      "--tempLocation=gs://data-integration-test-eu/temp"
    )
    .create()
}

class TypedBigQueryIT extends PipelineSpec with BeforeAndAfterAll {
  import TypedBigQueryIT._

  override protected def beforeAll(): Unit = {
    val sc = ScioContext(options)
    sc.parallelize(records).saveAsTypedBigQueryTable(table)

    sc.run()
    ()
  }

  override protected def afterAll(): Unit =
    BigQuery.defaultInstance().tables.delete(table.ref)

  "TypedBigQuery" should "read records" in {
    val sc = ScioContext(options)
    sc.typedBigQuery[Record](table) should containInAnyOrder(records)
    sc.run()
  }
} 
Example 21
Source File: JodaSerializer.scala    From scio   with Apache License 2.0 5 votes vote down vote up
package com.spotify.scio.coders.instances.kryo

import com.esotericsoftware.kryo.io.{Input, Output}
import com.esotericsoftware.kryo.{Kryo, Serializer}
import org.joda.time.{DateTime, DateTimeZone, LocalDate, LocalDateTime, LocalTime}
import org.joda.time.chrono.ISOChronology

private[coders] class JodaLocalDateTimeSerializer extends Serializer[LocalDateTime] {
  setImmutable(true)

  def write(kryo: Kryo, output: Output, ldt: LocalDateTime): Unit = {
    output.writeInt(ldt.getYear,  false)
    val month = input.readByte().toInt
    val day = input.readByte().toInt

    new LocalDate(year, month, day)
  }
}

private[coders] class JodaDateTimeSerializer extends Serializer[DateTime] {
  setImmutable(true)

  def write(kryo: Kryo, output: Output, dt: DateTime): Unit = {
    output.writeLong(dt.getMillis)
    output.writeString(dt.getZone.getID)
  }

  def read(kryo: Kryo, input: Input, tpe: Class[DateTime]): DateTime = {
    val millis = input.readLong()
    val zone = DateTimeZone.forID(input.readString())
    new DateTime(millis, zone)
  }
} 
Example 22
Source File: Schemas.scala    From scio   with Apache License 2.0 5 votes vote down vote up
package com.spotify.scio.bigquery.types

import com.google.protobuf.ByteString
import org.joda.time.{Instant, LocalDate, LocalDateTime, LocalTime}

object Schemas {
  // primitives
  case class Required(
    boolF: Boolean,
    intF: Int,
    longF: Long,
    floatF: Float,
    doubleF: Double,
    stringF: String,
    byteArrayF: Array[Byte],
    byteStringF: ByteString,
    timestampF: Instant,
    dateF: LocalDate,
    timeF: LocalTime,
    datetimeF: LocalDateTime,
    bigDecimalF: BigDecimal,
    geographyF: Geography
  )
  case class Optional(
    boolF: Option[Boolean],
    intF: Option[Int],
    longF: Option[Long],
    floatF: Option[Float],
    doubleF: Option[Double],
    stringF: Option[String],
    byteArrayF: Option[Array[Byte]],
    byteStringF: Option[ByteString],
    timestampF: Option[Instant],
    dateF: Option[LocalDate],
    timeF: Option[LocalTime],
    datetimeF: Option[LocalDateTime],
    bigDecimalF: Option[BigDecimal],
    geographyF: Option[Geography]
  )
  case class Repeated(
    boolF: List[Boolean],
    intF: List[Int],
    longF: List[Long],
    floatF: List[Float],
    doubleF: List[Double],
    stringF: List[String],
    byteArrayF: List[Array[Byte]],
    byteStringF: List[ByteString],
    timestampF: List[Instant],
    dateF: List[LocalDate],
    timeF: List[LocalTime],
    datetimeF: List[LocalDateTime],
    bigDecimalF: List[BigDecimal],
    geographyF: List[Geography]
  )

  // records
  case class RequiredNested(required: Required, optional: Optional, repeated: Repeated)
  case class OptionalNested(
    required: Option[Required],
    optional: Option[Optional],
    repeated: Option[Repeated]
  )
  case class RepeatedNested(
    required: List[Required],
    optional: List[Optional],
    repeated: List[Repeated]
  )

  case class User(@description("user name") name: String, @description("user age") age: Int)
  case class Account(
    @description("account user") user: User,
    @description("in USD") balance: Double
  )
} 
Example 23
Source File: TableRowSyntax.scala    From scio   with Apache License 2.0 5 votes vote down vote up
package com.spotify.scio.bigquery.syntax

// import com.google.api.services.bigquery.model.{TableRow => GTableRow}
import com.spotify.scio.bigquery.{Date, DateTime, TableRow, Time, Timestamp}
import org.joda.time.{Instant, LocalDate, LocalDateTime, LocalTime}

import scala.jdk.CollectionConverters._

import scala.util.Try


final class TableRowOps(private val r: TableRow) extends AnyVal {
  def getBoolean(name: AnyRef): Boolean =
    this.getValue(name, _.toString.toBoolean, false)

  def getBooleanOpt(name: AnyRef): Option[Boolean] =
    this.getValueOpt(name, _.toString.toBoolean)

  def getLong(name: AnyRef): Long = this.getValue(name, _.toString.toLong, 0L)

  def getLongOpt(name: AnyRef): Option[Long] =
    this.getValueOpt(name, _.toString.toLong)

  def getDouble(name: AnyRef): Double =
    this.getValue(name, _.toString.toDouble, 0.0)

  def getDoubleOpt(name: AnyRef): Option[Double] =
    this.getValueOpt(name, _.toString.toDouble)

  def getString(name: AnyRef): String = this.getValue(name, _.toString, null)

  def getStringOpt(name: AnyRef): Option[String] =
    this.getValueOpt(name, _.toString)

  def getTimestamp(name: AnyRef): Instant =
    this.getValue(name, v => Timestamp.parse(v.toString), null)

  def getTimestampOpt(name: AnyRef): Option[Instant] =
    this.getValueOpt(name, v => Timestamp.parse(v.toString))

  def getDate(name: AnyRef): LocalDate =
    this.getValue(name, v => Date.parse(v.toString), null)

  def getDateOpt(name: AnyRef): Option[LocalDate] =
    this.getValueOpt(name, v => Date.parse(v.toString))

  def getTime(name: AnyRef): LocalTime =
    this.getValue(name, v => Time.parse(v.toString), null)

  def getTimeOpt(name: AnyRef): Option[LocalTime] =
    this.getValueOpt(name, v => Time.parse(v.toString))

  def getDateTime(name: AnyRef): LocalDateTime =
    this.getValue(name, v => DateTime.parse(v.toString), null)

  def getDateTimeOpt(name: AnyRef): Option[LocalDateTime] =
    this.getValueOpt(name, v => DateTime.parse(v.toString))

  def getRepeated(name: AnyRef): Seq[AnyRef] =
    this.getValue(name, x => x.asInstanceOf[java.util.List[AnyRef]].iterator().asScala.toSeq, null)

  def getRecord(name: AnyRef): Map[String, AnyRef] =
    r.get(name).asInstanceOf[java.util.Map[String, AnyRef]].asScala.toMap

  private def getValue[T](name: AnyRef, fn: AnyRef => T, default: T): T = {
    val o = r.get(name)
    if (o == null) {
      default
    } else {
      fn(o)
    }
  }

  private def getValueOpt[T](name: AnyRef, fn: AnyRef => T): Option[T] = {
    val o = r.get(name)
    if (o == null) {
      None
    } else {
      Try(fn(o)).toOption
    }
  }
}

trait TableRowSyntax {
  implicit def bigQueryTableRowOps(tr: TableRow): TableRowOps = new TableRowOps(tr)
} 
Example 24
Source File: ImplicitJodaTimeGenerators.scala    From scalacheck-ops   with Apache License 2.0 5 votes vote down vote up
package org.scalacheck.ops.time.joda

import org.joda.time.chrono._
import org.joda.time.{Chronology, DateTime, DateTimeZone, LocalDateTime}
import org.scalacheck.Arbitrary
import org.scalacheck.Gen._
import org.scalacheck.ops.time.joda.ChronologyOps._

import scala.collection.JavaConverters._

trait ImplicitJodaTimeGenerators {

  implicit val arbDateTimeZone: Arbitrary[DateTimeZone] = {
    val ids = DateTimeZone.getAvailableIDs.asScala.toSeq
    val zones = ids map DateTimeZone.forID
    Arbitrary(oneOf(zones))
  }

  implicit def arbChronology(implicit arbZone: Arbitrary[DateTimeZone]): Arbitrary[Chronology] = {
    val chronologyBuilders: Seq[DateTimeZone => Chronology] = Seq(
      BuddhistChronology.getInstance,
      CopticChronology.getInstance,
      EthiopicChronology.getInstance,
      GregorianChronology.getInstance,
      IslamicChronology.getInstance,
      ISOChronology.getInstance,
      JulianChronology.getInstance
    )
    Arbitrary {
      for {
        build <- oneOf(chronologyBuilders)
        zone <- arbZone.arbitrary
      } yield build(zone)
    }
  }

  implicit def arbDateTime(implicit params: JodaTimeParams = JodaDateTimeGenerators.defaultParams): Arbitrary[DateTime] = {
    val maxMillis = params.chronology.maxMillis
    Arbitrary(chooseNum(0, maxMillis).flatMap(new DateTime(_, params.chronology)))
  }

  implicit def arbLocalDateTime(implicit params: JodaTimeParams = JodaLocalDateTimeGenerators.defaultParams): Arbitrary[LocalDateTime] = {
    val maxMillis = params.chronology.maxMillis
    Arbitrary(chooseNum(0, maxMillis).flatMap(new LocalDateTime(_, params.chronology)))
  }

} 
Example 25
Source File: JodaLocalDateTimeGenerators.scala    From scalacheck-ops   with Apache License 2.0 5 votes vote down vote up
package org.scalacheck.ops.time.joda

import org.joda.time.{LocalDateTime, ReadableDuration}
import org.scalacheck.ops.time.joda.ChronologyOps._

sealed trait JodaLocalDateTimeGenerators extends JodaAbstractDateTimeGenerators
  with UTCTimeZoneDefault
  with ISOChronologyDefault {
  override type InstantType = LocalDateTime

  override protected[time] def asInstant(millis: Long)(implicit params: JodaTimeParams): LocalDateTime =
    new LocalDateTime(millis, params.chronology)

  override protected[time] def addToCeil(instant: LocalDateTime, duration: ReadableDuration)
    (implicit params: JodaTimeParams): LocalDateTime = {
    try instant plus duration
    catch {
      case tooLarge: ArithmeticException => params.chronology.maxLocalDateTime
    }
  }

  override protected[time] def subtractToFloor(instant: LocalDateTime, duration: ReadableDuration)
    (implicit params: JodaTimeParams): LocalDateTime = {
    try instant minus duration
    catch {
      case tooSmall: ArithmeticException => params.chronology.minLocalDateTime
    }
  }

  override protected[time] def asLong(instant: LocalDateTime)(implicit params: JodaTimeParams): Long = {
    instant.toDateTime(params.dateTimeZone).getMillis
  }
}

object JodaLocalDateTimeGenerators extends JodaLocalDateTimeGenerators 
Example 26
Source File: LimitedGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.missing

import be.cetic.tsimulus.config.GeneratorFormat
import be.cetic.tsimulus.generators.missing.LimitedGenerator
import org.joda.time.LocalDateTime
import org.scalatest.{FlatSpec, Inspectors, Matchers}
import spray.json._


class LimitedGeneratorTest extends FlatSpec with Matchers with Inspectors
{
   val source =
      """
        |{
        |  "name": "limited-generator",
        |  "type": "limited",
        |  "generator": "daily-generator",
        |  "from": "2016-01-01 00:00:00.000",
        |  "to": "2016-04-23 01:23:45.678"
        |}
        |
      """.stripMargin

   "A Limited generator" should "be correctly read from a json document" in {
      val generator = LimitedGenerator(source.parseJson)

      generator.name shouldBe Some("limited-generator")
      generator.generator shouldBe Left("daily-generator")
      generator.from shouldBe Some(new LocalDateTime(2016, 1, 1, 0, 0, 0))
      generator.to shouldBe Some(new LocalDateTime(2016, 4, 23, 1, 23, 45, 678))
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe LimitedGenerator(source.parseJson)
   }

   it should "be correctly exported to a json document" in {
      val generator = new LimitedGenerator(
         Some("limited-generator"),
         Left("daily-generator"),
         Some(new LocalDateTime(2016, 1, 1, 0, 0, 0)),
         Some(new LocalDateTime(2016, 4, 23, 1, 23, 45, 678))
      )
      generator shouldBe LimitedGenerator(generator.toJson)
   }
} 
Example 27
Source File: DayOfMonthGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.dt

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.composite.TimeShiftTimeSeries
import be.cetic.tsimulus.timeseries.dt.{DayOfMonthTimeSeries, MonthTimeSeries}
import org.joda.time.LocalDateTime
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _}


class DayOfMonthGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "dom")
{
   override def timeseries(generators: String => Generator[Any]) = {
      val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      new DayOfMonthTimeSeries(ts)
   }

   override def toString = s"DayOfMonthGenerator(${base})"

   override def equals(o: Any) = o match {
      case that: DayOfMonthGenerator => that.name == this.name && this.base == that.base
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson,
         "base" -> either2json(base)
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object DayOfMonthGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], base: String) = new DayOfMonthGenerator(name, Left(base))
   def apply(name: Option[String], base: Generator[LocalDateTime]) = new DayOfMonthGenerator(name, Right(base))

   def apply(json: JsValue): DayOfMonthGenerator = {

      val fields = json.asJsObject.fields
      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val base = fields("base") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      new DayOfMonthGenerator(name, base)
   }
} 
Example 28
Source File: PartialGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.missing

import be.cetic.tsimulus.config.GeneratorFormat
import be.cetic.tsimulus.generators.missing.PartialGenerator
import org.joda.time.LocalDateTime
import org.scalatest.{FlatSpec, Inspectors, Matchers}
import spray.json._


class PartialGeneratorTest extends FlatSpec with Matchers with Inspectors
{
   val source =
      """
        |{
        |  "name": "partial-generator",
        |  "type": "partial",
        |  "generator": "daily-generator",
        |  "from": "2016-01-01 00:00:00.000",
        |  "to": "2016-04-23 01:23:45.678",
        |  "missing-rate": 0.001
        |}
      """.stripMargin

   "A Partial generator" should "be correctly read from a json document" in {
      val generator = PartialGenerator(source.parseJson)

      generator.name shouldBe Some("partial-generator")
      generator.generator shouldBe Left("daily-generator")
      generator.from shouldBe Some(new LocalDateTime(2016, 1, 1, 0, 0, 0))
      generator.to shouldBe Some(new LocalDateTime(2016, 4, 23, 1, 23, 45, 678))
      generator.missingRate shouldBe Some(0.001)
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe PartialGenerator(source.parseJson)
   }

   it should "be correctly exported to a json document" in {
      val generator = new PartialGenerator(
         Some("limited-generator"),
         Left("daily-generator"),
         Some(new LocalDateTime(2016, 1, 1, 0, 0, 0)),
         Some(new LocalDateTime(2016, 4, 23, 1, 23, 45, 678)),
         Some(0.001)
      )
      generator shouldBe PartialGenerator(generator.toJson)
   }
} 
Example 29
Source File: LimitedGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.composite

import be.cetic.tsimulus.config.GeneratorFormat
import org.joda.time.LocalDateTime
import org.scalatest.{FlatSpec, Matchers}
import spray.json._
import be.cetic.tsimulus.generators.missing.LimitedGenerator

class LimitedGeneratorTest extends FlatSpec with Matchers
{
   val source =
      """
        |{
        |   "name" : "limited-generator",
        |   "type": "limited",
        |   "generator": "daily-generator",
        |   "from": "2016-04-06 00:00:00.000",
        |   "to": "2016-04-23 00:00:00.000"
        |}
      """.stripMargin

   "A limited generator" should "be correctly read from a json document" in {
      val generator = LimitedGenerator(source.parseJson)

      generator.name shouldBe Some("limited-generator")
      generator.generator shouldBe Left("daily-generator")
      generator.from shouldBe Some(new LocalDateTime(2016, 4, 6, 0, 0, 0))
      generator.to shouldBe Some(new LocalDateTime(2016, 4, 23, 0, 0, 0))
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe LimitedGenerator(source.parseJson)
   }

   it should "be correctly exported to a json document" in {
      val generator = new LimitedGenerator(
         Some("limited-generator"),
         Left("daily-generator"),
         Some(new LocalDateTime(2016, 4, 6, 0, 0, 0)),
         Some(new LocalDateTime(2016, 4, 23, 0, 0, 0))
      )
      generator shouldBe LimitedGenerator(generator.toJson)
   }

   it should "have a correct textual representation" in {
      val generator = new LimitedGenerator(
         Some("limited-generator"),
         Left("daily-generator"),
         Some(new LocalDateTime(2016, 4, 6, 0, 0, 0)),
         Some(new LocalDateTime(2016, 4, 23, 0, 0, 0))
      )

      generator.toString shouldBe """Limited(Some(limited-generator), Left(daily-generator), Some(2016-04-06T00:00:00.000), Some(2016-04-23T00:00:00.000))"""
   }
} 
Example 30
Source File: SlidingWindowGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.composite

import be.cetic.tsimulus.config.GeneratorFormat
import org.scalatest.{FlatSpec, Matchers}
import spray.json._
import be.cetic.tsimulus.generators.composite.SlidingWindowGenerator
import org.joda.time.{Duration, LocalDateTime}

class SlidingWindowGeneratorTest extends FlatSpec with Matchers
{
   val source =
      """
        |{
        |  "name": "window-generator",
        |  "type": "window",
        |  "aggregator": "sum",
        |  "n": 5,
        |  "window-length" : 5000,
        |  "generator": "daily-generator"
        |}
      """.stripMargin

   "A Sliding Window generator" should "be correctly read from a json document" in {
      val generator = SlidingWindowGenerator(source.parseJson)

      generator.name shouldBe Some("window-generator")
      generator.aggregator shouldBe "sum"
      generator.duration shouldBe new Duration(5000)
      generator.generator shouldBe Left("daily-generator")
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe SlidingWindowGenerator(source.parseJson)
   }

   it should "be correctly exported to a json document" in {
      val generator = new SlidingWindowGenerator(
         Some("window-generator"),
         "sum",
         Left("daily-generator"),
         5,
         new Duration(5000)
      )
      generator shouldBe SlidingWindowGenerator(generator.toJson)
   }

   it should "have a correct textual representation" in {
      val generator = new SlidingWindowGenerator(
         Some("window-generator"),
         "sum",
         Left("daily-generator"),
         5,
         new Duration(5000)
      )

      generator.toString shouldBe """SlidingWindow(Some(window-generator), sum, Left(daily-generator), PT5S)"""
   }
} 
Example 31
Source File: PartialGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.composite

import be.cetic.tsimulus.config.GeneratorFormat
import org.scalatest.{FlatSpec, Matchers}
import spray.json._
import be.cetic.tsimulus.generators.missing.PartialGenerator
import org.joda.time.LocalDateTime

class PartialGeneratorTest extends FlatSpec with Matchers
{
   val source =
      """
        |{
        |   "name" : "partial-generator",
        |   "type" : "partial",
        |   "generator": "daily-generator",
        |   "from": "2016-04-06 00:00:00.000",
        |   "to": "2016-04-23 00:00:00.000",
        |   "missing-rate" : 0.001
        |}
      """.stripMargin

   "A partial generator" should "be correctly read from a json document" in {
      val generator = PartialGenerator(source.parseJson)

      generator.name shouldBe Some("partial-generator")
      generator.generator shouldBe Left("daily-generator")
      generator.from shouldBe Some(new LocalDateTime(2016, 4, 6, 0, 0, 0))
      generator.to shouldBe Some(new LocalDateTime(2016, 4, 23, 0, 0, 0))
      generator.missingRate shouldBe Some(0.001)
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe PartialGenerator(source.parseJson)
   }

   it should "be correctly exported to a json document" in {
      val generator = new PartialGenerator(
         Some("partial-generator"),
         Left("daily-generator"),
         Some(new LocalDateTime(2016, 4, 6, 0, 0, 0)),
         Some(new LocalDateTime(2016, 4, 23, 0, 0, 0)),
         Some(0.2)
      )
      generator shouldBe PartialGenerator(generator.toJson)
   }

   it should "have a correct textual representation" in {
      val generator = new PartialGenerator(
         Some("partial-generator"),
         Left("daily-generator"),
         Some(new LocalDateTime(2016, 4, 6, 0, 0, 0)),
         Some(new LocalDateTime(2016, 4, 23, 0, 0, 0)),
         Some(0.2)
      )

      generator.toString shouldBe """Partial(Some(partial-generator), Left(daily-generator), Some(2016-04-06T00:00:00.000), Some(2016-04-23T00:00:00.000), Some(0.2))"""
   }
} 
Example 32
Source File: OrGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.binary

import be.cetic.tsimulus.config.GeneratorFormat
import be.cetic.tsimulus.timeseries.binary.{FalseTimeSeries, OrTimeSeries, TrueTimeSeries}
import be.cetic.tsimulus.timeseries.missing.UndefinedTimeSeries
import org.joda.time.LocalDateTime
import com.github.nscala_time.time.Imports._
import be.cetic.tsimulus.generators.binary.OrGenerator
import org.scalatest.{FlatSpec, Inspectors, Matchers}
import spray.json._
import be.cetic.tsimulus.test.RTSTest


class OrGeneratorTest extends FlatSpec with Matchers with Inspectors with RTSTest
{
  val source =
      """
        |{
        |   "name": "or-generator",
        |   "type": "or",
        |   "a": "daily-generator",
        |   "b": "monthly-generator"
        |}
      """.stripMargin

   "A OR generator" should "be correctly read from a json document" in {
      val generator = OrGenerator(source.parseJson)

      generator.name shouldBe Some("or-generator")
      generator.a shouldBe Left("daily-generator")
      generator.b shouldBe Left("monthly-generator")
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe OrGenerator(source.parseJson)
   }

   it should "be correctly exported to a json document" in {
      val generator = new OrGenerator(
         Some("or-generator"),
         Left("daily-generator"),
         Left("monthly-generator")
      )
      generator shouldBe OrGenerator(generator.toJson)
   }

   it should "have a correct textual representation" in {
      val generator = new OrGenerator(
         Some("or-generator"),
         Left("a-generator"),
         Left("b-generator")
      )

      generator.toString shouldBe """Or(Some(or-generator), Left(a-generator), Left(b-generator))"""
   }
} 
Example 33
Source File: XorGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.binary

import be.cetic.tsimulus.config.GeneratorFormat
import be.cetic.tsimulus.timeseries.binary.{FalseTimeSeries, TrueTimeSeries, XorTimeSeries}
import be.cetic.tsimulus.timeseries.missing.UndefinedTimeSeries
import org.joda.time.LocalDateTime
import com.github.nscala_time.time.Imports._
import org.scalatest.{FlatSpec, Inspectors, Matchers}
import spray.json._
import be.cetic.tsimulus.generators.binary.XorGenerator
import be.cetic.tsimulus.test.RTSTest

class XorGeneratorTest extends FlatSpec with Matchers with Inspectors with RTSTest
{

   val source =
      """
        |{
        |   "name": "xor-generator",
        |   "type": "xor",
        |   "a": "daily-generator",
        |   "b": "monthly-generator"
        |}
      """.stripMargin

   "A XOR generator" should "be correctly read from a json document" in {
      val generator = XorGenerator(source.parseJson)

      generator.name shouldBe Some("xor-generator")
      generator.a shouldBe Left("daily-generator")
      generator.b shouldBe Left("monthly-generator")
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe XorGenerator(source.parseJson)
   }

   it should "be correctly exported to a json document" in {
      val generator = new XorGenerator(
         Some("xor-generator"),
         Left("daily-generator"),
         Left("monthly-generator")
      )
      generator shouldBe XorGenerator(generator.toJson)
   }

   it should "have a correct textual representation" in {
      val generator = new XorGenerator(
         Some("xor-generator"),
         Left("a-generator"),
         Left("b-generator")
      )

      generator.toString shouldBe """Xor(Some(xor-generator), Left(a-generator), Left(b-generator))"""
   }
} 
Example 34
Source File: NotGeneratorTest.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.test.generators.binary

import be.cetic.tsimulus.config.GeneratorFormat
import be.cetic.tsimulus.timeseries.binary.{FalseTimeSeries, NotTimeSeries, TrueTimeSeries}
import be.cetic.tsimulus.timeseries.missing.UndefinedTimeSeries
import org.joda.time.LocalDateTime
import com.github.nscala_time.time.Imports._
import org.scalatest.{FlatSpec, Inspectors, Matchers}
import spray.json._
import be.cetic.tsimulus.generators.binary.NotGenerator
import be.cetic.tsimulus.test.RTSTest

class NotGeneratorTest extends FlatSpec with Matchers with Inspectors with RTSTest
{
   val source =
      """
        |{
        |   "name": "not-generator",
        |   "type": "not",
        |   "generator": "binary-generator"
        |}
      """.stripMargin

   "A NOT generator" should "be correctly read from a json document" in {
      val generator = NotGenerator(source.parseJson)

      generator.name shouldBe Some("not-generator")
      generator.generator shouldBe Left("binary-generator")
   }

   it should "be extracted from the global extractor without any error" in {
      noException should be thrownBy GeneratorFormat.read(source.parseJson)
   }

   it should "be correctly extracted from the global extractor" in {
      GeneratorFormat.read(source.parseJson) shouldBe NotGenerator(source.parseJson)
   }

   it should "be correctly exported to a json document" in {
      val generator = new NotGenerator(
         Some("not-generator"),
         Left("binary-generator")
      )
      generator shouldBe NotGenerator(generator.toJson)
   }

   it should "have a correct textual representation" in {
      val generator = new NotGenerator(
         Some("not-generator"),
         Left("a-generator")
      )

      generator.toString shouldBe """Not(Some(not-generator), Left(a-generator))"""
   }
} 
Example 35
Source File: OAuth2InfoQueries.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package models.queries

import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.OAuth2Info
import jdub.async.{ Row, Statement }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime
import play.api.libs.json.{ JsValue, Json }

object OAuth2InfoQueries extends BaseQueries[OAuth2Info] {
  override protected val tableName = "oauth2_info"
  override protected val columns = Seq("provider", "key", "access_token", "token_type", "expires_in", "refresh_token", "params", "created")
  override protected val idColumns = Seq("provider", "key")
  override protected val searchColumns = Seq("key", "access_token")

  val getById = GetById
  val removeById = RemoveById

  case class CreateOAuth2Info(l: LoginInfo, o: OAuth2Info) extends Statement {
    override val sql = insertSql
    override val values = Seq(l.providerID, l.providerKey) ++ toDataSeq(o)
  }

  case class UpdateOAuth2Info(l: LoginInfo, o: OAuth2Info) extends Statement {
    override val sql = {
      s"update $tableName set access_token = ?, token_type = ?, expires_in = ?, refresh_token = ?, params = ?, created = ? where provider = ? and key = ?"
    }
    override val values = toDataSeq(o) ++ Seq(l.providerID, l.providerKey)
  }

  override protected def fromRow(row: Row) = {
    val params = row.asOpt[String]("params").map { p =>
      Json.parse(p).as[Map[String, JsValue]].map(x => x._1 -> x._2.as[String])
    }
    OAuth2Info(
      accessToken = row.as[String]("access_token"),
      tokenType = row.asOpt[String]("token_type"),
      expiresIn = row.asOpt[Int]("expires_in"),
      refreshToken = row.asOpt[String]("refresh_token"),
      params = params
    )
  }

  override protected def toDataSeq(o: OAuth2Info) = {
    val params = o.params.map(p => Json.prettyPrint(Json.toJson(p)))
    Seq(o.accessToken, o.tokenType, o.expiresIn, o.refreshToken, params, new LocalDateTime())
  }
} 
Example 36
Source File: BaseController.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package controllers

import java.util.UUID

import org.joda.time.LocalDateTime
import play.api.i18n.I18nSupport
import services.user.AuthenticationEnvironment
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.impl.authenticators.CookieAuthenticator
import models.user.{ Role, User }
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.mvc.{ AnyContent, Result }

import scala.concurrent.Future

abstract class BaseController() extends Silhouette[User, CookieAuthenticator] with I18nSupport {
  def env: AuthenticationEnvironment

  def withAdminSession(block: (SecuredRequest[AnyContent]) => Future[Result]) = SecuredAction.async { implicit request =>
    if (request.identity.roles.contains(Role.Admin)) {
      block(request)
    } else {
      Future.successful(NotFound("404 Not Found"))
    }
  }

  def withSession(block: (SecuredRequest[AnyContent]) => Future[Result]) = UserAwareAction.async { implicit request =>
    val response = request.identity match {
      case Some(user) =>
        val secured = SecuredRequest(user, request.authenticator.getOrElse(throw new IllegalStateException()), request)
        block(secured).map { r =>
          r
        }
      case None =>
        val user = User(
          id = UUID.randomUUID(),
          username = None,
          profiles = Nil,
          created = new LocalDateTime()
        )

        for {
          user <- env.userService.save(user)
          authenticator <- env.authenticatorService.create(LoginInfo("anonymous", user.id.toString))
          value <- env.authenticatorService.init(authenticator)
          result <- block(SecuredRequest(user, authenticator, request))
          authedResponse <- env.authenticatorService.embed(value, result)
        } yield {
          env.eventBus.publish(SignUpEvent(user, request, request2Messages))
          env.eventBus.publish(LoginEvent(user, request, request2Messages))
          authedResponse
        }
    }
    response
  }
} 
Example 37
Source File: User.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package models.user

import java.util.UUID

import com.mohiva.play.silhouette.api.{ Identity, LoginInfo }
import org.joda.time.LocalDateTime

case class User(
    id: UUID,
    username: Option[String],
    profiles: Seq[LoginInfo],
    roles: Set[Role] = Set(Role.User),
    created: LocalDateTime
) extends Identity {
  def isGuest = profiles.isEmpty
  def isAdmin = roles.contains(models.user.Role.Admin)
} 
Example 38
Source File: UserQueries.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package models.queries

import java.util.UUID

import com.mohiva.play.silhouette.api.LoginInfo
import jdub.async.{ FlatSingleRowQuery, Row, Statement }
import jdub.async.queries.BaseQueries
import models.user.{ Role, User }
import org.joda.time.LocalDateTime

object UserQueries extends BaseQueries[User] {
  override protected val tableName = "users"
  override protected val columns = Seq("id", "username", "profiles", "roles", "created")
  override protected val searchColumns = Seq("id::text", "username")

  val insert = Insert
  val getById = GetById
  def searchCount(q: String, groupBy: Option[String] = None) = new SearchCount(q, groupBy)
  val search = Search
  val removeById = RemoveById

  case class UpdateUser(u: User) extends Statement {
    override val sql = updateSql(Seq("username", "profiles", "roles"))
    override val values = {
      val profiles = u.profiles.map(l => s"${l.providerID}:${l.providerKey}").toArray
      val roles = u.roles.map(_.name).toArray
      Seq(u.username, profiles, roles, u.id)
    }
  }

  case class SetUsername(userId: UUID, username: Option[String]) extends Statement {
    override val sql = updateSql(Seq("username"))
    override val values = Seq(username, userId)
  }

  case class AddRole(id: UUID, role: Role) extends Statement {
    override val sql = s"update $tableName set roles = array_append(roles, ?) where id = ?"
    override val values = Seq(role.name, id)
  }

  case class FindUserByUsername(username: String) extends FlatSingleRowQuery[User] {
    override val sql = getSql(Some("username = ?"))
    override val values = Seq(username)
    override def flatMap(row: Row) = Some(fromRow(row))
  }

  case class FindUserByProfile(loginInfo: LoginInfo) extends FlatSingleRowQuery[User] {
    override val sql = getSql(Some("profiles @> ARRAY[?]::text[]"))
    override val values = Seq(s"${loginInfo.providerID}:${loginInfo.providerKey}")
    override def flatMap(row: Row) = Some(fromRow(row))
  }

  override protected def fromRow(row: Row) = {
    val id = UUID.fromString(row.as[String]("id"))
    val profiles = row.as[collection.mutable.ArrayBuffer[_]]("profiles").map { l =>
      val info = l.toString
      val delimiter = info.indexOf(':')
      val provider = info.substring(0, delimiter)
      val key = info.substring(delimiter + 1)
      LoginInfo(provider, key)
    }
    val username = row.asOpt[String]("username")
    val roles = row.as[collection.mutable.ArrayBuffer[_]]("roles").map(x => Role(x.toString)).toSet
    val created = row.as[LocalDateTime]("created")
    User(id, username, profiles, roles, created)
  }

  override protected def toDataSeq(u: User) = {
    val profiles = u.profiles.map(l => s"${l.providerID}:${l.providerKey}").toArray
    val roles = u.roles.map(_.name).toArray
    Seq(u.id, u.username, profiles, roles, u.created)
  }
} 
Example 39
Source File: ProfileQueries.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package models.queries

import java.util.UUID

import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.CommonSocialProfile
import jdub.async.queries.BaseQueries
import jdub.async.{ Query, Row, FlatSingleRowQuery }
import org.joda.time.LocalDateTime

object ProfileQueries extends BaseQueries[CommonSocialProfile] {
  override protected val tableName = "user_profiles"
  override protected val columns = Seq("provider", "key", "email", "first_name", "last_name", "full_name", "avatar_url", "created")
  override protected val searchColumns = Seq("key", "email", "first_name", "last_name", "full_name")

  val insert = Insert
  val remove = RemoveById

  case class FindProfile(provider: String, key: String) extends FlatSingleRowQuery[CommonSocialProfile] {
    override val sql = getSql(Some("provider = ? and key = ?"))
    override val values = Seq(provider, key)
    override def flatMap(row: Row) = Some(fromRow(row))
  }

  case class FindProfilesByUser(id: UUID) extends Query[List[CommonSocialProfile]] {
    override val sql = s"select ${columns.mkString(", ")} from $tableName p " +
      "where (p.provider || ':' || p.key) in (select unnest(profiles) from users where users.id = ?)"
    override val values = Seq(id)
    override def reduce(rows: Iterator[Row]) = rows.map(fromRow).toList
  }

  override protected def fromRow(row: Row) = {
    val loginInfo = LoginInfo(
      providerID = row.as[String]("provider"),
      providerKey = row.as[String]("key")
    )
    val firstName = row.asOpt[String]("first_name")
    val lastName = row.asOpt[String]("last_name")
    val fullName = row.asOpt[String]("full_name")
    val email = row.asOpt[String]("email")
    val avatarUrl = row.asOpt[String]("avatar_url")

    CommonSocialProfile(loginInfo, firstName, lastName, fullName, email, avatarUrl)
  }

  override protected def toDataSeq(p: CommonSocialProfile) = Seq(
    p.loginInfo.providerID, p.loginInfo.providerKey, p.email, p.firstName, p.lastName, p.fullName, p.avatarURL, new LocalDateTime()
  )
} 
Example 40
Source File: PasswordInfoQueries.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package models.queries

import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.api.util.PasswordInfo
import jdub.async.{ Row, Statement }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime

object PasswordInfoQueries extends BaseQueries[PasswordInfo] {
  override protected val tableName = "password_info"
  override protected val columns = Seq("provider", "key", "hasher", "password", "salt", "created")
  override protected def idColumns = Seq("provider", "key")
  override protected val searchColumns = Seq("key")

  val getById = GetById
  val removeById = RemoveById

  case class CreatePasswordInfo(l: LoginInfo, p: PasswordInfo) extends Statement {
    override val sql = insertSql
    override val values = Seq(l.providerID, l.providerKey) ++ toDataSeq(p)
  }

  case class UpdatePasswordInfo(l: LoginInfo, p: PasswordInfo) extends Statement {
    override val sql = s"update $tableName set hasher = ?, password = ?, salt = ?, created = ? where provider = ? and key = ?"
    override val values = toDataSeq(p) ++ Seq(l.providerID, l.providerKey)
  }

  override protected def fromRow(row: Row) = PasswordInfo(
    hasher = row.as[String]("hasher"),
    password = row.as[String]("password"),
    salt = row.asOpt[String]("salt")
  )

  override protected def toDataSeq(p: PasswordInfo) = Seq(p.hasher, p.password, p.salt, new LocalDateTime())
} 
Example 41
Source File: OAuth1InfoQueries.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package models.queries

import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.OAuth1Info
import jdub.async.{ Row, Statement }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime

object OAuth1InfoQueries extends BaseQueries[OAuth1Info] {
  override protected val tableName = "oauth1_info"
  override protected val columns = Seq("provider", "key", "token", "secret", "created")
  override protected val idColumns = Seq("provider", "key")
  override protected val searchColumns = Seq("key", "token")

  val getById = GetById
  val removeById = RemoveById

  case class CreateOAuth1Info(l: LoginInfo, o: OAuth1Info) extends Statement {
    override val sql = insertSql
    override val values = Seq(l.providerID, l.providerKey) ++ toDataSeq(o)
  }

  case class UpdateOAuth1Info(l: LoginInfo, o: OAuth1Info) extends Statement {
    override val sql = s"update $tableName set token = ?, secret = ?, created = ? where provider = ? and key = ?"
    override val values = toDataSeq(o) ++ Seq(l.providerID, l.providerKey)
  }

  override protected def fromRow(row: Row) = {
    val token = row.as[String]("token")
    val secret = row.as[String]("secret")
    OAuth1Info(token, secret)
  }

  override protected def toDataSeq(o: OAuth1Info) = Seq(o.token, o.secret, new LocalDateTime())
} 
Example 42
Source File: AuthenticatorQueries.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package models.queries

import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.authenticators.CookieAuthenticator
import jdub.async.{ Statement, Row, FlatSingleRowQuery }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime

object AuthenticatorQueries extends BaseQueries[CookieAuthenticator] {
  override protected val tableName = "session_info"
  override protected val columns = Seq("id", "provider", "key", "last_used", "expiration", "fingerprint", "created")
  override protected val searchColumns = Seq("id::text", "key")

  val insert = Insert
  val getById = GetById
  val removeById = RemoveById

  case class FindSessionInfoByLoginInfo(l: LoginInfo) extends FlatSingleRowQuery[CookieAuthenticator] {
    override val sql = getSql(Some("provider = ? and key = ?"))
    override val values = Seq(l.providerID, l.providerKey)
    override def flatMap(row: Row) = Some(fromRow(row))
  }

  case class UpdateAuthenticator(ca: CookieAuthenticator) extends Statement {
    override val sql = updateSql(Seq("provider", "key", "last_used", "expiration", "fingerprint"))
    override val values = Seq(
      ca.loginInfo.providerID,
      ca.loginInfo.providerKey,
      ca.lastUsedDateTime.toLocalDateTime,
      ca.expirationDateTime.toLocalDateTime,
      ca.fingerprint,
      ca.id
    )
  }

  override protected def fromRow(row: Row) = {
    val id = row.as[String]("id")
    val provider = row.as[String]("provider")
    val key = row.as[String]("key")
    val lastUsed = row.as[LocalDateTime]("last_used").toDateTime
    val expiration = row.as[LocalDateTime]("expiration").toDateTime
    val idleTimeout = None
    val cookieMaxAge = None
    val fingerprint = row.asOpt[String]("fingerprint")
    CookieAuthenticator(id, LoginInfo(provider, key), lastUsed, expiration, idleTimeout, cookieMaxAge, fingerprint)
  }

  override protected def toDataSeq(ca: CookieAuthenticator) = Seq(
    ca.id,
    ca.loginInfo.providerID,
    ca.loginInfo.providerKey,
    ca.lastUsedDateTime.toLocalDateTime,
    ca.expirationDateTime.toLocalDateTime,
    ca.fingerprint,
    new LocalDateTime()
  )
} 
Example 43
Source File: OpenIdInfoQueries.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package models.queries

import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.OpenIDInfo
import jdub.async.{ Row, Statement }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime
import play.api.libs.json.{ JsValue, Json }

object OpenIdInfoQueries extends BaseQueries[OpenIDInfo] {
  override protected val tableName = "openid_info"
  override protected val columns = Seq("provider", "key", "id", "attributes", "created")
  override protected val idColumns = Seq("provider", "key")
  override protected val searchColumns = Seq("key")

  val getById = GetById
  val removeById = RemoveById

  case class CreateOpenIdInfo(l: LoginInfo, o: OpenIDInfo) extends Statement {
    override val sql = insertSql
    override val values = Seq(l.providerID, l.providerKey) ++ toDataSeq(o)
  }

  case class UpdateOpenIdInfo(l: LoginInfo, o: OpenIDInfo) extends Statement {
    override val sql = s"update $tableName set id = ?, attributes = ?, created = ? where provider = ? and key = ?"
    val attributes = Json.prettyPrint(Json.toJson(o.attributes))
    override val values = toDataSeq(o) ++ Seq(l.providerID, l.providerKey)
  }

  override protected def fromRow(row: Row) = {
    val id = row.as[String]("id")
    val attributesString = row.as[String]("attributes")
    val attributes = Json.parse(attributesString).as[Map[String, JsValue]].map(x => x._1 -> x._2.as[String])
    OpenIDInfo(id, attributes)
  }

  override protected def toDataSeq(o: OpenIDInfo) = {
    val attributes = Json.prettyPrint(Json.toJson(o.attributes))
    Seq(o.id, attributes, new LocalDateTime())
  }
} 
Example 44
Source File: DateTimeDifferenceTimeSeries.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.timeseries.dt

import be.cetic.tsimulus.timeseries.TimeSeries
import org.joda.time.{DateTimeZone, LocalDateTime, Duration}


class DateTimeDifferenceTimeSeries(a: TimeSeries[LocalDateTime], b: TimeSeries[LocalDateTime]) extends TimeSeries[Duration]
{
   override def compute(times: Stream[LocalDateTime]): Stream[(LocalDateTime, Option[Duration])] = {
      val z = a.compute(times) zip b.compute(times)

      z.map(s => {
         if(s._1._2.isEmpty || s._2._2.isEmpty) (s._1._1, None)
         else (s._1._1, Some(new Duration(s._1._2.get.toDateTime(DateTimeZone.UTC), s._2._2.get.toDateTime(DateTimeZone.UTC))))
      })
   }

   override def compute(time: LocalDateTime): Option[Duration] = {
      val aTime = a.compute(time)
      val bTime = b.compute(time)

      if (aTime.isEmpty || bTime.isEmpty) None
      else Some(new Duration(aTime.get.toDateTime(DateTimeZone.UTC), bTime.get.toDateTime(DateTimeZone.UTC)))
   }
} 
Example 45
Source File: MailTokenService.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.api.service

import javax.inject.Inject
import akka.Done
import org.hatdex.hat.dal.ModelTranslation
import org.hatdex.hat.dal.Tables._
import org.hatdex.hat.phata.models.{ MailToken, MailTokenUser }
import org.hatdex.libs.dal.HATPostgresProfile.api._
import org.joda.time.LocalDateTime

import scala.concurrent._

trait MailTokenService[T <: MailToken] {
  def create(token: T)(implicit db: Database): Future[Option[T]]
  def retrieve(id: String)(implicit db: Database): Future[Option[T]]
  def retrieve(email: String, isSignup: Boolean)(implicit db: Database): Future[Option[T]]
  def consume(id: String)(implicit db: Database): Future[Done]
  def expire(id: String)(implicit db: Database): Future[Done]
}

class MailTokenUserService @Inject() (implicit val ec: DalExecutionContext) extends MailTokenService[MailTokenUser] {
  def create(token: MailTokenUser)(implicit db: Database): Future[Option[MailTokenUser]] = {
    save(token).map(Some(_))
  }
  def retrieve(id: String)(implicit db: Database): Future[Option[MailTokenUser]] = {
    findById(id)
  }
  def consume(id: String)(implicit db: Database): Future[Done] = {
    delete(id)
  }
  def retrieve(email: String, isSignup: Boolean)(implicit db: Database): Future[Option[MailTokenUser]] = {
    findByEmailAndIsSignup(email, isSignup)
  }
  def expire(id: String)(implicit db: Database): Future[Done] = {
    expireNow(id)
  }

  private def findById(id: String)(implicit db: Database): Future[Option[MailTokenUser]] = {
    db.run(UserMailTokens.filter(_.id === id).result).map { tokens =>
      tokens.headOption.map(ModelTranslation.fromDbModel)
    }
  }

  private def findByEmailAndIsSignup(email: String, isSignup: Boolean)(implicit db: Database): Future[Option[MailTokenUser]] = {
    db.run(UserMailTokens.filter(t => t.email === email && t.isSignup === isSignup).result).map { tokens =>
      tokens.headOption.map(ModelTranslation.fromDbModel)
    }
  }

  private def save(token: MailTokenUser)(implicit db: Database): Future[MailTokenUser] = {
    val query = (UserMailTokens returning UserMailTokens) += UserMailTokensRow(token.id, token.email, token.expirationTime.toLocalDateTime, token.isSignUp)
    db.run(query)
      .map(ModelTranslation.fromDbModel)
  }

  private def delete(id: String)(implicit db: Database): Future[Done] = {
    db.run(UserMailTokens.filter(_.id === id).delete).map(_ ⇒ Done)
  }

  private def expireNow(id: String)(implicit db: Database): Future[Done] = {
    db.run(UserMailTokens
      .filter(_.id === id)
      .map(_.expirationTime)
      .update(LocalDateTime.now())).map(_ => Done)
  }
} 
Example 46
Source File: MLFlowModelSpec.scala    From ForestFlow   with Apache License 2.0 5 votes vote down vote up
package ai.forestflow.serving.MLFlow

import ai.forestflow.serving.impl.{LocalFileArtifactReader, MLFlowH2OLoader, UnsupportedServableFlavor}
import ai.forestflow.serving.interfaces.{ArtifactReader, Loader}
import cats.syntax.either._
import ai.forestflow.serving.impl.{LocalFileArtifactReader, MLFlowH2OLoader, UnsupportedServableFlavor}
import ai.forestflow.serving.interfaces.ArtifactReader
import ai.forestflow.utils.SourceStorageProtocols
import io.circe.generic.extras._
import io.circe.generic.extras.semiauto.deriveDecoder
import io.circe.{CursorOp, Decoder, DecodingFailure}
import ai.forestflow.serving.interfaces.Loader
import ai.forestflow.utils.ThrowableImplicits._
import org.joda.time.{DateTimeZone, LocalDateTime}


case class MLFlowModelSpec(
  artifactReader: ArtifactReader,

  runId: Option[String],

  timeCreated: Long,

  flavors: Map[String, Loader]
) {
  def getServableFlavor: Option[(String, Loader)] = flavors.collectFirst { case (flavor, loader) if !loader.isInstanceOf[UnsupportedServableFlavor] => (flavor, loader) }
}

object MLFlowModelSpec {

  implicit val config: Configuration = {
    val baseConfig = Configuration.default.withSnakeCaseMemberNames
    baseConfig.copy(transformMemberNames = baseConfig.transformMemberNames andThen {
      // from snake_case in class to snake_case file
      case "artifact_reader" => "artifact_path"
      case "time_created" => "utc_time_created" // utc_time_created is a string!
      case other => other
    })
  }

  implicit val decodeTimeCreated: Decoder[Long] = Decoder.decodeString.emap{ tm: String =>
    Either.catchNonFatal[Long]({
      var ts = tm.replace(" ", "T")
      if (ts.takeRight(1) != "Z")
        ts = ts + "Z"
      val ll = LocalDateTime.parse(tm.replace(" ", "T")).toDateTime(DateTimeZone.UTC)
      ll.getMillis
    }
    ).leftMap(t => s"timeCreated Decoder Failed: ${t.printableStackTrace}")
  }.handleErrorWith(_ => Decoder.decodeLong)

  implicit val decodeMLFlowModel: Decoder[MLFlowModelSpec] = deriveDecoder[MLFlowModelSpec]

  implicit val decodeArtifactReaderString: Decoder[ArtifactReader] = Decoder.decodeOption[String].emap { artifactPath: Option[String] =>
    Either.catchNonFatal[ArtifactReader]({
      artifactPath match {
        case Some(path) => ArtifactReader.getArtifactReader(path)
        case _ => LocalFileArtifactReader("")
      }
    }
    ).leftMap(t => s"Artifact Reader Decoder Failed: ${t.printableStackTrace}")
  }

  implicit val decodeServableFlavor: Decoder[Map[String, Loader]] = Decoder.decodeMap[String, Map[String, String]].emap { flavors =>
    Either.catchNonFatal[Map[String, Loader]](
      flavors
        .map { case (flavor, props) => (flavor.toLowerCase, props) }
        .map {
          case (f@"h2o_mojo", props) => f -> MLFlowH2OLoader(dataPath = props.getOrElse("data", ""), version = props.get("h2o_version"))
          
          case (f, props) => f -> UnsupportedServableFlavor(props)
          case (f, props) => throw DecodingFailure(s"Unexpected or unsupported flavor type [$f] with props $props", List[CursorOp]())
          // TODO: Support POJO?
          // case (f, _) => p -> BasicSourceProvider()
        }
    ).leftMap(t => t.printableStackTrace)

  }

} 
Example 47
Source File: package.scala    From cron4s   with Apache License 2.0 5 votes vote down vote up
package cron4s.lib

import cats.{Eq, Show}

import cron4s.datetime.IsDateTime

import org.joda.time.{DateTime, LocalDate, LocalDateTime, LocalTime}


package object joda {
  implicit val jodaDateTimeEq: Eq[DateTime] = Eq.fromUniversalEquals[DateTime]
  implicit val jodaLocalDateEq: Eq[LocalDate] =
    Eq.fromUniversalEquals[LocalDate]
  implicit val jodaLocalTimeEq: Eq[LocalTime] =
    Eq.fromUniversalEquals[LocalTime]
  implicit val jodaLocalDateTimeEq: Eq[LocalDateTime] =
    Eq.fromUniversalEquals[LocalDateTime]

  implicit val jodaDateTimeShow: Show[DateTime]           = Show.fromToString
  implicit val jodaLocalDateShow: Show[LocalDate]         = Show.fromToString
  implicit val jodaLocalTimeShow: Show[LocalTime]         = Show.fromToString
  implicit val jodaLocalDateTimeShow: Show[LocalDateTime] = Show.fromToString

  implicit val jodaDateTimeInstance: IsDateTime[DateTime] =
    new JodaDateTimeInstance
  implicit val jodaLocalDateInstance: IsDateTime[LocalDate] =
    new JodaLocalDateInstance
  implicit val jodaLocalTimeInstance: IsDateTime[LocalTime] =
    new JodaLocalTimeInstance
  implicit val jodaLocalDateTimeInstance: IsDateTime[LocalDateTime] =
    new JodaLocalDateTimeInstance
} 
Example 48
Source File: RestFormatsSpec.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.http.controllers

import org.joda.time.{DateTime, DateTimeZone, LocalDate, LocalDateTime}
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatest.matchers.should.Matchers
import play.api.libs.json.{JsSuccess, _}

class RestFormatsSpec extends AnyWordSpecLike with Matchers {
  "localDateTimeRead" should {
    "return a LocalDateTime for correctly formatted JsString" in {
      val testDate = new LocalDateTime(0)
      val jsValue  = RestFormats.localDateTimeWrite.writes(testDate)

      val JsSuccess(result, _) = RestFormats.localDateTimeRead.reads(jsValue)
      result shouldBe testDate
    }

    "return a JsError for a json value that is not a JsString" in {
      RestFormats.localDateTimeRead.reads(Json.obj()) shouldBe a[JsError]
    }

    "return a JsError for a JsString that is not a well-formatted date" in {
      RestFormats.localDateTimeRead.reads(JsString("not a valid date")) shouldBe a[JsError]
    }
  }

  "dateTimeRead" should {
    "return a DateTime in zone UTC for correctly formatted JsString" in {
      val testDate = new DateTime(0)
      val jsValue  = RestFormats.dateTimeWrite.writes(testDate)

      val JsSuccess(result, _) = RestFormats.dateTimeRead.reads(jsValue)
      result shouldBe testDate.withZone(DateTimeZone.UTC)
    }

    "return a JsError for a json value that is not a JsString" in {
      RestFormats.dateTimeRead.reads(Json.obj()) shouldBe a[JsError]
    }

    "return a JsError for a JsString that is not a well-formatted date" in {
      RestFormats.dateTimeRead.reads(JsString("not a valid date")) shouldBe a[JsError]
    }
  }

  "localDateRead" should {
    "return a LocalDate in zone UTC for correctly formatted JsString" in {
      val json         = JsString("1994-05-01")
      val expectedDate = new LocalDate(1994, 5, 1)

      val JsSuccess(result, _) = RestFormats.localDateRead.reads(json)
      result shouldBe expectedDate
    }

    "return a JsError for a json value that is not a JsString" in {
      RestFormats.localDateRead.reads(Json.obj()) shouldBe a[JsError]
    }

    "return a JsError for a JsString that is not a well-formatted date" in {
      RestFormats.localDateRead.reads(JsString("not a valid date")) shouldBe a[JsError]
    }

    "return a JsError for a JsString that is well formatted but has bad values" in {
      RestFormats.localDateRead.reads(JsString("1994-13-32")) shouldBe a[JsError]
    }
  }
} 
Example 49
Source File: RestFormats.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.http.controllers

import org.joda.time.format.ISODateTimeFormat
import play.api.libs.json._
import play.api.libs.json.JsString
import org.joda.time.{DateTime, DateTimeZone, LocalDate, LocalDateTime}
import scala.util.Try

object RestFormats extends RestFormats

trait RestFormats {

  private val dateTimeFormat = ISODateTimeFormat.dateTime.withZoneUTC
  private val localDateRegex = """^(\d\d\d\d)-(\d\d)-(\d\d)$""".r

  implicit val localDateTimeRead: Reads[LocalDateTime] = new Reads[LocalDateTime] {
    override def reads(json: JsValue): JsResult[LocalDateTime] =
      json match {
        case JsString(s) =>
          Try {
            JsSuccess(new LocalDateTime(dateTimeFormat.parseDateTime(s), DateTimeZone.UTC))
          }.getOrElse {
            JsError(s"Could not parse $s as a DateTime with format ${dateTimeFormat.toString}")
          }
        case _ => JsError(s"Expected value to be a string, was actually $json")
      }
  }

  implicit val localDateTimeWrite: Writes[LocalDateTime] = new Writes[LocalDateTime] {
    def writes(dateTime: LocalDateTime): JsValue = JsString(dateTimeFormat.print(dateTime.toDateTime(DateTimeZone.UTC)))
  }

  implicit val dateTimeRead: Reads[DateTime] = new Reads[DateTime] {
    override def reads(json: JsValue): JsResult[DateTime] =
      json match {
        case JsString(s) =>
          Try {
            JsSuccess(dateTimeFormat.parseDateTime(s))
          }.getOrElse {
            JsError(s"Could not parse $s as a DateTime with format ${dateTimeFormat.toString}")
          }
        case _ => JsError(s"Expected value to be a string, was actually $json")
      }
  }

  implicit val dateTimeWrite: Writes[DateTime] = new Writes[DateTime] {
    def writes(dateTime: DateTime): JsValue = JsString(dateTimeFormat.print(dateTime))
  }

  implicit val localDateRead: Reads[LocalDate] = new Reads[LocalDate] {
    override def reads(json: JsValue): JsResult[LocalDate] =
      json match {
        case JsString(s @ localDateRegex(y, m, d)) =>
          Try {
            JsSuccess(new LocalDate(y.toInt, m.toInt, d.toInt))
          }.getOrElse {
            JsError(s"$s is not a valid date")
          }
        case JsString(s) => JsError(s"Cannot parse $s as a LocalDate")
        case _           => JsError(s"Expected value to be a string, was actually $json")
      }
  }

  implicit val localDateWrite: Writes[LocalDate] = new Writes[LocalDate] {
    def writes(date: LocalDate): JsValue =
      JsString("%04d-%02d-%02d".format(date.getYear, date.getMonthOfYear, date.getDayOfMonth))
  }

  implicit val dateTimeFormats      = Format(dateTimeRead, dateTimeWrite)
  implicit val localDateTimeFormats = Format(localDateTimeRead, localDateTimeWrite)
  implicit val localDateFormats     = Format(localDateRead, localDateWrite)

} 
Example 50
Source File: BigQueryTypeSpec.scala    From shapeless-datatype   with Apache License 2.0 5 votes vote down vote up
package shapeless.datatype.bigquery

import java.net.URI

import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}
import com.google.api.services.bigquery.model.TableRow
import com.google.common.io.BaseEncoding
import com.google.protobuf.ByteString
import org.joda.time.{Instant, LocalDate, LocalDateTime, LocalTime}
import org.scalacheck.Prop.forAll
import org.scalacheck.ScalacheckShapeless._
import org.scalacheck._
import shapeless._
import shapeless.datatype.record._

import scala.reflect.runtime.universe._

object BigQueryTypeSpec extends Properties("BigQueryType") {
  import shapeless.datatype.test.Records._
  import shapeless.datatype.test.SerializableUtils._

  val mapper = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)

  implicit def compareByteArrays(x: Array[Byte], y: Array[Byte]) = java.util.Arrays.equals(x, y)
  implicit def compareIntArrays(x: Array[Int], y: Array[Int]) = java.util.Arrays.equals(x, y)

  def roundTrip[A: TypeTag, L <: HList](m: A)(implicit
    gen: LabelledGeneric.Aux[A, L],
    fromL: FromTableRow[L],
    toL: ToTableRow[L],
    mr: MatchRecord[L]
  ): Boolean = {
    BigQuerySchema[A] // FIXME: verify the generated schema
    val t = ensureSerializable(BigQueryType[A])
    val f1: SerializableFunction[A, TableRow] =
      new SerializableFunction[A, TableRow] {
        override def apply(m: A): TableRow = t.toTableRow(m)
      }
    val f2: SerializableFunction[TableRow, Option[A]] =
      new SerializableFunction[TableRow, Option[A]] {
        override def apply(m: TableRow): Option[A] = t.fromTableRow(m)
      }
    val toFn = ensureSerializable(f1)
    val fromFn = ensureSerializable(f2)
    val copy = fromFn(mapper.readValue(mapper.writeValueAsString(toFn(m)), classOf[TableRow]))
    val rm = RecordMatcher[A]
    copy.exists(rm(_, m))
  }

  implicit val byteStringBigQueryMappableType = BigQueryType.at[ByteString]("BYTES")(
    x => ByteString.copyFrom(BaseEncoding.base64().decode(x.toString)),
    x => BaseEncoding.base64().encode(x.toByteArray)
  )
  property("required") = forAll { m: Required => roundTrip(m) }
  property("optional") = forAll { m: Optional => roundTrip(m) }
  property("repeated") = forAll { m: Repeated => roundTrip(m) }
  property("mixed") = forAll { m: Mixed => roundTrip(m) }
  property("nested") = forAll { m: Nested => roundTrip(m) }
  property("seqs") = forAll { m: Seqs => roundTrip(m) }

  implicit val arbDate = Arbitrary(arbInstant.arbitrary.map(i => new LocalDate(i.getMillis)))
  implicit val arbTime = Arbitrary(arbInstant.arbitrary.map(i => new LocalTime(i.getMillis)))
  implicit val arbDateTime = Arbitrary(
    arbInstant.arbitrary.map(i => new LocalDateTime(i.getMillis))
  )

  case class DateTimeTypes(
    instant: Instant,
    date: LocalDate,
    time: LocalTime,
    dateTime: LocalDateTime
  )
  property("date time types") = forAll { m: DateTimeTypes => roundTrip(m) }

  implicit val uriBigQueryType =
    BigQueryType.at[URI]("STRING")(v => URI.create(v.toString), _.toASCIIString)
  property("custom") = forAll { m: Custom => roundTrip(m) }
} 
Example 51
Source File: BigQuerySchema.scala    From shapeless-datatype   with Apache License 2.0 5 votes vote down vote up
package shapeless.datatype.bigquery

import com.google.api.services.bigquery.model.{TableFieldSchema, TableSchema}
import org.joda.time.{Instant, LocalDate, LocalDateTime, LocalTime}

import scala.collection.JavaConverters._
import scala.reflect.runtime.universe._

object BigQuerySchema {
  private def isField(s: Symbol): Boolean =
    s.isPublic && s.isMethod && !s.isSynthetic && !s.isConstructor && s.asMethod.isCaseAccessor

  private def isCaseClass(tpe: Type): Boolean =
    !tpe.toString.startsWith("scala.") &&
      List(typeOf[Product], typeOf[Serializable], typeOf[Equals])
        .forall(b => tpe.baseClasses.contains(b.typeSymbol))

  private def rawType(tpe: Type): (String, Iterable[TableFieldSchema]) = tpe match {
    case t if t =:= typeOf[Boolean]       => ("BOOLEAN", Nil)
    case t if t =:= typeOf[Int]           => ("INTEGER", Nil)
    case t if t =:= typeOf[Long]          => ("INTEGER", Nil)
    case t if t =:= typeOf[Float]         => ("FLOAT", Nil)
    case t if t =:= typeOf[Double]        => ("FLOAT", Nil)
    case t if t =:= typeOf[String]        => ("STRING", Nil)
    case t if t =:= typeOf[Array[Byte]]   => ("BYTES", Nil)
    case t if t =:= typeOf[Instant]       => ("TIMESTAMP", Nil)
    case t if t =:= typeOf[LocalDate]     => ("DATE", Nil)
    case t if t =:= typeOf[LocalTime]     => ("TIME", Nil)
    case t if t =:= typeOf[LocalDateTime] => ("DATETIME", Nil)
    case t if isCaseClass(t)              => ("RECORD", toFields(t))
  }

  private def toField(s: Symbol): TableFieldSchema = {
    val name = s.name.toString
    val tpe = s.asMethod.returnType

    val (mode, valType) = tpe match {
      case t if t.erasure =:= typeOf[Option[_]].erasure => ("NULLABLE", t.typeArgs.head)
      case t
          if t.erasure <:< typeOf[Traversable[_]].erasure || (t.erasure <:< typeOf[
            Array[_]
          ] && !(t.typeArgs.head =:= typeOf[
            Byte
          ])) =>
        ("REPEATED", t.typeArgs.head)
      case t => ("REQUIRED", t)
    }
    val (tpeParam, nestedParam) = customTypes.get(valType.toString) match {
      case Some(t) => (t, Nil)
      case None    => rawType(valType)
    }
    val tfs = new TableFieldSchema().setMode(mode).setName(name).setType(tpeParam)
    if (nestedParam.nonEmpty) {
      tfs.setFields(nestedParam.toList.asJava)
    }
    tfs
  }

  private def toFields(t: Type): Iterable[TableFieldSchema] = t.decls.filter(isField).map(toField)

  private val customTypes = scala.collection.mutable.Map[String, String]()
  private val cachedSchemas = scala.collection.concurrent.TrieMap.empty[TypeTag[_], TableSchema]

  private[bigquery] def register(tpe: Type, typeName: String): Unit =
    customTypes += tpe.toString -> typeName

  def apply[T: TypeTag]: TableSchema = {
    val tt = implicitly[TypeTag[T]]
    cachedSchemas.getOrElseUpdate(tt, new TableSchema().setFields(toFields(tt.tpe).toList.asJava))
  }
} 
Example 52
Source File: PartialTimeSeries.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.timeseries.missing

import be.cetic.tsimulus.timeseries.TimeSeries
import com.github.nscala_time.time.Imports._
import org.joda.time.LocalDateTime

import scala.util.Random


case class PartialTimeSeries[T](base: TimeSeries[T],
                                from: Option[LocalDateTime],
                                to: Option[LocalDateTime],
                                missingRate: Option[Double]) extends TimeSeries[T]
{
   override def compute(times: Stream[LocalDateTime]) =
   {
      base.compute(times).map
      { case (t, v) =>
      {
         (t, v.flatMap(x =>
         {
            if ((from.isDefined && t < from.get) || (to.isDefined && t > to.get)) None
            else
            {
               missingRate match
               {
                  case None => Some(x)
                  case Some(odds) => if (Random.nextDouble() < odds) None
                                     else Some(x)
               }
            }
         }))
      }
      }
   }

   override def compute(time: LocalDateTime): Option[T] =
   {
      if ((from.isDefined && time < from.get) || (to.isDefined && time > to.get)) None
      else
      {
         missingRate match
         {
            case None => base.compute(time)
            case Some(odds) => if (Random.nextDouble() < odds) None
                               else base.compute(time)
         }
      }
   }
} 
Example 53
Source File: DayOfWeekGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.dt

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.dt.DayOfWeekTimeSeries
import org.joda.time.LocalDateTime
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _}


class DayOfWeekGenerator(name: Option[String], base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "dow")
{
   override def timeseries(generators: String => Generator[Any]) = {
      val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      new DayOfWeekTimeSeries(ts)
   }

   override def toString = "DayOfWeekGenerator()"

   override def equals(o: Any) = o match {
      case that: DayOfWeekGenerator => that.name == this.name
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson,
         "base" -> either2json(base)
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object DayOfWeekGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], base: String) = new DayOfWeekGenerator(name, Left(base))
   def apply(name: Option[String], base: Generator[LocalDateTime]) = new DayOfWeekGenerator(name, Right(base))

   def apply(json: JsValue): DayOfWeekGenerator = {

      val fields = json.asJsObject.fields

      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val base = fields("base") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      new DayOfWeekGenerator(name, base)
   }
} 
Example 54
Source File: SecondTimeGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.dt

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.dt.{MinuteTimeSeries, SecondTimeSeries}
import org.joda.time.LocalDateTime
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _}


class SecondTimeGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "second")
{
   override def timeseries(generators: String => Generator[Any]) = {
      val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      new SecondTimeSeries(ts)
   }

   override def toString = "SecondTimeGenerator()"

   override def equals(o: Any) = o match {
      case that: SecondTimeGenerator => (that.name == this.name) && (that.base == this.base)
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson,
         "base" -> either2json(base)
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object SecondTimeGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], base: String) = new SecondTimeGenerator(name, Left(base))
   def apply(name: Option[String], base: Generator[LocalDateTime]) = new SecondTimeGenerator(name, Right(base))

   def apply(json: JsValue): SecondTimeGenerator = {

      val fields = json.asJsObject.fields

      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val base = fields("base") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      new SecondTimeGenerator(name, base)
   }
} 
Example 55
Source File: DateTimeDifferenceGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.dt

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.dt.DateTimeDifferenceTimeSeries
import org.joda.time.{Duration, LocalDateTime}
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _}


class DateTimeDifferenceGenerator(name: Option[String], val a: Either[String, Generator[LocalDateTime]], val b: Either[String, Generator[LocalDateTime]]) extends Generator[Duration](name, "dt::diff")
{
   override def timeseries(generators: String => Generator[Any]) = {
      val aTS = Model.generator(generators)(a).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      val bTS = Model.generator(generators)(b).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      new DateTimeDifferenceTimeSeries(aTS, bTS)
   }

   override def toString = s"DateTimeDifferenceGenerator(${a}, ${b})"

   override def equals(o: Any) = o match {
      case that: DateTimeDifferenceGenerator => that.name == this.name && this.a == that.a && this.b == that.b
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson,
         "a" -> either2json(a),
         "b" -> either2json(b)
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object DateTimeDifferenceGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], a: String, b: String) = new DateTimeDifferenceGenerator(name, Left(a), Left(b))
   def apply(name: Option[String], a: String, b: Generator[LocalDateTime]) = new DateTimeDifferenceGenerator(name, Left(a), Right(b))
   def apply(name: Option[String], a: Generator[LocalDateTime], b: String) = new DateTimeDifferenceGenerator(name, Right(a), Left(b))
   def apply(name: Option[String], a: Generator[LocalDateTime], b: Generator[LocalDateTime]) = new DateTimeDifferenceGenerator(name, Right(a), Right(b))

   def apply(json: JsValue): DateTimeDifferenceGenerator = {

      val fields = json.asJsObject.fields
      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val a = fields("a") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      val b = fields("b") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      new DateTimeDifferenceGenerator(name, a, b)
   }
} 
Example 56
Source File: HourGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.dt

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.dt.{DayOfYearTimeSeries, HourTimeSeries}
import org.joda.time.LocalDateTime
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _}


class HourGenerator(name: Option[String], base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "hour")
{
   override def timeseries(generators: String => Generator[Any]) = {
      val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      new HourTimeSeries(ts)
   }

   override def toString = "HourGenerator()"

   override def equals(o: Any) = o match {
      case that: HourGenerator => that.name == this.name
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson,
         "base" -> either2json(base)
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object HourGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], base: String) = new HourGenerator(name, Left(base))
   def apply(name: Option[String], base: Generator[LocalDateTime]) = new HourGenerator(name, Right(base))

   def apply(json: JsValue): HourGenerator = {

      val fields = json.asJsObject.fields
      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val base = fields("base") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      new HourGenerator(name, base)
   }
} 
Example 57
Source File: MonthGenerator.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.generators.dt

import be.cetic.tsimulus.config.{GeneratorFormat, Model}
import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import be.cetic.tsimulus.timeseries.dt.MonthTimeSeries
import org.joda.time.LocalDateTime
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, _}


class MonthGenerator(name: Option[String], val base: Either[String, Generator[LocalDateTime]]) extends Generator[Int](name, "month")
{
   override def timeseries(generators: String => Generator[Any]) = {
      val ts = Model.generator(generators)(base).timeseries(generators).asInstanceOf[TimeSeries[LocalDateTime]]
      new MonthTimeSeries(ts)
   }

   override def toString = "MonthGenerator()"

   override def equals(o: Any) = o match {
      case that: MonthGenerator => (that.name == this.name) && (that.base == this.base)
      case _ => false
   }

   override def toJson: JsValue = {

      val t = Map(
         "type" -> `type`.toJson,
         "base" -> either2json(base)
      )

      new JsObject(
         name.map(n => t + ("name" -> n.toJson)).getOrElse(t)
      )
   }
}

object MonthGenerator extends DefaultJsonProtocol with TimeToJson
{
   def apply(name: Option[String], base: String) = new MonthGenerator(name, Left(base))
   def apply(name: Option[String], base: Generator[LocalDateTime]) = new MonthGenerator(name, Right(base))

   def apply(json: JsValue): MonthGenerator = {

      val fields = json.asJsObject.fields

      val name = fields.get("name") .map(f => f match {
         case JsString(x) => x
      })

      val base = fields("base") match {
         case JsString(s) => Left(s)
         case g => Right(GeneratorFormat.read(g).asInstanceOf[Generator[LocalDateTime]])
      }

      new MonthGenerator(name, base)
   }
} 
Example 58
Source File: Models.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.config

import be.cetic.tsimulus.generators._
import be.cetic.tsimulus.generators.binary._
import be.cetic.tsimulus.generators.composite._
import be.cetic.tsimulus.generators.missing.{DefaultGenerator, LimitedGenerator, PartialGenerator, UndefinedGenerator}
import be.cetic.tsimulus.generators.primary._
import be.cetic.tsimulus.generators.dt._
import org.joda.time.LocalDateTime
import spray.json.{JsString, _}

object Model
{
   def generator(generators: String => Generator[Any])(element: Either[String, Generator[Any]]): Generator[Any] =
      element match {
         case Left(s) => generators(s)
         case Right(g) => g
      }
}


case class ARMAModel(phi: Option[Seq[Double]],
                     theta: Option[Seq[Double]],
                     std: Double,
                     c: Double,
                     seed: Option[Long])


object GeneratorFormat extends JsonFormat[Generator[Any]]
{

   def deserializationError(s: String): Generator[Any] = throw DeserializationException(s)

   def serializationError(s: String): JsValue = throw new SerializationException(s)

   override def read(json: JsValue): Generator[Any] = json match {
      case known:JsObject if known.fields.contains("type") =>
         known.fields("type") match{
            case JsString("arma") => ARMAGenerator(known)
            case JsString("daily") => DailyGenerator(known)
            case JsString("weekly") => WeeklyGenerator(known)
            case JsString("monthly") => MonthlyGenerator(known)
            case JsString("yearly") => YearlyGenerator(known)
            case JsString("constant") => ConstantGenerator(known)
            case JsString("aggregate") => AggregateGenerator(known)
            case JsString("divide") => DivideGenerator(known)
            case JsString("correlated") => CorrelatedGenerator(known)
            case JsString("logistic") => LogisticGenerator(known)
            case JsString("conditional") => ConditionalGenerator(known)
            case JsString("true") => TrueGenerator(known)
            case JsString("false") => FalseGenerator(known)
            case JsString("transition") => TransitionGenerator(known)
            case JsString("binary-transition") => BinaryTransitionGenerator(known)
            case JsString("window") => SlidingWindowGenerator(known)
            case JsString("limited") => LimitedGenerator(known)
            case JsString("partial") => PartialGenerator(known)
            case JsString("time-shift") => TimeShiftGenerator(known)
            case JsString("function") => FunctionGenerator(known)
            case JsString("and") => AndGenerator(known)
            case JsString("or") => OrGenerator(known)
            case JsString("not") => NotGenerator(known)
            case JsString("xor") => XorGenerator(known)
            case JsString("implies") => ImpliesGenerator(known)
            case JsString("equiv") => EquivGenerator(known)
            case JsString("undefined") => UndefinedGenerator(known)
            case JsString("first-of") => DefaultGenerator(known)
            case JsString("greater-than") => GreaterThanGenerator(known)
            case JsString("lesser-than") => LesserThanGenerator(known)
            case JsString("gaussian") => GaussianNoiseGenerator(known)
            case JsString("year") => YearGenerator(known)
            case JsString("month") => MonthGenerator(known)
            case JsString("dom") => DayOfMonthGenerator(known)
            case JsString("hour") => HourGenerator(known)
            case JsString("minute") => MinuteGenerator(known)
            case JsString("second") => SecondTimeGenerator(known)
            case JsString("ms") => MillisecondTimeGenerator(known)
            case JsString("week") => WeekGenerator(known)
            case JsString("dow") => DayOfWeekGenerator(known)
            case JsString("doy") => DayOfYearGenerator(known)
            case JsString("now") => NowGenerator(known)
            case JsString("dt::diff") => DateTimeDifferenceGenerator(known)
            case JsString("sinus") => SinusGenerator(known)
            case unknown => deserializationError(s"unknown Generator object: $unknown")
         }
      case unknown => deserializationError(s"unknown  Generator object: $unknown")
   }

   override def write(obj: Generator[Any]): JsValue = obj.toJson
} 
Example 59
Source File: Configuration.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.config

import be.cetic.tsimulus.generators.{Generator, TimeToJson}
import be.cetic.tsimulus.timeseries.TimeSeries
import com.github.nscala_time.time.Imports._
import org.joda.time.LocalDateTime
import spray.json.{JsArray, JsObject, JsValue, _}

case class Configuration(generators: Option[Seq[Generator[Any]]],
                         series: Seq[Series[Any]],
                         from: LocalDateTime,
                         to: LocalDateTime) extends TimeToJson
{
   
   def timeSeries: Map[String, (TimeSeries[Any], Duration)] =
   {
      val memory = firstOrderGenerators

      series.map(s => {
         val duration = s.frequency
         val generator = Model.generator(memory)(s.generator)

         s.name -> (generator.timeseries(memory), duration)
      }).toMap
   }

   def firstOrderGenerators: Map[String, Generator[Any]] =
   {
      generators match {
         case None => Map()
         case Some(gens) => {
            val memory = scala.collection.mutable.Map[String, Generator[Any]]()

            gens.foreach(g => {
               memory.put(g.name.get, g)
            })

            memory.toMap
         }
      }
   }

   def toJson: JsValue = {
      new JsObject(Map(
         "generators" -> generators.map(g => g.map(_.toJson)).toJson,
         "exported" -> series.map(s => s.toJson).toJson,
         "from" -> from.toJson,
         "to" -> to.toJson
      ))
   }
}

object Configuration extends TimeToJson
{
   def apply(value: JsValue): Configuration = {
      val fields = value.asJsObject.fields

      val generators = fields.get("generators").map
      {
         case JsArray(l) => l.map(GeneratorFormat.read)
         case _ => throw new ClassCastException
      }

      val series = fields("exported") match {
         case JsArray(x) => x.map(Series[Any](_)).toSeq
         case _ => throw new ClassCastException
      }

      val from = fields("from").convertTo[LocalDateTime]
      val to = fields("to").convertTo[LocalDateTime]

      Configuration(generators, series, from, to)
   }
} 
Example 60
Source File: DefaultTimeSeries.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.timeseries.missing

import be.cetic.tsimulus.timeseries.TimeSeries
import org.joda.time.LocalDateTime


case class DefaultTimeSeries[T](generators: Seq[TimeSeries[T]]) extends TimeSeries[T]
{
   override def compute(times: Stream[LocalDateTime]) =
   {
      generators match {
         case Seq() => times.map(t => (t, None))
         case _ => {
            val others = generators.map(c => c.compute(times).map(Seq(_)))
               .reduce((s1, s2) => (s1 zip s2).map(e => e._1 ++ e._2))
               .map(seq => (seq.head._1, seq.map(_._2)))
               .map(entry => (entry._1, entry._2.flatten) )

            others map {
               case(t: LocalDateTime, s: Seq[T]) => (t, s.headOption)
            }
         }
      }
   }

   override def compute(time: LocalDateTime): Option[T] = generators.map(g => g.compute(time)).flatten.headOption
} 
Example 61
Source File: ViewSchedulingListenerActorSpec.scala    From schedoscope   with Apache License 2.0 5 votes vote down vote up
package org.schedoscope.scheduler.actors

import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, TestActorRef, TestKit, TestProbe}
import org.joda.time.LocalDateTime
import org.scalatest.{BeforeAndAfterAll, FlatSpecLike, Matchers}
import org.schedoscope.dsl.Parameter._
import org.schedoscope.scheduler.listeners.{RetryableViewSchedulingListenerException, ViewSchedulingListenerException}
import org.schedoscope.scheduler.messages.{RegisterFailedListener, ViewSchedulingMonitoringEvent}
import org.schedoscope.scheduler.states._
import test.views.Brand

import scala.concurrent.duration._

class ViewSchedulingListenerActorSpec extends TestKit(ActorSystem("schedoscope"))
  with ImplicitSender
  with FlatSpecLike
  with Matchers
  with BeforeAndAfterAll {

  override def afterAll() = {
    TestKit.shutdownActorSystem(system)
  }

  val TIMEOUT = 5 seconds

  "A viewSchedulingListenerActor" should "execute listener handler methods" in {
    val viewSchedulingListenerManagerActor = TestProbe()
    val handlerClassName = "org.schedoscope.test.TestViewListener"
    val viewSchedulingListenerActor = TestActorRef(ViewSchedulingListenerActor.props(
      handlerClassName, viewSchedulingListenerManagerActor.ref))

    val view = Brand(p("ec01"))
    val prevState1 = CreatedByViewManager(view)

    // confirm listener method is being executed correctly
    intercept[RetryableViewSchedulingListenerException] {
      viewSchedulingListenerActor.receive(
        ViewSchedulingMonitoringEvent(prevState1, prevState1, Set(Transform(view)), new LocalDateTime()))
    }
    // since at it, confirm that listener actor handles retryable exceptions
    // and tries to cache in viewSchedulingListenerManagerActor as receiver of
    // latest events
    viewSchedulingListenerManagerActor.expectMsg(RegisterFailedListener(handlerClassName))

    val newState1 = Failed(view)
    // confirm listener method is being executed correctly
    intercept[ViewSchedulingListenerException] {
      viewSchedulingListenerActor.receive(
        ViewSchedulingMonitoringEvent(prevState1, newState1, Set(Transform(view)), new LocalDateTime()))
    }
    // Confirm that listener actor does not register for receiving latest events!
    viewSchedulingListenerManagerActor.expectNoMsg(TIMEOUT)
  }


} 
Example 62
Source File: LimitedTimeSeries.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.timeseries.missing

import be.cetic.tsimulus.timeseries.TimeSeries
import com.github.nscala_time.time.Imports._
import org.joda.time.LocalDateTime



case class LimitedTimeSeries[T] (base: TimeSeries[T], from: Option[LocalDateTime], to: Option[LocalDateTime]) extends TimeSeries[T]
{
   override def compute(times: Stream[LocalDateTime]): Stream[(LocalDateTime, Option[T])] =
   {
      base.compute(times).map {case (t,v) => {

         val fromCondition = from match {
            case None => true
            case Some(x) => t >= x
         }

         val toCondition = to match {
            case None => true
            case Some(x) => t <= x
         }

         val modified = if(fromCondition && toCondition) None
                        else v

         (t, modified)
      }}
   }

   override def compute(time: LocalDateTime): Option[T] =
   {
      val fromCondition = from match {
         case None => true
         case Some(x) => time >= x
      }

      val toCondition = to match {
         case None => true
         case Some(x) => time <= x
      }

      if(fromCondition && toCondition) None
      else base.compute(time)
   }
} 
Example 63
Source File: CorrelatedTimeSeries.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.timeseries.composite

import be.cetic.tsimulus.timeseries.TimeSeries
import org.joda.time.{DateTimeZone, LocalDateTime}

import scala.util.Random


case class CorrelatedTimeSeries(base: TimeSeries[Double],
                                seed: Int,
                                rho: Double) extends TimeSeries[Double]
{
   val rho_square = rho*rho

   override def compute(times: Stream[LocalDateTime]) =
   {
      val r = new Random(seed)
      base.compute(times)
          .map {case(t,v) => (t, v.map(a => (rho * a) + (math.sqrt(1 - rho_square) * r.nextGaussian)))}
   }

   override def compute(time: LocalDateTime): Option[Double] =
   {
      val r = new Random(seed+time.toDateTime(DateTimeZone.UTC).getMillis)

      base.compute(time) match {
         case None => None
         case Some(x) => Some((rho * x) + (math.sqrt(1 - rho_square) * r.nextGaussian))
      }
   }
} 
Example 64
Source File: SlidingWindowTimeSeries.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.timeseries.composite

import be.cetic.tsimulus.Utils
import be.cetic.tsimulus.timeseries.{IndependantTimeSeries, TimeSeries}
import org.joda.time.{Duration, LocalDateTime}
import com.github.nscala_time.time.Imports._


case class SlidingWindowTimeSeries[T](base: TimeSeries[T], duration: Duration, n: Int, aggregator: Seq[(Duration, T)] => Option[T]) extends IndependantTimeSeries[T]
{
   override def compute(time: LocalDateTime): Option[T] =
   {
      val start = time - duration
      val dates = Utils.sampling(start, time, n)
      val values = base.compute(dates).map(v => v match {
         case (l: LocalDateTime, Some(x)) => Some((new Duration(l.toDateTime(DateTimeZone.UTC), time.toDateTime(DateTimeZone.UTC)), x))
         case (l: LocalDateTime, None) => None
      } ).flatten
         .toSeq

      aggregator(values)
   }
} 
Example 65
Source File: TransitionTimeSeries.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.timeseries.composite

import be.cetic.tsimulus.timeseries.TimeSeries
import com.github.nscala_time.time.Imports._
import org.joda.time.{Duration, LocalDateTime}


case class TransitionTimeSeries[T](first: TimeSeries[T],
                                   second: TimeSeries[T],
                                   time: LocalDateTime,
                                   transition: Option[(Duration, (T,T,Double) => T)]) extends TimeSeries[T]
{
   override def compute(times: Stream[LocalDateTime]): Stream[(LocalDateTime, Option[T])] =
   {
      val vFirst = first.compute(times)
      val vSecond = second.compute(times)

      (vFirst zip vSecond).map { case (s1, s2) => {
         val t = s1._1

         val v1 = s1._2
         val v2 = s2._2

         (t, process(t, v1, v2))
      }}
   }

   override def compute(time: LocalDateTime): Option[T] = process(time, first.compute(time), second.compute(time))

   private def process(t: LocalDateTime, v1: Option[T], v2: Option[T]): Option[T] =
   {
      if (t <= time) v1
      else
      {
         transition match
         {
            case None => v2
            case Some((duration, f)) =>
            {
               if (t > time + duration)
               {
                  v2
               }
               else // Real mixing
               {
                  if (v1.isEmpty)
                  {
                     v2
                  }
                  else if (v2.isEmpty)
                       {
                          v1
                       }
                  else
                  {
                     val ratio = new Duration(
                        time.toDateTime(DateTimeZone.UTC),
                        t.toDateTime(DateTimeZone.UTC)
                     ).getMillis / duration.getMillis.toDouble

                     Some(f(v1.get, v2.get, ratio))
                  }
               }
            }
         }
      }
   }
} 
Example 66
Source File: ConditionalTimeSeries.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.timeseries.composite

import be.cetic.tsimulus.timeseries.TimeSeries
import org.joda.time.LocalDateTime


case class ConditionalTimeSeries[T](condition: TimeSeries[Boolean], success: TimeSeries[T], failure: TimeSeries[T]) extends TimeSeries[T]
{
   override def compute(times: Stream[LocalDateTime]) =
   {
      (condition.compute(times), success.compute(times), failure.compute(times)).zipped.map { (c,s,f) => {
         assert(c._1 == s._1)
         assert(c._1 == f._1)

         val time = c._1

         c._2 match {
            case Some(true) => (time, s._2)
            case Some(false) => (time, f._2)
            case _ => (time, None)
         }
      }}
   }

   override def compute(time: LocalDateTime): Option[T] =
   {
      condition.compute(time) match {
         case None => None
         case Some(true) => success.compute(time)
         case Some(false) => failure.compute(time)
      }
   }
} 
Example 67
Source File: LogisticTimeSeries.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.timeseries.binary

import be.cetic.tsimulus.timeseries.TimeSeries
import org.joda.time.{DateTimeZone, LocalDateTime}

import scala.util.Random


case class LogisticTimeSeries(base: TimeSeries[Double],
                              location: Double,
                              scale: Double,
                              seed: Int) extends TimeSeries[Boolean]
{

   override def compute(times: Stream[LocalDateTime]) =
   {
      def logit(x: Double) = 1 / (1 + Math.exp(- ((x - location) / scale)))

      base.compute(times).map { case (t,v) => (t, v.map(x => {
         val r = new Random(seed + t.toDateTime(DateTimeZone.UTC).getMillis)
         r.nextDouble() < logit(x)
      } ))}
   }

   override def compute(time: LocalDateTime): Option[Boolean] =
   {
      val r = new Random(seed+time.toDateTime(DateTimeZone.UTC).getMillis)
      def logit(x: Double) = 1 / (1 + Math.exp(- ((x - location) / scale)))

      base.compute(time).map(x => r.nextDouble() < logit(x))
   }
} 
Example 68
Source File: BinaryTimeSeries.scala    From TSimulus   with Apache License 2.0 5 votes vote down vote up
package be.cetic.tsimulus.timeseries

import org.joda.time.LocalDateTime


case class BinaryTimeSeries[T,R](a: TimeSeries[T], b: TimeSeries[T], operator: (Option[T],Option[T]) => Option[R]) extends TimeSeries[R]
{
   override def compute(time: LocalDateTime) = operator(a.compute(time), b.compute(time))

   override def compute(times: Stream[LocalDateTime]) =
   {
      val xs = a.compute(times)
      val ys = b.compute(times)

      (xs zip ys) map {
         case (x,y) =>
         {
            assert(x._1 == y._1)

            (x._1, operator(x._2, y._2))
         }
      }
   }
}