@yarkivaev/scada 2.8.0 → 2.9.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.
Files changed (54) hide show
  1. package/package.json +4 -2
  2. package/stream-processing/dependencies/timeline-segmentation.jar +0 -0
  3. package/stream-processing/src/main/scala/scada/processing/core/Decision.scala +1 -11
  4. package/stream-processing/src/main/scala/scada/processing/core/MachineConfig.scala +1 -0
  5. package/stream-processing/src/main/scala/scada/processing/core/ReadingWorker.scala +6 -16
  6. package/stream-processing/src/main/scala/scada/processing/core/Segments.scala +1 -0
  7. package/stream-processing/src/main/scala/scada/processing/infra/checkpoint/Checkpoint.scala +1 -0
  8. package/stream-processing/src/main/scala/scada/processing/infra/checkpoint/ClickhouseReadingsCheckpoint.scala +1 -0
  9. package/stream-processing/src/main/scala/scada/processing/infra/checkpoint/SupervisorSinkCheckpoint.scala +1 -0
  10. package/stream-processing/src/main/scala/scada/processing/infra/config/AppConfig.scala +2 -2
  11. package/stream-processing/src/main/scala/scada/processing/infra/config/ConfigLoad.scala +1 -0
  12. package/stream-processing/src/main/scala/scada/processing/infra/config/ConfigProfile.scala +0 -1
  13. package/stream-processing/src/main/scala/scada/processing/infra/mqtt/Mqtt.scala +1 -1
  14. package/stream-processing/src/main/scala/scada/processing/infra/state/CheckpointSegments.scala +1 -0
  15. package/stream-processing/src/main/scala/scada/processing/infra/state/SegmentStatePolicy.scala +1 -2
  16. package/stream-processing/src/main/scala/scada/processing/infra/state/Segments.scala +1 -0
  17. package/stream-processing/src/main/scala/scada/processing/infra/stomp/DecisionMessage.scala +2 -2
  18. package/stream-processing/src/main/scala/scada/processing/infra/stomp/SegmentMessage.scala +2 -2
  19. package/stream-processing/src/main/scala/scada/processing/infra/stomp/SegmentWirePolicy.scala +1 -2
  20. package/stream-processing/src/main/scala/scada/processing/infra/stomp/SplitFamily.scala +2 -2
  21. package/stream-processing/src/main/scala/scada/processing/infra/stomp/StompMachineDecisions.scala +1 -1
  22. package/stream-processing/src/main/scala/scada/processing/infra/stomp/StompReadings.scala +1 -1
  23. package/stream-processing/src/main/scala/scada/processing/infra/stomp/StompSegments.scala +1 -0
  24. package/stream-processing/src/main/scala/scada/processing/runtime/ClassifyHistory.scala +2 -1
  25. package/stream-processing/src/main/scala/scada/processing/runtime/ClassifyStreams.scala +1 -0
  26. package/stream-processing/src/main/scala/scada/processing/runtime/Close.scala +1 -1
  27. package/stream-processing/src/main/scala/scada/processing/runtime/HistoryBus.scala +1 -0
  28. package/stream-processing/src/main/scala/scada/processing/runtime/LocalHistory.scala +13 -61
  29. package/stream-processing/src/main/scala/scada/processing/runtime/PendingIsland.scala +2 -1
  30. package/stream-processing/src/main/scala/scada/processing/runtime/ProcessingApplication.scala +2 -12
  31. package/stream-processing/src/main/scala/scada/processing/runtime/RuntimeProfile.scala +2 -1
  32. package/stream-processing/src/main/scala/scada/processing/runtime/Seeding.scala +2 -1
  33. package/stream-processing/timeline-segmentation.lock +2 -0
  34. package/stream-processing/src/main/scala/scada/processing/core/Condition.scala +0 -33
  35. package/stream-processing/src/main/scala/scada/processing/core/CyclePhases.scala +0 -65
  36. package/stream-processing/src/main/scala/scada/processing/core/Emission.scala +0 -19
  37. package/stream-processing/src/main/scala/scada/processing/core/Params.scala +0 -15
  38. package/stream-processing/src/main/scala/scada/processing/core/Reading.scala +0 -11
  39. package/stream-processing/src/main/scala/scada/processing/core/Segment.scala +0 -28
  40. package/stream-processing/src/main/scala/scada/processing/core/SegmentDef.scala +0 -14
  41. package/stream-processing/src/main/scala/scada/processing/core/Segmentation.scala +0 -47
  42. package/stream-processing/src/main/scala/scada/processing/core/SplitFollow.scala +0 -50
  43. package/stream-processing/src/main/scala/scada/processing/core/SplitPieces.scala +0 -102
  44. package/stream-processing/src/main/scala/scada/processing/core/TagDef.scala +0 -12
  45. package/stream-processing/src/main/scala/scada/processing/core/TaggedSegment.scala +0 -19
  46. package/stream-processing/src/main/scala/scada/processing/core/Tagging.scala +0 -42
  47. package/stream-processing/src/main/scala/scada/processing/core/Timestamp.scala +0 -26
  48. package/stream-processing/src/main/scala/scada/processing/core/segmentation/Timeline.scala +0 -43
  49. package/stream-processing/vendor/gaps-and-islands/LICENSE +0 -21
  50. package/stream-processing/vendor/gaps-and-islands/UPSTREAM +0 -4
  51. package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/Island.scala +0 -40
  52. package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandEvent.scala +0 -55
  53. package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsFlow.scala +0 -127
  54. package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsPolicy.scala +0 -105
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yarkivaev/scada",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -33,7 +33,7 @@
33
33
  "test": "mocha --exit 'test/**/test*.js'",
