@yarkivaev/scada 2.22.0 → 2.24.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 (48) hide show
  1. package/charts/scada-workloads/Chart.yaml +5 -0
  2. package/charts/scada-workloads/README.md +45 -0
  3. package/charts/scada-workloads/templates/_collector.tpl +103 -0
  4. package/charts/scada-workloads/templates/_control-agent.tpl +80 -0
  5. package/charts/scada-workloads/templates/_controller.tpl +69 -0
  6. package/charts/scada-workloads/templates/_etcd-member.tpl +92 -0
  7. package/charts/scada-workloads/templates/_history-maintenance.tpl +44 -0
  8. package/charts/scada-workloads/templates/_history-receiver.tpl +88 -0
  9. package/charts/scada-workloads/templates/_journal-supervisor.tpl +82 -0
  10. package/charts/scada-workloads/templates/_postgres-role.tpl +43 -0
  11. package/charts/scada-workloads/templates/_reserve-supervisor.tpl +65 -0
  12. package/charts/scada-workloads/templates/_site-server.tpl +74 -0
  13. package/charts/scada-workloads/templates/_supervisor-history.tpl +12 -0
  14. package/charts/scada-workloads/templates/_supervisor.tpl +87 -0
  15. package/charts/scada-workloads/templates/_telemetry.tpl +181 -0
  16. package/db/migrations/E0027__metric_claim_trigger.sql +33 -0
  17. package/package.json +4 -1
  18. package/src/application/runtime/applicationRuntime.js +5 -2
  19. package/src/application/runtime/index.js +3 -0
  20. package/src/application/runtime/opcuaTask.js +63 -0
  21. package/src/application/runtime/runCommand.js +10 -0
  22. package/src/application/runtime/runService.js +14 -0
  23. package/src/application/siteRuntime/backfillOperationPayloads.js +41 -0
  24. package/src/application/siteRuntime/centralConnections.js +13 -0
  25. package/src/application/siteRuntime/edgeIngress.js +25 -0
  26. package/src/application/siteRuntime/index.js +5 -0
  27. package/src/application/siteRuntime/startAnalyticsRuntime.js +33 -0
  28. package/src/infrastructure/deploy/ci/applicationBundleLock.js +51 -0
  29. package/src/infrastructure/deploy/ci/archiveApplication.py +30 -0
  30. package/src/infrastructure/deploy/ci/packageApplication.js +63 -0
  31. package/src/infrastructure/deploy/ci/releaseArtifacts.js +55 -0
  32. package/src/infrastructure/deploy/ci/scada_gitlab.py +75 -0
  33. package/src/infrastructure/deploy/ci/scada_registry.py +91 -0
  34. package/src/infrastructure/deploy/configuredSiteAssets.js +8 -2
  35. package/src/infrastructure/deploy/executeStages.js +8 -1
  36. package/src/infrastructure/deploy/index.js +3 -1
  37. package/src/infrastructure/deploy/installVerifiedTools.js +44 -0
  38. package/src/infrastructure/deploy/stagedSitePlan.js +3 -1
  39. package/src/infrastructure/deploy/useApplicationBundle.sh +22 -0
  40. package/src/infrastructure/opcua/index.js +2 -0
  41. package/src/infrastructure/opcua/opcuaRecord.js +44 -0
  42. package/src/infrastructure/opcua/publishOpcuaRecord.js +23 -0
  43. package/src/infrastructure/persistence/pg/orderedRows.js +31 -0
  44. package/src/testing/boundedQueue.js +50 -0
  45. package/stream-processing/project/plugins.sbt +2 -0
  46. package/stream-processing/src/main/resources/logback.xml +16 -0
  47. package/stream-processing/src/main/scala/scada/processing/runtime/DurableMachineInput.scala +6 -0
  48. package/stream-processing/src/main/scala/scada/processing/runtime/WorkerStores.scala +31 -2
