object StreamUtils {
def sequencedSource[T](sources: immutable.Seq[Source[T, Unit]]): Source[immutable.Seq[T], Unit] =
Source.fromGraph(FlowGraph.create() { implicit builder =>
import FlowGraph.Implicits._
val sequenceGraphStage = builder.add(new SequenceGraphStage[T](sources.size))
for ((source, index) <- sources.zipWithIndex) source ~> sequenceGraphStage.in(index)
SourceShape(sequenceGraphStage.out)
})
}
class SequenceGraphStage[T](numInputs: Int) extends GraphStage[UniformFanInShape[T, immutable.Seq[T]]] {
require(numInputs > 0, "1 or more inputs required")
val inputs = immutable.IndexedSeq.tabulate(numInputs)(num => Inlet[T]("Sequence.in" + num))
val output = Outlet[immutable.Seq[T]]("Sequence.out")
override val shape = UniformFanInShape(output, inputs: _*)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) {
val elements = mutable.Buffer.empty[T]
setHandler(output, new OutHandler {
override def onPull(): Unit =
inputs.foreach { input =>
read(input) { element =>
elements += element
if (elements.size == inputs.size) {
push(output, elements.to[immutable.Seq])
elements.clear()
}
}
}
})
inputs.foreach(setHandler(_, eagerTerminateInput))
}
override val toString = "Sequence"
}
val seqOfSources: Seq[Source[Int, Unit]] = Seq(List(1,2), List(3,4), List(5,6)).map(Source(_))
val sourceOfSeqs: Source[Seq[Int], Unit] = Source(seqOfSources.toList).flatMapConcat(identity).grouped(100)
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
> That sounds like a feature of questionable value
I have multiple sources of time series data. Each source is over the same time range and I'd like to aggregate them into one time series. So I use this method then on each of the resulting source's sequences I map it into the sum or average of all those points.
I'm genuinely curious if there's a better way to do it.
> it's very easy to arrive at OOMEs with such a method.
How so?
type TimeSeriesPoint = (DateTime, Int)
val timeSeriesSources: List[Source[TimeSeriesPoint, Unit]] = ???
def total(points: immutable.Seq[TimeSeriesPoint]): TimeSeriesPoint =
points.reduce((point1, point2) => (point1._1, point1._2 + point2._2))
StreamUtils.sequencedSource(timeSeriesSources).map(total)
>Why would you need to turn them into Seqs for that?Because....that's just the way I did it and it seems to be working. I find it useful because it meets my business requirement. I'm not saying it's the answer, I'm just seeking a code critique and advice from any of you much smarter people who have a little time to spare :)
This is basically what I do:type TimeSeriesPoint = (DateTime, Int)
val timeSeriesSources: List[Source[TimeSeriesPoint, Unit]] = ???
def total(points: immutable.Seq[TimeSeriesPoint]): TimeSeriesPoint =
points.reduce((point1, point2) => (point1._1, point1._2 + point2._2))
StreamUtils.sequencedSource(timeSeriesSources).map(total)
>Because Sources are not bounded in nature.Ah, yes. Good point. I only use this on Sources of time series data we've queried from the database. So we're only using it on bounded sources by convention; is there a type that represents a bounded source? That might be useful.