34
34
  "test:deep": "mocha --exit 'test/domain/**/test*.js' 'test/infrastructure/ingest/**/test*.js'",
35
35
  "coverage": "c8 --all --src src npm test",
36
- "prepack": "sh stream-processing/resolve-worker.sh",
36
+ "prepack": "sh stream-processing/resolve-dependencies.sh",
37
37
  "lint": "eslint . && node scripts/check-eo-names.mjs",
38
38
  "lint:fix": "eslint . --fix",
39
39
  "lint:eo": "node scripts/check-eo-names.mjs"
@@ -70,6 +70,8 @@
70
70
  "stream-processing/vendor/",
71
71
  "stream-processing/dependencies/stream-worker.jar",
72
72
  "stream-processing/stream-worker.lock",
73
+ "stream-processing/dependencies/timeline-segmentation.jar",
74
+ "stream-processing/timeline-segmentation.lock",
73
75
  "src/"
74
76
  ]
75
77
  }
@@ -1,5 +1,6 @@
1
1
  package scada.processing.core
2
2
 
3
+ import io.yarkivaev.segmentation.{SplitEntry, SplitPieces, TagDef, TaggedSegment, Tagging, Timestamp}
3
4
  import cats.Monad
4
5
  import cats.MonadThrow
5
6
  import cats.syntax.all.*
@@ -54,14 +55,3 @@ enum Decision:
54
55
  ): F[Unit] =
55
56
  val cascaded = tagging.resolve(tagDefs, seg.segment, tags)
56
57
  segments.resolve(machine, seg.segment, cascaded.tags, cascaded.options, properties)
57
-
58
- /**
59
- * One piece of a [[Decision.Split]]. `tag` may be empty when the
60
- * operator does not know the reason. `open` marks the live tail.
61
- *
62
- * {{{
63
- * SplitEntry(Timestamp(100.0), Timestamp(160.0), "maintenance")
64
- * }}}
65
- */
66
- final case class SplitEntry(start: Timestamp, end: Timestamp, tag: String, open: Boolean = false):
67
- def tagged: TaggedSegment = SplitPieces.row(start, this)
@@ -1,5 +1,6 @@
1
1
  package scada.processing.core
2
2
 
3
+ import io.yarkivaev.segmentation.{SegmentDef, TagDef}
3
4
  /**
4
5
  * Per-machine supervision configuration.
5
6
  *
@@ -1,10 +1,11 @@
1
1
  package scada.processing.core
2
2
 
3
+ import io.yarkivaev.segmentation.{CyclePhases, Reading, SegmentCursor, Segmentation, TaggedSegment, Tagging, Timestamp}
3
4
  import cats.effect.IO
4
5
  import cats.syntax.all.*
5
6
  import fs2.Stream
6
7
  import io.yarkivaev.streamworker.*
7
- import scada.processing.core.segmentation.Timeline.Interval
8
+ import io.yarkivaev.segmentation.Timeline.Interval
8
9
  import scala.concurrent.duration.*
9
10
 
10
11
  /** Assembles the MQTT session input and its segment output port. */
@@ -21,12 +22,12 @@ final class ReadingWorker(
21
22
  def step(machine: MachineConfig, state: State, input: Input): Either[ProcessingError, StepResult[State, TaggedSegment]] =
22
23
  input match
23
24
  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))))
25
+ val result = segmentation.step(machine.segments, machine.window, state, reading)
26
+ Right(StepResult(result.state, result.outputs.map(emission => tagging.resolve(machine.tags, emission.value))))
26
27
  case Input.Restore(cursor) =>
27
- Right(StepResult(state, Vector(pending(cursor.name, cursor.start, cursor.end))))
28
+ Right(StepResult(state, Vector(Segmentation.pending(cursor.name, cursor.start, cursor.end))))
28
29
  case Input.Pulse(now, rows) =>
