Guide

Formato delle regole di avviso (kyr/1)

Il documento JSON portabile che è un avviso: campi, condizioni, segnali derivati e semantica.

Campi

NomeTipoDescrizione
kyr1La versione del formato.
idstringUn id univoco (un UUID).
versionnumberAumenta di uno a ogni modifica salvata.
namestringDa 1 a 120 caratteri. Visibile ovunque l’avviso sia elencato: non metterci valori.
severity'info' | 'warning' | 'critical'Come viene mostrato l’avviso.
enabledbooleanUna regola disabilitata viene conservata ma non controllata.
targets{ streams?, datasets? }Elenchi facoltativi di id di stream e nomi di file a cui si applica la regola; assente o null: tutti.
signalsRecord<alias, { channel, stream?, dataset? } | { formula }>Alias (lettere, cifre, trattino basso; fino a 32 caratteri) per i nomi dei canali. Un ambito (id dello stream o nome del file) limita dove cercare il canale. formula indica un canale matematico (solo browser).
derivedRecord<alias, Derived>{ op: "rate" | "mean" | "min" | "max", of, windowS }, { op: "abs", of } oppure { op: "diff", a, b }. Un segnale derivato può usarne un altro.
whenConditionL’albero delle condizioni (sotto).
forSnumberSecondi per cui la condizione deve restare vera prima che l’avviso scatti.
clearForSnumberSecondi per cui la condizione deve restare falsa prima che l’avviso si chiuda.
staleSnumber | nullUn segnale senza campioni per questo tempo conta come sconosciuto. Assente o null: mai.
runWhere('browser' | 'collector')[]Dove gira la regola: ['browser'], oppure ['browser', 'collector'] per farla girare anche sulla macchina mittente (il collector o un mittente Python; vedi Avvisi senza nessuno che guarda). Una regola con una formula gira solo nel browser.
origin{ workbookId, worksheetId, chartId, field, seriesId? } | nullLa linea di allarme del grafico da cui è nato l’avviso, se c’è.

Condizioni

CondizioneVero quando
{ above: a, value, hysteresis? }a > value; torna falso quando a ≤ value − hysteresis.
{ below: a, value, hysteresis? }a < value; torna falso quando a ≥ value + hysteresis.
{ outside: a, lo, hi, hysteresis? }a < lo oppure a > hi; torna falso quando lo + h ≤ a ≤ hi − h.
{ inside: a, lo, hi, hysteresis? }lo ≤ a ≤ hi; torna falso quando a < lo − h oppure a > hi + h.
{ equals: a, value }a è uguale a value (numeri di stato, posizioni).
{ changes: a }A un campione il cui valore differisce dal precedente.
{ noData: a, forS }Nessun campione di a per forS secondi.
{ all: [...] }, { any: [...] }, { not: c }Combinazioni, fino a 3 livelli di profondità, 16 condizioni in tutto.

Esempi

Soglia con isteresi
{
  "kyr": 1,
  "id": "6f0c1c1e-0000-4000-8000-000000000001",
  "version": 1,
  "name": "Oil temperature high",
  "severity": "critical",
  "enabled": true,
  "signals": { "oil": { "channel": "Oil_Temp" } },
  "when": { "above": "oil", "value": 120, "hysteresis": 2 },
  "forS": 3,
  "clearForS": 10,
  "runWhere": ["browser"]
}
Velocità di variazione
{
  "kyr": 1, "id": "c1a2…", "version": 1, "name": "Pressure rising fast", "severity": "warning", "enabled": true,
  "signals": { "p": { "channel": "Line_Pressure" } },
  "derived": { "pRate": { "op": "rate", "of": "p", "windowS": 2 } },
  "when": { "above": "pRate", "value": 5 },
  "forS": 0, "clearForS": 5, "runWhere": ["browser"]
}
Watchdog di assenza dati su uno stream
{
  "kyr": 1, "id": "9d3e…", "version": 1, "name": "Press 2 silent", "severity": "warning", "enabled": true,
  "signals": { "rpm": { "channel": "RPM", "stream": "<stream id>" } },
  "when": { "noData": "rpm", "forS": 60 },
  "forS": 0, "clearForS": 0, "runWhere": ["browser"]
}

