@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.
- package/package.json +4 -2
- package/stream-processing/dependencies/timeline-segmentation.jar +0 -0
- package/stream-processing/src/main/scala/scada/processing/core/Decision.scala +1 -11
- package/stream-processing/src/main/scala/scada/processing/core/MachineConfig.scala +1 -0
- package/stream-processing/src/main/scala/scada/processing/core/ReadingWorker.scala +6 -16
- package/stream-processing/src/main/scala/scada/processing/core/Segments.scala +1 -0
- package/stream-processing/src/main/scala/scada/processing/infra/checkpoint/Checkpoint.scala +1 -0
- package/stream-processing/src/main/scala/scada/processing/infra/checkpoint/ClickhouseReadingsCheckpoint.scala +1 -0
- package/stream-processing/src/main/scala/scada/processing/infra/checkpoint/SupervisorSinkCheckpoint.scala +1 -0
- package/stream-processing/src/main/scala/scada/processing/infra/config/AppConfig.scala +2 -2
- package/stream-processing/src/main/scala/scada/processing/infra/config/ConfigLoad.scala +1 -0
- package/stream-processing/src/main/scala/scada/processing/infra/config/ConfigProfile.scala +0 -1
- package/stream-processing/src/main/scala/scada/processing/infra/mqtt/Mqtt.scala +1 -1
- package/stream-processing/src/main/scala/scada/processing/infra/state/CheckpointSegments.scala +1 -0
- package/stream-processing/src/main/scala/scada/processing/infra/state/SegmentStatePolicy.scala +1 -2
- package/stream-processing/src/main/scala/scada/processing/infra/state/Segments.scala +1 -0
- package/stream-processing/src/main/scala/scada/processing/infra/stomp/DecisionMessage.scala +2 -2
- package/stream-processing/src/main/scala/scada/processing/infra/stomp/SegmentMessage.scala +2 -2
- package/stream-processing/src/main/scala/scada/processing/infra/stomp/SegmentWirePolicy.scala +1 -2
- package/stream-processing/src/main/scala/scada/processing/infra/stomp/SplitFamily.scala +2 -2
- package/stream-processing/src/main/scala/scada/processing/infra/stomp/StompMachineDecisions.scala +1 -1
- package/stream-processing/src/main/scala/scada/processing/infra/stomp/StompReadings.scala +1 -1
- package/stream-processing/src/main/scala/scada/processing/infra/stomp/StompSegments.scala +1 -0
- package/stream-processing/src/main/scala/scada/processing/runtime/ClassifyHistory.scala +2 -1
- package/stream-processing/src/main/scala/scada/processing/runtime/ClassifyStreams.scala +1 -0
- package/stream-processing/src/main/scala/scada/processing/runtime/Close.scala +1 -1
- package/stream-processing/src/main/scala/scada/processing/runtime/HistoryBus.scala +1 -0
- package/stream-processing/src/main/scala/scada/processing/runtime/LocalHistory.scala +13 -61
- package/stream-processing/src/main/scala/scada/processing/runtime/PendingIsland.scala +2 -1
- package/stream-processing/src/main/scala/scada/processing/runtime/ProcessingApplication.scala +2 -12
- package/stream-processing/src/main/scala/scada/processing/runtime/RuntimeProfile.scala +2 -1
- package/stream-processing/src/main/scala/scada/processing/runtime/Seeding.scala +2 -1
- package/stream-processing/timeline-segmentation.lock +2 -0
- package/stream-processing/src/main/scala/scada/processing/core/Condition.scala +0 -33
- package/stream-processing/src/main/scala/scada/processing/core/CyclePhases.scala +0 -65
- package/stream-processing/src/main/scala/scada/processing/core/Emission.scala +0 -19
- package/stream-processing/src/main/scala/scada/processing/core/Params.scala +0 -15
- package/stream-processing/src/main/scala/scada/processing/core/Reading.scala +0 -11
- package/stream-processing/src/main/scala/scada/processing/core/Segment.scala +0 -28
- package/stream-processing/src/main/scala/scada/processing/core/SegmentDef.scala +0 -14
- package/stream-processing/src/main/scala/scada/processing/core/Segmentation.scala +0 -47
- package/stream-processing/src/main/scala/scada/processing/core/SplitFollow.scala +0 -50
- package/stream-processing/src/main/scala/scada/processing/core/SplitPieces.scala +0 -102
- package/stream-processing/src/main/scala/scada/processing/core/TagDef.scala +0 -12
- package/stream-processing/src/main/scala/scada/processing/core/TaggedSegment.scala +0 -19
- package/stream-processing/src/main/scala/scada/processing/core/Tagging.scala +0 -42
- package/stream-processing/src/main/scala/scada/processing/core/Timestamp.scala +0 -26
- package/stream-processing/src/main/scala/scada/processing/core/segmentation/Timeline.scala +0 -43
- package/stream-processing/vendor/gaps-and-islands/LICENSE +0 -21
- package/stream-processing/vendor/gaps-and-islands/UPSTREAM +0 -4
- package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/Island.scala +0 -40
- package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandEvent.scala +0 -55
- package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsFlow.scala +0 -127
- package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsPolicy.scala +0 -105
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
package scada.processing.core
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Immutable snapshot of the named metric values attached to one
|
|
5
|
-
* [[Reading]]. Keys are metric names (as configured by routing);
|
|
6
|
-
* values are raw sensor doubles.
|
|
7
|
-
*
|
|
8
|
-
* {{{
|
|
9
|
-
* Params(Map("power" -> 0.95)).value("power")
|
|
10
|
-
* }}}
|
|
11
|
-
*/
|
|
12
|
-
final case class Params(values: Map[String, Double]):
|
|
13
|
-
def value(key: String): Option[Double] = values.get(key)
|
|
14
|
-
|
|
15
|
-
def merge(other: Params): Params = Params(values ++ other.values)
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
package scada.processing.core
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* One sensor observation for a machine at one instant. The atomic
|
|
5
|
-
* input of segmentation.
|
|
6
|
-
*
|
|
7
|
-
* {{{
|
|
8
|
-
* Reading(Timestamp(1700000000.0), Params(Map("power" -> 42.0)))
|
|
9
|
-
* }}}
|
|
10
|
-
*/
|
|
11
|
-
final case class Reading(timestamp: Timestamp, params: Params)
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
package scada.processing.core
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A named, closed time interval produced by segmentation from a
|
|
5
|
-
* contiguous run of readings that matched the same [[SegmentDef]].
|
|
6
|
-
*
|
|
7
|
-
* `duration == 0.0` marks a just-started (pending) segment whose
|
|
8
|
-
* closing boundary a later reading will reveal.
|
|
9
|
-
*
|
|
10
|
-
* {{{
|
|
11
|
-
* Segment("on", Timestamp(100.0), Timestamp(200.0), 100.0)
|
|
12
|
-
* }}}
|
|
13
|
-
*/
|
|
14
|
-
final case class Segment(
|
|
15
|
-
name: String,
|
|
16
|
-
start: Timestamp,
|
|
17
|
-
end: Timestamp,
|
|
18
|
-
duration: Double,
|
|
19
|
-
properties: Map[String, String] = Map.empty
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Open-segment resume cursor from Postgres (`segments` row with
|
|
24
|
-
* `duration = 0`). Fed into [[Segmentation]] as the initial
|
|
25
|
-
* gaps-and-islands island; `end` may advance beyond `start` via
|
|
26
|
-
* pending heartbeats.
|
|
27
|
-
*/
|
|
28
|
-
final case class SegmentCursor(name: String, start: Timestamp, end: Timestamp)
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
package scada.processing.core
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* One named segment kind for a machine: a label and the predicate
|
|
5
|
-
* deciding whether a reading belongs to it.
|
|
6
|
-
*
|
|
7
|
-
* Order inside [[MachineConfig.segments]] is significant — the first
|
|
8
|
-
* matching definition wins.
|
|
9
|
-
*
|
|
10
|
-
* {{{
|
|
11
|
-
* SegmentDef("on", Condition.Above("power", 0.1))
|
|
12
|
-
* }}}
|
|
13
|
-
*/
|
|
14
|
-
final case class SegmentDef(name: String, condition: Condition)
|
|
@@ -1,47 +0,0 @@
|
|
|
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,50 +0,0 @@
|
|
|
1
|
-
package scada.processing.core
|
|
2
|
-
|
|
3
|
-
import scala.collection.immutable.SortedMap
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Keeps a classifier heartbeat on an already split `off` island from
|
|
7
|
-
* rewriting the closed pieces. The heartbeat still names the original
|
|
8
|
-
* start. Only the open tail moves, and a later close finishes that tail.
|
|
9
|
-
*
|
|
10
|
-
* {{{
|
|
11
|
-
* SplitFollow.project(rows, heartbeat)
|
|
12
|
-
* }}}
|
|
13
|
-
*/
|
|
14
|
-
object SplitFollow:
|
|
15
|
-
def owned(rows: Iterable[TaggedSegment], incoming: TaggedSegment): Boolean =
|
|
16
|
-
incoming.segment.name == "off" && family(rows, incoming).nonEmpty
|
|
17
|
-
|
|
18
|
-
def project(
|
|
19
|
-
rows: SortedMap[Timestamp, TaggedSegment],
|
|
20
|
-
incoming: TaggedSegment
|
|
21
|
-
): SortedMap[Timestamp, TaggedSegment] =
|
|
22
|
-
val pieces = family(rows.values, incoming)
|
|
23
|
-
if pieces.isEmpty then rows
|
|
24
|
-
else
|
|
25
|
-
val tail = pieces.maxBy(_.segment.start.epoch)
|
|
26
|
-
moved(tail, incoming) match
|
|
27
|
-
case None => rows
|
|
28
|
-
case Some(next) => rows.updated(tail.segment.start, next)
|
|
29
|
-
|
|
30
|
-
def affected(incoming: TaggedSegment, rows: Iterable[TaggedSegment]): Option[TaggedSegment] =
|
|
31
|
-
val pieces = family(rows, incoming)
|
|
32
|
-
if incoming.segment.name != "off" || pieces.isEmpty then None
|
|
33
|
-
else
|
|
34
|
-
val tail = pieces.maxBy(_.segment.start.epoch)
|
|
35
|
-
moved(tail, incoming).orElse(Some(tail))
|
|
36
|
-
|
|
37
|
-
private def family(rows: Iterable[TaggedSegment], incoming: TaggedSegment): List[TaggedSegment] =
|
|
38
|
-
val key = SplitPieces.originKey(incoming.segment.start)
|
|
39
|
-
rows.filter(_.properties.get("split_of").contains(key)).toList
|
|
40
|
-
|
|
41
|
-
private def moved(tail: TaggedSegment, incoming: TaggedSegment): Option[TaggedSegment] =
|
|
42
|
-
val start = tail.segment.start
|
|
43
|
-
if tail.segment.duration != 0.0 then None
|
|
44
|
-
else if incoming.segment.duration == 0.0 then
|
|
45
|
-
if !incoming.segment.end.isAfter(tail.segment.end) then None
|
|
46
|
-
else Some(tail.copy(segment = tail.segment.copy(end = incoming.segment.end, duration = 0.0)))
|
|
47
|
-
else if !incoming.segment.end.isAfter(start) then None
|
|
48
|
-
else
|
|
49
|
-
val end = incoming.segment.end
|
|
50
|
-
Some(tail.copy(segment = Segment("off", start, end, end.minus(start))))
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
package scada.processing.core
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Turns one operator split of an `off` island into flat `off` rows.
|
|
5
|
-
* Each row keeps `name = off`, carries at most one tag, and records
|
|
6
|
-
* `properties.split_of` as the original island start in epoch millis.
|
|
7
|
-
* The first row reuses that start. A last row with `open` stays pending
|
|
8
|
-
* (`duration == 0`). [[span]] widens the parent to the contiguous saved
|
|
9
|
-
* family when the new entries cover that whole island, so deleting one
|
|
10
|
-
* piece merges the rest.
|
|
11
|
-
*
|
|
12
|
-
* {{{
|
|
13
|
-
* SplitPieces.materialize(parent, List(
|
|
14
|
-
* SplitEntry(parent.start, Timestamp(160.0), "maintenance"),
|
|
15
|
-
* SplitEntry(Timestamp(160.0), parent.end, "")
|
|
16
|
-
* ))
|
|
17
|
-
* }}}
|
|
18
|
-
*/
|
|
19
|
-
object SplitPieces:
|
|
20
|
-
def originKey(start: Timestamp): String = start.millis.toString
|
|
21
|
-
|
|
22
|
-
def row(origin: Timestamp, entry: SplitEntry): TaggedSegment =
|
|
23
|
-
val duration = if entry.open then 0.0 else entry.end.minus(entry.start)
|
|
24
|
-
val tags = if entry.tag.isEmpty then Nil else List(entry.tag)
|
|
25
|
-
TaggedSegment(
|
|
26
|
-
Segment("off", entry.start, entry.end, duration),
|
|
27
|
-
tags,
|
|
28
|
-
Nil,
|
|
29
|
-
Map("split_of" -> originKey(origin))
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
def span(anchor: TaggedSegment, rows: List[TaggedSegment], entries: List[SplitEntry]): Segment =
|
|
33
|
-
val key = anchor.properties.getOrElse("split_of", originKey(anchor.segment.start))
|
|
34
|
-
val kin = rows.filter(row => row.segment.name == "off" && row.properties.get("split_of").contains(key))
|
|
35
|
-
.sortBy(_.segment.start.millis)
|
|
36
|
-
val run = pieceRun(anchor, kin)
|
|
37
|
-
if run.nonEmpty && covers(run, entries) then bounds(run) else anchor.segment
|
|
38
|
-
|
|
39
|
-
private def pieceRun(anchor: TaggedSegment, kin: List[TaggedSegment]): List[TaggedSegment] =
|
|
40
|
-
val index = kin.indexWhere(_.segment.start.millis == anchor.segment.start.millis)
|
|
41
|
-
if index < 0 then Nil
|
|
42
|
-
else kin.slice(leftOf(kin, index), rightOf(kin, index) + 1)
|
|
43
|
-
|
|
44
|
-
private def leftOf(kin: List[TaggedSegment], index: Int): Int =
|
|
45
|
-
if index == 0 || kin(index - 1).segment.end.millis != kin(index).segment.start.millis then index
|
|
46
|
-
else leftOf(kin, index - 1)
|
|
47
|
-
|
|
48
|
-
private def rightOf(kin: List[TaggedSegment], index: Int): Int =
|
|
49
|
-
if index == kin.length - 1 || kin(index).segment.end.millis != kin(index + 1).segment.start.millis then index
|
|
50
|
-
else rightOf(kin, index + 1)
|
|
51
|
-
|
|
52
|
-
private def covers(run: List[TaggedSegment], entries: List[SplitEntry]): Boolean =
|
|
53
|
-
val ordered = entries.sortBy(_.start.millis)
|
|
54
|
-
val head = run.head.segment
|
|
55
|
-
val tail = run.last.segment
|
|
56
|
-
ordered.nonEmpty && ordered.head.start.millis == head.start.millis &&
|
|
57
|
-
(if tail.duration == 0.0 then ordered.last.open else !ordered.last.open && ordered.last.end.millis == tail.end.millis)
|
|
58
|
-
|
|
59
|
-
private def bounds(run: List[TaggedSegment]): Segment =
|
|
60
|
-
val head = run.head.segment
|
|
61
|
-
val tail = run.last.segment
|
|
62
|
-
val duration = if tail.duration == 0.0 then 0.0 else tail.end.minus(head.start)
|
|
63
|
-
Segment("off", head.start, tail.end, duration)
|
|
64
|
-
|
|
65
|
-
def materialize(parent: Segment, entries: List[SplitEntry]): List[TaggedSegment] =
|
|
66
|
-
if parent.name != "off" then
|
|
67
|
-
throw new IllegalArgumentException(
|
|
68
|
-
s"refusing to split ${parent.name} starting at ${parent.start.millis}"
|
|
69
|
-
)
|
|
70
|
-
if entries.isEmpty then
|
|
71
|
-
throw new IllegalArgumentException(
|
|
72
|
-
s"refusing an empty split of off at ${parent.start.millis}"
|
|
73
|
-
)
|
|
74
|
-
val ordered = entries.sortBy(_.start.epoch)
|
|
75
|
-
if ordered.head.start.millis != parent.start.millis then
|
|
76
|
-
throw new IllegalArgumentException(
|
|
77
|
-
s"split of off at ${parent.start.millis} must keep that start, got ${ordered.head.start.millis}"
|
|
78
|
-
)
|
|
79
|
-
if !ordered.exists(_.tag.nonEmpty) then
|
|
80
|
-
throw new IllegalArgumentException(
|
|
81
|
-
s"split of off at ${parent.start.millis} has no tagged piece"
|
|
82
|
-
)
|
|
83
|
-
ordered.zipWithIndex.foreach: (entry, index) =>
|
|
84
|
-
val last = index == ordered.length - 1
|
|
85
|
-
if entry.open && !last then
|
|
86
|
-
throw new IllegalArgumentException(
|
|
87
|
-
s"open piece at ${entry.start.millis} is not the tail of off at ${parent.start.millis}"
|
|
88
|
-
)
|
|
89
|
-
if index > 0 && ordered(index - 1).end.millis != entry.start.millis then
|
|
90
|
-
throw new IllegalArgumentException(
|
|
91
|
-
s"split of off at ${parent.start.millis} has a gap before ${entry.start.millis}"
|
|
92
|
-
)
|
|
93
|
-
val span = entry.end.minus(entry.start)
|
|
94
|
-
if span < 0 then
|
|
95
|
-
throw new IllegalArgumentException(
|
|
96
|
-
s"piece at ${entry.start.millis} ends before it starts"
|
|
97
|
-
)
|
|
98
|
-
if parent.duration != 0.0 && (ordered.last.open || ordered.last.end.millis != parent.end.millis) then
|
|
99
|
-
throw new IllegalArgumentException(
|
|
100
|
-
s"closed off at ${parent.start.millis} must be covered through ${parent.end.millis}"
|
|
101
|
-
)
|
|
102
|
-
ordered.map(entry => row(parent.start, entry))
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
package scada.processing.core
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* One conditional label. When exactly one [[TagDef]] matches a
|
|
5
|
-
* [[Segment]] the label is applied automatically; when several match
|
|
6
|
-
* they land in `options` and trigger a [[Decision]].
|
|
7
|
-
*
|
|
8
|
-
* {{{
|
|
9
|
-
* TagDef("running", Condition.When("on"))
|
|
10
|
-
* }}}
|
|
11
|
-
*/
|
|
12
|
-
final case class TagDef(name: String, condition: Condition)
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
package scada.processing.core
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A [[Segment]] decorated with tagging output:
|
|
5
|
-
* - `tags` — labels applied automatically or after a decision;
|
|
6
|
-
* - `options` — labels the operator still has to choose between
|
|
7
|
-
* (non-empty means awaiting [[Decision]]);
|
|
8
|
-
* - `properties` — operator-attached metadata.
|
|
9
|
-
*
|
|
10
|
-
* {{{
|
|
11
|
-
* TaggedSegment(segment, List("running"), Nil, Map.empty)
|
|
12
|
-
* }}}
|
|
13
|
-
*/
|
|
14
|
-
final case class TaggedSegment(
|
|
15
|
-
segment: Segment,
|
|
16
|
-
tags: List[String],
|
|
17
|
-
options: List[String],
|
|
18
|
-
properties: Map[String, String]
|
|
19
|
-
)
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
package scada.processing.core
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Pure tagging of a [[Segment]] against a list of [[TagDef]]s.
|
|
5
|
-
*
|
|
6
|
-
* Matching:
|
|
7
|
-
* - with empty `currentTags`, candidates whose `when` equals the segment name;
|
|
8
|
-
* - with non-empty `currentTags`, candidates whose `when` is in that path.
|
|
9
|
-
*
|
|
10
|
-
* Cascade:
|
|
11
|
-
* - no matches → keep `currentTags`, empty options (leaf / done);
|
|
12
|
-
* - one match → append and continue (auto-deepen);
|
|
13
|
-
* - many → keep path as tags, list matches as options.
|
|
14
|
-
*
|
|
15
|
-
* {{{
|
|
16
|
-
* Tagging().resolve(tagDefs, segment)
|
|
17
|
-
* Tagging().resolve(tagDefs, segment, List("metal_structure"))
|
|
18
|
-
* }}}
|
|
19
|
-
*/
|
|
20
|
-
final class Tagging(allow: Segment => Boolean = _ => true):
|
|
21
|
-
def resolve(tagDefs: List[TagDef], segment: Segment): TaggedSegment =
|
|
22
|
-
resolve(tagDefs, segment, Nil)
|
|
23
|
-
|
|
24
|
-
def resolve(tagDefs: List[TagDef], segment: Segment, currentTags: List[String]): TaggedSegment =
|
|
25
|
-
if allow(segment) then deepen(tagDefs, segment, currentTags)
|
|
26
|
-
else TaggedSegment(segment, Nil, Nil, segment.properties)
|
|
27
|
-
|
|
28
|
-
private def deepen(tagDefs: List[TagDef], segment: Segment, currentTags: List[String]): TaggedSegment =
|
|
29
|
-
val matching = candidates(tagDefs, segment, currentTags)
|
|
30
|
-
matching match
|
|
31
|
-
case Nil => TaggedSegment(segment, currentTags, Nil, segment.properties)
|
|
32
|
-
case one :: Nil => deepen(tagDefs, segment, currentTags :+ one)
|
|
33
|
-
case many => TaggedSegment(segment, currentTags, many, segment.properties)
|
|
34
|
-
|
|
35
|
-
private def candidates(tagDefs: List[TagDef], segment: Segment, currentTags: List[String]): List[String] =
|
|
36
|
-
tagDefs.collect:
|
|
37
|
-
case TagDef(name, Condition.When(when)) if matches(when, segment, currentTags) && !currentTags.contains(name) =>
|
|
38
|
-
name
|
|
39
|
-
|
|
40
|
-
private def matches(when: String, segment: Segment, currentTags: List[String]): Boolean =
|
|
41
|
-
if currentTags.isEmpty then when == segment.name
|
|
42
|
-
else currentTags.contains(when)
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
package scada.processing.core
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Epoch seconds (double, millisecond precision) as an opaque type so
|
|
5
|
-
* it stays allocation-free in segmentation hot paths yet never mixes
|
|
6
|
-
* with raw numeric values.
|
|
7
|
-
*
|
|
8
|
-
* {{{
|
|
9
|
-
* val t = Timestamp(1700000000.0)
|
|
10
|
-
* t.minus(Timestamp(1699999985.0))
|
|
11
|
-
* }}}
|
|
12
|
-
*/
|
|
13
|
-
opaque type Timestamp = Double
|
|
14
|
-
|
|
15
|
-
object Timestamp:
|
|
16
|
-
def apply(epochSeconds: Double): Timestamp = epochSeconds
|
|
17
|
-
|
|
18
|
-
given Ordering[Timestamp] = Ordering.Double.TotalOrdering
|
|
19
|
-
|
|
20
|
-
extension (self: Timestamp)
|
|
21
|
-
def epoch: Double = self
|
|
22
|
-
def millis: Long = (self * 1000).toLong
|
|
23
|
-
def minus(other: Timestamp): Double = self - other
|
|
24
|
-
def plus(seconds: Double): Timestamp = self + seconds
|
|
25
|
-
def isAfter(other: Timestamp): Boolean = self > other
|
|
26
|
-
def isBefore(other: Timestamp): Boolean = self < other
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
package scada.processing.core.segmentation
|
|
2
|
-
|
|
3
|
-
import scada.processing.core.{Params, SegmentDef}
|
|
4
|
-
|
|
5
|
-
/** Transitions over classified samples in input order. The coordinate type is
|
|
6
|
-
* chosen by the caller and is never rounded or compared here.
|
|
7
|
-
*/
|
|
8
|
-
object Timeline:
|
|
9
|
-
enum Boundary:
|
|
10
|
-
case Live, Observed
|
|
11
|
-
|
|
12
|
-
final case class Interval[+A](name: String, start: A, end: A)
|
|
13
|
-
|
|
14
|
-
enum Event[+A]:
|
|
15
|
-
case Sample(at: A, name: String, continuous: Boolean)
|
|
16
|
-
case DeadlineExpired(at: A)
|
|
17
|
-
case EndOfInput
|
|
18
|
-
|
|
19
|
-
enum Output[+A]:
|
|
20
|
-
case Pending(interval: Interval[A])
|
|
21
|
-
case Closed(interval: Interval[A])
|
|
22
|
-
|
|
23
|
-
final case class Result[A](open: Option[Interval[A]], outputs: Vector[Output[A]])
|
|
24
|
-
|
|
25
|
-
def classifier(definitions: List[SegmentDef]): Params => Option[String] =
|
|
26
|
-
val lookup = (name: String) => definitions.find(_.name == name).map(_.condition)
|
|
27
|
-
params => definitions.find(_.condition.matches(params, lookup)).map(_.name)
|
|
28
|
-
|
|
29
|
-
def step[A](boundary: Boundary, open: Option[Interval[A]], event: Event[A]): Result[A] =
|
|
30
|
-
event match
|
|
31
|
-
case Event.Sample(at, name, continuous) =>
|
|
32
|
-
open match
|
|
33
|
-
case Some(previous) if continuous && previous.name == name =>
|
|
34
|
-
val updated = previous.copy(end = at)
|
|
35
|
-
Result(Some(updated), Vector(Output.Pending(updated)))
|
|
36
|
-
case _ =>
|
|
37
|
-
val next = Interval(name, at, at)
|
|
38
|
-
val closed = open.map: previous =>
|
|
39
|
-
val end = if boundary == Boundary.Live && previous.name != name then at else previous.end
|
|
40
|
-
Output.Closed(previous.copy(end = end))
|
|
41
|
-
Result(Some(next), closed.toVector :+ Output.Pending(next))
|
|
42
|
-
// The existing policies keep the last interval open through silence and finite EOF.
|
|
43
|
-
case Event.DeadlineExpired(_) | Event.EndOfInput => Result(open, Vector.empty)
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Yaroslav Kivaev
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
package yarkivaev.gapsandislands.api
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A run of consecutive observations that share the same classification
|
|
5
|
-
* kind.
|
|
6
|
-
*
|
|
7
|
-
* An `Island` is the fundamental unit of output from an [[IslandsFlow]].
|
|
8
|
-
* It represents a contiguous stretch of the input stream in which every
|
|
9
|
-
* observation was classified as the same `kind` by an [[IslandsPolicy]].
|
|
10
|
-
*
|
|
11
|
-
* The library is deliberately agnostic about what an observation is and
|
|
12
|
-
* how time is represented. The island therefore carries the actual
|
|
13
|
-
* `first` and `last` observations of the run rather than any derived
|
|
14
|
-
* metadata: anything domain-specific (duration, midpoint, summary,
|
|
15
|
-
* sampling count) is computed by the caller from the payloads.
|
|
16
|
-
*
|
|
17
|
-
* An `Island` is always non-empty: it is born when an observation is
|
|
18
|
-
* first classified as `kind` and grows as subsequent observations of the
|
|
19
|
-
* same kind arrive. A single-observation run yields an island with
|
|
20
|
-
* `first == last`.
|
|
21
|
-
*
|
|
22
|
-
* ==Example==
|
|
23
|
-
* {{{
|
|
24
|
-
* final case class Tick(timestamp: Long, value: Double)
|
|
25
|
-
*
|
|
26
|
-
* val island = Island(kind = "on", first = Tick(100L, 5.0), last = Tick(180L, 5.2))
|
|
27
|
-
* val durationMs = island.last.timestamp - island.first.timestamp
|
|
28
|
-
* }}}
|
|
29
|
-
*
|
|
30
|
-
* @tparam A the observation payload type chosen by the caller
|
|
31
|
-
* @tparam K the classification kind type chosen by the caller
|
|
32
|
-
* @param kind the classification shared by every observation in the run
|
|
33
|
-
* @param first the observation that opened this island
|
|
34
|
-
* @param last the most recent observation that extended this island
|
|
35
|
-
*/
|
|
36
|
-
final case class Island[A, K](
|
|
37
|
-
kind: K,
|
|
38
|
-
first: A,
|
|
39
|
-
last: A
|
|
40
|
-
)
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
package yarkivaev.gapsandislands.api
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Event emitted by [[IslandsFlow.pipe]] for every observation.
|
|
5
|
-
*
|
|
6
|
-
* Every input observation produces exactly one or two events:
|
|
7
|
-
* - [[IslandEvent.IslandStarted]] — the observation opened a new island;
|
|
8
|
-
* - [[IslandEvent.IslandExtended]] — the observation extended the open
|
|
9
|
-
* island (same kind as before);
|
|
10
|
-
* - [[IslandEvent.IslandClosed]] followed by [[IslandEvent.IslandStarted]]
|
|
11
|
-
* — the observation's kind differed, so the previous island ended
|
|
12
|
-
* and a new one began.
|
|
13
|
-
*
|
|
14
|
-
* Because every event carries the [[Island]] it describes (with the
|
|
15
|
-
* latest `first`/`last` observations), the stream of events is a
|
|
16
|
-
* lossless record of state: the open island after step N is exactly
|
|
17
|
-
* the island of the most recent event that is not an
|
|
18
|
-
* [[IslandEvent.IslandClosed]].
|
|
19
|
-
*
|
|
20
|
-
* @tparam A the observation payload type
|
|
21
|
-
* @tparam K the classification kind type
|
|
22
|
-
*/
|
|
23
|
-
sealed trait IslandEvent[A, K]:
|
|
24
|
-
/** The island described by this event. */
|
|
25
|
-
def island: Island[A, K]
|
|
26
|
-
|
|
27
|
-
object IslandEvent:
|
|
28
|
-
/**
|
|
29
|
-
* A new island has just opened.
|
|
30
|
-
*
|
|
31
|
-
* Emitted on the first observation of a flow or when an
|
|
32
|
-
* observation's classified kind differs from the kind of the
|
|
33
|
-
* currently open island. The island's `first` and `last` both
|
|
34
|
-
* reference the observation that triggered the start.
|
|
35
|
-
*/
|
|
36
|
-
final case class IslandStarted[A, K](island: Island[A, K]) extends IslandEvent[A, K]
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* The current island has absorbed one more observation.
|
|
40
|
-
*
|
|
41
|
-
* Emitted whenever an observation is classified with the same kind
|
|
42
|
-
* as the currently open island. The island's `first` is unchanged;
|
|
43
|
-
* `last` now references the new observation.
|
|
44
|
-
*/
|
|
45
|
-
final case class IslandExtended[A, K](island: Island[A, K]) extends IslandEvent[A, K]
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* A previously open island has just closed.
|
|
49
|
-
*
|
|
50
|
-
* Emitted when a new observation has a different kind than the
|
|
51
|
-
* currently open island; an [[IslandStarted]] follows immediately.
|
|
52
|
-
* The closed island's `last` references the most recent observation
|
|
53
|
-
* that belonged to that run.
|
|
54
|
-
*/
|
|
55
|
-
final case class IslandClosed[A, K](island: Island[A, K]) extends IslandEvent[A, K]
|