29
- Right(StepResult(state, pulse(now, rows).toVector))
30
+ Right(StepResult(state, Segmentation.extendPending(now, rows).toVector))
30
31
 
31
32
  def run(machine: MachineConfig, readings: Stream[IO, Reading], initial: Option[SegmentCursor] = None): IO[Unit] =
32
33
  if machine.mode.isDefined then
@@ -69,14 +70,3 @@ object ReadingWorker:
69
70
  case Sample(reading: Reading)
70
71
  case Restore(cursor: SegmentCursor)
71
72
  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))
@@ -1,5 +1,6 @@
1
1
  package scada.processing.core
2
2
 
3
+ import io.yarkivaev.segmentation.{Segment, TaggedSegment, Timestamp}
3
4
  import cats.Monad
4
5
  import cats.syntax.all.*
5
6
 
@@ -1,5 +1,6 @@
1
1
  package scada.processing.infra.checkpoint
2
2
 
3
+ import io.yarkivaev.segmentation.{TaggedSegment, Timestamp}
3
4
  import cats.effect.IO
4
5
  import fs2.Stream
5
6
  import scada.processing.core.*
@@ -1,5 +1,6 @@
1
1
  package scada.processing.infra.checkpoint
2
2
 
3
+ import io.yarkivaev.segmentation.{TaggedSegment, Timestamp}
3
4
  import cats.effect.IO
4
5
  import cats.syntax.all.*
5
6
  import fs2.Stream
@@ -1,5 +1,6 @@
1
1
  package scada.processing.infra.checkpoint
2
2
 
3
+ import io.yarkivaev.segmentation.{Segment, TaggedSegment, Timestamp}
3
4
  import cats.data.NonEmptyList
4
5
  import cats.effect.IO
5
6
  import fs2.Stream
@@ -1,7 +1,7 @@
1
1
  package scada.processing.infra.config
2
2
 
3
- import scada.processing.core.{CyclePhases, MachineConfig, MachineId}
4
-
3
+ import io.yarkivaev.segmentation.CyclePhases
4
+ import scada.processing.core.{MachineConfig, MachineId}
5
5
  /**
6
6
  * Alias routing entry: which MQTT topic carries which metric.
7
7
  *
@@ -1,5 +1,6 @@
1
1
  package scada.processing.infra.config
2
2
 
3
+ import io.yarkivaev.segmentation.{Condition, CycleAnchors, CyclePhase, CyclePhases, SegmentDef, TagDef}
3
4
  import cats.effect.IO
4
5
  import com.typesafe.config.{Config, ConfigFactory, ConfigValueType}
5
6
  import scada.processing.core.*
@@ -2,7 +2,6 @@ package scada.processing.infra.config
2
2
 
3
3
  import com.typesafe.config.Config
4
4
  import scada.processing.core.{MachineConfig, MachineMode}
5
-
6
5
  /** Extension points for site-specific HOCON rules and groups. */
7
6
  trait ConfigProfile:
8
7
  def mode(machine: Config): Option[MachineMode] = None
@@ -1,5 +1,6 @@
1
1
  package scada.processing.infra.mqtt
2
2
 
3
+ import io.yarkivaev.segmentation.{Params, Reading, Timestamp}
3
4
  import cats.effect.{IO, Resource}
4
5
  import fs2.Stream
5
6
  import io.circe.parser.parse as parseJson