Semantica

  • Il tempo è quello proprio dei dati (il t del campione), non l’orologio del computer che controlla. Solo “has no data for” su uno stream live usa anche l’ora di arrivo.
  • Una regola viene controllata a ogni campione di qualsiasi canale che usa, in ordine di tempo. Gli altri canali mantengono l’ultimo valore, così i canali campionati a frequenze diverse si combinano.
  • Un canale senza ancora un valore, o senza valori da staleS secondi, è sconosciuto. Una condizione su di esso è sconosciuta; All of è falso se una parte è falsa, altrimenti sconosciuto se una è sconosciuta; Any of è vero se una parte è vera. Una regola sconosciuta non genera né chiude avvisi.
  • L’ora di inizio dell’avviso è il primo campione in cui la condizione era vera (l’innesco), anche quando For at least ritarda l’avviso; la fine è il primo campione in cui era falsa.
  • Un campione più vecchio dell’ultimo controllato viene saltato e contato.
  • Se un canale usato dalla regola scompare da uno stream, un avviso aperto termina all’ultimo campione controllato e la regola torna in attesa.

JSON Schema

Lo schema qui sotto descrive ogni campo e limite; è incluso nell’SDK come @kymoscope/sdk/rules/kyr1.schema.json. validateRule dell’SDK applica gli stessi controlli e verifica anche che gli alias esistano e che i segnali derivati non formino un ciclo.

