@yarkivaev/scada 2.8.0 → 2.9.1

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 (70) hide show
  1. package/package.json +4 -2
  2. package/src/application/management/loadText.js +37 -0
  3. package/src/application/management/management.js +17 -3
  4. package/src/application/management/networkLoad.js +80 -0
  5. package/src/application/management/terminalLabel.js +6 -0
  6. package/src/application/management/topology.js +36 -0
  7. package/src/application/management/topologyStatus.js +44 -0
  8. package/src/application/management/topologyText.js +28 -0
  9. package/src/bin/scada.js +25 -7
  10. package/src/infrastructure/management/controlLoad.js +33 -0
  11. package/src/infrastructure/management/inventory.js +34 -5
  12. package/src/infrastructure/management/kubernetes.js +35 -11
  13. package/src/infrastructure/management/observations.js +80 -0
  14. package/src/infrastructure/management/prometheusLoad.js +69 -0
  15. package/src/infrastructure/management/protocol.js +51 -5
  16. package/src/infrastructure/management/rabbitmqLoad.js +79 -0
  17. package/src/infrastructure/management/responseJson.js +13 -0
  18. package/stream-processing/dependencies/timeline-segmentation.jar +0 -0
  19. package/stream-processing/src/main/scala/scada/processing/core/Decision.scala +1 -11
  20. package/stream-processing/src/main/scala/scada/processing/core/MachineConfig.scala +1 -0
  21. package/stream-processing/src/main/scala/scada/processing/core/ReadingWorker.scala +6 -16
  22. package/stream-processing/src/main/scala/scada/processing/core/Segments.scala +1 -0
  23. package/stream-processing/src/main/scala/scada/processing/infra/checkpoint/Checkpoint.scala +1 -0
  24. package/stream-processing/src/main/scala/scada/processing/infra/checkpoint/ClickhouseReadingsCheckpoint.scala +1 -0
  25. package/stream-processing/src/main/scala/scada/processing/infra/checkpoint/SupervisorSinkCheckpoint.scala +1 -0
  26. package/stream-processing/src/main/scala/scada/processing/infra/config/AppConfig.scala +2 -2
  27. package/stream-processing/src/main/scala/scada/processing/infra/config/ConfigLoad.scala +1 -0
  28. package/stream-processing/src/main/scala/scada/processing/infra/config/ConfigProfile.scala +0 -1
  29. package/stream-processing/src/main/scala/scada/processing/infra/mqtt/Mqtt.scala +1 -1
  30. package/stream-processing/src/main/scala/scada/processing/infra/state/CheckpointSegments.scala +1 -0
  31. package/stream-processing/src/main/scala/scada/processing/infra/state/SegmentStatePolicy.scala +1 -2
  32. package/stream-processing/src/main/scala/scada/processing/infra/state/Segments.scala +1 -0
  33. package/stream-processing/src/main/scala/scada/processing/infra/stomp/DecisionMessage.scala +2 -2
  34. package/stream-processing/src/main/scala/scada/processing/infra/stomp/SegmentMessage.scala +2 -2
  35. package/stream-processing/src/main/scala/scada/processing/infra/stomp/SegmentWirePolicy.scala +1 -2
  36. package/stream-processing/src/main/scala/scada/processing/infra/stomp/SplitFamily.scala +2 -2
  37. package/stream-processing/src/main/scala/scada/processing/infra/stomp/StompMachineDecisions.scala +1 -1
  38. package/stream-processing/src/main/scala/scada/processing/infra/stomp/StompReadings.scala +1 -1
  39. package/stream-processing/src/main/scala/scada/processing/infra/stomp/StompSegments.scala +1 -0
  40. package/stream-processing/src/main/scala/scada/processing/runtime/ClassifyHistory.scala +2 -1
  41. package/stream-processing/src/main/scala/scada/processing/runtime/ClassifyStreams.scala +1 -0
  42. package/stream-processing/src/main/scala/scada/processing/runtime/Close.scala +1 -1
  43. package/stream-processing/src/main/scala/scada/processing/runtime/HistoryBus.scala +1 -0
  44. package/stream-processing/src/main/scala/scada/processing/runtime/LocalHistory.scala +13 -61
  45. package/stream-processing/src/main/scala/scada/processing/runtime/PendingIsland.scala +2 -1
  46. package/stream-processing/src/main/scala/scada/processing/runtime/ProcessingApplication.scala +2 -12
  47. package/stream-processing/src/main/scala/scada/processing/runtime/RuntimeProfile.scala +2 -1
  48. package/stream-processing/src/main/scala/scada/processing/runtime/Seeding.scala +2 -1
  49. package/stream-processing/timeline-segmentation.lock +2 -0
  50. package/stream-processing/src/main/scala/scada/processing/core/Condition.scala +0 -33
  51. package/stream-processing/src/main/scala/scada/processing/core/CyclePhases.scala +0 -65
  52. package/stream-processing/src/main/scala/scada/processing/core/Emission.scala +0 -19
  53. package/stream-processing/src/main/scala/scada/processing/core/Params.scala +0 -15
  54. package/stream-processing/src/main/scala/scada/processing/core/Reading.scala +0 -11
  55. package/stream-processing/src/main/scala/scada/processing/core/Segment.scala +0 -28
  56. package/stream-processing/src/main/scala/scada/processing/core/SegmentDef.scala +0 -14
  57. package/stream-processing/src/main/scala/scada/processing/core/Segmentation.scala +0 -47
  58. package/stream-processing/src/main/scala/scada/processing/core/SplitFollow.scala +0 -50
  59. package/stream-processing/src/main/scala/scada/processing/core/SplitPieces.scala +0 -102
  60. package/stream-processing/src/main/scala/scada/processing/core/TagDef.scala +0 -12
  61. package/stream-processing/src/main/scala/scada/processing/core/TaggedSegment.scala +0 -19
  62. package/stream-processing/src/main/scala/scada/processing/core/Tagging.scala +0 -42
  63. package/stream-processing/src/main/scala/scada/processing/core/Timestamp.scala +0 -26
  64. package/stream-processing/src/main/scala/scada/processing/core/segmentation/Timeline.scala +0 -43
  65. package/stream-processing/vendor/gaps-and-islands/LICENSE +0 -21
  66. package/stream-processing/vendor/gaps-and-islands/UPSTREAM +0 -4
  67. package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/Island.scala +0 -40
  68. package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandEvent.scala +0 -55
  69. package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsFlow.scala +0 -127
  70. package/stream-processing/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsPolicy.scala +0 -105