@@ -0,0 +1,23 @@
1
+ export const TELEMETRY = 'scada.telemetry';
2
+
3
+ /**
4
+ * Publishes one OPC UA metric record to scada.telemetry.
5
+ *
6
+ * @param {object} channel - amqplib channel
7
+ * @param {{ routingKey: string, body: string }} record - telemetry record
8
+ * @returns {boolean} publish accepted
9
+ */
10
+ export default function publishRecord(channel, record) {
11
+ if (typeof channel.write === 'function') {
12
+ return channel.write([{
13
+ exchange: TELEMETRY, routingKey: record.routingKey, body: Buffer.from(record.body),
14
+ options: { contentType: 'application/json', headers: { 'scada-route': record.routingKey }, persistent: true, mandatory: true }
15
+ }]);
16
+ }
17
+ return channel.publish(TELEMETRY, record.routingKey, Buffer.from(record.body), {
18
+ contentType: 'application/json',
19
+ headers: { 'scada-route': record.routingKey },
20
+ persistent: true,
21
+ mandatory: true
22
+ });
23
+ }
@@ -0,0 +1,31 @@
1
+ function identifier(name) {
2
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/u.test(name)) { throw new TypeError('Invalid PostgreSQL identifier'); }
3
+ return /^[a-z_][a-z0-9_]*$/u.test(name) ? name : `"${name}"`;
4
+ }
5
+
6
+ /** Query a configured PostgreSQL relation by timestamp and a stable tie-break value. */
7
+ export default function orderedRows(pool, { relation, group, timestamp, tieBreak }) {
8
+ const table = relation.split('.').map(identifier).join('.');
9
+ const object = identifier(group);
10
+ const at = identifier(timestamp);
11
+ const id = identifier(tieBreak);
12
+ const select = `SELECT * FROM ${table} WHERE ${object} = $1`;
13
+ const order = `ORDER BY ${at}, ${id}`;
14
+
15
+ async function fetch(value, cursor, limit) {
16
+ const values = [value];
17
+ let filter = '';
18
+ if (cursor.kind === 'cursor') {
19
+ values.push(cursor.at.toISOString());
20
+ filter = ` AND ${at} > $2`;
21
+ if (cursor.id !== undefined) {
22
+ values.push(cursor.id);
23
+ filter = ` AND (${at}, ${id}) > ($2, $3)`;
24
+ }
25
+ }
26
+ values.push(limit);
27
+ return (await pool.query(`${select}${filter} ${order} LIMIT $${values.length}`, values)).rows;
28
+ }
29
+
30
+ return fetch;
31
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * In-memory durable-style queue with max-length and drop-head overflow.
3
+ * Models RabbitMQ x-max-length / x-overflow=drop-head for tests and local drains.
4
+ *
5
+ * @param {number} maxLength - maximum ready messages retained
6
+ * @returns {object} queue with publish/take/ack/nack/depth/dropped
7
+ */
8
+ export default function boundedQueue(maxLength) {
9
+ const ready = [];
10
+ let held = null;
11
+ let dropped = 0;
12
+ return Object.freeze({
13
+ depth() {
14
+ return ready.length + (held === null ? 0 : 1);
15
+ },
16
+ ready() {
17
+ return ready.length;
18
+ },
19
+ dropped() {
20
+ return dropped;
21
+ },
22
+ publish(message) {
23
+ ready.push(message);
24
+ while (ready.length > maxLength) {
25
+ ready.shift();
26
+ dropped += 1;
27
+ }
28
+ },
29
+ take() {
30
+ if (held !== null) {
31
+ throw new Error('boundedQueue already holds an unacked message');
32
+ }
33
+ if (ready.length === 0) {
34
+ return null;
35
+ }
36
+ held = ready.shift();
37
+ return held;
38
+ },
39
+ ack() {
40
+ held = null;
41
+ },
42
+ nack() {
43
+ if (held === null) {
44
+ return;
45
+ }
46
+ ready.unshift(held);
47
+ held = null;
48
+ }
49
+ });
50
+ }
@@ -0,0 +1,2 @@
1
+ addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
2
+ addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.3.0")
@@ -0,0 +1,16 @@
1
+ <configuration>
2
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3
+ <encoder>
4
+ <pattern>%d{ISO8601} %-5level [%thread] %logger{36} %mdc - %msg%n</pattern>
5
+ </encoder>
6
+ </appender>
7
+
8
+ <logger name="com.zaxxer.hikari" level="INFO"/>
9
+ <logger name="org.eclipse.paho" level="INFO"/>
10
+ <logger name="org.postgresql" level="WARN"/>
11
+ <logger name="org.http4s.ember.server" level="INFO"/>
12
+
13
+ <root level="INFO">
14
+ <appender-ref ref="STDOUT"/>
15
+ </root>
16
+ </configuration>
@@ -15,3 +15,9 @@ final class DurableMachineInput(config: AppConfig, machine: MachineConfig, queue
15
15
  def readings(aliases: List[AliasMapping], seeding: Option[Seeding])(ready: IO[Unit]): Stream[IO, (Reading, IO[Unit])] =
16
16
  val live = StompReadings(config.connectivity.rabbitmq, queue, aliases).subscribeDurable
17
17
  seeding.fold(live)(_.seedDurable(machine, aliases, live, ready))
18
+
19
+
20
+ def run(aliases: List[AliasMapping], seeding: Option[Seeding])(
21
+ process: (IO[Unit] => Stream[IO, (Reading, IO[Unit])], Stream[IO, Decision], Boolean) => IO[Unit]
22
+ ): Stream[IO, Unit] =
23
+ Stream.eval(process(readings(aliases, seeding), decisions, seeding.isDefined))
@@ -1,10 +1,11 @@
1
1
  package scada.processing.runtime
2
2
 
3
- import cats.effect.{IO, Ref}
3
+ import cats.effect.{IO, Ref, Resource}
4
4
  import cats.effect.std.Semaphore
5
5
  import io.circe.{Decoder, Encoder}
6
- import io.yarkivaev.streamworker.{Checkpoint, CheckpointStore, StaleOwnership, TaskIdentity}
6
+ import io.yarkivaev.streamworker.{Checkpoint, CheckpointStore, Output, OutputId, ProcessorVersion, StaleOwnership, TaskIdentity}
7
7
  import io.yarkivaev.streamworker.postgres.PgCheckpointStore
8
+ import scada.processing.infra.state.StateJournal
8
9
 
9
10
  object WorkerStores:
10
11
  final case class Binding[S, O](store: CheckpointStore[S, O], task: TaskIdentity, ownership: Long)
@@ -41,3 +42,31 @@ object WorkerStores:
41
42
  IO.raiseUnless(current.revision == expectedRevision)(IllegalStateException("Worker checkpoint revision changed")) *>
42
43
  persist(next) *> state.set(next)
43
44
  Binding(store, initial.task, owner)
45
+
46
+
47
+ /** Stable receipt metadata for application-owned journal formats. */
48
+ final case class JournalCursor(runId: String, position: Option[Long], revision: Long, closedThrough: Long)
49
+ derives Encoder.AsObject, Decoder
50
+
51
+ final case class JournalSnapshot[S, O](state: S, pending: Option[O], cursor: Option[JournalCursor], observedThrough: Long)
52
+
53
+ /** Restore a local outbox without changing its wire format or replay identity. */
54
+ def journal[A, S, O](open: Resource[IO, StateJournal[A]], machine: String, source: String,
55
+ version: ProcessorVersion, publishedThrough: Long)(
56
+ read: A => JournalSnapshot[S, O], restore: (S, Long) => S, attachCursor: (A, JournalCursor) => A,
57
+ write: (Checkpoint[S, O], JournalCursor) => Option[A], closedThrough: S => Long
58
+ ): Resource[IO, Binding[S, O]] =
59
+ open.evalMap: journal =>
60
+ for
61
+ record <- journal.load
62
+ saved <- IO(read(record))
63
+ edge = math.max(publishedThrough, math.max(saved.observedThrough, saved.cursor.fold(0L)(_.closedThrough)))
64
+ cursor <- saved.cursor.fold(IO.randomUUID.map(id => JournalCursor(id.toString, None, 0L, edge)))(IO.pure)
65
+ task = TaskIdentity(machine, source, cursor.runId)
66
+ pending = saved.pending.map(value => Output(OutputId(task, cursor.position.getOrElse(0L), 0), value))
67
+ start = Checkpoint(task, version, cursor.revision, cursor.position, restore(saved.state, edge), pending.toVector)
68
+ _ <- IO.whenA(saved.cursor.isEmpty && saved.pending.nonEmpty)(journal.save(attachCursor(record, cursor)))
69
+ binding <- local(start): next =>
70
+ val receipt = JournalCursor(next.task.runId, next.position, next.revision, closedThrough(next.state))
71
+ write(next, receipt).fold(IO.unit)(journal.save)
72
+ yield binding