@yarkivaev/scada 2.7.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/db/migrations/C0008__segment_write_receipts_grants.sql +1 -0
- package/db/migrations/E0019__segment_write_fence.sql +36 -0
- package/db/migrations/E0020__segment_write_fence_grants.sql +4 -0
- package/db/migrations/V0015__segment_write_receipts.sql +7 -0
- package/index.js +2 -0
- package/package.json +6 -3
- package/src/application/supervisorSink.js +15 -5
- package/src/domain/segment/segmentCoalesce.js +2 -1
- package/src/infrastructure/http/edge/edgeApi.js +1 -1
- package/src/infrastructure/http/edge/routes/history/historyRoutes.js +8 -3
- package/src/infrastructure/ingest/codecs/segmentCodec.js +26 -4
- package/src/infrastructure/ingest/db/migrationProfile.js +4 -1
- package/src/infrastructure/ingest/pipelines/segmentPipeline.js +11 -5
- package/src/infrastructure/ingest/sinks/fencedSegments.js +117 -0
- package/src/infrastructure/persistence/pg/history.js +4 -3
- package/src/infrastructure/persistence/pg/segmentWriteFence.js +112 -0
- package/stream-processing/dependencies/stream-worker.jar +0 -0
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Condition.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/CyclePhases.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Decision.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Emission.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/MachineConfig.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/MachineId.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/MachineMode.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Params.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Reading.scala +1 -1
- package/stream-processing/src/main/scala/scada/processing/core/ReadingWorker.scala +82 -0
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Segment.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/SegmentDef.scala +1 -1
- package/stream-processing/src/main/scala/scada/processing/core/Segmentation.scala +47 -0
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Segments.scala +3 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/SplitFollow.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/SplitPieces.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/TagDef.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/TaggedSegment.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Tagging.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Timestamp.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/segmentation/Timeline.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/checkpoint/Checkpoint.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/checkpoint/ClickhouseReadingsCheckpoint.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/checkpoint/SupervisorSinkCheckpoint.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/config/AppConfig.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/config/ConfigLoad.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/config/ConfigProfile.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/mqtt/Mqtt.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/mqtt/Sessions.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/state/AtomicStateFile.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/state/CheckpointSegments.scala +7 -5
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/state/SegmentStatePolicy.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/state/Segments.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/ClassifyQueue.scala +4 -4
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/DecisionMessage.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/DurableStomp.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/SegmentMessage.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/SegmentWirePolicy.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/SplitFamily.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompConnection.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompDecisions.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompFrame.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompMachineDecisions.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompQueue.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompReadings.scala +3 -4
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompSegments.scala +20 -13
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/ClassifyHistory.scala +3 -3
- package/stream-processing/src/main/scala/scada/processing/runtime/ClassifyStreams.scala +128 -0
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/Close.scala +4 -4
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/HealthServer.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/HistoryBus.scala +40 -8
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/HistoryClient.scala +11 -4
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/HistoryRun.scala +42 -40
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/LocalHistory.scala +122 -36
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/ObservationLead.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/Orphans.scala +4 -4
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/PendingIsland.scala +3 -3
- package/stream-processing/src/main/scala/scada/processing/runtime/ProcessingApp.scala +8 -0
- package/{supervisor-engine/src/main/scala/scada/supervisor/runtime/SupervisorRuntime.scala → stream-processing/src/main/scala/scada/processing/runtime/ProcessingApplication.scala} +108 -106
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/RuntimeProfile.scala +10 -12
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/Seeding.scala +4 -4
- package/stream-processing/src/main/scala/scada/processing/runtime/TimelineWriter.scala +81 -0
- package/stream-processing/src/main/scala/scada/processing/runtime/WorkerDatabase.scala +7 -0
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/WriterEpoch.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/WriterGate.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/WriterLease.scala +11 -9
- package/{supervisor-engine → stream-processing}/src/main/scala/scada/stream/replay/Replay.scala +1 -1
- package/stream-processing/stream-worker.lock +2 -0
- package/supervisor-engine/src/main/scala/scada/stream/worker/Processor.scala +0 -8
- package/supervisor-engine/src/main/scala/scada/stream/worker/StreamWorker.scala +0 -119
- package/supervisor-engine/src/main/scala/scada/stream/worker/postgres/PgCheckpointStore.scala +0 -83
- package/supervisor-engine/src/main/scala/scada/supervisor/core/Segmentation.scala +0 -52
- package/supervisor-engine/src/main/scala/scada/supervisor/core/Supervisor.scala +0 -66
- package/supervisor-engine/src/main/scala/scada/supervisor/runtime/ClassifierCursors.scala +0 -58
- package/supervisor-engine/src/main/scala/scada/supervisor/runtime/ClassifyStreams.scala +0 -67
- package/supervisor-engine/src/main/scala/scada/supervisor/runtime/EngineApp.scala +0 -4
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/LICENSE +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/UPSTREAM +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/Island.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandEvent.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsFlow.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsPolicy.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/temporal-streams/LICENSE +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/temporal-streams/UPSTREAM +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/temporal-streams/src/main/scala/io/yarkivaev/temporal/History.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/temporal-streams/src/main/scala/io/yarkivaev/temporal/LocalStreams.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/temporal-streams/src/main/scala/io/yarkivaev/temporal/Overlay.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/temporal-streams/src/main/scala/io/yarkivaev/temporal/Worker.scala +0 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
package scada.processing.core
|
|
2
|
+
|
|
3
|
+
import cats.effect.IO
|
|
4
|
+
import cats.syntax.all.*
|
|
5
|
+
import fs2.Stream
|
|
6
|
+
import io.yarkivaev.streamworker.*
|
|
7
|
+
import scada.processing.core.segmentation.Timeline.Interval
|
|
8
|
+
import scala.concurrent.duration.*
|
|
9
|
+
|
|
10
|
+
/** Assembles the MQTT session input and its segment output port. */
|
|
11
|
+
final class ReadingWorker(
|
|
12
|
+
segmentation: Segmentation,
|
|
13
|
+
tagging: Tagging,
|
|
14
|
+
segments: Segments[IO],
|
|
15
|
+
phases: CyclePhases = CyclePhases.empty,
|
|
16
|
+
outputs: Option[OutputScope] = None
|
|
17
|
+
):
|
|
18
|
+
import ReadingWorker.*
|
|
19
|
+
|
|
20
|
+
private val processor = new Processor[MachineConfig, State, Input, TaggedSegment]:
|
|
21
|
+
def step(machine: MachineConfig, state: State, input: Input): Either[ProcessingError, StepResult[State, TaggedSegment]] =
|
|
22
|
+
input match
|
|
23
|
+
case Input.Sample(reading) =>
|
|
24
|
+
segmentation.step(machine, state, reading).map(result =>
|
|
25
|
+
StepResult(result.state, result.outputs.map(emission => tagging.resolve(machine.tags, emission.value))))
|
|
26
|
+
case Input.Restore(cursor) =>
|
|
27
|
+
Right(StepResult(state, Vector(pending(cursor.name, cursor.start, cursor.end))))
|
|
28
|
+
case Input.Pulse(now, rows) =>
|
|
29
|
+
Right(StepResult(state, pulse(now, rows).toVector))
|
|
30
|
+
|
|
31
|
+
def run(machine: MachineConfig, readings: Stream[IO, Reading], initial: Option[SegmentCursor] = None): IO[Unit] =
|
|
32
|
+
if machine.mode.isDefined then
|
|
33
|
+
IO.raiseError(IllegalStateException("A machine mode requires its own processor profile"))
|
|
34
|
+
else
|
|
35
|
+
for
|
|
36
|
+
id <- IO.randomUUID
|
|
37
|
+
task = TaskIdentity(machine.id.raw, "mqtt", id.toString)
|
|
38
|
+
// MQTT reseeds from the timeline checkpoint rather than replaying a stable broker position.
|
|
39
|
+
snapshot = Checkpoint[State, TaggedSegment](task, mqttVersion, 0, None, segmentation.seed(initial), Vector.empty)
|
|
40
|
+
store <- CheckpointStore.session(snapshot, 1)
|
|
41
|
+
worker = StreamWorker(processor, store, (_, row) =>
|
|
42
|
+
outputs.fold(write(machine, row.value))(_.within(row.id)(write(machine, row.value))))
|
|
43
|
+
_ <- worker.run(task, mqttVersion, machine, 1): _ =>
|
|
44
|
+
val samples = prepareInput(readings).map(reading => IO.pure(Input.Sample(reading)))
|
|
45
|
+
val beats = Stream.awakeEvery[IO](1.second).as(for
|
|
46
|
+
rows <- segments.list(machine.id)
|
|
47
|
+
now <- IO.realTime.map(value => Timestamp(value.toMillis.toDouble / 1000.0))
|
|
48
|
+
yield Input.Pulse(now, rows))
|
|
49
|
+
val restore = Stream.emits(initial.toList).map(cursor => IO.pure(Input.Restore(cursor)))
|
|
50
|
+
(restore ++ samples.mergeHaltL(beats)).evalMap(identity).zipWithIndex.map: (input, position) =>
|
|
51
|
+
Delivery(position, input, IO.unit)
|
|
52
|
+
yield ()
|
|
53
|
+
|
|
54
|
+
private def write(machine: MachineConfig, tagged: TaggedSegment): IO[Unit] = segments.atRevision(machine.id):
|
|
55
|
+
if phases.isEmpty then segments.upsert(machine.id, tagged)
|
|
56
|
+
else segments.list(machine.id).flatMap(all => segments.upsert(machine.id, phases.narrow(tagged, all)))
|
|
57
|
+
|
|
58
|
+
object ReadingWorker:
|
|
59
|
+
private type State = Option[Interval[Reading]]
|
|
60
|
+
private val mqttVersion = ProcessorVersion("mqtt-segments/1", "session", "timeline/1")
|
|
61
|
+
|
|
62
|
+
private[processing] def prepareInput(input: Stream[IO, Reading]): Stream[IO, Reading] =
|
|
63
|
+
input.groupWithin(32, 50.millis).flatMap: chunk =>
|
|
64
|
+
Stream.emits:
|
|
65
|
+
chunk.toList.groupBy(_.timestamp.epoch).toList.sortBy(_._1).map: (_, rows) =>
|
|
66
|
+
rows.reduce((left, right) => Reading(right.timestamp, left.params.merge(right.params)))
|
|
67
|
+
|
|
68
|
+
private enum Input:
|
|
69
|
+
case Sample(reading: Reading)
|
|
70
|
+
case Restore(cursor: SegmentCursor)
|
|
71
|
+
case Pulse(now: Timestamp, rows: List[TaggedSegment])
|
|
72
|
+
|
|
73
|
+
private def pending(name: String, start: Timestamp, end: Timestamp): TaggedSegment =
|
|
74
|
+
TaggedSegment(Segment(name, start, end, 0.0), Nil, Nil, Map.empty)
|
|
75
|
+
|
|
76
|
+
private def pulse(now: Timestamp, rows: List[TaggedSegment]): Option[TaggedSegment] =
|
|
77
|
+
rows.filter(_.segment.duration == 0).maxByOption(_.segment.start) match
|
|
78
|
+
case Some(open) =>
|
|
79
|
+
Option.when(now.isAfter(open.segment.end))(open.copy(segment = open.segment.copy(end = now)))
|
|
80
|
+
case None =>
|
|
81
|
+
rows.maxByOption(_.segment.end).filter(last => now.isAfter(last.segment.end))
|
|
82
|
+
.map(last => pending(last.segment.name, last.segment.end, now))
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
package scada.processing.core
|
|
2
|
+
|
|
3
|
+
import io.yarkivaev.streamworker.{ProcessingError, Processor, StepResult}
|
|
4
|
+
import scada.processing.core.segmentation.Timeline
|
|
5
|
+
import scada.processing.core.segmentation.Timeline.{Boundary, Event, Interval, Output}
|
|
6
|
+
|
|
7
|
+
final case class ReadingOverride(name: String, properties: Map[String, String])
|
|
8
|
+
|
|
9
|
+
/** Merges same-time readings and applies the live interval policy.
|
|
10
|
+
* Class changes close at the next phase's first sample. A gap over twice
|
|
11
|
+
* the machine's window splits equal phases without inventing downtime.
|
|
12
|
+
*/
|
|
13
|
+
final class Segmentation(overrideReading: Reading => Option[ReadingOverride] = _ => None)
|
|
14
|
+
extends Processor[MachineConfig, Option[Interval[Reading]], Reading, Emission[Segment]]:
|
|
15
|
+
private val unclassified = "unclassified"
|
|
16
|
+
|
|
17
|
+
private[processing] def seed(initial: Option[SegmentCursor]): Option[Interval[Reading]] =
|
|
18
|
+
initial.map: cursor =>
|
|
19
|
+
Interval(cursor.name, Reading(cursor.start, Params(Map.empty)), Reading(cursor.end, Params(Map.empty)))
|
|
20
|
+
|
|
21
|
+
override def step(machine: MachineConfig, open: Option[Interval[Reading]], reading: Reading)
|
|
22
|
+
: Either[ProcessingError, StepResult[Option[Interval[Reading]], Emission[Segment]]] =
|
|
23
|
+
Right(advance(machine, open, reading))
|
|
24
|
+
|
|
25
|
+
private def advance(machine: MachineConfig, open: Option[Interval[Reading]], reading: Reading)
|
|
26
|
+
: StepResult[Option[Interval[Reading]], Emission[Segment]] =
|
|
27
|
+
val classify = Timeline.classifier(machine.segments)
|
|
28
|
+
val name = overrideReading(reading).map(_.name).orElse(classify(reading.params))
|
|
29
|
+
.orElse(open.map(_.name)).getOrElse(unclassified)
|
|
30
|
+
val continuous = open.exists(interval => !(reading.timestamp.minus(interval.end.timestamp) > machine.window.toDouble * 2.0))
|
|
31
|
+
val result = Timeline.step(Boundary.Live, open, Event.Sample(reading, name, continuous))
|
|
32
|
+
StepResult(result.open, result.outputs.flatMap(toEmission))
|
|
33
|
+
|
|
34
|
+
private def toEmission(output: Output[Reading]): Option[Emission[Segment]] =
|
|
35
|
+
def segment(interval: Interval[Reading], closed: Boolean): Segment =
|
|
36
|
+
val start = interval.start.timestamp
|
|
37
|
+
val end = interval.end.timestamp
|
|
38
|
+
val duration = if closed then end.minus(start) else 0.0
|
|
39
|
+
val properties = overrideReading(interval.end).fold(Map.empty[String, String])(_.properties)
|
|
40
|
+
Segment(interval.name, start, end, duration, properties)
|
|
41
|
+
|
|
42
|
+
output match
|
|
43
|
+
case Output.Pending(interval) if interval.name != unclassified =>
|
|
44
|
+
Some(Emission.Started(segment(interval, closed = false)))
|
|
45
|
+
case Output.Closed(interval) if interval.name != unclassified =>
|
|
46
|
+
Some(Emission.Emitted(segment(interval, closed = true)))
|
|
47
|
+
case _ => None
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.core
|
|
2
2
|
|
|
3
3
|
import cats.Monad
|
|
4
4
|
import cats.syntax.all.*
|
|
@@ -21,6 +21,8 @@ import cats.syntax.all.*
|
|
|
21
21
|
* }}}
|
|
22
22
|
*/
|
|
23
23
|
trait Segments[F[_]]:
|
|
24
|
+
def atRevision[A](machine: MachineId)(prepareAndWrite: F[A]): F[A] = prepareAndWrite
|
|
25
|
+
|
|
24
26
|
def upsert(machine: MachineId, segment: TaggedSegment): F[Unit]
|
|
25
27
|
|
|
26
28
|
def upsertTransition(machine: MachineId, rows: List[TaggedSegment])(using Monad[F]): F[Unit] =
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.core.segmentation
|
|
2
2
|
|
|
3
|
-
import scada.
|
|
3
|
+
import scada.processing.core.{Params, SegmentDef}
|
|
4
4
|
|
|
5
5
|
/** Transitions over classified samples in input order. The coordinate type is
|
|
6
6
|
* chosen by the caller and is never rounded or compared here.
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.checkpoint
|
|
2
2
|
|
|
3
3
|
import cats.effect.IO
|
|
4
4
|
import fs2.Stream
|
|
5
|
-
import scada.
|
|
5
|
+
import scada.processing.core.*
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* One raw metric row used to seed segmentation on startup.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.checkpoint
|
|
2
2
|
|
|
3
3
|
import cats.effect.IO
|
|
4
4
|
import cats.syntax.all.*
|
|
@@ -7,8 +7,8 @@ import io.circe.parser.parse
|
|
|
7
7
|
import java.net.URI
|
|
8
8
|
import java.net.http.{HttpClient, HttpRequest, HttpResponse}
|
|
9
9
|
import java.time.Duration
|
|
10
|
-
import scada.
|
|
11
|
-
import scada.
|
|
10
|
+
import scada.processing.core.*
|
|
11
|
+
import scada.processing.infra.config.ClickhouseReadingsConfig
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Decorator that overrides readings fetch with ClickHouse paging.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.checkpoint
|
|
2
2
|
|
|
3
3
|
import cats.data.NonEmptyList
|
|
4
4
|
import cats.effect.IO
|
|
@@ -13,8 +13,8 @@ import java.util.concurrent.TimeUnit
|
|
|
13
13
|
import org.typelevel.log4cats.Logger
|
|
14
14
|
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
|
15
15
|
import scala.concurrent.duration.*
|
|
16
|
-
import scada.
|
|
17
|
-
import scada.
|
|
16
|
+
import scada.processing.core.*
|
|
17
|
+
import scada.processing.infra.config.CheckpointApiConfig
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* HTTP checkpoint client against supervisor-sink /v1/checkpoint API.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.config
|
|
2
2
|
|
|
3
|
-
import scada.
|
|
3
|
+
import scada.processing.core.{CyclePhases, MachineConfig, MachineId}
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Alias routing entry: which MQTT topic carries which metric.
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.config
|
|
2
2
|
|
|
3
3
|
import cats.effect.IO
|
|
4
4
|
import com.typesafe.config.{Config, ConfigFactory, ConfigValueType}
|
|
5
|
-
import scada.
|
|
5
|
+
import scada.processing.core.*
|
|
6
6
|
import scala.jdk.CollectionConverters.*
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.config
|
|
2
2
|
|
|
3
3
|
import com.typesafe.config.Config
|
|
4
|
-
import scada.
|
|
4
|
+
import scada.processing.core.{MachineConfig, MachineMode}
|
|
5
5
|
|
|
6
6
|
/** Extension points for site-specific HOCON rules and groups. */
|
|
7
7
|
trait ConfigProfile:
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.mqtt
|
|
2
2
|
|
|
3
3
|
import cats.effect.{IO, Resource}
|
|
4
4
|
import fs2.Stream
|
|
@@ -14,8 +14,8 @@ import org.eclipse.paho.client.mqttv3.{
|
|
|
14
14
|
import org.slf4j.LoggerFactory
|
|
15
15
|
import org.typelevel.log4cats.Logger
|
|
16
16
|
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
|
17
|
-
import scada.
|
|
18
|
-
import scada.
|
|
17
|
+
import scada.processing.core.*
|
|
18
|
+
import scada.processing.infra.config.{AliasMapping, MqttConfig}
|
|
19
19
|
import scala.concurrent.duration.*
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.state
|
|
2
2
|
|
|
3
3
|
import cats.effect.IO
|
|
4
4
|
import cats.syntax.all.*
|
|
5
|
-
import scada.
|
|
6
|
-
import scada.
|
|
7
|
-
import scada.
|
|
5
|
+
import scada.processing.core.*
|
|
6
|
+
import scada.processing.core.Segments as CoreSegments
|
|
7
|
+
import scada.processing.infra.checkpoint.Checkpoint
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* [[CoreSegments]] decorator that falls back to checkpoint API when
|
|
11
|
-
* the in-memory store misses one segment row.
|
|
11
|
+
* the in-memory store misses one segment row. `list` uses the local cache unless checkpoint-first reads are configured.
|
|
12
12
|
* History-mode decision reads may prefer checkpoint snapshots, refreshing exact
|
|
13
13
|
* cached keys through an injected port. Lists refresh already-known sibling keys;
|
|
14
14
|
* this decorator does not download or discover the complete historical family.
|
|
@@ -27,6 +27,8 @@ final class CheckpointSegments(
|
|
|
27
27
|
) extends CoreSegments[IO]:
|
|
28
28
|
require(!preferCheckpoint || refreshCache.isDefined,"Checkpoint-first decisions require an exact cache refresh port")
|
|
29
29
|
|
|
30
|
+
override def atRevision[A](machine: MachineId)(action: IO[A]): IO[A] = underlying.atRevision(machine)(action)
|
|
31
|
+
|
|
30
32
|
def find(machine: MachineId, start: Timestamp): IO[Option[TaggedSegment]] =
|
|
31
33
|
if preferCheckpoint then checkpoint.fetchSegment(machine,start).flatTap(row => refreshCache.get(machine,start,row))
|
|
32
34
|
else underlying.find(machine, start).flatMap:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.state
|
|
2
2
|
|
|
3
|
-
import scada.
|
|
3
|
+
import scada.processing.core.TaggedSegment
|
|
4
4
|
|
|
5
5
|
/** Preserves or replaces operator annotations when a segment is updated. */
|
|
6
6
|
trait SegmentStatePolicy:
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.state
|
|
2
2
|
|
|
3
3
|
import cats.Functor
|
|
4
4
|
import cats.effect.Ref
|
|
5
5
|
import cats.syntax.all.*
|
|
6
|
-
import scada.
|
|
7
|
-
import scada.
|
|
6
|
+
import scada.processing.core.*
|
|
7
|
+
import scada.processing.core.Segments as CoreSegments
|
|
8
8
|
import scala.collection.immutable.SortedMap
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.stomp
|
|
2
2
|
|
|
3
3
|
import cats.effect.{IO, Resource}
|
|
4
4
|
import fs2.Stream
|
|
5
5
|
import io.circe.parser.parse
|
|
6
6
|
import io.yarkivaev.temporal.Observation
|
|
7
|
-
import scada.
|
|
7
|
+
import scada.processing.infra.config.{AliasMapping, RabbitMqConfig}
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Durable AMQP classify queue for one machine. MQTT slash aliases match
|
|
11
11
|
* dotted routing keys. First-wins identity is the published eventId.
|
|
12
12
|
*
|
|
13
13
|
* {{{
|
|
14
|
-
* ClassifyQueue(config, "scada.classify.machine-2", aliases).
|
|
14
|
+
* ClassifyQueue(config, "scada.classify.machine-2", aliases).subscribeDurable
|
|
15
15
|
* }}}
|
|
16
16
|
*/
|
|
17
17
|
final class ClassifyQueue(open: Resource[IO, StompLink], queue: String, aliases: List[AliasMapping]):
|
|
18
18
|
def this(config: RabbitMqConfig, queue: String, aliases: List[AliasMapping]) =
|
|
19
19
|
this(StompConnection.open(config.address.host, config.address.port, config.vhost, config.credentials.login, config.credentials.passcode).map(connection => connection: StompLink), queue, aliases)
|
|
20
|
-
def
|
|
20
|
+
def subscribeDurable: Stream[IO, (Observation, IO[Unit])] = StompQueue(open, queue, decode).subscribeDurable
|
|
21
21
|
private[stomp] def decode(frame: StompFrame): Option[Observation] =
|
|
22
22
|
for
|
|
23
23
|
route <- routed(frame.headers)
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.stomp
|
|
2
2
|
|
|
3
3
|
import io.circe.Json
|
|
4
4
|
import io.circe.parser.parse as parseJson
|
|
5
|
-
import scada.
|
|
5
|
+
import scada.processing.core.{Decision, MachineId, SplitEntry, Timestamp}
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Inbound-wire parser for operator [[Decision]]s arriving on the
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.stomp
|
|
2
2
|
|
|
3
3
|
import io.circe.Json
|
|
4
4
|
import io.circe.syntax.*
|
|
5
|
-
import scada.
|
|
5
|
+
import scada.processing.core.{MachineId, TaggedSegment}
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Wire-format envelope for one event on the supervisor-sink channel.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.stomp
|
|
2
2
|
|
|
3
|
-
import scada.
|
|
3
|
+
import scada.processing.core.{TaggedSegment, Timestamp}
|
|
4
4
|
|
|
5
5
|
/** Controls the wire representation and publishing semantics for segment types. */
|
|
6
6
|
trait SegmentWirePolicy:
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.stomp
|
|
2
2
|
|
|
3
3
|
import io.circe.Json
|
|
4
4
|
import io.circe.syntax.*
|
|
5
|
-
import scada.
|
|
5
|
+
import scada.processing.core.{MachineId, TaggedSegment, Timestamp}
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* One STOMP body that replaces a whole split family. The sink applies
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.stomp
|
|
2
2
|
|
|
3
3
|
import cats.effect.{IO, Resource}
|
|
4
4
|
import fs2.Stream
|
|
5
|
-
import scada.
|
|
6
|
-
import scada.
|
|
5
|
+
import scada.processing.core.Decision
|
|
6
|
+
import scada.processing.infra.config.RabbitMqConfig
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* STOMP subscription emitting operator [[Decision]]s parsed from the
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.stomp
|
|
2
2
|
|
|
3
3
|
import cats.effect.IO
|
|
4
4
|
import fs2.Stream
|
|
5
|
-
import scada.
|
|
6
|
-
import scada.
|
|
5
|
+
import scada.processing.core.{Decision,MachineId}
|
|
6
|
+
import scada.processing.infra.config.RabbitMqConfig
|
|
7
7
|
|
|
8
8
|
/** A durable per-machine copy; unrelated machine decisions are discarded only from this copy. */
|
|
9
9
|
final class StompMachineDecisions(config: RabbitMqConfig, queue: String, machine: MachineId):
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.stomp
|
|
2
2
|
|
|
3
3
|
import cats.effect.{IO, Resource}
|
|
4
4
|
import fs2.Stream
|
|
5
5
|
import io.circe.parser.parse
|
|
6
|
-
import scada.
|
|
7
|
-
import scada.
|
|
6
|
+
import scada.processing.core.*
|
|
7
|
+
import scada.processing.infra.config.{AliasMapping,RabbitMqConfig}
|
|
8
8
|
|
|
9
9
|
/** One pre-bound durable AMQP queue preserves parameter/rise order. Slash aliases match dotted routing keys. */
|
|
10
10
|
final class StompReadings(open: Resource[IO,StompLink], queue: String, aliases: List[AliasMapping]):
|
|
11
11
|
def this(config: RabbitMqConfig, queue: String, aliases: List[AliasMapping]) =
|
|
12
12
|
this(StompConnection.open(config.address.host,config.address.port,config.vhost,config.credentials.login,config.credentials.passcode)
|
|
13
13
|
.map(connection => connection: StompLink),queue,aliases)
|
|
14
|
-
def subscribe: Stream[IO,Reading] = StompQueue(open,queue,decode).subscribe
|
|
15
14
|
def subscribeDurable: Stream[IO,(Reading,IO[Unit])] = StompQueue(open,queue,decode).subscribeDurable
|
|
16
15
|
private[stomp] def decode(frame: StompFrame): Option[Reading] =
|
|
17
16
|
for
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.infra.stomp
|
|
2
2
|
|
|
3
3
|
import cats.Monad
|
|
4
4
|
import cats.effect.IO
|
|
@@ -7,8 +7,9 @@ import io.circe.Json
|
|
|
7
7
|
import io.circe.syntax.*
|
|
8
8
|
import org.typelevel.log4cats.Logger
|
|
9
9
|
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
|
10
|
-
import scada.
|
|
11
|
-
import scada.
|
|
10
|
+
import scada.processing.core.*
|
|
11
|
+
import scada.processing.core.Segments as CoreSegments
|
|
12
|
+
import scada.processing.runtime.{HistoryHttpError, TimelineWriter}
|
|
12
13
|
import scala.collection.concurrent.TrieMap
|
|
13
14
|
import scala.concurrent.duration.*
|
|
14
15
|
|
|
@@ -40,7 +41,12 @@ final class StompSegments(
|
|
|
40
41
|
|
|
41
42
|
private given Logger[IO] = Slf4jLogger.getLogger[IO]
|
|
42
43
|
|
|
43
|
-
def
|
|
44
|
+
override def atRevision[A](machine: MachineId)(action: IO[A]): IO[A] =
|
|
45
|
+
connection match
|
|
46
|
+
case sink: TimelineWriter => sink.atRevision(machine.raw)(action)
|
|
47
|
+
case _ => action
|
|
48
|
+
|
|
49
|
+
def upsert(machine: MachineId, segment: TaggedSegment): IO[Unit] = atRevision(machine):
|
|
44
50
|
underlying.upsert(machine, segment) *>
|
|
45
51
|
underlying.list(machine).flatMap: rows =>
|
|
46
52
|
SplitFollow.affected(segment, rows) match
|
|
@@ -50,7 +56,7 @@ final class StompSegments(
|
|
|
50
56
|
case None => announce(machine, segment)
|
|
51
57
|
case Some(seg) => announce(machine, seg)
|
|
52
58
|
|
|
53
|
-
override def upsertTransition(machine: MachineId, rows: List[TaggedSegment])(using Monad[IO]): IO[Unit] =
|
|
59
|
+
override def upsertTransition(machine: MachineId, rows: List[TaggedSegment])(using Monad[IO]): IO[Unit] = atRevision(machine):
|
|
54
60
|
if !policy.atomicTransition || rows.size < 2 then super.upsertTransition(machine, rows)
|
|
55
61
|
else
|
|
56
62
|
val body = Json.obj("type" -> "phase-transition".asJson, "machine" -> machine.raw.asJson,
|
|
@@ -63,7 +69,7 @@ final class StompSegments(
|
|
|
63
69
|
def list(machine: MachineId): IO[List[TaggedSegment]] =
|
|
64
70
|
underlying.list(machine)
|
|
65
71
|
|
|
66
|
-
def replace(machine: MachineId, from: Timestamp, to: Timestamp, segments: List[TaggedSegment]): IO[Unit] =
|
|
72
|
+
def replace(machine: MachineId, from: Timestamp, to: Timestamp, segments: List[TaggedSegment]): IO[Unit] = atRevision(machine):
|
|
67
73
|
underlying.replace(machine, from, to, segments) *>
|
|
68
74
|
(if segments.isEmpty then IO.unit
|
|
69
75
|
else publishRaw(SplitFamily.json(machine, from, segments)))
|
|
@@ -74,7 +80,7 @@ final class StompSegments(
|
|
|
74
80
|
tags: List[String],
|
|
75
81
|
options: List[String],
|
|
76
82
|
properties: Map[String, String]
|
|
77
|
-
): IO[Unit] =
|
|
83
|
+
): IO[Unit] = atRevision(machine):
|
|
78
84
|
underlying.resolve(machine, original, tags, options, properties) *>
|
|
79
85
|
underlying.find(machine, original.start).flatMap:
|
|
80
86
|
case None => IO.unit
|
|
@@ -106,10 +112,11 @@ final class StompSegments(
|
|
|
106
112
|
retrying(connection.send(sent),3,100.millis)
|
|
107
113
|
|
|
108
114
|
private def retrying[A](io: IO[A], attempts: Int, delay: FiniteDuration): IO[A] =
|
|
109
|
-
io.handleErrorWith:
|
|
110
|
-
if
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
+
io.handleErrorWith:
|
|
116
|
+
case error: HistoryHttpError if error.status >= 400 && error.status < 500 => IO.raiseError(error)
|
|
117
|
+
case error if attempts <= 1 =>
|
|
118
|
+
Logger[IO].error(error)(s"timeline publish failed after all retries on $destination") *>
|
|
119
|
+
IO.raiseError(error)
|
|
120
|
+
case error =>
|
|
121
|
+
Logger[IO].warn(error)(s"timeline publish retry in ${delay.toMillis}ms on $destination") *>
|
|
115
122
|
IO.sleep(delay) *> retrying(io, attempts - 1, delay * 2)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
3
|
import cats.effect.{IO, Ref}
|
|
4
4
|
import cats.syntax.all.*
|
|
@@ -6,8 +6,8 @@ import fs2.Stream
|
|
|
6
6
|
import io.circe.Json
|
|
7
7
|
import io.yarkivaev.temporal.{Conflict, History, Observation}
|
|
8
8
|
import java.time.Instant
|
|
9
|
-
import scada.
|
|
10
|
-
import scada.
|
|
9
|
+
import scada.processing.core.{MachineConfig, MachineId, Segment, TaggedSegment, Timestamp}
|
|
10
|
+
import scada.processing.infra.stomp.SplitFamily
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Event-time machine classification from an AMQP classify stream.
|