@@ -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,4 +0,0 @@
1
- https://github.com/yarkivaev/gaps-and-islands
2
- Commit: 1a59628544dfc2f788b535f78dae14506cf861cf
3
- Version: 0.1.1
4
- The Scala sources are unmodified. See LICENSE.
@@ -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]
@@ -1,127 +0,0 @@
1
- package yarkivaev.gapsandislands.api
2
-
3
- import fs2.{Chunk, Pipe, Pull, Stream}
4
-
5
- /**
6
- * Pure stream transformation that turns a stream of observations into
7
- * a stream of [[IslandEvent]]s.
8
- *
9
- * The library offers no stateful "flow object". State is threaded
10
- * internally through the pipe's fold over the input; the caller never
11
- * sees the intermediate cursor. Every input observation produces one
12
- * or two events, so the emitted stream is a lossless record of the
13
- * open island over time — any snapshot or derived view is a plain
14
- * stream operation over that output.
15
- *
16
- * The pipe leaves the trailing island open when the input terminates.
17
- * Callers that need a final [[IslandEvent.IslandClosed]] (finite
18
- * batches, explicit end-of-run markers) compose their own terminator;
19
- * doing so is intentionally the caller's concern because the semantics
20
- * of "the input ended" vary by domain.
21
- *
22
- * ==Effect polymorphism==
23
- * The pipe is parametric in the effect `F[_]`. It imposes no typeclass
24
- * constraint: the only effectful step is evaluating the supplied
25
- * [[IslandsPolicy]], which is lifted into the stream via `Pull.eval`.
26
- * `F` is typically `cats.effect.IO`, but anything that the policy
27
- * speaks (`SyncIO`, a custom tagless-final algebra, etc.) works.
28
- *
29
- * ==Usage==
30
- * {{{
31
- * // `F` is inferred from the policy; with an `IslandsPolicy[IO, _, _]`
32
- * // the pipe is `Pipe[IO, A, IslandEvent[A, K]]`.
33
- * val events: Stream[F, IslandEvent[Tick, String]] =
34
- * observations.through(IslandsFlow.pipe(policy, evidence))
35
- *
36
- * // lifecycle-only view
37
- * events.collect {
38
- * case e: IslandEvent.IslandStarted[Tick, String] => e
39
- * case e: IslandEvent.IslandClosed[Tick, String] => e
40
- * }
41
- *
42
- * // open-island checkpoint at each step
43
- * events.map(_.island)
44
- * }}}
45
- */
46
- object IslandsFlow:
47
-
48
- /**
49
- * Build a gaps-and-islands transformation.
50
- *
51
- * The returned pipe consumes observations of type `A`, asks the
52
- * supplied [[IslandsPolicy]] to classify each into a kind of type
53
- * `K`, and emits [[IslandEvent]]s describing how the island
54
- * structure evolves.
55
- *
56
- * ==Recovery==
57
- * Pass `initial = Some(island)` to resume from a previously
58
- * snapshotted open island. No [[IslandEvent.IslandStarted]] is
59
- * emitted for the resumed island; the next observation will either
60
- * extend it (emitting [[IslandEvent.IslandExtended]]) or close it
61
- * (emitting [[IslandEvent.IslandClosed]] followed by
62
- * [[IslandEvent.IslandStarted]]).
63
- *
64
- * ==Snapshot==
65
- * The open island at any point is the `island` of the latest
66
- * non-`IslandClosed` event. Derive it with a plain `scan`:
67
- * {{{
68
- * events.scan(Option.empty[Island[A, K]]) {
69
- * case (_, IslandEvent.IslandClosed(_)) => None
70
- * case (_, other) => Some(other.island)
71
- * }
72
- * }}}
73
- *
74
- * ==Terminal close==
75
- * The pipe emits no event when the input terminates. Callers that
76
- * treat the input as a finite batch and want a final
77
- * [[IslandEvent.IslandClosed]] add one themselves, for example via
78
- * a post-processing pipe that tracks the open island and emits a
79
- * close on end-of-stream.
80
- *
81
- * @tparam F the effect used to evaluate the policy
82
- * @tparam A the observation payload type
83
- * @tparam K the classification kind type
84
- * @tparam E the evidence type consumed by the policy
85
- * @param policy decision rule used to classify each observation
86
- * @param evidence builds the policy's input from the currently open
87
- * island (if any) and the incoming observation
88
- * @param initial optional open island to resume from
89
- */
90
- def pipe[F[_], A, K, E](
91
- policy: IslandsPolicy[F, E, K],
92
- evidence: (Option[Island[A, K]], A) => E,
93
- initial: Option[Island[A, K]] = None
94
- ): Pipe[F, A, IslandEvent[A, K]] =
95
- input => advance(initial, input, policy, evidence).stream
96
-
97
- private def advance[F[_], A, K, E](
98
- state: Option[Island[A, K]],
99
- input: Stream[F, A],
100
- policy: IslandsPolicy[F, E, K],
101
- evidence: (Option[Island[A, K]], A) => E
102
- ): Pull[F, IslandEvent[A, K], Unit] =
103
- input.pull.uncons1.flatMap:
104
- case None => Pull.done
105
- case Some((item, rest)) =>
106
- Pull.eval(policy.decide(evidence(state, item))).flatMap: kind =>
107
- val (next, events) = step(state, item, kind)
108
- Pull.output(Chunk.from(events)) >> advance(next, rest, policy, evidence)
109
-
110
- private def step[A, K](
111
- state: Option[Island[A, K]],
112
- item: A,
113
- kind: K
114
- ): (Option[Island[A, K]], List[IslandEvent[A, K]]) =
115
- state match
116
- case None =>
117
- val fresh = Island(kind, item, item)
118
- (Some(fresh), List(IslandEvent.IslandStarted(fresh)))
119
- case Some(open) if open.kind == kind =>
120
- val extended = Island(kind, open.first, item)
121
- (Some(extended), List(IslandEvent.IslandExtended(extended)))
122
- case Some(open) =>
123
- val fresh = Island(kind, item, item)
124
- (Some(fresh), List(
125
- IslandEvent.IslandClosed(open),
126
- IslandEvent.IslandStarted(fresh)
127
- ))
@@ -1,105 +0,0 @@
1
- package yarkivaev.gapsandislands.api
2
-
3
- import cats.Monad
4
- import cats.data.Kleisli
5
-
6
- /**
7
- * Decision rule that classifies an observation into a kind.
8
- *
9
- * An `IslandsPolicy` is the only place where the caller expresses domain
10
- * meaning. Given whatever evidence it needs — the observation itself, a
11
- * rolling window, a lookup against a database, the previously open
12
- * island — it returns the kind `K` to which the current observation
13
- * belongs. The [[IslandsFlow]] then uses that kind to decide whether to
14
- * extend the current island or close it and open a new one.
15
- *
16
- * There is no privileged "boundary" or "gap" outcome: every observation
17
- * belongs to some island. If the caller wants to distinguish missing
18
- * data, idle periods, or rejected samples from useful runs, they encode
19
- * that distinction as a dedicated value of `K` (for example a
20
- * `"gap"` string or a `Gap` variant of a sealed trait).
21
- *
22
- * ==Composition==
23
- * Policies form a monad over `K` in their shared `F` and `Evidence`.
24
- * `flatMap` runs every stage on every observation, which lets callers
25
- * build refined decisions pipeline-style, for example "first tag as
26
- * live-or-stale, then classify live samples as on-or-off":
27
- *
28
- * {{{
29
- * val tagFreshness: IslandsPolicy[IO, Tick, String] =
30
- * IslandsPolicy(t => IO.pure(if t.ageMs > 30000 then "stale" else "live"))
31
- *
32
- * val refine: String => IslandsPolicy[IO, Tick, String] =
33
- * case "live" => IslandsPolicy(t => IO.pure(if t.power >= 5.0 then "on" else "off"))
34
- * case other => IslandsPolicy(_ => IO.pure(other))
35
- *
36
- * val policy = tagFreshness.flatMap(refine)
37
- * }}}
38
- *
39
- * @tparam F the effect type used to evaluate the decision
40
- * @tparam Evidence the contextual input the policy needs to decide;
41
- * contravariant so narrower evidence types can be
42
- * supplied than declared
43
- * @tparam K the classification kind returned by this policy
44
- */
45
- trait IslandsPolicy[F[_], -Evidence, K]:
46
- /**
47
- * Classify the observation described by `evidence`.
48
- *
49
- * Implementations should be referentially transparent with respect to
50
- * the supplied evidence; any mutable state they need must live inside
51
- * `F`.
52
- *
53
- * @param evidence everything the caller has decided the policy needs
54
- * @return the kind of island the current observation belongs to
55
- */
56
- def decide(evidence: Evidence): F[K]
57
-
58
- object IslandsPolicy:
59
- /**
60
- * Build a policy from a plain decision function.
61
- *
62
- * @param fn function mapping evidence to an effectful kind
63
- */
64
- def apply[F[_], E, K](fn: E => F[K]): IslandsPolicy[F, E, K] =
65
- new IslandsPolicy[F, E, K]:
66
- def decide(evidence: E): F[K] =
67
- fn(evidence)
68
-
69
- /**
70
- * Lift a `cats.data.Kleisli` into an `IslandsPolicy`.
71
- *
72
- * Useful when callers already express their decision logic as a
73
- * `Kleisli[F, E, K]` and want to reuse its combinators before handing
74
- * the rule to a flow.
75
- */
76
- def liftKleisli[F[_], E, K](rule: Kleisli[F, E, K]): IslandsPolicy[F, E, K] =
77
- apply(rule.run)
78
-
79
- extension [F[_], E, K](policy: IslandsPolicy[F, E, K])
80
- /**
81
- * Post-process the kind produced by this policy.
82
- *
83
- * `map` is a pure relabelling: it never changes whether an
84
- * observation is classified, only how the resulting kind is named.
85
- *
86
- * @param fn transformation applied to the classified kind
87
- * @param monad monad instance for the policy's effect
88
- */
89
- def map[L](fn: K => L)(using monad: Monad[F]): IslandsPolicy[F, E, L] =
90
- IslandsPolicy(evidence => monad.map(policy.decide(evidence))(fn))
91
-
92
- /**
93
- * Chain a second decision stage onto this policy.
94
- *
95
- * Both stages are evaluated on every observation; the kind produced
96
- * by the first stage is fed into the function that chooses the next
97
- * policy. This models refinement pipelines where later stages
98
- * specialise the classification of earlier stages.
99
- *
100
- * @param next function producing the next policy from the previous kind
101
- * @param monad monad instance for the policy's effect
102
- */
103
- def flatMap[L](next: K => IslandsPolicy[F, E, L])(using monad: Monad[F]): IslandsPolicy[F, E, L] =
104
- IslandsPolicy: evidence =>
105
- monad.flatMap(policy.decide(evidence))(kind => next(kind).decide(evidence))