kyr1.schema.json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kymoscope.com/schemas/kyr1.schema.json",
  "title": "Kymoscope alert rule (kyr/1)",
  "description": "A portable alert rule. Channels are matched by exact, case-sensitive name on each target; a rule whose channels a target lacks waits there. Runtimes check the semantic limits (aliases referenced exist, derived signals are acyclic, at most 16 conditions) themselves.",
  "type": "object",
  "additionalProperties": true,
  "required": [
    "kyr",
    "id",
    "version",
    "name",
    "severity",
    "enabled",
    "signals",
    "when",
    "forS",
    "clearForS",
    "runWhere"
  ],
  "properties": {
    "kyr": {
      "const": 1
    },
    "id": {
      "type": "string",
      "minLength": 1
    },
    "version": {
      "type": "integer",
      "minimum": 1
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 120,
      "pattern": "\\S"
    },
    "severity": {
      "enum": [
        "info",
        "warning",
        "critical"
      ]
    },
    "enabled": {
      "type": "boolean"
    },
    "targets": {
      "type": "object",
      "properties": {
        "streams": {
          "$ref": "#/definitions/idList"
        },
        "datasets": {
          "$ref": "#/definitions/idList"
        }
      }
    },
    "signals": {
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "$ref": "#/definitions/alias"
      },
      "additionalProperties": {
        "$ref": "#/definitions/signal"
      }
    },
    "derived": {
      "type": "object",
      "propertyNames": {
        "$ref": "#/definitions/alias"
      },
      "additionalProperties": {
        "$ref": "#/definitions/derived"
      }
    },
    "when": {
      "$ref": "#/definitions/condition"
    },
    "forS": {
      "$ref": "#/definitions/seconds"
    },
    "clearForS": {
      "$ref": "#/definitions/seconds"
    },
    "staleS": {
      "oneOf": [
        {
          "$ref": "#/definitions/seconds"
        },
        {
          "type": "null"
        }
      ]
    },
    "notify": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "runWhere": {
      "type": "array",
      "minItems": 1,
      "items": {
        "enum": [
          "browser",
          "collector"
        ]
      }
    },
    "keep": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "required": [
            "beforeS",
            "afterS"
          ],
          "properties": {
            "beforeS": {
              "$ref": "#/definitions/seconds"
            },
            "afterS": {
              "$ref": "#/definitions/seconds"
            }
          }
        }
      ]
    },
    "origin": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "required": [
            "workbookId",
            "worksheetId",
            "chartId",
            "field"
          ],
          "properties": {
            "workbookId": {
              "type": [
                "string",
                "null"
              ]
            },
            "worksheetId": {
              "type": "string"
            },
            "chartId": {
              "type": "string"
            },
            "field": {
              "type": "string"
            },
            "seriesId": {
              "type": "string"
            }
          }
        }
      ]
    }
  },
  "definitions": {
    "alias": {
      "type": "string",
      "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,31}$"
    },
    "channel": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200
    },
    "seconds": {
      "type": "number",
      "minimum": 0,
      "maximum": 86400
    },
    "hysteresis": {
      "type": "number",
      "minimum": 0
    },
    "idList": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      ]
    },
    "signal": {
      "oneOf": [
        {
          "type": "object",
          "required": [
            "channel"
          ],
          "additionalProperties": false,
          "properties": {
            "channel": {
              "$ref": "#/definitions/channel"
            },
            "stream": {
              "type": "string",
              "description": "Only on this stream id."
            },
            "dataset": {
              "type": "string",
              "description": "Only on loaded files with this name."
            },
            "tag": {
              "type": "string",
              "description": "Collector only: bind by the source address (OPC UA node id) instead of the published name."
            }
          }
        },
        {
          "type": "object",
          "required": [
            "formula"
          ],
          "additionalProperties": false,
          "description": "A math channel by name. The rule's runWhere must be exactly [\"browser\"].",
          "properties": {
            "formula": {
              "$ref": "#/definitions/channel"
            }
          }
        }
      ]
    },
    "derived": {
      "oneOf": [
        {
          "type": "object",
          "required": [
            "op",
            "of",
            "windowS"
          ],
          "additionalProperties": false,
          "properties": {
            "op": {
              "enum": [
                "rate",
                "mean",
                "min",
                "max"
              ]
            },
            "of": {
              "$ref": "#/definitions/alias"
            },
            "windowS": {
              "type": "number",
              "exclusiveMinimum": 0,
              "maximum": 86400
            }
          }
        },
        {
          "type": "object",
          "required": [
            "op",
            "of"
          ],
          "additionalProperties": false,
          "properties": {
            "op": {
              "const": "abs"
            },
            "of": {
              "$ref": "#/definitions/alias"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "op",
            "a",
            "b"
          ],
          "additionalProperties": false,
          "properties": {
            "op": {
              "const": "diff"
            },
            "a": {
              "$ref": "#/definitions/alias"
            },
            "b": {
              "$ref": "#/definitions/alias"
            }
          }
        }
      ]
    },
    "condition": {
      "description": "Nesting depth at most 3, at most 16 leaves. Other condition keys are refused (unsupported_condition).",
      "oneOf": [
        {
          "type": "object",
          "required": [
            "above",
            "value"
          ],
          "additionalProperties": false,
          "properties": {
            "above": {
              "$ref": "#/definitions/alias"
            },
            "value": {
              "type": "number"
            },
            "hysteresis": {
              "$ref": "#/definitions/hysteresis"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "below",
            "value"
          ],
          "additionalProperties": false,
          "properties": {
            "below": {
              "$ref": "#/definitions/alias"
            },
            "value": {
              "type": "number"
            },
            "hysteresis": {
              "$ref": "#/definitions/hysteresis"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "outside",
            "lo",
            "hi"
          ],
          "additionalProperties": false,
          "properties": {
            "outside": {
              "$ref": "#/definitions/alias"
            },
            "lo": {
              "type": "number"
            },
            "hi": {
              "type": "number"
            },
            "hysteresis": {
              "$ref": "#/definitions/hysteresis"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "inside",
            "lo",
            "hi"
          ],
          "additionalProperties": false,
          "properties": {
            "inside": {
              "$ref": "#/definitions/alias"
            },
            "lo": {
              "type": "number"
            },
            "hi": {
              "type": "number"
            },
            "hysteresis": {
              "$ref": "#/definitions/hysteresis"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "equals",
            "value"
          ],
          "additionalProperties": false,
          "properties": {
            "equals": {
              "$ref": "#/definitions/alias"
            },
            "value": {
              "type": "number"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "changes"
          ],
          "additionalProperties": false,
          "properties": {
            "changes": {
              "$ref": "#/definitions/alias"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "noData",
            "forS"
          ],
          "additionalProperties": false,
          "properties": {
            "noData": {
              "$ref": "#/definitions/alias"
            },
            "forS": {
              "$ref": "#/definitions/seconds"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "all"
          ],
          "additionalProperties": false,
          "properties": {
            "all": {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#/definitions/condition"
              }
            }
          }
        },
        {
          "type": "object",
          "required": [
            "any"
          ],
          "additionalProperties": false,
          "properties": {
            "any": {
              "type": "array",
              "minItems": 1,
              "items": {
                "$ref": "#/definitions/condition"
              }
            }
          }
        },
        {
          "type": "object",
          "required": [
            "not"
          ],
          "additionalProperties": false,
          "properties": {
            "not": {
              "$ref": "#/definitions/condition"
            }
          }
        }
      ]
    }
  }
}