@@ -14,7 +15,6 @@ import org.eclipse.paho.client.mqttv3.{
14
15
  import org.slf4j.LoggerFactory
15
16
  import org.typelevel.log4cats.Logger
16
17
  import org.typelevel.log4cats.slf4j.Slf4jLogger
17
- import scada.processing.core.*
18
18
  import scada.processing.infra.config.{AliasMapping, MqttConfig}
19
19
  import scala.concurrent.duration.*
20
20
 
@@ -1,5 +1,6 @@
1
1
  package scada.processing.infra.state
2
2
 
3
+ import io.yarkivaev.segmentation.{Segment, TaggedSegment, Timestamp}
3
4
  import cats.effect.IO
4
5
  import cats.syntax.all.*
5
6
  import scada.processing.core.*
@@ -1,7 +1,6 @@
1
1
  package scada.processing.infra.state
2
2
 
3
- import scada.processing.core.TaggedSegment
4
-
3
+ import io.yarkivaev.segmentation.TaggedSegment
5
4
  /** Preserves or replaces operator annotations when a segment is updated. */
6
5
  trait SegmentStatePolicy:
7
6
  def merge(existing: TaggedSegment, incoming: TaggedSegment): TaggedSegment =
@@ -1,5 +1,6 @@
1
1
  package scada.processing.infra.state
2
2
 
3
+ import io.yarkivaev.segmentation.{Segment, SplitFollow, TaggedSegment, Timestamp}
3
4
  import cats.Functor
4
5
  import cats.effect.Ref
5
6
  import cats.syntax.all.*
@@ -1,9 +1,9 @@
1
1
  package scada.processing.infra.stomp
2
2
 
3
+ import io.yarkivaev.segmentation.{SplitEntry, Timestamp}
3
4
  import io.circe.Json
4
5
  import io.circe.parser.parse as parseJson
5
- import scada.processing.core.{Decision, MachineId, SplitEntry, Timestamp}
6
-
6
+ import scada.processing.core.{Decision, MachineId}
7
7
  /**
8
8
  * Inbound-wire parser for operator [[Decision]]s arriving on the
9
9
  * STOMP `user_decisions` destination. Reads a JSON body and picks the
@@ -1,9 +1,9 @@
1
1
  package scada.processing.infra.stomp
2
2
 
3
+ import io.yarkivaev.segmentation.TaggedSegment
3
4
  import io.circe.Json
4
5
  import io.circe.syntax.*
5
- import scada.processing.core.{MachineId, TaggedSegment}
6
-
6
+ import scada.processing.core.MachineId
7
7
  /**
8
8
  * Wire-format envelope for one event on the supervisor-sink channel.
9
9
  * Knows how to render itself as JSON; STOMP publishing just treats the
@@ -1,7 +1,6 @@
1
1
  package scada.processing.infra.stomp
2
2
 
3
- import scada.processing.core.{TaggedSegment, Timestamp}
4
-
3
+ import io.yarkivaev.segmentation.{TaggedSegment, Timestamp}
5
4
  /** Controls the wire representation and publishing semantics for segment types. */
6
5
  trait SegmentWirePolicy:
7
6
  def boundary(row: TaggedSegment, time: Timestamp): Long = time.millis
@@ -1,9 +1,9 @@
1
1
  package scada.processing.infra.stomp
2
2
 
3
+ import io.yarkivaev.segmentation.{TaggedSegment, Timestamp}
3
4
  import io.circe.Json
4
5
  import io.circe.syntax.*
5
- import scada.processing.core.{MachineId, TaggedSegment, Timestamp}
6
-
6
+ import scada.processing.core.MachineId
7
7
  /**
8
8
  * One STOMP body that replaces a whole split family. The sink applies
9
9
  * it in a single transaction so a boundary edit cannot leave the
@@ -2,7 +2,7 @@ package scada.processing.infra.stomp
2
2
 
3
3
  import cats.effect.IO
4
4
  import fs2.Stream
5
- import scada.processing.core.{Decision,MachineId}
5
+ import scada.processing.core.{Decision, MachineId}
6
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. */
@@ -1,9 +1,9 @@
1
1
  package scada.processing.infra.stomp
2
2
 
3
+ import io.yarkivaev.segmentation.{Params, Reading, Timestamp}
3
4
  import cats.effect.{IO, Resource}
4
5
  import fs2.Stream
5
6
  import io.circe.parser.parse
6
- import scada.processing.core.*
7
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. */
@@ -1,5 +1,6 @@
1
1
  package scada.processing.infra.stomp
2
2
 
3
+ import io.yarkivaev.segmentation.{Segment, SplitFollow, TaggedSegment, Timestamp}
3
4
  import cats.Monad
4
5
  import cats.effect.IO
5
6
  import cats.syntax.all.*
@@ -1,12 +1,13 @@
1
1
  package scada.processing.runtime
2
2
 
3
+ import io.yarkivaev.segmentation.{HistorySegment, Segment, TaggedSegment, Timestamp}
3
4
  import cats.effect.{IO, Ref}
4
5
  import cats.syntax.all.*
5
6
  import fs2.Stream
6
7
  import io.circe.Json
7
8
  import io.yarkivaev.temporal.{Conflict, History, Observation}
8
9
  import java.time.Instant
9
- import scada.processing.core.{MachineConfig, MachineId, Segment, TaggedSegment, Timestamp}
10
+ import scada.processing.core.{MachineConfig, MachineId}
10
11
  import scada.processing.infra.stomp.SplitFamily
11
12
 
12
13
  /**
@@ -1,5 +1,6 @@
1
1
  package scada.processing.runtime
2
2
 
3
+ import io.yarkivaev.segmentation.{HistorySegment, HistoryState}
3
4
  import cats.effect.IO
4
5
  import cats.effect.std.Semaphore
5
6
  import fs2.Stream
@@ -1,7 +1,7 @@
1
1
  package scada.processing.runtime
2
2
 
3
+ import io.yarkivaev.segmentation.{Segment, TaggedSegment, Timestamp}
3
4
  import cats.effect.IO
4
- import scada.processing.core.*
5
5
  import scada.processing.core.Segments as CoreSegments
6
6
  import scada.processing.infra.checkpoint.PendingSegmentData
7
7
 
@@ -1,5 +1,6 @@
1
1
  package scada.processing.runtime
2
2
 
3
+ import io.yarkivaev.segmentation.{Segment, TaggedSegment, Timestamp}
3
4
  import cats.effect.IO
4
5
  import cats.syntax.all.*
5
6
  import io.circe.Json
@@ -1,5 +1,6 @@
1
1
  package scada.processing.runtime
2
2
 
3
+ import io.yarkivaev.segmentation.{HistoryRange, HistorySeed, HistorySegment, HistoryState, ObservedSegmentation, Reading, Segment, Tagging, Timestamp}
3
4
  import cats.effect.{IO, Ref}
4
5
  import cats.syntax.all.*
5
6
  import fs2.Stream
@@ -15,15 +16,10 @@ import java.util.HexFormat
15
16
  import scada.stream.replay.{Replay, ReplayFailure, ReplayInput, ReplayManifest, ReplayRange, ReplaySeed}
16
17
  import io.yarkivaev.streamworker.{ProcessingError, Processor, StepResult}
17
18
  import scada.processing.core.*
18
- import scada.processing.core.segmentation.Timeline
19
- import scada.processing.core.segmentation.Timeline.{Boundary, Event, Interval, Output}
19
+ import io.yarkivaev.segmentation.ObservedSegmentation.{Sample, State, segment}
20
20
  import scada.processing.infra.config.{AliasMapping, CheckpointApiConfig}
21
21
  import scala.concurrent.duration.*
22
22
 
23
- final case class HistoryRange(from: Long, until: Long)
24
- final case class HistorySeed(time: Long, values: Map[String,Double], segment: Option[HistorySegment] = None)
25
- final case class HistorySegment(name: String, start: Long, end: Long)
26
- final case class HistoryState(position: Long, parameters: Map[String,(Long,Double)], segment: Option[HistorySegment])
27
23
  final case class HistoryPool(cursors: LocalStreams, states: Map[Long,HistoryState]):
28
24
  def expire(now: Long): HistoryPool =
29
25
  val alive = cursors.evict(now)
@@ -35,72 +31,28 @@ final case class HistoryPool(cursors: LocalStreams, states: Map[Long,HistoryStat
35
31
  val merged = equal.foldLeft(advanced)((pool,id) => pool.merge(stream,id,true))
36
32
  HistoryPool(merged,known.filter((id,_) => merged.streams.exists(_.id == id)))
37
33
  object LocalHistory:
38
- def connected(before: Long, after: Long, gap: Long, continuity: Vector[HistoryRange]): Boolean =
39
- before <= after && (BigInt(after)-BigInt(before) <= gap || continuity.exists(r => r.from <= before && r.until >= after))
40
- def state(machine: MachineConfig, points: Vector[Observation], result: Vector[HistorySegment], seed: Option[HistorySeed] = None, continuity: Vector[HistoryRange] = Vector.empty): Option[HistoryState] =
41
- points.map(_.time).maxOption.map: end =>
42
- val gap = machine.window.toLong*2000
43
- val state = points.groupBy(_.time).toVector.sortBy(_._1).foldLeft((seed.map(_.time),seed.fold(Map.empty[String,(Long,Double)])(s => s.values.map((k,v) => k -> (s.time,v))))):
44
- case ((previous,values),(time,group)) =>
45
- val prior = if previous.exists(t => connected(t,time,gap,continuity)) then values else Map.empty[String,(Long,Double)]
46
- (Some(time),prior ++ group.map(p => p.channel -> (p.time,p.value)))
47
- HistoryState(end,state._2,result.lastOption)
34
+ def classify(machine: MachineConfig, points: Vector[Observation], seed: Option[HistorySeed] = None,
35
+ continuity: Vector[HistoryRange] = Vector.empty): Vector[HistorySegment] =
36
+ ObservedSegmentation(machine.segments, machine.window, continuity).run(grouped(points), seed)
48
37
 
49
- /** Finite event-time classification: never inject wall-clock silence into past data. */
50
- def classify(machine: MachineConfig, points: Vector[Observation], seed: Option[HistorySeed] = None, continuity: Vector[HistoryRange] = Vector.empty): Vector[HistorySegment] =
51
- val classifier = Classification(machine, continuity)
52
- val (terminal, closed) = grouped(points).foldLeft((classifier.initial(seed), Vector.empty[HistorySegment])):
53
- case ((state, completed), sample) =>
54
- val next = classifier.advance(state, sample)
55
- (next.state, completed ++ next.outputs)
56
- closed ++ terminal.segment.map(segment)
57
-
58
- private final case class Sample(time: Long, values: Map[String, Double])
59
- private final case class ClassificationState(
60
- time: Option[Long], parameters: Map[String, (Long, Double)], segment: Option[Interval[Long]]
61
- )
62
38
  private[runtime] final case class Replayed(segments: Vector[HistorySegment], state: Option[HistoryState])
63
39
 
64
- private def segment(row: Interval[Long]): HistorySegment = HistorySegment(row.name, row.start, row.end)
65
-
66
40
  private def grouped(points: Vector[Observation]): Vector[Sample] =
67
41
  points.groupBy(_.time).toVector.sortBy(_._1).map: (time, group) =>
68
42
  Sample(time, group.map(p => p.channel -> p.value).toMap)
69
43
 
70
- private final class Classification(machine: MachineConfig, continuity: Vector[HistoryRange])
71
- extends Processor[Unit, ClassificationState, Sample, HistorySegment]:
72
- private val classify = Timeline.classifier(machine.segments)
73
- private val gap = machine.window.toLong * 2000
74
-
75
- def initial(seed: Option[HistorySeed]): ClassificationState =
76
- val carried = seed.flatMap: value =>
77
- val stored = value.segment.filter(_.start <= value.time)
78
- val evaluated = classify(Params(value.values)) match
79
- case Some(name) => Some(stored.filter(_.name == name).getOrElse(HistorySegment(name, value.time, value.time)))
80
- case None => stored
81
- evaluated.map(row => Interval(row.name, row.start, row.end))
82
- val parameters = seed.fold(Map.empty[String, (Long, Double)])(value => value.values.map((key, reading) => key -> (value.time, reading)))
83
- ClassificationState(seed.map(_.time), parameters, carried)
84
-
85
- def advance(state: ClassificationState, sample: Sample): StepResult[ClassificationState, HistorySegment] =
86
- val continuous = state.time.exists(connected(_, sample.time, gap, continuity))
87
- val parameters = (if continuous then state.parameters else Map.empty) ++
88
- sample.values.map((key, value) => key -> (sample.time, value))
89
- val name = classify(Params(parameters.map((key, value) => key -> value._2)))
90
- .orElse(if continuous then state.segment.map(_.name) else None)
91
- val next = ClassificationState(Some(sample.time), parameters, state.segment)
92
- name.fold(StepResult(next, Vector.empty[HistorySegment])): classified =>
93
- val result = Timeline.step(Boundary.Observed, state.segment, Event.Sample(sample.time, classified, continuous))
94
- StepResult(next.copy(segment = result.open), result.outputs.collect { case Output.Closed(row) => segment(row) })
95
-
96
- override def step(config: Unit, state: ClassificationState, sample: Sample)
97
- : Either[ProcessingError, StepResult[ClassificationState, HistorySegment]] = Right(advance(state, sample))
44
+ private def processor(classifier: ObservedSegmentation): Processor[Unit, State, Sample, HistorySegment] =
45
+ new Processor[Unit, State, Sample, HistorySegment]:
46
+ def step(config: Unit, state: State, sample: Sample)
47
+ : Either[ProcessingError, StepResult[State, HistorySegment]] =
48
+ val result = classifier.step(state, sample)
49
+ Right(StepResult(result.state, result.outputs))
98
50
 
99
51
  private[runtime] def replay(
100
52
  machine: MachineConfig, points: Vector[Observation], baseRevision: String, runId: String,
101
53
  seed: Option[HistorySeed] = None, continuity: Vector[HistoryRange] = Vector.empty
102
54
  ): IO[Replayed] = IO.defer:
103
- val classifier = Classification(machine, continuity)
55
+ val classifier = ObservedSegmentation(machine.segments, machine.window, continuity)
104
56
  val initial = classifier.initial(seed)
105
57
  val samples = grouped(points)
106
58
  def values(readings: Map[String, Double]): Json =
@@ -118,7 +70,7 @@ object LocalHistory:
118
70
  val manifest = ReplayManifest(timeline, ReplayRange(0, samples.size), rulesVersion, input.id, seedId, baseRevision, runId)
119
71
  def failure(problem: ReplayFailure) = IllegalStateException(s"History page replay failed: $problem")
120
72
  IO.fromEither(Replay.plan(manifest, input, start).leftMap(failure)).flatMap: plan =>
121
- Replay.run(plan, (), classifier).flatMap(result => IO.fromEither(result.leftMap(failure))).map: result =>
73
+ Replay.run(plan, (), processor(classifier)).flatMap(result => IO.fromEither(result.leftMap(failure))).map: result =>
122
74
  val terminal = result.terminalState
123
75
  val tail = terminal.segment.map(segment)
124
76
  val state = samples.lastOption.map(last => HistoryState(last.time, terminal.parameters, tail))
@@ -1,9 +1,10 @@
1
1
  package scada.processing.runtime
2
2
 
3
+ import io.yarkivaev.segmentation.SegmentCursor
3
4
  import cats.effect.IO
4
5
  import org.typelevel.log4cats.Logger
5
6
  import org.typelevel.log4cats.slf4j.Slf4jLogger
6
- import scada.processing.core.{MachineConfig, SegmentCursor}
7
+ import scada.processing.core.MachineConfig
7
8
  import scada.processing.infra.checkpoint.Checkpoint
8
9
 
9
10
  /**
@@ -1,5 +1,6 @@
1
1
  package scada.processing.runtime
2
2
 
3
+ import io.yarkivaev.segmentation.{Reading, Segmentation, TagDef, TaggedSegment, Timestamp}
3
4
  import cats.effect.{ExitCode, IO, Ref, Resource}
4
5
  import cats.effect.std.Semaphore
5
6
  import cats.syntax.all.*
@@ -7,18 +8,7 @@ import fs2.Stream
7
8
  import io.yarkivaev.streamworker.OutputScope
8
9
  import org.typelevel.log4cats.Logger
9
10
  import org.typelevel.log4cats.slf4j.Slf4jLogger
10
- import scada.processing.core.{
11
- Decision,
12
- MachineConfig,
13
- MachineId,
14
- Reading,
15
- Segmentation,
16
- Segments as CoreSegments,
17
- ReadingWorker,
18
- TagDef,
19
- TaggedSegment,
20
- Timestamp
21
- }
11
+ import scada.processing.core.{Decision, MachineConfig, MachineId, Segments as CoreSegments, ReadingWorker}
22
12
  import scada.processing.infra.checkpoint.{Checkpoint, ClickhouseReadingsCheckpoint, SupervisorSinkCheckpoint}
23
13
  import scada.processing.infra.config.{AliasMapping, AppConfig, ConfigLoad, RabbitMqConfig}
24
14
  import scada.processing.infra.mqtt.Mqtt
@@ -1,10 +1,11 @@
1
1
  package scada.processing.runtime
2
2
 
3
+ import io.yarkivaev.segmentation.{Reading, ReadingOverride, Tagging}
3
4
  import cats.effect.IO
4
5
  import fs2.Stream
5
6
  import io.circe.Json
6
7
  import io.yarkivaev.streamworker.OutputScope
7
- import scada.processing.core.{MachineConfig, Reading, ReadingOverride, Segments, Tagging}
8
+ import scada.processing.core.{MachineConfig, Segments}
8
9
  import scada.processing.infra.checkpoint.Checkpoint
9
10
  import scada.processing.infra.config.{AliasMapping, AppConfig, CheckpointApiConfig, ConfigProfile}
10
11
  import scada.processing.infra.state.SegmentStatePolicy
@@ -1,12 +1,13 @@
1
1
  package scada.processing.runtime
2
2
 
3
+ import io.yarkivaev.segmentation.{Params, Reading, Timestamp}
3
4
  import cats.effect.{IO, Outcome}
4
5
  import cats.effect.std.Queue
5
6
  import fs2.Stream
6
7
  import org.typelevel.log4cats.Logger
7
8
  import org.typelevel.log4cats.slf4j.Slf4jLogger
8
9
  import scala.concurrent.duration.*
9
- import scada.processing.core.{MachineConfig, MachineId, Params, Reading, Timestamp}
10
+ import scada.processing.core.{MachineConfig, MachineId}
10
11
  import scada.processing.infra.checkpoint.{Checkpoint, RawReadingData}
11
12
  import scada.processing.infra.config.AliasMapping
12
13
 
@@ -0,0 +1,2 @@
1
+ 0.1.0
2
+ 495505f53a60028903b212b765bf09b37a67f578191dfce29a662b00f20c09dc
@@ -1,33 +0,0 @@
1
- package scada.processing.core
2
-
3
- /**
4
- * Declarative predicate over a single [[Reading]]. Smart object:
5
- * every case answers [[matches]] for itself.
6
- *
7
- * `When(name)` delegates to the condition of the same-named
8
- * [[SegmentDef]]; callers pass a `lookup` function that resolves the
9
- * reference. In tagging context `When` is matched by name only, not
10
- * by evaluating this predicate.
11
- *
12
- * {{{
13
- * Condition.Above("power", 5.0).matches(Params(Map("power" -> 42.0)))
14
- * }}}
15
- */
16
- enum Condition:
17
- case Above(param: String, threshold: Double)
18
- case Below(param: String, threshold: Double)
19
- case Equal(param: String, value: Double)
20
- case And(left: Condition, right: Condition)
21
- case Or(left: Condition, right: Condition)
22
- case When(segmentName: String)
23
-
24
- def matches(params: Params): Boolean =
25
- matches(params, _ => None)
26
-
27
- def matches(params: Params, lookup: String => Option[Condition]): Boolean = this match
28
- case Above(param, threshold) => params.value(param).exists(_ >= threshold)
29
- case Below(param, threshold) => params.value(param).exists(_ < threshold)
30
- case Equal(param, value) => params.value(param).contains(value)
31
- case And(left, right) => left.matches(params, lookup) && right.matches(params, lookup)
32
- case Or(left, right) => left.matches(params, lookup) || right.matches(params, lookup)
33
- case When(name) => lookup(name).exists(_.matches(params, lookup))
@@ -1,65 +0,0 @@
1
- package scada.processing.core
2
-
3
- /**
4
- * One cycle phase loaded from HOCON: optional `enter` anchor and the
5
- * flat tag ids allowed while that phase is open.
6
- *
7
- * {{{
8
- * CyclePhase(List("cycle_start", "transfer", "repair"), None)
9
- * CyclePhase(List("transfer", "repair"), Some("ready"))
10
- * }}}
11
- */
12
- final case class CyclePhase(allow: List[String], enter: Option[String])
13
-
14
- /**
15
- * Anchor tag ids that close or advance a production cycle.
16
- *
17
- * {{{
18
- * CycleAnchors("cycle_start", List("planned_stop"))
19
- * }}}
20
- */
21
- final case class CycleAnchors(start: String, stop: List[String])
22
-
23
- /**
24
- * Cycle allowlists loaded from generated HOCON. Phase lookup uses
25
- * anchors of other segments; `flat` keeps HMI button order.
26
- *
27
- * {{{
28
- * phases.options(others)
29
- * phases.accept(others, standing, tags)
30
- * }}}
31
- */
32
- final case class CyclePhases(flat: List[String], anchors: CycleAnchors, phases: List[CyclePhase]):
33
- def isEmpty: Boolean = phases.isEmpty
34
-
35
- def options(others: List[TaggedSegment]): List[String] =
36
- val allowed = phaseOf(others).allow.toSet
37
- flat.filter(allowed.contains)
38
-
39
- def accept(others: List[TaggedSegment], standing: List[String], tags: List[String]): Boolean =
40
- if isEmpty then true
41
- else
42
- val ok = options(others).toSet ++ standing.toSet
43
- tags.forall(ok.contains)
44
-
45
- def narrow(segment: TaggedSegment, all: List[TaggedSegment]): TaggedSegment =
46
- if isEmpty || segment.segment.name != "off" then segment
47
- else
48
- val past = all.filter(_.segment.start.epoch < segment.segment.start.epoch)
49
- TaggedSegment(segment.segment, segment.tags, options(past :+ segment), segment.properties)
50
-
51
- private def phaseOf(others: List[TaggedSegment]): CyclePhase =
52
- val idle = phases.find(_.enter.isEmpty).getOrElse(CyclePhase(Nil, None))
53
- val tags = others.sortBy(_.segment.start).flatMap(_.tags)
54
- val lastStop = tags.lastIndexWhere(id => anchors.stop.contains(id))
55
- val lastStart = tags.lastIndexWhere(_ == anchors.start)
56
- if lastStart < 0 || lastStart < lastStop then idle
57
- else entered(tags.drop(lastStart + 1), idle)
58
-
59
- private def entered(after: List[String], idle: CyclePhase): CyclePhase =
60
- val last = after.reverse.find(id => phases.exists(_.enter.contains(id)))
61
- last.flatMap(id => phases.find(_.enter.contains(id))).getOrElse:
62
- phases.find(_.enter.contains(anchors.start)).getOrElse(idle)
63
-
64
- object CyclePhases:
65
- val empty: CyclePhases = CyclePhases(Nil, CycleAnchors("", Nil), Nil)
@@ -1,19 +0,0 @@
1
- package scada.processing.core
2
-
3
- /**
4
- * Lifecycle marker for a value flowing through the pipeline.
5
- *
6
- * `Started` carries a pending value whose boundaries may still grow;
7
- * `Emitted` carries the finalised value.
8
- *
9
- * {{{
10
- * Emission.Started(segment): Emission[Segment]
11
- * Emission.Emitted(segment): Emission[Segment]
12
- * }}}
13
- */
14
- sealed trait Emission[+A]:
15
- def value: A
16
-
17
- object Emission:
18
- final case class Started[A](value: A) extends Emission[A]
19
- final case class Emitted[A](value: A) extends Emission[A]